Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic update of Kubespray and K8s [#10] #11

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions bin/k8sinfra
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ class K8sInfra < Thor
if options['dry-run'] then
puts "Dry-run successfully completed"
else
if @cluster_hash['k8s_infra']["release_type"]=="kubespray" then
latest = ks.latest_kubespray_release
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indent these lines

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the file it looks as follows - I think that's correct?

      else
        if @cluster_hash['k8s_infra']["release_type"]=="kubespray" then
	  latest = ks.latest_kubespray_release
	  ks.update_kubespray(latest)
        end

ks.update_kubespray(latest)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updating kubespray itself every time might be a good idea even if using the latest stable K8s release from upstream.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That can be done. What about the addition of Multus and CNI plugins in the installation? Should that be part of default, or should it be kept as a separate e.g. "release" similar to how it is with dynamic Kubespray/K8s now?

end
cluster = ks.start_kubespray
if cluster[:exit_code] == 0 then
puts "KUBECONFIG path: #{full_kubeconfig_path}"
Expand Down Expand Up @@ -233,8 +237,17 @@ class K8sInfra < Thor
puts "All required options not handled"
end

kubernetes_release = K8sUtils.kubernetes_release("#{options['release-type']}/#{options['arch']}")
stable_k8s_release = K8sUtils.kubernetes_release('stable')
unless options['release-type'] == "kubespray"
kubernetes_release = K8sUtils.kubernetes_release("#{options['release-type']}/#{options['arch']}")
stable_k8s_release = K8sUtils.kubernetes_release('stable')
else
ks = Kubespray.new(@cluster_hash)
ks_version = ks.latest_kubespray_release
k8s_version = ks.latest_supported_kubernetes(ks_version)
kubernetes_release = k8s_version + "\n"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are the newlines necessary?

Copy link
Collaborator Author

@michaelspedersen michaelspedersen May 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used to ensure the configuration file prints properly. I noticed lines were combined without it. I added it here as it is only a problem when kubernetes_release and stable_k8s_release are fetched dynamically.

stable_k8s_release = k8s_version + "\n"
end

config = ERB.new(cluster_config_template("#{options['arch']}", "#{options['provision-type']}", "#{kubernetes_release}", "#{stable_k8s_release}", "#{options['release-type']}"), nil, '-')
if options['output'] then
if File.exist?(options['output']) then
Expand Down
50 changes: 50 additions & 0 deletions bin/kubespray-integration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'open3'
require 'fileutils'
require 'logger'
require 'json'
require 'yaml'
require_relative './k8sutils'

class Kubespray
Expand Down Expand Up @@ -57,7 +59,51 @@ def set_urls(cluster_hash)
cluster_hash
end

def latest_kubespray_release
release_url = "https://api.github.com/repos/kubernetes-sigs/kubespray/releases/latest"
response = Faraday.get release_url
if response.status != 200
@logger.error "Failed to get Kubespray release info"
exit 1
end
results = JSON.parse(response.body)
latest = results['tag_name']
return "#{latest}"
end

def latest_supported_kubernetes(tag)
version_url = "https://raw.githubusercontent.com/kubernetes-sigs/kubespray/#{tag}/roles/download/defaults/main.yml"
response = Faraday.get version_url
if response.status != 200
@logger.error "Failed to Kubespray raw file"
exit 1
end
parsed = YAML.load(response.body)
return parsed['kube_version']
end

def update_kubespray(tag)
if ENV["RUBY_ENV"]=="test" then
kubedir=File.expand_path '../../lib/provisioner/kubespray/kubespray'
else
kubedir=File.expand_path 'lib/provisioner/kubespray/kubespray'
end

commands = [ ]
commands.push("git -C #{kubedir} checkout master -q")
commands.push("git -C #{kubedir} pull --all -q")
commands.push("git -C #{kubedir} checkout tags/#{tag} -q")

commands.each do |command|
Open3.popen3(command) do |stdin, stdout, stderr, wait_thr|
unless stderr.read.to_s.strip.empty?
@logger.error "#{stderr.read}"
exit 1
end
end
end
puts "Kubespray updated to #{tag}"
end

def start_kubespray
@logger.debug "pwd: #{FileUtils.pwd()}"
Expand Down Expand Up @@ -142,6 +188,10 @@ def provision_template
container_manager: containerd
download_container: False
kubeconfig_localhost: true
<%- if @cluster_hash['k8s_infra']['release_type']=='kubespray' -%>
kube_network_plugin: calico
kube_network_plugin_multus: true
<%- end -%>
kubectl_localhost: false
kubelet_download_url: <%= @cluster_hash['k8s_infra']['kubelet_download_url'] %>
kubelet_binary_checksum: <%= @cluster_hash['k8s_infra']['kubelet_binary_checksum'] %>
Expand Down