Class: Porkadot::Install::KubeletList

Inherits:
Object
  • Object
show all
Includes:
SSHKit::DSL
Defined in:
lib/porkadot/install/kubelet.rb

Constant Summary collapse

KUBE_TEMP =
File.join(Porkadot::Install::KUBE_TEMP, 'kubelet')
KUBE_SECRETS_TEMP =
File.join(Porkadot::Install::KUBE_TEMP, '.kubelet')
KUBE_DEFAULT_TEMP =
File.join(Porkadot::Install::KUBE_TEMP, '.default')
KUBE_SECRETS_DEFAULT_TEMP =
File.join(Porkadot::Install::KUBE_TEMP, '.default.kubelet')
ETCD_TEMP =
'/opt/porkadot'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(global_config) ⇒ KubeletList

Returns a new instance of KubeletList.



13
14
15
16
17
18
19
20
# File 'lib/porkadot/install/kubelet.rb', line 13

def initialize global_config
  @global_config = global_config
  @logger = global_config.logger
  @kubelets = {}
  global_config.nodes.each do |k, config|
    @kubelets[k] = Kubelet.new(config)
  end
end

Instance Attribute Details

#global_configObject (readonly)

Returns the value of attribute global_config.



9
10
11
# File 'lib/porkadot/install/kubelet.rb', line 9

def global_config
  @global_config
end

#kubeletsObject (readonly)

Returns the value of attribute kubelets.



11
12
13
# File 'lib/porkadot/install/kubelet.rb', line 11

def kubelets
  @kubelets
end

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/porkadot/install/kubelet.rb', line 10

def logger
  @logger
end

Instance Method Details

#[](name) ⇒ Object



189
190
191
# File 'lib/porkadot/install/kubelet.rb', line 189

def [](name)
  self.kubelets[name]
end

#backup_etcd(host: nil, path: "./backup/etcd.db") ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/porkadot/install/kubelet.rb', line 81

def backup_etcd host: nil, path: "./backup/etcd.db"
  unless host
    self.kubelets.each do |_, v|
      if v.etcd?
        host = v
      end
    end
  end

  on(:local) do |local|
    execute(:mkdir, '-p', File.dirname(path))
  end

  options = self.etcd_options
  on(host) do |host|
    execute(:mkdir, '-p', KUBE_TEMP)
    execute(:"/opt/bin/etcdctl", *options, "snapshot", "save", "#{KUBE_TEMP}/etcd.db")
    download! "#{KUBE_TEMP}/etcd.db", path
  end
end

#etcd_optionsObject



180
181
182
183
184
185
186
187
# File 'lib/porkadot/install/kubelet.rb', line 180

def etcd_options
  %w(
    --cacert /etc/etcd/pki/ca.crt
    --cert /etc/etcd/pki/etcd.crt
    --key /etc/etcd/pki/etcd.key
    --endpoints=https://127.0.0.1:2379
  )
end

#exec(hosts: nil) ⇒ Object



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
# File 'lib/porkadot/install/kubelet.rb', line 53

def exec hosts: nil
  unless hosts
    hosts = []
    self.kubelets.each do |_, v|
      hosts << v
    end
  end

  on(hosts) do |host|
    execute(:mkdir, '-p', Porkadot::Install::KUBE_TEMP)
    if test("[ -d #{KUBE_TEMP} ]")
      execute(:rm, '-rf', KUBE_TEMP)
      execute(:rm, '-rf', KUBE_SECRETS_TEMP)
      execute(:rm, '-rf', KUBE_DEFAULT_TEMP)
      execute(:rm, '-rf', KUBE_SECRETS_DEFAULT_TEMP)
    end
    upload! host.global_config.kubelet_default.target_path, KUBE_TEMP, recursive: true
    upload! host.global_config.kubelet_default.target_secrets_path, KUBE_SECRETS_TEMP, recursive: true
    upload! host.config.target_path, KUBE_DEFAULT_TEMP, recursive: true
    upload! host.config.target_secrets_path, KUBE_SECRETS_DEFAULT_TEMP, recursive: true
    execute(:cp, '-r', KUBE_SECRETS_TEMP + '/*', KUBE_TEMP)
    execute(:cp, '-r', KUBE_DEFAULT_TEMP + '/*', KUBE_TEMP)
    execute(:cp, '-r', KUBE_SECRETS_DEFAULT_TEMP + '/*', KUBE_TEMP)
  end

  return hosts
