libnetwork/config: merge DaemonCfg into Config

It was unclear what the distinction was between these configuration
structs, so merging them to simplify.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-09-23 19:40:11 +02:00
parent 571baffd59
commit 528428919e
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
9 changed files with 40 additions and 47 deletions

View file

@ -128,7 +128,7 @@ func (iface *lnInterface) Set(s *specs.Spec) error {
s.Hooks = &specs.Hooks{
Prestart: []specs.Hook{{
Path: filepath.Join("/proc", strconv.Itoa(os.Getpid()), "exe"),
Args: []string{"libnetwork-setkey", "-exec-root=" + iface.provider.Config().Daemon.ExecRoot, iface.sbx.ContainerID(), shortNetCtlrID},
Args: []string{"libnetwork-setkey", "-exec-root=" + iface.provider.Config().ExecRoot, iface.sbx.ContainerID(), shortNetCtlrID},
}},
}
return nil

View file

@ -465,7 +465,7 @@ func (daemon *Daemon) updateContainerNetworkSettings(container *container.Contai
networkName := mode.NetworkName()
if mode.IsDefault() {
networkName = daemon.netController.Config().Daemon.DefaultNetwork
networkName = daemon.netController.Config().DefaultNetwork
}
if mode.IsUserDefined() {

View file

@ -108,7 +108,7 @@ func (daemon *Daemon) GetNetworkByName(name string) (libnetwork.Network, error)
return nil, libnetwork.ErrNoSuchNetwork(name)
}
if name == "" {
name = c.Config().Daemon.DefaultNetwork
name = c.Config().DefaultNetwork
}
return c.NetworkByName(name)
}
@ -316,7 +316,7 @@ func (daemon *Daemon) createNetwork(create types.NetworkCreateRequest, id string
c := daemon.netController
driver := create.Driver
if driver == "" {
driver = c.Config().Daemon.DefaultDriver
driver = c.Config().DefaultDriver
}
nwOptions := []libnetwork.NetworkOption{

View file

@ -223,7 +223,7 @@ func (c *controller) agentSetup(clusterProvider cluster.Provider) error {
listenAddr, _, _ := net.SplitHostPort(listen)
logrus.Infof("Initializing Libnetwork Agent Listen-Addr=%s Local-addr=%s Adv-addr=%s Data-addr=%s Remote-addr-list=%v MTU=%d",
listenAddr, bindAddr, advAddr, dataAddr, remoteAddrList, c.Config().Daemon.NetworkControlPlaneMTU)
listenAddr, bindAddr, advAddr, dataAddr, remoteAddrList, c.Config().NetworkControlPlaneMTU)
if advAddr != "" && agent == nil {
if err := c.agentInit(listenAddr, bindAddr, advAddr, dataAddr); err != nil {
logrus.Errorf("error in agentInit: %v", err)
@ -295,12 +295,12 @@ func (c *controller) agentInit(listenAddr, bindAddrOrInterface, advertiseAddr, d
netDBConf.BindAddr = listenAddr
netDBConf.AdvertiseAddr = advertiseAddr
netDBConf.Keys = keys
if c.Config().Daemon.NetworkControlPlaneMTU != 0 {
if c.Config().NetworkControlPlaneMTU != 0 {
// Consider the MTU remove the IP hdr (IPv4 or IPv6) and the TCP/UDP hdr.
// To be on the safe side let's cut 100 bytes
netDBConf.PacketBufferSize = (c.Config().Daemon.NetworkControlPlaneMTU - 100)
netDBConf.PacketBufferSize = (c.Config().NetworkControlPlaneMTU - 100)
logrus.Debugf("Control plane MTU: %d will initialize NetworkDB with: %d",
c.Config().Daemon.NetworkControlPlaneMTU, netDBConf.PacketBufferSize)
c.Config().NetworkControlPlaneMTU, netDBConf.PacketBufferSize)
}
nDB, err := networkdb.New(netDBConf)
if err != nil {

View file

@ -20,14 +20,6 @@ const (
// Config encapsulates configurations of various Libnetwork components
type Config struct {
Daemon DaemonCfg
Scopes map[string]*datastore.ScopeCfg
ActiveSandboxes map[string]interface{}
PluginGetter plugingetter.PluginGetter
}
// DaemonCfg represents libnetwork core configuration
type DaemonCfg struct {
DataDir string
ExecRoot string
DefaultNetwork string
@ -37,6 +29,9 @@ type DaemonCfg struct {
ClusterProvider cluster.Provider
NetworkControlPlaneMTU int
DefaultAddressPool []*ipamutils.NetworkToSplit
Scopes map[string]*datastore.ScopeCfg
ActiveSandboxes map[string]interface{}
PluginGetter plugingetter.PluginGetter
}
// LoadDefaultScopes loads default scope configs for scopes which
@ -53,10 +48,8 @@ func (c *Config) LoadDefaultScopes(dataDir string) {
// a reference to the corresponding Config structure
func ParseConfigOptions(opts ...Option) *Config {
cfg := &Config{
Daemon: DaemonCfg{
DriverCfg: make(map[string]interface{}),
},
Scopes: make(map[string]*datastore.ScopeCfg),
DriverCfg: make(map[string]interface{}),
Scopes: make(map[string]*datastore.ScopeCfg),
}
for _, opt := range opts {
if opt != nil {
@ -64,7 +57,7 @@ func ParseConfigOptions(opts ...Option) *Config {
}
}
cfg.LoadDefaultScopes(cfg.Daemon.DataDir)
cfg.LoadDefaultScopes(cfg.DataDir)
return cfg
}
@ -77,7 +70,7 @@ type Option func(c *Config)
func OptionDefaultNetwork(dn string) Option {
return func(c *Config) {
logrus.Debugf("Option DefaultNetwork: %s", dn)
c.Daemon.DefaultNetwork = strings.TrimSpace(dn)
c.DefaultNetwork = strings.TrimSpace(dn)
}
}
@ -85,21 +78,21 @@ func OptionDefaultNetwork(dn string) Option {
func OptionDefaultDriver(dd string) Option {
return func(c *Config) {
logrus.Debugf("Option DefaultDriver: %s", dd)
c.Daemon.DefaultDriver = strings.TrimSpace(dd)
c.DefaultDriver = strings.TrimSpace(dd)
}
}
// OptionDefaultAddressPoolConfig function returns an option setter for default address pool
func OptionDefaultAddressPoolConfig(addressPool []*ipamutils.NetworkToSplit) Option {
return func(c *Config) {
c.Daemon.DefaultAddressPool = addressPool
c.DefaultAddressPool = addressPool
}
}
// OptionDriverConfig returns an option setter for driver configuration.
func OptionDriverConfig(networkType string, config map[string]interface{}) Option {
return func(c *Config) {
c.Daemon.DriverCfg[networkType] = config
c.DriverCfg[networkType] = config
}
}
@ -108,7 +101,7 @@ func OptionLabels(labels []string) Option {
return func(c *Config) {
for _, label := range labels {
if strings.HasPrefix(label, netlabel.Prefix) {
c.Daemon.Labels = append(c.Daemon.Labels, label)
c.Labels = append(c.Labels, label)
}
}
}
@ -117,14 +110,14 @@ func OptionLabels(labels []string) Option {
// OptionDataDir function returns an option setter for data folder
func OptionDataDir(dataDir string) Option {
return func(c *Config) {
c.Daemon.DataDir = dataDir
c.DataDir = dataDir
}
}
// OptionExecRoot function returns an option setter for exec root folder
func OptionExecRoot(execRoot string) Option {
return func(c *Config) {
c.Daemon.ExecRoot = execRoot
c.ExecRoot = execRoot
osl.SetBasePath(execRoot)
}
}
@ -147,7 +140,7 @@ func OptionNetworkControlPlaneMTU(exp int) Option {
exp = minimumNetworkControlPlaneMTU
}
}
c.Daemon.NetworkControlPlaneMTU = exp
c.NetworkControlPlaneMTU = exp
}
}

View file

@ -17,10 +17,10 @@ func TestOptionsLabels(t *testing.T) {
}
f := OptionLabels(l)
f(c)
if len(c.Daemon.Labels) != 3 {
t.Fatalf("Expecting 3 labels, seen %d", len(c.Daemon.Labels))
if len(c.Labels) != 3 {
t.Fatalf("Expecting 3 labels, seen %d", len(c.Labels))
}
for _, l := range c.Daemon.Labels {
for _, l := range c.Labels {
if !strings.HasPrefix(l, netlabel.Prefix) {
t.Fatalf("config must accept only libnetwork labels. Not : %s", l)
}

View file

@ -219,7 +219,7 @@ func New(cfgOptions ...config.Option) (NetworkController, error) {
}
}
if err = initIPAMDrivers(drvRegistry, nil, c.getStore(datastore.GlobalScope), c.cfg.Daemon.DefaultAddressPool); err != nil {
if err = initIPAMDrivers(drvRegistry, nil, c.getStore(datastore.GlobalScope), c.cfg.DefaultAddressPool); err != nil {
return nil, err
}
@ -249,12 +249,12 @@ func (c *controller) SetClusterProvider(provider cluster.Provider) {
var sameProvider bool
c.Lock()
// Avoids to spawn multiple goroutine for the same cluster provider
if c.cfg.Daemon.ClusterProvider == provider {
if c.cfg.ClusterProvider == provider {
// If the cluster provider is already set, there is already a go routine spawned
// that is listening for events, so nothing to do here
sameProvider = true
} else {
c.cfg.Daemon.ClusterProvider = provider
c.cfg.ClusterProvider = provider
}
c.Unlock()
@ -301,7 +301,7 @@ func (c *controller) getAgent() *agent {
}
func (c *controller) clusterAgentInit() {
clusterProvider := c.cfg.Daemon.ClusterProvider
clusterProvider := c.cfg.ClusterProvider
var keysAvailable bool
for {
eventType := <-clusterProvider.ListenClusterEvents()
@ -408,7 +408,7 @@ func (c *controller) makeDriverConfig(ntype string) map[string]interface{} {
config := make(map[string]interface{})
for _, label := range c.cfg.Daemon.Labels {
for _, label := range c.cfg.Labels {
if !strings.HasPrefix(netlabel.Key(label), netlabel.DriverPrefix+"."+ntype) {
continue
}
@ -416,7 +416,7 @@ func (c *controller) makeDriverConfig(ntype string) map[string]interface{} {
config[netlabel.Key(label)] = netlabel.Value(label)
}
drvCfg, ok := c.cfg.Daemon.DriverCfg[ntype]
drvCfg, ok := c.cfg.DriverCfg[ntype]
if ok {
for k, v := range drvCfg.(map[string]interface{}) {
config[k] = v
@ -580,19 +580,19 @@ func (c *controller) Config() config.Config {
func (c *controller) isManager() bool {
c.Lock()
defer c.Unlock()
if c.cfg == nil || c.cfg.Daemon.ClusterProvider == nil {
if c.cfg == nil || c.cfg.ClusterProvider == nil {
return false
}
return c.cfg.Daemon.ClusterProvider.IsManager()
return c.cfg.ClusterProvider.IsManager()
}
func (c *controller) isAgent() bool {
c.Lock()
defer c.Unlock()
if c.cfg == nil || c.cfg.Daemon.ClusterProvider == nil {
if c.cfg == nil || c.cfg.ClusterProvider == nil {
return false
}
return c.cfg.Daemon.ClusterProvider.IsAgent()
return c.cfg.ClusterProvider.IsAgent()
}
func (c *controller) isDistributedControl() bool {
@ -1038,8 +1038,8 @@ func (c *controller) NewSandbox(containerID string, options ...SandboxOption) (S
if sb.ingress {
c.ingressSandbox = sb
sb.config.hostsPath = filepath.Join(c.cfg.Daemon.DataDir, "/network/files/hosts")
sb.config.resolvConfPath = filepath.Join(c.cfg.Daemon.DataDir, "/network/files/resolv.conf")
sb.config.hostsPath = filepath.Join(c.cfg.DataDir, "/network/files/hosts")
sb.config.resolvConfPath = filepath.Join(c.cfg.DataDir, "/network/files/resolv.conf")
sb.id = "ingress_sbox"
} else if sb.loadBalancerNID != "" {
sb.id = "lb_" + sb.loadBalancerNID
@ -1287,7 +1287,7 @@ func (c *controller) iptablesEnabled() bool {
return false
}
// parse map cfg["bridge"]["generic"]["EnableIPTable"]
cfgBridge, ok := c.cfg.Daemon.DriverCfg["bridge"].(map[string]interface{})
cfgBridge, ok := c.cfg.DriverCfg["bridge"].(map[string]interface{})
if !ok {
return false
}

View file

@ -52,7 +52,7 @@ func TestUserChain(t *testing.T) {
tc := tc
t.Run(fmt.Sprintf("iptables=%v,insert=%v", tc.iptables, tc.insert), func(t *testing.T) {
c := nc.(*controller)
c.cfg.Daemon.DriverCfg["bridge"] = map[string]interface{}{
c.cfg.DriverCfg["bridge"] = map[string]interface{}{
netlabel.GenericData: options.Generic{
"EnableIPTables": tc.iptables,
},

View file

@ -114,7 +114,7 @@ func processReturn(r io.Reader) error {
func (c *controller) startExternalKeyListener() error {
execRoot := defaultExecRoot
if v := c.Config().Daemon.ExecRoot; v != "" {
if v := c.Config().ExecRoot; v != "" {
execRoot = v
}
udsBase := filepath.Join(execRoot, execSubdir)