Skip to content

Commit

Permalink
Updates config options and tests for socketfile and cniconfdir betwee…
Browse files Browse the repository at this point in the history
…n thin/thick (#1002)
  • Loading branch information
dougbtv authored Dec 19, 2022
1 parent b660d69 commit 95b45ef
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
8 changes: 8 additions & 0 deletions cmd/multus-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const (
defaultMultusMasterCNIFile = ""
defaultMultusNamespaceIsolation = false
defaultMultusReadinessIndicatorFile = ""
defaultSocketDir = "/run/multus/"
)

func main() {
Expand All @@ -75,6 +76,7 @@ func main() {
logMaxBackups := flag.Int("multus-log-max-backups", defaultMultusLogMaxBackups, "The maximum number of old log files to retain")
logCompress := flag.Bool("multus-log-compress", defaultMultusLogCompress, "Compress determines if the rotated log files should be compressed using gzip")
cniVersion := flag.String("cni-version", "", "Allows you to specify CNI spec version. Used only with --multus-conf-file=auto.")
socketDir := flag.String("socket-dir", defaultSocketDir, "Specifies the directory where the socket file resides.")
forceCNIVersion := flag.Bool("force-cni-version", false, "Force to use given CNI version. only for kind-e2e testing") // this is only for kind-e2e
readinessIndicator := flag.String("readiness-indicator-file", "", "Which file should be used as the readiness indicator. Used only with --multus-conf-file=auto.")
overrideNetworkName := flag.Bool("override-network-name", false, "Used when we need overrides the name of the multus configuration with the name of the delegated primary CNI")
Expand Down Expand Up @@ -136,6 +138,12 @@ func main() {
configurationOptions, config.WithReadinessFileIndicator(*readinessIndicator))
}

configurationOptions = append(
configurationOptions, config.WithCniConfigDir(*cniConfigDir))

configurationOptions = append(
configurationOptions, config.WithSocketDir(*socketDir))

// logOptions

var logOptionFuncs []config.LogOptionFunc
Expand Down
19 changes: 19 additions & 0 deletions pkg/server/config/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type MultusConf struct {
ReadinessIndicatorFile string `json:"readinessindicatorfile,omitempty"`
Type string `json:"type"`
CniDir string `json:"cniDir,omitempty"`
CniConfigDir string `json:"cniConfigDir,omitempty"`
SocketDir string `json:"socketDir,omitempty"`
}

// LogOptions specifies the configuration of the log
Expand Down Expand Up @@ -222,6 +224,23 @@ func WithCniDir(cniDir string) Option {
}
}

// WithCniConfigDir mutates the inner state to set the
// multus CNI configuration directory
func WithCniConfigDir(confDir string) Option {
return func(conf *MultusConf) error {
conf.CniConfigDir = confDir
return nil
}
}

// WithSocketDir mutates the socket directory
func WithSocketDir(sockDir string) Option {
return func(conf *MultusConf) error {
conf.SocketDir = sockDir
return nil
}
}

func withClusterNetwork(clusterNetwork string) Option {
return func(conf *MultusConf) error {
conf.ClusterNetwork = clusterNetwork
Expand Down
38 changes: 38 additions & 0 deletions pkg/server/config/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,44 @@ var _ = Describe("Configuration Generator", func() {
Expect(multusConfig.Generate()).Should(MatchJSON(expectedResult))
})

It("generates a multus config with CNI configuration directory", func() {
cniConfigDir := "/host/etc/cni/net.d"
multusConfig, err := newMultusConfigWithDelegates(
primaryCNIName,
cniVersion,
primaryCNIFile,
WithCniConfigDir(cniConfigDir))
Expect(err).NotTo(HaveOccurred())
expectedResult := fmt.Sprintf(`
{
"cniVersion":"0.4.0",
"clusterNetwork":"%s",
"name":"multus-cni-network",
"cniConfigDir":"/host/etc/cni/net.d",
"type":"myCNI"
}`, primaryCNIFile)
Expect(multusConfig.Generate()).Should(MatchJSON(expectedResult))
})

It("generates a multus config with a custom socket directory", func() {
socketDir := "/var/run/multus/multussock/"
multusConfig, err := newMultusConfigWithDelegates(
primaryCNIName,
cniVersion,
primaryCNIFile,
WithSocketDir(socketDir))
Expect(err).NotTo(HaveOccurred())
expectedResult := fmt.Sprintf(`
{
"cniVersion":"0.4.0",
"clusterNetwork":"%s",
"name":"multus-cni-network",
"socketDir":"/var/run/multus/multussock/",
"type":"myCNI"
}`, primaryCNIFile)
Expect(multusConfig.Generate()).Should(MatchJSON(expectedResult))
})

It("multus config with global namespace", func() {
const globalNamespace = "come-along-ns"
multusConfig, err := newMultusConfigWithDelegates(
Expand Down
7 changes: 4 additions & 3 deletions pkg/server/config/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ type Manager struct {
primaryCNIConfigPath string
}

// NewManager returns a config manager object, configured to persist the
// configuration to `multusAutoconfigDir`. This constructor will auto-discover
// NewManager returns a config manager object, configured to read the
// primary CNI configuration in `multusAutoconfigDir`. This constructor will auto-discover
// the primary CNI for which it will delegate.
func NewManager(config MultusConf, multusAutoconfigDir string, forceCNIVersion bool) (*Manager, error) {
defaultCNIPluginName, err := getPrimaryCNIPluginName(multusAutoconfigDir)
Expand Down Expand Up @@ -108,7 +108,7 @@ func newManager(config MultusConf, multusConfigDir, defaultCNIPluginName string,
configWatcher: watcher,
multusConfig: &config,
multusConfigDir: multusConfigDir,
multusConfigFilePath: cniPluginConfigFilePath(multusConfigDir, multusConfigFileName),
multusConfigFilePath: cniPluginConfigFilePath(config.CniConfigDir, multusConfigFileName),
primaryCNIConfigPath: cniPluginConfigFilePath(multusConfigDir, defaultCNIPluginName),
}

Expand Down Expand Up @@ -217,6 +217,7 @@ func (m Manager) MonitorPluginConfiguration(shutDown <-chan struct{}, done chan<
// PersistMultusConfig persists the provided configuration to the disc, with
// Read / Write permissions. The output file path is `<multus auto config dir>/00-multus.conf`
func (m Manager) PersistMultusConfig(config string) error {
logging.Debugf("Writing Multus CNI configuration @ %s", m.multusConfigFilePath)
return os.WriteFile(m.multusConfigFilePath, []byte(config), UserRWPermission)
}

Expand Down

0 comments on commit 95b45ef

Please sign in to comment.