end

#install(hosts: nil, force: false) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/porkadot/install/kubelet.rb', line 40

def install hosts: nil, force: false
  hosts = self.exec hosts: hosts
  on(hosts) do |host|
    as user: 'root' do
      unless test("[ -f /opt/bin/kubelet-#{host.global_config.k8s.kubernetes_version} ]") && !force
        execute(:bash, File.join(KUBE_TEMP, 'install-deps.sh'))
      end
      execute(:bash, File.join(KUBE_TEMP, 'install-pkgs.sh'))
      execute(:bash, File.join(KUBE_TEMP, 'install.sh'))
    end
  end
end

#restore_etcd(path: "./backup/etcd.db") ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/porkadot/install/kubelet.rb', line 102

def restore_etcd path: "./backup/etcd.db"
  require 'date'
  hosts = []
  self.kubelets.each do |_, v|
    hosts << v if v.etcd?
  end

  options = self.etcd_options
  on(hosts) do |host|
    if test("[ -d #{KUBE_TEMP} ]")
      execute(:rm, '-rf', KUBE_TEMP)
      execute(:rm, '-rf', KUBE_SECRETS_TEMP)
    end
    execute(:mkdir, '-p', KUBE_TEMP)
    upload! path, "#{KUBE_TEMP}/etcd.db"

    as user: 'root' do
      execute(:mkdir, '-p', ETCD_TEMP)
      if test('[ -d /var/lib/etcd ]')
        execute(:mv, '/var/lib/etcd', "${ETCD_TEMP}/data-#{DateTime.now.to_s}")
      end
      execute(:"/opt/bin/etcdctl", *options, "snapshot", "restore", "#{KUBE_TEMP}/etcd.db")
    end
  end
end

#setup_containerd(hosts: nil, force: false) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/porkadot/install/kubelet.rb', line 22

def setup_containerd hosts: nil, force: false
  hosts = self.exec hosts: hosts
  on(hosts) do |host|
    as user: 'root' do
      execute(:bash, File.join(KUBE_TEMP, 'setup-containerd.sh'))
    end
  end
end

#setup_default(hosts: nil, force: false) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/porkadot/install/kubelet.rb', line 31

def setup_default hosts: nil, force: false
  hosts = self.exec hosts: hosts
  on(hosts) do |host|
    as user: 'root' do
      execute(:bash, File.join(KUBE_TEMP, 'setup-node.sh'))
    end
  end
end

#start_etcd(hosts: nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/porkadot/install/kubelet.rb', line 128

def start_etcd hosts: nil
  unless hosts
    hosts = []
    self.kubelets.each do |_, v|
      hosts << v if v.etcd?
    end
  end

  on(hosts) do |host|
    as user: 'root' do
      execute(:mkdir, '-p', ETCD_TEMP)

      result = capture(:"/opt/bin/crictl", 'ps', '-q', '--name', 'etcd')
      with(container_runtime_endpoint: "unix:///run/containerd/containerd.sock") do
        if result.empty?
          info 'Trying to start etcd'
          execute(:mv, "${ETCD_TEMP}/etcd-server.yaml", "/etc/kubernetes/manifests/etcd-server.yaml")
        else
          info 'etcd is already started...'
        end
      end
    end
  end
end

#stop_etcd(hosts: nil) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/porkadot/install/kubelet.rb', line 153

def stop_etcd hosts: nil
  unless hosts
    hosts = []
    self.kubelets.each do |_, v|
      hosts << v if v.etcd?
    end
  end

  on(hosts) do |host|
    as user: 'root' do
      execute(:mkdir, '-p', ETCD_TEMP)

      info "Waiting for etcd to stop..."
      with(container_runtime_endpoint: "unix:///run/containerd/containerd.sock") do
        unless capture(:"/opt/bin/crictl", 'ps', '-q', '--name', 'etcd').empty?
          execute(:mv, "/etc/kubernetes/manifests/etcd-server.yaml", "${ETCD_TEMP}/etcd-server.yaml")
          while capture(:"/opt/bin/crictl", 'ps', '-q', '--name', 'etcd') != ''
            info 'Still waiting for stopping etcd...'
            sleep 5
          end
        end
      end
      info 'etcd was stopped.'
    end
  end
end