From ab8816c3086b8539004f33a2341bca5da62ac949 Mon Sep 17 00:00:00 2001 From: Vito Botta Date: Mon, 15 Apr 2024 12:10:25 +0000 Subject: [PATCH 1/8] Extract Hetzner secret installation to its own class --- src/kubernetes/installer.cr | 29 ++-------------- src/kubernetes/software/hetzner/secret.cr | 34 +++++++++++++++++++ .../software/system_upgrade_controller.cr | 1 - 3 files changed, 36 insertions(+), 28 deletions(-) create mode 100644 src/kubernetes/software/hetzner/secret.cr diff --git a/src/kubernetes/installer.cr b/src/kubernetes/installer.cr index 1613d77..399bdf1 100644 --- a/src/kubernetes/installer.cr +++ b/src/kubernetes/installer.cr @@ -9,11 +9,11 @@ require "../hetzner/server" require "../hetzner/load_balancer" require "../configuration/loader" require "./software/system_upgrade_controller" +require "./software/hetzner/secret" 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") }} - HETZNER_CLOUD_SECRET_MANIFEST = {{ read_file("#{__DIR__}/../../templates/hetzner_cloud_secret_manifest.yaml") }} CLUSTER_AUTOSCALER_MANIFEST = {{ read_file("#{__DIR__}/../../templates/cluster_autoscaler.yaml") }} getter configuration : Configuration::Loader @@ -45,7 +45,7 @@ class Kubernetes::Installer add_labels_and_taints_to_masters add_labels_and_taints_to_workers - create_hetzner_cloud_secret + Kubernetes::Software::Hetzner::Secret.new(configuration, settings).create deploy_cloud_controller_manager deploy_csi_driver Kubernetes::Software::SystemUpgradeController.new(configuration, settings).install @@ -226,31 +226,6 @@ class Kubernetes::Installer File.chmod kubeconfig_path, 0o600 end - private def create_hetzner_cloud_secret - puts "\nCreating secret for Hetzner Cloud token..." - - secret_manifest = Crinja.render(HETZNER_CLOUD_SECRET_MANIFEST, { - network: (settings.existing_network || settings.cluster_name), - token: settings.hetzner_token - }) - - command = <<-BASH - kubectl apply -f - <<-EOF - #{secret_manifest} - EOF - BASH - - result = Util::Shell.run(command, configuration.kubeconfig_path, settings.hetzner_token) - - unless result.success? - puts "Failed to create Hetzner Cloud secret:" - puts result.output - exit 1 - end - - puts "...secret created." - end - private def deploy_cloud_controller_manager puts "\nDeploying Hetzner Cloud Controller Manager..." diff --git a/src/kubernetes/software/hetzner/secret.cr b/src/kubernetes/software/hetzner/secret.cr new file mode 100644 index 0000000..995fc64 --- /dev/null +++ b/src/kubernetes/software/hetzner/secret.cr @@ -0,0 +1,34 @@ +class Kubernetes::Software::Hetzner::Secret + HETZNER_CLOUD_SECRET_MANIFEST = {{ read_file("#{__DIR__}/../../../../templates/hetzner_cloud_secret_manifest.yaml") }} + + getter configuration : Configuration::Loader + getter settings : Configuration::Main { configuration.settings } + + def initialize(@configuration, @settings) + end + + def create + puts "\nCreating secret for Hetzner Cloud token..." + + secret_manifest = Crinja.render(HETZNER_CLOUD_SECRET_MANIFEST, { + network: (settings.existing_network || settings.cluster_name), + token: settings.hetzner_token + }) + + command = <<-BASH + kubectl apply -f - <<-EOF + #{secret_manifest} + EOF + BASH + + result = Util::Shell.run(command, configuration.kubeconfig_path, settings.hetzner_token) + + unless result.success? + puts "Failed to create Hetzner Cloud secret:" + puts result.output + exit 1 + end + + puts "...secret created." + end +end diff --git a/src/kubernetes/software/system_upgrade_controller.cr b/src/kubernetes/software/system_upgrade_controller.cr index 3092126..f0be60f 100644 --- a/src/kubernetes/software/system_upgrade_controller.cr +++ b/src/kubernetes/software/system_upgrade_controller.cr @@ -9,7 +9,6 @@ class Kubernetes::Software::SystemUpgradeController getter settings : Configuration::Main { configuration.settings } def initialize(@configuration, @settings) - end def install From 45a552602e84bd7629b2174899f1e6a120cd0cbb Mon Sep 17 00:00:00 2001 From: Vito Botta Date: Mon, 15 Apr 2024 12:14:40 +0000 Subject: [PATCH 2/8] Extract installation of Cloud Controller Manager into a separate class --- src/kubernetes/installer.cr | 34 +---------------- .../hetzner/cloud_controller_manager.cr | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 32 deletions(-) create mode 100644 src/kubernetes/software/hetzner/cloud_controller_manager.cr diff --git a/src/kubernetes/installer.cr b/src/kubernetes/installer.cr index 399bdf1..93f998f 100644 --- a/src/kubernetes/installer.cr +++ b/src/kubernetes/installer.cr @@ -10,6 +10,7 @@ require "../hetzner/load_balancer" require "../configuration/loader" require "./software/system_upgrade_controller" require "./software/hetzner/secret" +require "./software/hetzner/cloud_controller_manager" class Kubernetes::Installer MASTER_INSTALL_SCRIPT = {{ read_file("#{__DIR__}/../../templates/master_install_script.sh") }} @@ -46,7 +47,7 @@ class Kubernetes::Installer add_labels_and_taints_to_workers Kubernetes::Software::Hetzner::Secret.new(configuration, settings).create - deploy_cloud_controller_manager + Kubernetes::Software::Hetzner::CloudControllerManager.new(configuration, settings).install deploy_csi_driver Kubernetes::Software::SystemUpgradeController.new(configuration, settings).install deploy_cluster_autoscaler unless autoscaling_worker_node_pools.size.zero? @@ -226,37 +227,6 @@ class Kubernetes::Installer File.chmod kubeconfig_path, 0o600 end - private def deploy_cloud_controller_manager - puts "\nDeploying Hetzner Cloud Controller Manager..." - - response = Crest.get(settings.cloud_controller_manager_manifest_url) - - unless response.success? - puts "Failed to download CCM manifest from #{settings.cloud_controller_manager_manifest_url}" - puts "Server responded with status #{response.status_code}" - exit 1 - end - - ccm_manifest = response.body.to_s - ccm_manifest = ccm_manifest.gsub(/--cluster-cidr=[^"]+/, "--cluster-cidr=#{settings.cluster_cidr}") - - ccm_manifest_path = "/tmp/ccm_manifest.yaml" - - File.write(ccm_manifest_path, ccm_manifest) - - command = "kubectl apply -f #{ccm_manifest_path}" - - result = Util::Shell.run(command, configuration.kubeconfig_path, settings.hetzner_token) - - unless result.success? - puts "Failed to deploy Cloud Controller Manager:" - puts result.output - exit 1 - end - - puts "...Cloud Controller Manager deployed" - end - private def deploy_csi_driver puts "\nDeploying Hetzner CSI Driver..." diff --git a/src/kubernetes/software/hetzner/cloud_controller_manager.cr b/src/kubernetes/software/hetzner/cloud_controller_manager.cr new file mode 100644 index 0000000..1d7a43a --- /dev/null +++ b/src/kubernetes/software/hetzner/cloud_controller_manager.cr @@ -0,0 +1,38 @@ +class Kubernetes::Software::Hetzner::CloudControllerManager + getter configuration : Configuration::Loader + getter settings : Configuration::Main { configuration.settings } + + def initialize(@configuration, @settings) + end + + def install + puts "\n[Hetzner Cloud Controller] Installing Hetzner Cloud Controller Manager..." + + response = Crest.get(settings.cloud_controller_manager_manifest_url) + + unless response.success? + puts "Failed to download CCM manifest from #{settings.cloud_controller_manager_manifest_url}" + puts "Server responded with status #{response.status_code}" + exit 1 + end + + ccm_manifest = response.body.to_s + ccm_manifest = ccm_manifest.gsub(/--cluster-cidr=[^"]+/, "--cluster-cidr=#{settings.cluster_cidr}") + + command = <<-BASH + kubectl apply -f - <<-EOF + #{ccm_manifest} + EOF + BASH + + result = Util::Shell.run(command, configuration.kubeconfig_path, settings.hetzner_token, prefix: "Hetzner Cloud Controller") + + unless result.success? + puts "Failed to deploy Cloud Controller Manager:" + puts result.output + exit 1 + end + + puts "[Hetzner Cloud Controller] Hetzner Cloud Controller Manager installed" + end +end From 218950c519326359dac1a4dbf1313fbc87e496ed Mon Sep 17 00:00:00 2001 From: Vito Botta Date: Mon, 15 Apr 2024 12:15:28 +0000 Subject: [PATCH 3/8] Refactor Hetzner secret creation to include prefix in output messages --- src/kubernetes/software/hetzner/secret.cr | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/kubernetes/software/hetzner/secret.cr b/src/kubernetes/software/hetzner/secret.cr index 995fc64..8e3f6a1 100644 --- a/src/kubernetes/software/hetzner/secret.cr +++ b/src/kubernetes/software/hetzner/secret.cr @@ -8,7 +8,7 @@ class Kubernetes::Software::Hetzner::Secret end def create - puts "\nCreating secret for Hetzner Cloud token..." + puts "\n[Hetzner Cloud Secret] Creating secret for Hetzner Cloud token..." secret_manifest = Crinja.render(HETZNER_CLOUD_SECRET_MANIFEST, { network: (settings.existing_network || settings.cluster_name), @@ -21,7 +21,7 @@ class Kubernetes::Software::Hetzner::Secret EOF BASH - result = Util::Shell.run(command, configuration.kubeconfig_path, settings.hetzner_token) + result = Util::Shell.run(command, configuration.kubeconfig_path, settings.hetzner_token, prefix: "Hetzner Cloud Secret") unless result.success? puts "Failed to create Hetzner Cloud secret:" @@ -29,6 +29,6 @@ class Kubernetes::Software::Hetzner::Secret exit 1 end - puts "...secret created." + puts "[Hetzner Cloud Secret] ...secret created" end end From cc3904e5fee257f50ea9066efa79b1f70ca2ec8e Mon Sep 17 00:00:00 2001 From: Vito Botta Date: Mon, 15 Apr 2024 12:15:55 +0000 Subject: [PATCH 4/8] Cleanup --- src/kubernetes/software/hetzner/cloud_controller_manager.cr | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/kubernetes/software/hetzner/cloud_controller_manager.cr b/src/kubernetes/software/hetzner/cloud_controller_manager.cr index 1d7a43a..9d4ca35 100644 --- a/src/kubernetes/software/hetzner/cloud_controller_manager.cr +++ b/src/kubernetes/software/hetzner/cloud_controller_manager.cr @@ -16,8 +16,7 @@ class Kubernetes::Software::Hetzner::CloudControllerManager exit 1 end - ccm_manifest = response.body.to_s - ccm_manifest = ccm_manifest.gsub(/--cluster-cidr=[^"]+/, "--cluster-cidr=#{settings.cluster_cidr}") + ccm_manifest = response.body.to_s.gsub(/--cluster-cidr=[^"]+/, "--cluster-cidr=#{settings.cluster_cidr}") command = <<-BASH kubectl apply -f - <<-EOF From 60c18521c563cabd9c6a5c7f64fbbe92d9e9a099 Mon Sep 17 00:00:00 2001 From: Vito Botta Date: Mon, 15 Apr 2024 12:18:42 +0000 Subject: [PATCH 5/8] Extract Hetzner CSI Driver installation into a separate class --- src/kubernetes/installer.cr | 19 ++------------- src/kubernetes/software/hetzner/csi_driver.cr | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 17 deletions(-) create mode 100644 src/kubernetes/software/hetzner/csi_driver.cr diff --git a/src/kubernetes/installer.cr b/src/kubernetes/installer.cr index 93f998f..ae2204f 100644 --- a/src/kubernetes/installer.cr +++ b/src/kubernetes/installer.cr @@ -11,6 +11,7 @@ require "../configuration/loader" require "./software/system_upgrade_controller" require "./software/hetzner/secret" require "./software/hetzner/cloud_controller_manager" +require "./software/hetzner/csi_driver" class Kubernetes::Installer MASTER_INSTALL_SCRIPT = {{ read_file("#{__DIR__}/../../templates/master_install_script.sh") }} @@ -48,7 +49,7 @@ class Kubernetes::Installer Kubernetes::Software::Hetzner::Secret.new(configuration, settings).create Kubernetes::Software::Hetzner::CloudControllerManager.new(configuration, settings).install - deploy_csi_driver + Kubernetes::Software::Hetzner::CSIDriver.new(configuration, settings).install Kubernetes::Software::SystemUpgradeController.new(configuration, settings).install deploy_cluster_autoscaler unless autoscaling_worker_node_pools.size.zero? end @@ -227,22 +228,6 @@ class Kubernetes::Installer File.chmod kubeconfig_path, 0o600 end - private def deploy_csi_driver - puts "\nDeploying Hetzner CSI Driver..." - - command = "kubectl apply -f #{settings.csi_driver_manifest_url}" - - result = Util::Shell.run(command, configuration.kubeconfig_path, settings.hetzner_token) - - unless result.success? - puts "Failed to deploy CSI Driver:" - puts result.output - exit 1 - end - - puts "...CSI Driver deployed" - end - private def deploy_cluster_autoscaler puts "\nDeploying Cluster Autoscaler..." diff --git a/src/kubernetes/software/hetzner/csi_driver.cr b/src/kubernetes/software/hetzner/csi_driver.cr new file mode 100644 index 0000000..e9c0782 --- /dev/null +++ b/src/kubernetes/software/hetzner/csi_driver.cr @@ -0,0 +1,23 @@ +class Kubernetes::Software::Hetzner::CSIDriver + getter configuration : Configuration::Loader + getter settings : Configuration::Main { configuration.settings } + + def initialize(@configuration, @settings) + end + + def install + puts "\n[Hetzner CSI Driver] Installing Hetzner CSI Driver..." + + command = "kubectl apply -f #{settings.csi_driver_manifest_url}" + + result = Util::Shell.run(command, configuration.kubeconfig_path, settings.hetzner_token, prefix: "Hetzner CSI Driver") + + unless result.success? + puts "Failed to deploy CSI Driver:" + puts result.output + exit 1 + end + + puts "[Hetzner CSI Driver] ...CSI Driver installed" + end +end From 3f4d8c7e2f2b6d72ca4205759b08bf7abf2b2d00 Mon Sep 17 00:00:00 2001 From: Vito Botta Date: Mon, 15 Apr 2024 12:19:07 +0000 Subject: [PATCH 6/8] Cleanup --- src/kubernetes/installer.cr | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/kubernetes/installer.cr b/src/kubernetes/installer.cr index ae2204f..1c83a14 100644 --- a/src/kubernetes/installer.cr +++ b/src/kubernetes/installer.cr @@ -34,15 +34,14 @@ class Kubernetes::Installer end def run + Util.check_kubectl + puts "\n=== Setting up Kubernetes ===\n" set_up_first_master set_up_other_masters set_up_workers - puts "\n=== Deploying Hetzner drivers ===\n" - - Util.check_kubectl add_labels_and_taints_to_masters add_labels_and_taints_to_workers From 7d3b00e2b7c4828aea7e637e0182521bd3fab29f Mon Sep 17 00:00:00 2001 From: Vito Botta Date: Mon, 15 Apr 2024 12:19:30 +0000 Subject: [PATCH 7/8] Refactor Kubernetes::Installer to always deploy cluster autoscaler --- src/kubernetes/installer.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kubernetes/installer.cr b/src/kubernetes/installer.cr index 1c83a14..637fb86 100644 --- a/src/kubernetes/installer.cr +++ b/src/kubernetes/installer.cr @@ -50,7 +50,7 @@ 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 unless autoscaling_worker_node_pools.size.zero? + deploy_cluster_autoscaler end private def set_up_first_master From c057f48cd43f2b73748b451ebe7d869618be5c38 Mon Sep 17 00:00:00 2001 From: Vito Botta Date: Mon, 15 Apr 2024 12:21:06 +0000 Subject: [PATCH 8/8] Refactor Kubernetes::Installer to extract software installation into a separate method --- src/kubernetes/installer.cr | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/kubernetes/installer.cr b/src/kubernetes/installer.cr index 637fb86..d4788cf 100644 --- a/src/kubernetes/installer.cr +++ b/src/kubernetes/installer.cr @@ -46,11 +46,7 @@ class Kubernetes::Installer add_labels_and_taints_to_masters add_labels_and_taints_to_workers - Kubernetes::Software::Hetzner::Secret.new(configuration, settings).create - 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 + install_software end private def set_up_first_master @@ -314,4 +310,12 @@ class Kubernetes::Installer end sans.join(" ") end + + private def install_software + Kubernetes::Software::Hetzner::Secret.new(configuration, settings).create + 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 + end end