From 7148c6baa5f74a1fa56784a2810bab0d145a83cf Mon Sep 17 00:00:00 2001 From: Vito Botta Date: Mon, 15 Apr 2024 12:46:28 +0000 Subject: [PATCH 1/3] Extract ClusterAutoscaler installation into a separate class --- src/kubernetes/installer.cr | 43 +------------ src/kubernetes/software/cluster_autoscaler.cr | 62 +++++++++++++++++++ 2 files changed, 64 insertions(+), 41 deletions(-) create mode 100644 src/kubernetes/software/cluster_autoscaler.cr diff --git a/src/kubernetes/installer.cr b/src/kubernetes/installer.cr index d4788cfc..e2ec9fb9 100644 --- a/src/kubernetes/installer.cr +++ b/src/kubernetes/installer.cr @@ -12,11 +12,11 @@ require "./software/system_upgrade_controller" require "./software/hetzner/secret" require "./software/hetzner/cloud_controller_manager" require "./software/hetzner/csi_driver" +require "./software/cluster_autoscaler" class Kubernetes::Installer MASTER_INSTALL_SCRIPT = {{ read_file("#{__DIR__}/../../templates/master_install_script.sh") }} WORKER_INSTALL_SCRIPT = {{ read_file("#{__DIR__}/../../templates/worker_install_script.sh") }} - CLUSTER_AUTOSCALER_MANIFEST = {{ read_file("#{__DIR__}/../../templates/cluster_autoscaler.yaml") }} getter configuration : Configuration::Loader getter settings : Configuration::Main { configuration.settings } @@ -223,46 +223,7 @@ class Kubernetes::Installer File.chmod kubeconfig_path, 0o600 end - private def deploy_cluster_autoscaler - puts "\nDeploying Cluster Autoscaler..." - node_pool_args = autoscaling_worker_node_pools.map do |pool| - autoscaling = pool.autoscaling.not_nil! - "- --nodes=#{autoscaling.min_instances}:#{autoscaling.max_instances}:#{pool.instance_type.upcase}:#{pool.location.upcase}:#{pool.name}" - end.join("\n ") - - k3s_join_script = "|\n #{worker_install_script.gsub("\n", "\n ")}" - - cloud_init = Hetzner::Server::Create.cloud_init(settings.ssh_port, settings.snapshot_os, settings.additional_packages, settings.post_create_commands, [k3s_join_script]) - - certificate_path = ssh.run(first_master, settings.ssh_port, "[ -f /etc/ssl/certs/ca-certificates.crt ] && echo 1 || echo 2", settings.use_ssh_agent, false).chomp == "1" ? "/etc/ssl/certs/ca-certificates.crt" : "/etc/ssl/certs/ca-bundle.crt" - - cluster_autoscaler_manifest = Crinja.render(CLUSTER_AUTOSCALER_MANIFEST, { - node_pool_args: node_pool_args, - cloud_init: Base64.strict_encode(cloud_init), - image: settings.autoscaling_image || settings.image, - firewall_name: settings.cluster_name, - ssh_key_name: settings.cluster_name, - network_name: (settings.existing_network || settings.cluster_name), - certificate_path: certificate_path - }) - - cluster_autoscaler_manifest_path = "/tmp/cluster_autoscaler_manifest_path.yaml" - - File.write(cluster_autoscaler_manifest_path, cluster_autoscaler_manifest) - - command = "kubectl apply -f #{cluster_autoscaler_manifest_path}" - - result = Util::Shell.run(command, configuration.kubeconfig_path, settings.hetzner_token) - - unless result.success? - puts "Failed to deploy Cluster Autoscaler:" - puts result.output - exit 1 - end - - puts "...Cluster Autoscaler deployed." - end private def add_labels_and_taints_to_masters add_labels_or_taints(:label, masters, settings.masters_pool.labels, :master) @@ -316,6 +277,6 @@ class Kubernetes::Installer Kubernetes::Software::Hetzner::CloudControllerManager.new(configuration, settings).install Kubernetes::Software::Hetzner::CSIDriver.new(configuration, settings).install Kubernetes::Software::SystemUpgradeController.new(configuration, settings).install - deploy_cluster_autoscaler + Kubernetes::Software::ClusterAutoscaler.new(configuration, settings, first_master, ssh, autoscaling_worker_node_pools, worker_install_script).install end end diff --git a/src/kubernetes/software/cluster_autoscaler.cr b/src/kubernetes/software/cluster_autoscaler.cr new file mode 100644 index 00000000..d8e2abd5 --- /dev/null +++ b/src/kubernetes/software/cluster_autoscaler.cr @@ -0,0 +1,62 @@ +require "../../configuration/loader" +require "../../configuration/main" +require "../../hetzner/server" +require "../../hetzner/server/create" +require "../../util" +require "../../util/shell" +require "../../util/ssh" + +class Kubernetes::Software::ClusterAutoscaler + CLUSTER_AUTOSCALER_MANIFEST = {{ read_file("#{__DIR__}/../../../templates/cluster_autoscaler.yaml") }} + + getter configuration : Configuration::Loader + getter settings : Configuration::Main { configuration.settings } + getter autoscaling_worker_node_pools : Array(Configuration::NodePool) + getter worker_install_script : String + getter first_master : ::Hetzner::Server + getter ssh : Util::SSH + + def initialize(@configuration, @settings, @first_master, @ssh, @autoscaling_worker_node_pools, @worker_install_script) + end + + def install + puts "\n[Cluster Autoscaler] Installing Cluster Autoscaler..." + + node_pool_args = autoscaling_worker_node_pools.map do |pool| + autoscaling = pool.autoscaling.not_nil! + "- --nodes=#{autoscaling.min_instances}:#{autoscaling.max_instances}:#{pool.instance_type.upcase}:#{pool.location.upcase}:#{pool.name}" + end.join("\n ") + + k3s_join_script = "|\n #{worker_install_script.gsub("\n", "\n ")}" + + cloud_init = ::Hetzner::Server::Create.cloud_init(settings.ssh_port, settings.snapshot_os, settings.additional_packages, settings.post_create_commands, [k3s_join_script]) + + certificate_path = ssh.run(first_master, settings.ssh_port, "[ -f /etc/ssl/certs/ca-certificates.crt ] && echo 1 || echo 2", settings.use_ssh_agent, false).chomp == "1" ? "/etc/ssl/certs/ca-certificates.crt" : "/etc/ssl/certs/ca-bundle.crt" + + cluster_autoscaler_manifest = Crinja.render(CLUSTER_AUTOSCALER_MANIFEST, { + node_pool_args: node_pool_args, + cloud_init: Base64.strict_encode(cloud_init), + image: settings.autoscaling_image || settings.image, + firewall_name: settings.cluster_name, + ssh_key_name: settings.cluster_name, + network_name: (settings.existing_network || settings.cluster_name), + certificate_path: certificate_path + }) + + cluster_autoscaler_manifest_path = "/tmp/cluster_autoscaler_manifest_path.yaml" + + File.write(cluster_autoscaler_manifest_path, cluster_autoscaler_manifest) + + command = "kubectl apply -f #{cluster_autoscaler_manifest_path}" + + result = Util::Shell.run(command, configuration.kubeconfig_path, settings.hetzner_token, prefix: "Cluster Autoscaler") + + unless result.success? + puts "Failed to deploy Cluster Autoscaler:" + puts result.output + exit 1 + end + + puts "[Cluster Autoscaler] ...Cluster Autoscaler installed" + end +end From 6771abe97d47a136e886b53cc7361f16e44fd450 Mon Sep 17 00:00:00 2001 From: Vito Botta Date: Mon, 15 Apr 2024 12:52:32 +0000 Subject: [PATCH 2/3] Reduce complexity by extracting methods --- src/kubernetes/software/cluster_autoscaler.cr | 62 ++++++++++++------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/src/kubernetes/software/cluster_autoscaler.cr b/src/kubernetes/software/cluster_autoscaler.cr index d8e2abd5..33b2a282 100644 --- a/src/kubernetes/software/cluster_autoscaler.cr +++ b/src/kubernetes/software/cluster_autoscaler.cr @@ -22,18 +22,48 @@ class Kubernetes::Software::ClusterAutoscaler def install puts "\n[Cluster Autoscaler] Installing Cluster Autoscaler..." - node_pool_args = autoscaling_worker_node_pools.map do |pool| - autoscaling = pool.autoscaling.not_nil! - "- --nodes=#{autoscaling.min_instances}:#{autoscaling.max_instances}:#{pool.instance_type.upcase}:#{pool.location.upcase}:#{pool.name}" - end.join("\n ") + command = <<-BASH + kubectl apply -f - <<-EOF + #{cluster_autoscaler_manifest} + EOF + BASH + + result = Util::Shell.run(command, configuration.kubeconfig_path, settings.hetzner_token, prefix: "Cluster Autoscaler") + + unless result.success? + puts "Failed to deploy Cluster Autoscaler:" + puts result.output + exit 1 + end - k3s_join_script = "|\n #{worker_install_script.gsub("\n", "\n ")}" + puts "[Cluster Autoscaler] ...Cluster Autoscaler installed" + end - cloud_init = ::Hetzner::Server::Create.cloud_init(settings.ssh_port, settings.snapshot_os, settings.additional_packages, settings.post_create_commands, [k3s_join_script]) + private def cloud_init + ::Hetzner::Server::Create.cloud_init(settings.ssh_port, settings.snapshot_os, settings.additional_packages, settings.post_create_commands, [k3s_join_script]) + end + + private def k3s_join_script + "|\n #{worker_install_script.gsub("\n", "\n ")}" + end + + private def certificate_path + if ssh.run(first_master, settings.ssh_port, "[ -f /etc/ssl/certs/ca-certificates.crt ] && echo 1 || echo 2", settings.use_ssh_agent, false).chomp == "1" + "/etc/ssl/certs/ca-certificates.crt" + else + "/etc/ssl/certs/ca-bundle.crt" + end + end - certificate_path = ssh.run(first_master, settings.ssh_port, "[ -f /etc/ssl/certs/ca-certificates.crt ] && echo 1 || echo 2", settings.use_ssh_agent, false).chomp == "1" ? "/etc/ssl/certs/ca-certificates.crt" : "/etc/ssl/certs/ca-bundle.crt" + private def node_pool_args + autoscaling_worker_node_pools.map do |pool| + autoscaling = pool.autoscaling.not_nil! + "- --nodes=#{autoscaling.min_instances}:#{autoscaling.max_instances}:#{pool.instance_type.upcase}:#{pool.location.upcase}:#{pool.name}" + end.join("\n ") + end - cluster_autoscaler_manifest = Crinja.render(CLUSTER_AUTOSCALER_MANIFEST, { + private def cluster_autoscaler_manifest + Crinja.render(CLUSTER_AUTOSCALER_MANIFEST, { node_pool_args: node_pool_args, cloud_init: Base64.strict_encode(cloud_init), image: settings.autoscaling_image || settings.image, @@ -42,21 +72,5 @@ class Kubernetes::Software::ClusterAutoscaler network_name: (settings.existing_network || settings.cluster_name), certificate_path: certificate_path }) - - cluster_autoscaler_manifest_path = "/tmp/cluster_autoscaler_manifest_path.yaml" - - File.write(cluster_autoscaler_manifest_path, cluster_autoscaler_manifest) - - command = "kubectl apply -f #{cluster_autoscaler_manifest_path}" - - result = Util::Shell.run(command, configuration.kubeconfig_path, settings.hetzner_token, prefix: "Cluster Autoscaler") - - unless result.success? - puts "Failed to deploy Cluster Autoscaler:" - puts result.output - exit 1 - end - - puts "[Cluster Autoscaler] ...Cluster Autoscaler installed" end end From 74f176bfe471510421c29e95f3077b60250656e8 Mon Sep 17 00:00:00 2001 From: Vito Botta Date: Mon, 15 Apr 2024 12:55:45 +0000 Subject: [PATCH 3/3] Only build binaries for tagged commits --- .github/workflows/release.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index da0e597e..d85538d9 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,9 +1,10 @@ name: Create release on: - push: - branches-ignore: - - 'main' + on: + push: + tags: + - 'v*' jobs: