Class: Kubernetes::Client

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/hetzner/kubernetes/client.rb

Constant Summary

Constants included from Utils

Utils::CMD_FILE_PATH

Instance Method Summary collapse

Methods included from Utils

#run, #ssh, #verify_host_key, #wait_for_ssh, #which, #write_file

Constructor Details

#initialize(configuration:) ⇒ Client

Returns a new instance of Client.



9
10
11
# File 'lib/hetzner/kubernetes/client.rb', line 9

def initialize(configuration:)
  @configuration = configuration
end

Instance Method Details

#deploy(masters:, workers:, master_definitions:, worker_definitions:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hetzner/kubernetes/client.rb', line 13

def deploy(masters:, workers:, master_definitions:, worker_definitions:)
  @masters = masters
  @workers = workers
  @master_definitions = master_definitions
  @worker_definitions = worker_definitions

  @kube_api_server_args = configuration.fetch('kube_api_server_args', [])
  @kube_scheduler_args = configuration.fetch('kube_scheduler_args', [])
  @kube_controller_manager_args = configuration.fetch('kube_controller_manager_args', [])
  @kube_cloud_controller_manager_args = configuration.fetch('kube_cloud_controller_manager_args', [])
  @kubelet_args = configuration.fetch('kubelet_args', [])
  @kube_proxy_args = configuration.fetch('kube_proxy_args', [])
  @private_ssh_key_path = File.expand_path(configuration['private_ssh_key_path'])
  @public_ssh_key_path = File.expand_path(configuration['public_ssh_key_path'])
  @cluster_name = configuration['cluster_name']

  set_up_k3s

  update_nodes

  post_setup_deployments
end

#upgradeObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/hetzner/kubernetes/client.rb', line 36

def upgrade
  worker_upgrade_concurrency = workers.size - 1
  worker_upgrade_concurrency = 1 if worker_upgrade_concurrency.zero?

  cmd = <<~BASH
    kubectl apply -f - <<-EOF
      apiVersion: upgrade.cattle.io/v1
      kind: Plan
      metadata:
        name: k3s-server
        namespace: system-upgrade
        labels:
          k3s-upgrade: server
      spec:
        concurrency: 1
        version: #{new_k3s_version}
        nodeSelector:
          matchExpressions:
            - {key: node-role.kubernetes.io/master, operator: In, values: ["true"]}
        serviceAccountName: system-upgrade
        tolerations:
        - key: "CriticalAddonsOnly"
          operator: "Equal"
          value: "true"
          effect: "NoExecute"
        cordon: true
        upgrade:
          image: rancher/k3s-upgrade
    EOF
  BASH

  run cmd, kubeconfig_path: kubeconfig_path

  cmd = <<~BASH
    kubectl apply -f - <<-EOF
      apiVersion: upgrade.cattle.io/v1
      kind: Plan
      metadata:
        name: k3s-agent
        namespace: system-upgrade
        labels:
          k3s-upgrade: agent
      spec:
        concurrency: #{worker_upgrade_concurrency}
        version: #{new_k3s_version}
        nodeSelector:
          matchExpressions:
            - {key: node-role.kubernetes.io/master, operator: NotIn, values: ["true"]}
        serviceAccountName: system-upgrade
        prepare:
          image: rancher/k3s-upgrade
          args: ["prepare", "k3s-server"]
        cordon: true
        upgrade:
          image: rancher/k3s-upgrade
    EOF
  BASH

  run cmd, kubeconfig_path: kubeconfig_path

  puts 'Upgrade will now start. Run `watch kubectl get nodes` to see the nodes being upgraded. This should take a few minutes for a small cluster.'
  puts 'The API server may be briefly unavailable during the upgrade of the controlplane.'

  updated_configuration = configuration.raw
  updated_configuration['k3s_version'] = new_k3s_version

  File.write(config_file, updated_configuration.to_yaml)
end