Skip to content

Commit

Permalink
Merge pull request #344 from vitobotta:hetzner-software-installation-…
Browse files Browse the repository at this point in the history
…refactoring

Refactor Kubernetes::Installer to extract software installation into a separate classes
  • Loading branch information
vitobotta authored Apr 15, 2024
2 parents aa0057e + c057f48 commit 2e6c1f1
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 82 deletions.
95 changes: 14 additions & 81 deletions src/kubernetes/installer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ require "../hetzner/server"
require "../hetzner/load_balancer"
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") }}
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
Expand All @@ -32,24 +34,19 @@ 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

create_hetzner_cloud_secret
deploy_cloud_controller_manager
deploy_csi_driver
Kubernetes::Software::SystemUpgradeController.new(configuration, settings).install
deploy_cluster_autoscaler unless autoscaling_worker_node_pools.size.zero?
install_software
end

private def set_up_first_master
Expand Down Expand Up @@ -226,78 +223,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..."

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..."

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..."

Expand Down Expand Up @@ -385,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
37 changes: 37 additions & 0 deletions src/kubernetes/software/hetzner/cloud_controller_manager.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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.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
23 changes: 23 additions & 0 deletions src/kubernetes/software/hetzner/csi_driver.cr
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions src/kubernetes/software/hetzner/secret.cr
Original file line number Diff line number Diff line change
@@ -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 "\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),
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, prefix: "Hetzner Cloud Secret")

unless result.success?
puts "Failed to create Hetzner Cloud secret:"
puts result.output
exit 1
end

puts "[Hetzner Cloud Secret] ...secret created"
end
end
1 change: 0 additions & 1 deletion src/kubernetes/software/system_upgrade_controller.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Kubernetes::Software::SystemUpgradeController
getter settings : Configuration::Main { configuration.settings }

def initialize(@configuration, @settings)

end

def install
Expand Down

0 comments on commit 2e6c1f1

Please sign in to comment.