-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and fix System Upgrade Controller installation
- Loading branch information
Showing
16 changed files
with
222 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require "./deployment/spec" | ||
|
||
module Kubernetes::Resources | ||
class Deployment | ||
include YAML::Serializable | ||
include YAML::Serializable::Unmapped | ||
|
||
property spec : Kubernetes::Resources::Deployment::Spec | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require "./spec/template" | ||
|
||
module Kubernetes::Resources | ||
class Deployment | ||
class Spec | ||
include YAML::Serializable | ||
include YAML::Serializable::Unmapped | ||
|
||
property template : Kubernetes::Resources::Deployment::Spec::Template | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require "../../pod/spec" | ||
|
||
module Kubernetes::Resources | ||
class Deployment | ||
class Spec | ||
class Template | ||
include YAML::Serializable | ||
include YAML::Serializable::Unmapped | ||
|
||
property spec : Kubernetes::Resources::Pod::Spec | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require "../../../pod/spec/toleration" | ||
|
||
module Kubernetes::Resources | ||
class Deployment | ||
class Spec | ||
class Template | ||
class Spec | ||
include YAML::Serializable | ||
include YAML::Serializable::Unmapped | ||
|
||
property tolerations : Array(Kubernetes::Resources::Pod::Spec::Toleration)? | ||
end | ||
end | ||
end | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
src/kubernetes/resources/manifest_sections/section_with_spec.cr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Kubernetes::Resources | ||
class Deployment | ||
class Spec | ||
class Template | ||
include YAML::Serializable | ||
include YAML::Serializable::Unmapped | ||
|
||
property spec : Kubernetes::Resources::Deployment::Spec::Template::Spec | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require "./pod/spec" | ||
|
||
module Kubernetes::Resources | ||
class Pod | ||
include YAML::Serializable | ||
include YAML::Serializable::Unmapped | ||
|
||
property spec : Kubernetes::Resources::Pod::Spec | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require "./spec/toleration" | ||
|
||
module Kubernetes::Resources | ||
class Pod | ||
class Spec | ||
include YAML::Serializable | ||
include YAML::Serializable::Unmapped | ||
|
||
property tolerations : Array(Kubernetes::Resources::Pod::Spec::Toleration)? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Kubernetes::Resources::Pod | ||
class Spec | ||
class Toleration | ||
include YAML::Serializable | ||
include YAML::Serializable::Unmapped | ||
|
||
property effect : String? | ||
property key : String? | ||
property value : String? | ||
|
||
def initialize(@effect, @key, @value) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module Kubernetes::Resources | ||
class Resource | ||
include YAML::Serializable | ||
include YAML::Serializable::Unmapped | ||
|
||
property kind : String | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
require "../resources/resource" | ||
require "../resources/deployment" | ||
require "../resources/pod/spec/toleration" | ||
require "../../configuration/loader" | ||
require "../../configuration/main" | ||
|
||
class Kubernetes::Software::SystemUpgradeController | ||
getter configuration : Configuration::Loader | ||
getter settings : Configuration::Main { configuration.settings } | ||
|
||
def initialize(@configuration, @settings) | ||
|
||
end | ||
|
||
def install | ||
puts "\n[System Upgrade Controller] Deploying k3s System Upgrade Controller..." | ||
|
||
create_namespace | ||
create_crd | ||
create_resources | ||
|
||
puts "[System Upgrade Controller] ...k3s System Upgrade Controller deployed." | ||
end | ||
|
||
private def create_namespace | ||
run_command "kubectl create ns system-upgrade --dry-run=client -o yaml | kubectl apply -f -" | ||
end | ||
|
||
private def create_crd | ||
run_command "kubectl apply -f #{settings.system_upgrade_controller_crd_manifest_url}" | ||
end | ||
|
||
private def create_resources | ||
manifest = fetch_resources_manifest | ||
resources = YAML.parse_all(manifest) | ||
patched_resources = patch_resources(resources) | ||
patched_manifest = patched_resources.map(&.to_yaml).join | ||
updated_manifest_path = "/tmp/manifest.yaml" | ||
|
||
File.write(updated_manifest_path, patched_manifest) | ||
|
||
run_command "kubectl apply -f #{updated_manifest_path}" | ||
|
||
File.delete(updated_manifest_path) | ||
end | ||
|
||
private def fetch_resources_manifest | ||
response = Crest.get(settings.system_upgrade_controller_deployment_manifest_url) | ||
|
||
unless response.success? | ||
puts "[System Upgrade Controller] Failed to download System Upgrade Controller manifest from #{settings.system_upgrade_controller_deployment_manifest_url}" | ||
puts "[System Upgrade Controller] Server responded with status #{response.status_code}" | ||
exit 1 | ||
end | ||
|
||
response.body.to_s | ||
end | ||
|
||
private def deployment_with_added_toleration(resource) | ||
deployment = Kubernetes::Resources::Deployment.from_yaml(resource.to_yaml) | ||
toleration = Kubernetes::Resources::Pod::Spec::Toleration.new(effect: "NoExecute", key: "CriticalAddonsOnly", value: "true") | ||
|
||
if tolerations = deployment.spec.template.spec.tolerations | ||
tolerations << toleration | ||
else | ||
deployment.spec.template.spec.tolerations = [toleration] | ||
end | ||
|
||
deployment | ||
end | ||
|
||
private def run_command(command) | ||
result = Util::Shell.run(command, configuration.kubeconfig_path, settings.hetzner_token, prefix: "System Upgrade Controller") | ||
|
||
unless result.success? | ||
puts "[System Upgrade Controller] Failed to deploy k3s System Upgrade Controller:" | ||
puts result.output | ||
exit 1 | ||
end | ||
end | ||
|
||
private def patch_resources(resources) | ||
resources.map do |resource| | ||
resource = Kubernetes::Resources::Resource.from_yaml(resource.to_yaml) | ||
|
||
if resource.kind == "Deployment" | ||
deployment_with_added_toleration(resource) | ||
else | ||
resource | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters