-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
cell.go
111 lines (88 loc) · 3.48 KB
/
cell.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package cni
import (
"context"
"fmt"
"path"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/controller"
"github.com/cilium/cilium/pkg/hive/cell"
"github.com/cilium/cilium/pkg/option"
cnitypes "github.com/cilium/cilium/plugins/cilium-cni/types"
)
var Cell = cell.Module(
"cni-config",
"CNI configuration manager",
cell.Config(defaultConfig),
cell.Provide(enableConfigManager),
)
type Config struct {
WriteCNIConfWhenReady string
ReadCNIConf string
CNIChainingMode string
CNILogFile string
CNIExclusive bool
CNIChainingTarget string
CNIExternalRouting bool
}
type CNIConfigManager interface {
// GetMTU provides the MTU from the provided CNI configuration file.
// This is only useful if ReadCNIConfiguration is set *and* the file specifies an MTU.
GetMTU() int
// GetChainingMode returns the configured CNI chaining mode
GetChainingMode() string
// Status returns the status of the CNI manager.
// Cannot return nil.
Status() *models.Status
GetCustomNetConf() *cnitypes.NetConf
// ExternalRoutingEnabled returns true if the chained plugin implements
// routing for Endpoints (Pods).
ExternalRoutingEnabled() bool
}
var defaultConfig = Config{
CNIChainingMode: "none",
CNILogFile: "/var/run/cilium/cilium-cni.log",
}
func (cfg Config) Flags(flags *pflag.FlagSet) {
flags.String(option.WriteCNIConfigurationWhenReady, defaultConfig.WriteCNIConfWhenReady, "Write the CNI configuration to the specified path when agent is ready")
flags.String(option.ReadCNIConfiguration, defaultConfig.ReadCNIConf, fmt.Sprintf("CNI configuration file to use as a source for --%s. If not supplied, a suitable one will be generated.", option.WriteCNIConfigurationWhenReady))
flags.String(option.CNIChainingMode, defaultConfig.CNIChainingMode, "Enable CNI chaining with the specified plugin")
flags.String(option.CNILogFile, defaultConfig.CNILogFile, "Path where the CNI plugin should write logs")
flags.String(option.CNIChainingTarget, defaultConfig.CNIChainingTarget, "CNI network name into which to insert the Cilium chained configuration. Use '*' to select any network.")
flags.Bool(option.CNIExclusive, defaultConfig.CNIExclusive, "Whether to remove other CNI configurations")
flags.Bool(option.CNIExternalRouting, defaultConfig.CNIExternalRouting, "Whether the chained CNI plugin handles routing on the node")
}
func enableConfigManager(lc cell.Lifecycle, log logrus.FieldLogger, cfg Config, dcfg *option.DaemonConfig /*only for .Debug*/) CNIConfigManager {
c := newConfigManager(log, cfg, dcfg.Debug)
lc.Append(c)
return c
}
func newConfigManager(log logrus.FieldLogger, cfg Config, debug bool) *cniConfigManager {
if cfg.CNIChainingMode == "aws-cni" && cfg.CNIChainingTarget == "" {
cfg.CNIChainingTarget = "aws-cni"
cfg.CNIExternalRouting = true
}
if cfg.CNIChainingTarget != "" && cfg.CNIChainingMode == "" {
cfg.CNIChainingMode = "generic-veth"
}
if cfg.CNIChainingMode == "" {
cfg.CNIChainingMode = "none"
}
s := models.Status{
Msg: "CNI controller not started",
State: models.StatusStateFailure,
}
c := &cniConfigManager{
config: cfg,
debug: debug,
log: log,
controller: controller.NewManager(),
}
c.status.Store(&s)
c.cniConfDir, c.cniConfFile = path.Split(cfg.WriteCNIConfWhenReady)
c.ctx, c.doneFunc = context.WithCancel(context.Background())
return c
}