-
Notifications
You must be signed in to change notification settings - Fork 7
/
install.go
505 lines (436 loc) · 14.2 KB
/
install.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package cniserver
import (
"bytes"
"context"
"encoding/json"
"fmt"
"html/template"
"os"
"path"
"path/filepath"
"sort"
"strings"
"github.com/containernetworking/cni/libcni"
"github.com/pkg/errors"
"github.com/flomesh-io/fsm/pkg/cni/config"
"github.com/flomesh-io/fsm/pkg/cni/util"
)
const (
fsmCniName = "fsm-cni"
kubeConfigFileName = "ZZZ-fsm-cni-kubeconfig"
//#nosec G101
tokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token"
listSuffix = "list"
kubeConfigTemplate = `# Kubeconfig file for FSM CNI plugin.
apiVersion: v1
kind: Config
clusters:
- name: local
cluster:
server: {{.KubernetesServiceProtocol}}://[{{.KubernetesServiceHost}}]:{{.KubernetesServicePort}}
{{.TLSConfig}}
users:
- name: fsm-cni
user:
token: "{{.ServiceAccountToken}}"
contexts:
- name: fsm-cni-context
context:
cluster: local
user: fsm-cni
current-context: fsm-cni-context
`
)
type installer struct {
kubeConfigFilepath string
cniConfigFilepath string
}
type kubeconfigFields struct {
KubernetesServiceProtocol string
KubernetesServiceHost string
KubernetesServicePort string
ServiceAccountToken string
TLSConfig string
}
// newInstaller returns an instance of Installer with the given config
func newInstaller() *installer {
return &installer{}
}
// Run starts the installation process, verifies the configuration, then sleeps.
// If an invalid configuration is detected, the installation process will restart to restore a valid state.
func (in *installer) Run(ctx context.Context, cniReady chan struct{}) error {
for {
if err := copyBinaries(); err != nil {
return err
}
saToken, err := readServiceAccountToken()
if err != nil {
return err
}
if in.kubeConfigFilepath, err = createKubeconfigFile(saToken); err != nil {
return err
}
if in.cniConfigFilepath, err = createCNIConfigFile(ctx); err != nil {
return err
}
if len(cniReady) == 0 {
cniReady <- struct{}{}
}
if err = sleepCheckInstall(ctx, in.cniConfigFilepath); err != nil {
return err
}
log.Info().Msg("Restarting CNI installer...")
}
}
// Cleanup remove CNI's config, kubeconfig file, and binaries.
func (in *installer) Cleanup() error {
log.Info().Msg("Cleaning up.")
if len(in.cniConfigFilepath) > 0 && util.Exists(in.cniConfigFilepath) {
log.Info().Msgf("Removing CNI config from CNI config file: %s", in.cniConfigFilepath)
// Read JSON from CNI config file
cniConfigMap, err := readCNIConfigMap(in.cniConfigFilepath)
if err != nil {
return err
}
// Find CNI and remove from plugin list
plugins, err := util.GetPlugins(cniConfigMap)
if err != nil {
return errors.Wrap(err, in.cniConfigFilepath)
}
for i, rawPlugin := range plugins {
plugin, err := util.GetPlugin(rawPlugin)
if err != nil {
return errors.Wrap(err, in.cniConfigFilepath)
}
if plugin["type"] == fsmCniName {
cniConfigMap["plugins"] = append(plugins[:i], plugins[i+1:]...)
break
}
}
cniConfig, err := util.MarshalCNIConfig(cniConfigMap)
if err != nil {
return err
}
if err = util.AtomicWrite(in.cniConfigFilepath, cniConfig, os.FileMode(0o644)); err != nil {
return err
}
}
if len(in.kubeConfigFilepath) > 0 && util.Exists(in.kubeConfigFilepath) {
log.Info().Msgf("Removing CNI kubeconfig file: %s", in.kubeConfigFilepath)
if err := os.Remove(in.kubeConfigFilepath); err != nil {
return err
}
}
log.Info().Msg("Removing existing binaries")
cniBinPath := path.Join(config.CNIBinDir, fsmCniName)
if util.Exists(cniBinPath) {
if err := os.Remove(cniBinPath); err != nil {
return err
}
}
return nil
}
func createCNIConfigFile(ctx context.Context) (string, error) {
// TODO(dddddai): support ExcludeNamespaces?
cniConfig := fmt.Sprintf(`
{
"type": "fsm-cni",
"kubernetes": {
"kubeconfig": "/etc/cni/net.d/%s"
}
}`, kubeConfigFileName)
return writeCNIConfig(ctx, []byte(cniConfig))
}
func insertCNIConfig(cniConfig, existingCNIConfig []byte) ([]byte, error) {
var pluginMap map[string]interface{}
if err := json.Unmarshal(cniConfig, &pluginMap); err != nil {
return nil, fmt.Errorf("error loading CNI config (JSON error): %v", err)
}
var existingMap map[string]interface{}
if err := json.Unmarshal(existingCNIConfig, &existingMap); err != nil {
return nil, fmt.Errorf("error loading existing CNI config (JSON error): %v", err)
}
var newMap map[string]interface{}
if _, ok := existingMap["type"]; ok {
// Assume it is a regular network conf file
delete(existingMap, "cniVersion")
plugins := make([]map[string]interface{}, 2)
plugins[0] = existingMap
plugins[1] = pluginMap
newMap = map[string]interface{}{
"name": "k8s-pod-network",
"cniVersion": "0.3.1",
"plugins": plugins,
}
} else {
// Assume it is a network list file
newMap = existingMap
plugins, err := util.GetPlugins(newMap)
if err != nil {
return nil, fmt.Errorf("existing CNI config: %v", err)
}
for _, rawPlugin := range plugins {
plugin, err := util.GetPlugin(rawPlugin)
if err != nil {
return nil, fmt.Errorf("existing CNI plugin: %v", err)
}
if plugin["type"] == fsmCniName {
// it already contains fsm-cni
return util.MarshalCNIConfig(newMap)
}
}
newMap["plugins"] = append(plugins, pluginMap)
}
return util.MarshalCNIConfig(newMap)
}
func writeCNIConfig(ctx context.Context, cniConfig []byte) (string, error) {
cniConfigFilepath, err := getCNIConfigFilepath(ctx)
if err != nil {
return "", err
}
// This section overwrites an existing plugins list entry for fsm-cni
//#nosec G304
existingCNIConfig, err := os.ReadFile(cniConfigFilepath)
if err != nil {
return "", err
}
mergeConfig, err := insertCNIConfig(cniConfig, existingCNIConfig)
if err != nil {
return "", err
}
if err = util.AtomicWrite(cniConfigFilepath, mergeConfig, os.FileMode(0o644)); err != nil {
return "", err
}
if strings.HasSuffix(cniConfigFilepath, ".conf") {
// If the old CNI config filename ends with .conf, rename it to .conflist, because it has to be changed to a list
log.Info().Msgf("Renaming %s extension to .conflist", cniConfigFilepath)
err = os.Rename(cniConfigFilepath, cniConfigFilepath+listSuffix)
if err != nil {
return "", err
}
cniConfigFilepath += listSuffix
}
log.Info().Msgf("Created CNI config %s", cniConfigFilepath)
return cniConfigFilepath, nil
}
// If configured as chained CNI plugin, waits indefinitely for a main CNI config file to exist before returning
// Or until cancelled by parent context
func getCNIConfigFilepath(ctx context.Context) (string, error) {
watcher, fileModified, errChan, err := util.CreateFileWatcher(config.CNIConfigDir)
if err != nil {
return "", err
}
defer func() {
_ = watcher.Close()
}()
filename, err := getDefaultCNINetwork(config.CNIConfigDir)
for len(filename) == 0 {
filename, err = getDefaultCNINetwork(config.CNIConfigDir)
if err == nil {
break
}
if err = util.WaitForFileMod(ctx, fileModified, errChan); err != nil {
return "", err
}
}
cniConfigFilepath := filepath.Join(config.CNIConfigDir, filename)
for !util.Exists(cniConfigFilepath) {
if strings.HasSuffix(cniConfigFilepath, ".conf") && util.Exists(cniConfigFilepath+listSuffix) {
log.Info().Msgf("%s doesn't exist, but %[1]slist does; Using it as the CNI config file instead.", cniConfigFilepath)
cniConfigFilepath += listSuffix
} else if strings.HasSuffix(cniConfigFilepath, ".conflist") && util.Exists(cniConfigFilepath[:len(cniConfigFilepath)-4]) {
log.Info().Msgf("%s doesn't exist, but %s does; Using it as the CNI config file instead.", cniConfigFilepath, cniConfigFilepath[:len(cniConfigFilepath)-4])
cniConfigFilepath = cniConfigFilepath[:len(cniConfigFilepath)-4]
} else {
log.Info().Msgf("CNI config file %s does not exist. Waiting for file to be written...", cniConfigFilepath)
if err = util.WaitForFileMod(ctx, fileModified, errChan); err != nil {
return "", err
}
}
}
log.Info().Msgf("CNI config file %s exists. Proceeding.", cniConfigFilepath)
return cniConfigFilepath, err
}
// sleepCheckInstall verifies the configuration then blocks until an invalid configuration is detected, and return nil.
// If an error occurs or context is canceled, the function will return the error.
// Returning from this function will set the pod to "NotReady".
func sleepCheckInstall(ctx context.Context, cniConfigFilepath string) error {
// Create file watcher before checking for installation
// so that no file modifications are missed while and after checking
watcher, fileModified, errChan, err := util.CreateFileWatcher(config.CNIConfigDir)
if err != nil {
return err
}
defer func() {
_ = watcher.Close()
}()
for {
if checkErr := checkInstall(cniConfigFilepath); checkErr != nil {
// Pod set to "NotReady" due to invalid configuration
log.Info().Msgf("Invalid configuration. %v", checkErr)
return nil
}
// Check if file has been modified or if an error has occurred during checkInstall before setting isReady to true
select {
case <-fileModified:
return nil
case err := <-errChan:
return err
case <-ctx.Done():
return ctx.Err()
default:
// Valid configuration; set isReady to true and wait for modifications before checking again
if err = util.WaitForFileMod(ctx, fileModified, errChan); err != nil {
// Pod set to "NotReady" before termination
return err
}
}
}
}
func copyBinaries() error {
srcFile := "/app/fsm-cni"
if util.IsDirWriteable(config.CNIBinDir) != nil {
return fmt.Errorf("directory %s is not writable", config.CNIBinDir)
}
err := util.AtomicCopy(srcFile, config.CNIBinDir, fsmCniName)
if err != nil {
return err
}
log.Info().Msgf("Copied %s to %s.", srcFile, config.CNIBinDir)
return nil
}
// checkInstall returns an error if an invalid CNI configuration is detected
func checkInstall(cniConfigFilepath string) error {
defaultCNIConfigFilename, err := getDefaultCNINetwork(config.CNIConfigDir)
if err != nil {
return err
}
defaultCNIConfigFilepath := filepath.Join(config.CNIConfigDir, defaultCNIConfigFilename)
if defaultCNIConfigFilepath != cniConfigFilepath {
return fmt.Errorf("cni config file %s preempted by %s", cniConfigFilepath, defaultCNIConfigFilepath)
}
if !util.Exists(cniConfigFilepath) {
return fmt.Errorf("cni config file removed: %s", cniConfigFilepath)
}
// Verify that CNI config exists in the CNI config plugin list
cniConfigMap, err := readCNIConfigMap(cniConfigFilepath)
if err != nil {
return err
}
plugins, err := util.GetPlugins(cniConfigMap)
if err != nil {
return errors.Wrap(err, cniConfigFilepath)
}
for _, rawPlugin := range plugins {
plugin, err := util.GetPlugin(rawPlugin)
if err != nil {
return errors.Wrap(err, cniConfigFilepath)
}
if plugin["type"] == fsmCniName {
return nil
}
}
return fmt.Errorf("CNI config removed from CNI config file: %s", cniConfigFilepath)
}
// Read CNI config from file and return the unmarshalled JSON as a map
func readCNIConfigMap(path string) (map[string]interface{}, error) {
//#nosec G304
cniConfig, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var cniConfigMap map[string]interface{}
if err = json.Unmarshal(cniConfig, &cniConfigMap); err != nil {
return nil, errors.Wrap(err, path)
}
return cniConfigMap, nil
}
func createKubeconfigFile(saToken string) (kubeconfigFilepath string, err error) {
tpl, err := template.New("kubeconfig").Parse(kubeConfigTemplate)
if err != nil {
return
}
protocol := os.Getenv("KUBERNETES_SERVICE_PROTOCOL")
if protocol == "" {
protocol = "https"
}
// TODO: support tls
tlsConfig := "insecure-skip-tls-verify: true"
fields := kubeconfigFields{
KubernetesServiceProtocol: protocol,
KubernetesServiceHost: os.Getenv("KUBERNETES_SERVICE_HOST"),
KubernetesServicePort: os.Getenv("KUBERNETES_SERVICE_PORT"),
ServiceAccountToken: saToken,
TLSConfig: tlsConfig,
}
var kcbb bytes.Buffer
if err := tpl.Execute(&kcbb, fields); err != nil {
return "", err
}
var kcbbToPrint bytes.Buffer
fields.ServiceAccountToken = "<redacted>"
if err := tpl.Execute(&kcbbToPrint, fields); err != nil {
return "", err
}
kubeconfigFilepath = filepath.Join(config.CNIConfigDir, kubeConfigFileName)
log.Info().Msgf("write kubeconfig file %s with: \n%+v", kubeconfigFilepath, kcbbToPrint.String())
if err = util.AtomicWrite(kubeconfigFilepath, kcbb.Bytes(), os.FileMode(0o600)); err != nil {
return "", err
}
return
}
func readServiceAccountToken() (string, error) {
if !util.Exists(tokenPath) {
return "", fmt.Errorf("service account token file %s does not exist. Is this not running within a pod?", tokenPath)
}
token, err := os.ReadFile(tokenPath)
if err != nil {
return "", err
}
return string(token), nil
}
// Follows the same semantics as kubelet
// https://github.com/kubernetes/kubernetes/blob/954996e231074dc7429f7be1256a579bedd8344c/pkg/kubelet/dockershim/network/cni/cni.go#L144-L184
func getDefaultCNINetwork(confDir string) (string, error) {
files, err := libcni.ConfFiles(confDir, []string{".conf", ".conflist"})
switch {
case err != nil:
return "", err
case len(files) == 0:
return "", fmt.Errorf("no networks found in %s", confDir)
}
sort.Strings(files)
for _, confFile := range files {
var confList *libcni.NetworkConfigList
if strings.HasSuffix(confFile, ".conflist") {
confList, err = libcni.ConfListFromFile(confFile)
if err != nil {
log.Warn().Msgf("Error loading CNI config list file %s: %v", confFile, err)
continue
}
} else {
conf, err := libcni.ConfFromFile(confFile)
if err != nil {
log.Warn().Msgf("Error loading CNI config file %s: %v", confFile, err)
continue
}
// Ensure the config has a "type" so we know what plugin to run.
// Also catches the case where somebody put a conflist into a conf file.
if conf.Network.Type == "" {
log.Warn().Msgf("Error loading CNI config file %s: no 'type'; perhaps this is a .conflist?", confFile)
continue
}
confList, err = libcni.ConfListFromConf(conf)
if err != nil {
log.Warn().Msgf("Error converting CNI config file %s to list: %v", confFile, err)
continue
}
}
if len(confList.Plugins) == 0 {
log.Warn().Msgf("CNI config list %s has no networks, skipping", confList.Name)
continue
}
return filepath.Base(confFile), nil
}
return "", fmt.Errorf("no valid networks found in %s", confDir)
}