-
Notifications
You must be signed in to change notification settings - Fork 372
/
kubelet.go
340 lines (292 loc) · 9.8 KB
/
kubelet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
Copyright 2022 k0s authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package worker
import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/avast/retry-go"
"github.com/docker/libnetwork/resolvconf"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/validation"
kubeletv1beta1 "k8s.io/kubelet/config/v1beta1"
"k8s.io/utils/pointer"
"sigs.k8s.io/yaml"
"github.com/k0sproject/k0s/internal/pkg/dir"
"github.com/k0sproject/k0s/internal/pkg/flags"
"github.com/k0sproject/k0s/internal/pkg/stringmap"
"github.com/k0sproject/k0s/pkg/assets"
"github.com/k0sproject/k0s/pkg/component"
"github.com/k0sproject/k0s/pkg/constant"
"github.com/k0sproject/k0s/pkg/supervisor"
)
// Kubelet is the component implementation to manage kubelet
type Kubelet struct {
CRISocket string
EnableCloudProvider bool
K0sVars constant.CfgVars
KubeletConfigClient *KubeletConfigClient
LogLevel string
Profile string
dataDir string
supervisor supervisor.Supervisor
ClusterDNS string
Labels []string
Taints []string
ExtraArgs string
}
var _ component.Component = (*Kubelet)(nil)
type kubeletConfig struct {
ClientCAFile string
VolumePluginDir string
KubeReservedCgroup string
KubeletCgroups string
CgroupsPerQOS bool
ResolvConf string
}
// Init extracts the needed binaries
func (k *Kubelet) Init(_ context.Context) error {
cmds := []string{"kubelet", "xtables-legacy-multi"}
if runtime.GOOS == "windows" {
cmds = []string{"kubelet.exe"}
}
for _, cmd := range cmds {
err := assets.Stage(k.K0sVars.BinDir, cmd, constant.BinDirMode)
if err != nil {
return err
}
}
if runtime.GOOS == "linux" {
for _, symlink := range []string{"iptables-save", "iptables-restore", "ip6tables", "ip6tables-save", "ip6tables-restore"} {
symlinkPath := filepath.Join(k.K0sVars.BinDir, symlink)
// remove if it exist and ignore error if it doesn't
_ = os.Remove(symlinkPath)
err := os.Symlink("xtables-legacy-multi", symlinkPath)
if err != nil {
return fmt.Errorf("failed to create symlink %s: %w", symlink, err)
}
}
}
k.dataDir = filepath.Join(k.K0sVars.DataDir, "kubelet")
err := dir.Init(k.dataDir, constant.DataDirMode)
if err != nil {
return fmt.Errorf("failed to create %s: %w", k.dataDir, err)
}
return nil
}
// Run runs kubelet
func (k *Kubelet) Run(ctx context.Context) error {
cmd := "kubelet"
kubeletConfigData := kubeletConfig{
ClientCAFile: filepath.Join(k.K0sVars.CertRootDir, "ca.crt"),
VolumePluginDir: k.K0sVars.KubeletVolumePluginDir,
KubeReservedCgroup: "system.slice",
KubeletCgroups: "/system.slice/containerd.service",
}
if runtime.GOOS == "windows" {
cmd = "kubelet.exe"
}
logrus.Info("Starting kubelet")
kubeletConfigPath := filepath.Join(k.K0sVars.DataDir, "kubelet-config.yaml")
// get the "real" resolv.conf file (in systemd-resolvd bases system,
// this will return /run/systemd/resolve/resolv.conf
resolvConfPath := resolvconf.Path()
args := stringmap.StringMap{
"--root-dir": k.dataDir,
"--config": kubeletConfigPath,
"--bootstrap-kubeconfig": k.K0sVars.KubeletBootstrapConfigPath,
"--kubeconfig": k.K0sVars.KubeletAuthConfigPath,
"--v": k.LogLevel,
"--runtime-cgroups": "/system.slice/containerd.service",
"--cert-dir": filepath.Join(k.dataDir, "pki"),
}
if len(k.Labels) > 0 {
args["--node-labels"] = strings.Join(k.Labels, ",")
}
if runtime.GOOS == "windows" {
node, err := getNodeName(ctx)
if err != nil {
return fmt.Errorf("can't get hostname: %v", err)
}
kubeletConfigData.CgroupsPerQOS = false
kubeletConfigData.ResolvConf = ""
args["--enforce-node-allocatable"] = ""
args["--pod-infra-container-image"] = "mcr.microsoft.com/oss/kubernetes/pause:1.4.1"
args["--network-plugin"] = "cni"
args["--cni-bin-dir"] = "C:\\k\\cni"
args["--cni-conf-dir"] = "C:\\k\\cni\\config"
args["--hostname-override"] = node
args["--hairpin-mode"] = "promiscuous-bridge"
args["--cert-dir"] = "C:\\var\\lib\\k0s\\kubelet_certs"
} else {
kubeletConfigData.CgroupsPerQOS = true
kubeletConfigData.ResolvConf = resolvConfPath
}
if k.CRISocket != "" {
// Due to the removal of dockershim from kube 1.24, we no longer need to
// handle any special docker case
_, rtSock, err := SplitRuntimeConfig(k.CRISocket)
if err != nil {
return err
}
args["--container-runtime-endpoint"] = rtSock
} else {
sockPath := path.Join(k.K0sVars.RunDir, "containerd.sock")
args["--container-runtime-endpoint"] = fmt.Sprintf("unix://%s", sockPath)
}
// We only support external providers
if k.EnableCloudProvider {
args["--cloud-provider"] = "external"
}
// Handle the extra args as last so they can be used to overrride some k0s "hardcodings"
if k.ExtraArgs != "" {
extras := flags.Split(k.ExtraArgs)
args.Merge(extras)
}
logrus.Debugf("starting kubelet with args: %v", args)
k.supervisor = supervisor.Supervisor{
Name: cmd,
BinPath: assets.BinPath(cmd, k.K0sVars.BinDir),
RunDir: k.K0sVars.RunDir,
DataDir: k.K0sVars.DataDir,
Args: args.ToArgs(),
}
err := retry.Do(func() error {
kubeletconfig, err := k.KubeletConfigClient.Get(ctx, k.Profile)
if err != nil {
logrus.Warnf("failed to get initial kubelet config with join token: %s", err.Error())
return err
}
kubeletconfig, err = k.prepareLocalKubeletConfig(kubeletconfig, kubeletConfigData)
if err != nil {
logrus.Warnf("failed to prepare local kubelet config: %s", err.Error())
return err
}
err = ioutil.WriteFile(kubeletConfigPath, []byte(kubeletconfig), 0644)
if err != nil {
return fmt.Errorf("failed to write kubelet config: %w", err)
}
return nil
},
retry.Context(ctx),
retry.Delay(time.Millisecond*500),
retry.DelayType(retry.BackOffDelay))
if err != nil {
return err
}
return k.supervisor.Supervise()
}
// Stop stops kubelet
func (k *Kubelet) Stop() error {
return k.supervisor.Stop()
}
// Health-check interface
func (k *Kubelet) Healthy() error { return nil }
func (k *Kubelet) prepareLocalKubeletConfig(kubeletconfig string, kubeletConfigData kubeletConfig) (string, error) {
var kubeletConfiguration kubeletv1beta1.KubeletConfiguration
err := yaml.Unmarshal([]byte(kubeletconfig), &kubeletConfiguration)
if err != nil {
return "", fmt.Errorf("can't unmarshal kubelet config: %v", err)
}
kubeletConfiguration.Authentication.X509.ClientCAFile = kubeletConfigData.ClientCAFile // filepath.Join(k.K0sVars.CertRootDir, "ca.crt")
kubeletConfiguration.VolumePluginDir = kubeletConfigData.VolumePluginDir // k.K0sVars.KubeletVolumePluginDir
kubeletConfiguration.KubeReservedCgroup = kubeletConfigData.KubeReservedCgroup
kubeletConfiguration.KubeletCgroups = kubeletConfigData.KubeletCgroups
kubeletConfiguration.ResolverConfig = pointer.String(kubeletConfigData.ResolvConf)
kubeletConfiguration.CgroupsPerQOS = pointer.Bool(kubeletConfigData.CgroupsPerQOS)
if len(k.Taints) > 0 {
var taints []corev1.Taint
for _, taint := range k.Taints {
parsedTaint, err := parseTaint(taint)
if err != nil {
return "", fmt.Errorf("can't parse taints for profile config map: %v", err)
}
taints = append(taints, parsedTaint)
}
kubeletConfiguration.RegisterWithTaints = taints
}
localKubeletConfig, err := yaml.Marshal(kubeletConfiguration)
if err != nil {
return "", fmt.Errorf("can't marshal kubelet config: %v", err)
}
return string(localKubeletConfig), nil
}
const awsMetaInformationURI = "http://169.254.169.254/latest/meta-data/local-hostname"
func getNodeName(ctx context.Context) (string, error) {
req, err := http.NewRequest("GET", awsMetaInformationURI, nil)
if err != nil {
return "", err
}
req = req.WithContext(ctx)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return os.Hostname()
}
defer resp.Body.Close()
h, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("can't read aws hostname: %v", err)
}
return string(h), nil
}
func parseTaint(st string) (corev1.Taint, error) {
var taint corev1.Taint
var key string
var value string
var effect corev1.TaintEffect
parts := strings.Split(st, ":")
switch len(parts) {
case 1:
key = parts[0]
case 2:
effect = corev1.TaintEffect(parts[1])
if err := validateTaintEffect(effect); err != nil {
return taint, err
}
partsKV := strings.Split(parts[0], "=")
if len(partsKV) > 2 {
return taint, fmt.Errorf("invalid taint spec: %v", st)
}
key = partsKV[0]
if len(partsKV) == 2 {
value = partsKV[1]
if errs := validation.IsValidLabelValue(value); len(errs) > 0 {
return taint, fmt.Errorf("invalid taint spec: %v, %s", st, strings.Join(errs, "; "))
}
}
default:
return taint, fmt.Errorf("invalid taint spec: %v", st)
}
if errs := validation.IsQualifiedName(key); len(errs) > 0 {
return taint, fmt.Errorf("invalid taint spec: %v, %s", st, strings.Join(errs, "; "))
}
taint.Key = key
taint.Value = value
taint.Effect = effect
return taint, nil
}
func validateTaintEffect(effect corev1.TaintEffect) error {
if effect != corev1.TaintEffectNoSchedule && effect != corev1.TaintEffectPreferNoSchedule && effect != corev1.TaintEffectNoExecute {
return fmt.Errorf("invalid taint effect: %v, unsupported taint effect", effect)
}
return nil
}