Skip to content
Open
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
3 changes: 3 additions & 0 deletions src/kubernetes/resources/pod/spec/container/volume_mount.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class Kubernetes::Resources::Pod
property name : String?
property mountPath : String?
property readOnly : Bool?

def initialize(@name : String? = nil, @mountPath : String? = nil, @readOnly : Bool? = nil)
end
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions src/kubernetes/resources/pod/spec/volume.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "./volume/host_path"
require "./volume/config_map"

class Kubernetes::Resources::Pod
class Spec
Expand All @@ -8,6 +9,10 @@ class Kubernetes::Resources::Pod

property name : String?
property hostPath : HostPath?
property configMap : ConfigMap?

def initialize(@name : String? = nil, @hostPath : HostPath? = nil, @configMap : ConfigMap? = nil)
end
end
end
end
15 changes: 15 additions & 0 deletions src/kubernetes/resources/pod/spec/volume/config_map.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Kubernetes::Resources::Pod
class Spec
class Volume
class ConfigMap
include YAML::Serializable
include YAML::Serializable::Unmapped

property name : String?

def initialize(@name : String? = nil)
end
end
end
end
end
47 changes: 42 additions & 5 deletions src/kubernetes/software/cluster_autoscaler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ class Kubernetes::Software::ClusterAutoscaler
VOLUME_ATTACHMENTS_RESOURCE = "volumeattachments"
HCLOUD_CLOUD_INIT_VAR = "HCLOUD_CLOUD_INIT"
HCLOUD_CLUSTER_CONFIG_VAR = "HCLOUD_CLUSTER_CONFIG"
HCLOUD_CLUSTER_CONFIG_FILE_VAR = "HCLOUD_CLUSTER_CONFIG_FILE"
HCLOUD_FIREWALL_VAR = "HCLOUD_FIREWALL"
HCLOUD_SSH_KEY_VAR = "HCLOUD_SSH_KEY"
HCLOUD_NETWORK_VAR = "HCLOUD_NETWORK"
HCLOUD_PUBLIC_IPV4_VAR = "HCLOUD_PUBLIC_IPV4"
HCLOUD_PUBLIC_IPV6_VAR = "HCLOUD_PUBLIC_IPV6"
CERT_CHECK_COMMAND = "[ -f /etc/ssl/certs/ca-certificates.crt ] && echo 1 || echo 2"
CLUSTER_CONFIG_CONFIGMAP_NAME = "cluster-autoscaler-config"
CLUSTER_CONFIG_VOLUME_NAME = "cluster-config"
CLUSTER_CONFIG_MOUNT_PATH = "/etc/cluster-autoscaler"
CLUSTER_CONFIG_FILE_NAME = "config.json"

getter configuration : Configuration::Loader
getter settings : Configuration::Main { configuration.settings }
Expand All @@ -53,7 +58,8 @@ class Kubernetes::Software::ClusterAutoscaler
def install : Nil
log_line "Installing Cluster Autoscaler...", log_prefix: default_log_prefix

apply_manifest_from_yaml(manifest, "Failed to install Cluster Autoscaler")
apply_manifest_server_side(configmap_manifest, "Failed to create Cluster Autoscaler ConfigMap")
apply_manifest_server_side(manifest, "Failed to install Cluster Autoscaler")

log_line "...Cluster Autoscaler installed", log_prefix: default_log_prefix
end
Expand Down Expand Up @@ -258,8 +264,9 @@ class Kubernetes::Software::ClusterAutoscaler
env_vars = container.env || [] of Kubernetes::Resources::Pod::Spec::Container::EnvVariable

remove_env_variable(env_vars, HCLOUD_CLOUD_INIT_VAR)
remove_env_variable(env_vars, HCLOUD_CLUSTER_CONFIG_VAR)

set_env_variable(env_vars, HCLOUD_CLUSTER_CONFIG_VAR, Base64.strict_encode(build_config_json))
set_env_variable(env_vars, HCLOUD_CLUSTER_CONFIG_FILE_VAR, "#{CLUSTER_CONFIG_MOUNT_PATH}/#{CLUSTER_CONFIG_FILE_NAME}")
set_env_variable(env_vars, HCLOUD_FIREWALL_VAR, settings.cluster_name)
set_env_variable(env_vars, HCLOUD_SSH_KEY_VAR, settings.cluster_name)
set_env_variable(env_vars, HCLOUD_NETWORK_VAR, resolve_network_name)
Expand All @@ -275,6 +282,13 @@ class Kubernetes::Software::ClusterAutoscaler
ssl_mount = volume_mounts.find { |mount| mount.name == SSL_CERTS_VOLUME_NAME }
ssl_mount.mountPath = certificate_path if ssl_mount

config_mount = Kubernetes::Resources::Pod::Spec::Container::VolumeMount.new(
name: CLUSTER_CONFIG_VOLUME_NAME,
mountPath: CLUSTER_CONFIG_MOUNT_PATH,
readOnly: true
)
volume_mounts << config_mount

container.volumeMounts = volume_mounts
end

Expand Down Expand Up @@ -309,10 +323,18 @@ class Kubernetes::Software::ClusterAutoscaler
return unless volumes

ssl_volume = volumes.find { |v| v.name == SSL_CERTS_VOLUME_NAME }
return unless ssl_volume
if ssl_volume
host_path = ssl_volume.hostPath
host_path.path = certificate_path if host_path
end

host_path = ssl_volume.hostPath
host_path.path = certificate_path if host_path
config_volume = Kubernetes::Resources::Pod::Spec::Volume.new(
name: CLUSTER_CONFIG_VOLUME_NAME,
configMap: Kubernetes::Resources::Pod::Spec::Volume::ConfigMap.new(
name: CLUSTER_CONFIG_CONFIGMAP_NAME
)
)
volumes << config_volume
end

private def manifest : String
Expand All @@ -325,6 +347,21 @@ class Kubernetes::Software::ClusterAutoscaler
patched_resources.map(&.to_yaml).join("---\n")
end

private def configmap_manifest : String
config_json = build_config_json
{
"apiVersion" => "v1",
"kind" => "ConfigMap",
"metadata" => {
"name" => CLUSTER_CONFIG_CONFIGMAP_NAME,
"namespace" => "kube-system",
},
"data" => {
CLUSTER_CONFIG_FILE_NAME => config_json,
},
}.to_yaml
end

private def default_log_prefix : String
"Cluster Autoscaler"
end
Expand Down
10 changes: 10 additions & 0 deletions src/kubernetes/util.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ module Kubernetes::Util
execute_kubectl_command(command, error_message)
end

def apply_manifest_server_side(yaml : String, error_message = "Failed to apply manifest") : Util::Shell::CommandResult
command = <<-BASH
kubectl apply --server-side --force-conflicts -f - <<-'EOF'
#{yaml}
EOF
BASH

execute_kubectl_command(command, error_message)
end

def apply_manifest_from_url(url : String, error_message = "Failed to apply manifest") : Util::Shell::CommandResult
command = "kubectl apply -f #{url}"
execute_kubectl_command(command, error_message)
Expand Down
2 changes: 1 addition & 1 deletion src/util/shell.cr
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module Util
end

output = status.success? ? stdout.to_s : stderr.to_s
result = CommandResult.new(output, status.exit_status)
result = CommandResult.new(output, status.exit_code)

unless result.success?
error_msg = error_message.blank? ? "" : error_message
Expand Down
Loading