Skip to content

Commit d6ba409

Browse files
committed
Same Opt API for Load and New
Signed-off-by: Michael Crosby <[email protected]>
1 parent 18e77c5 commit d6ba409

4 files changed

Lines changed: 81 additions & 90 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func main() {
2424
gocni.WithDefaultIfName(defaultIfName))
2525
2626
// Load the cni configuration
27-
err:= l.Load(gocni.WithLoNetwork(),gocni.WithDefaultConf())
27+
err:= l.Load(gocni.WithLoNetwork,gocni.WithDefaultConf)
2828
if err != nil{
2929
log.Errorf("failed to load cni configuration: %v", err)
3030
return

cni.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type CNI interface {
3131
// Remove tears down the network of the namespace.
3232
Remove(id string, path string, opts ...NamespaceOpts) error
3333
// Load loads the cni network config
34-
Load(opts ...LoadOption) error
34+
Load(opts ...CNIOpt) error
3535
// Status checks the status of the cni initialization
3636
Status() error
3737
}
@@ -59,7 +59,7 @@ func defaultCNIConfig() *libcni {
5959
}
6060
}
6161

62-
func New(config ...ConfigOption) (CNI, error) {
62+
func New(config ...CNIOpt) (CNI, error) {
6363
cni := defaultCNIConfig()
6464
var err error
6565
for _, c := range config {
@@ -70,7 +70,7 @@ func New(config ...ConfigOption) (CNI, error) {
7070
return cni, nil
7171
}
7272

73-
func (c *libcni) Load(opts ...LoadOption) error {
73+
func (c *libcni) Load(opts ...CNIOpt) error {
7474
var err error
7575
// Reset the networks on a load operation to ensure
7676
// config happens on a clean slate
@@ -95,8 +95,8 @@ func (c *libcni) Status() error {
9595

9696
// Setup setups the network in the namespace
9797
func (c *libcni) Setup(id string, path string, opts ...NamespaceOpts) (*CNIResult, error) {
98-
if err:=c.Status();err!=nil{
99-
return nil,err
98+
if err := c.Status(); err != nil {
99+
return nil, err
100100
}
101101
ns, err := newNamespace(id, path, opts...)
102102
if err != nil {
@@ -117,9 +117,9 @@ func (c *libcni) Setup(id string, path string, opts ...NamespaceOpts) (*CNIResul
117117

118118
// Remove removes the network config from the namespace
119119
func (c *libcni) Remove(id string, path string, opts ...NamespaceOpts) error {
120-
if err:=c.Status();err!=nil{
121-
return err
122-
}
120+
if err := c.Status(); err != nil {
121+
return err
122+
}
123123
ns, err := newNamespace(id, path, opts...)
124124
if err != nil {
125125
return err

cni_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestLibCNIType020(t *testing.T) {
4040
l.pluginConfDir = confDir
4141
// Set the minimum network count as 2 for this test
4242
l.networkCount = 2
43-
err := l.Load(WithDefaultConf())
43+
err := l.Load(WithDefaultConf)
4444
assert.NoError(t, err)
4545

4646
err = l.Status()
@@ -100,7 +100,7 @@ func TestLibCNITypeCurrent(t *testing.T) {
100100
l.pluginConfDir = confDir
101101
// Set the minimum network count as 2 for this test
102102
l.networkCount = 2
103-
err := l.Load(WithDefaultConf())
103+
err := l.Load(WithDefaultConf)
104104
assert.NoError(t, err)
105105

106106
err = l.Status()

opts.go

Lines changed: 70 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ import (
2424
"github.com/pkg/errors"
2525
)
2626

27-
type ConfigOption func(c *libcni) error
27+
type CNIOpt func(c *libcni) error
2828

2929
// WithInterfacePrefix sets the prefix for network interfaces
3030
// e.g. eth or wlan
31-
func WithInterfacePrefix(prefix string) ConfigOption {
31+
func WithInterfacePrefix(prefix string) CNIOpt {
3232
return func(c *libcni) error {
3333
c.prefix = prefix
3434
return nil
@@ -37,7 +37,7 @@ func WithInterfacePrefix(prefix string) ConfigOption {
3737

3838
// WithPluginDir can be used to set the locations of
3939
// the cni plugin binaries
40-
func WithPluginDir(dirs []string) ConfigOption {
40+
func WithPluginDir(dirs []string) CNIOpt {
4141
return func(c *libcni) error {
4242
c.pluginDirs = dirs
4343
c.cniConfig = &cnilibrary.CNIConfig{Path: dirs}
@@ -47,7 +47,7 @@ func WithPluginDir(dirs []string) ConfigOption {
4747

4848
// WithPluginConfDir can be used to configure the
4949
// cni configuration directory.
50-
func WithPluginConfDir(dir string) ConfigOption {
50+
func WithPluginConfDir(dir string) CNIOpt {
5151
return func(c *libcni) error {
5252
c.pluginConfDir = dir
5353
return nil
@@ -57,44 +57,37 @@ func WithPluginConfDir(dir string) ConfigOption {
5757
// WithMinNetworkCount can be used to configure the
5858
// minimum networks to be configured and initalized
5959
// for the status to report success. By default its 1.
60-
func WithMinNetworkCount(count int) ConfigOption {
60+
func WithMinNetworkCount(count int) CNIOpt {
6161
return func(c *libcni) error {
6262
c.networkCount = count
6363
return nil
6464
}
6565
}
6666

67-
// LoadOption can be used with Load API
68-
// to load network configuration from different
69-
// sources.
70-
type LoadOption func(c *libcni) error
71-
7267
// WithLoNetwork can be used to load the loopback
7368
// network config.
74-
func WithLoNetwork() LoadOption {
75-
return func(c *libcni) error {
76-
loConfig, _ := cnilibrary.ConfListFromBytes([]byte(`{
69+
func WithLoNetwork(c *libcni) error {
70+
loConfig, _ := cnilibrary.ConfListFromBytes([]byte(`{
7771
"cniVersion": "0.3.1",
7872
"name": "cni-loopback",
7973
"plugins": [{
8074
"type": "loopback"
8175
}]
8276
}`))
8377

84-
c.Lock()
85-
defer c.Unlock()
86-
c.networks = append(c.networks,&Network{
87-
cni: c.cniConfig,
88-
config: loConfig,
89-
ifName: "lo",
90-
})
91-
return nil
92-
}
78+
c.Lock()
79+
defer c.Unlock()
80+
c.networks = append(c.networks, &Network{
81+
cni: c.cniConfig,
82+
config: loConfig,
83+
ifName: "lo",
84+
})
85+
return nil
9386
}
9487

9588
// WithConf can be used to load config directly
9689
// from byte.
97-
func WithConf(bytes []byte) LoadOption {
90+
func WithConf(bytes []byte) CNIOpt {
9891
return func(c *libcni) error {
9992
conf, err := cnilibrary.ConfFromBytes(bytes)
10093
if err != nil {
@@ -118,7 +111,7 @@ func WithConf(bytes []byte) LoadOption {
118111
// WithConfFile can be used to load network config
119112
// from an .conf file. Supported with absolute fileName
120113
// with path only.
121-
func WithConfFile(fileName string) LoadOption {
114+
func WithConfFile(fileName string) CNIOpt {
122115
return func(c *libcni) error {
123116
conf, err := cnilibrary.ConfFromFile(fileName)
124117
if err != nil {
@@ -143,15 +136,15 @@ func WithConfFile(fileName string) LoadOption {
143136
// WithConfListFile can be used to load network config
144137
// from an .conflist file. Supported with absolute fileName
145138
// with path only.
146-
func WithConfListFile(fileName string) LoadOption {
139+
func WithConfListFile(fileName string) CNIOpt {
147140
return func(c *libcni) error {
148141
confList, err := cnilibrary.ConfListFromFile(fileName)
149142
if err != nil {
150143
return err
151144
}
152145
c.Lock()
153146
defer c.Unlock()
154-
c.networks = append(c.networks,&Network{
147+
c.networks = append(c.networks, &Network{
155148
cni: c.cniConfig,
156149
config: confList,
157150
ifName: getIfName(c.prefix, 0),
@@ -163,64 +156,62 @@ func WithConfListFile(fileName string) LoadOption {
163156
// WithDefaultConf can be used to detect network config
164157
// files from the configured cni config directory and load
165158
// them.
166-
func WithDefaultConf() LoadOption {
167-
return func(c *libcni) error {
168-
files, err := cnilibrary.ConfFiles(c.pluginConfDir, []string{".conf", ".conflist", ".json"})
169-
switch {
170-
case err != nil:
171-
return errors.Wrapf(ErrRead, "failed to read config file: %v", err)
172-
case len(files) == 0:
173-
return errors.Wrapf(ErrCNINotInitialized, "no network config found in %s", c.pluginConfDir)
174-
}
159+
func WithDefaultConf(c *libcni) error {
160+
files, err := cnilibrary.ConfFiles(c.pluginConfDir, []string{".conf", ".conflist", ".json"})
161+
switch {
162+
case err != nil:
163+
return errors.Wrapf(ErrRead, "failed to read config file: %v", err)
164+
case len(files) == 0:
165+
return errors.Wrapf(ErrCNINotInitialized, "no network config found in %s", c.pluginConfDir)
166+
}
175167

176-
// files contains the network config files associated with cni network.
177-
// Use lexicographical way as a defined order for network config files.
178-
sort.Strings(files)
179-
// Since the CNI spec does not specify a way to detect default networks,
180-
// the convention chosen is - the first network configuration in the sorted
181-
// list of network conf files as the default network and choose the default
182-
// interface provided during init as the network interface for this default
183-
// network. For every other network use a generated interface id.
184-
i := 0
185-
c.Lock()
186-
defer c.Unlock()
187-
for _, confFile := range files {
188-
var confList *cnilibrary.NetworkConfigList
189-
if strings.HasSuffix(confFile, ".conflist") {
190-
confList, err = cnilibrary.ConfListFromFile(confFile)
191-
if err != nil {
192-
return errors.Wrapf(ErrInvalidConfig, "failed to load CNI config list file %s: %v", confFile, err)
193-
}
194-
} else {
195-
conf, err := cnilibrary.ConfFromFile(confFile)
196-
if err != nil {
197-
return errors.Wrapf(ErrInvalidConfig, "failed to load CNI config file %s: %v", confFile, err)
198-
}
199-
// Ensure the config has a "type" so we know what plugin to run.
200-
// Also catches the case where somebody put a conflist into a conf file.
201-
if conf.Network.Type == "" {
202-
return errors.Wrapf(ErrInvalidConfig, "network type not found in %s", confFile)
203-
}
204-
205-
confList, err = cnilibrary.ConfListFromConf(conf)
206-
if err != nil {
207-
return errors.Wrapf(ErrInvalidConfig, "failed to convert CNI config file %s to list: %v", confFile, err)
208-
}
168+
// files contains the network config files associated with cni network.
169+
// Use lexicographical way as a defined order for network config files.
170+
sort.Strings(files)
171+
// Since the CNI spec does not specify a way to detect default networks,
172+
// the convention chosen is - the first network configuration in the sorted
173+
// list of network conf files as the default network and choose the default
174+
// interface provided during init as the network interface for this default
175+
// network. For every other network use a generated interface id.
176+
i := 0
177+
c.Lock()
178+
defer c.Unlock()
179+
for _, confFile := range files {
180+
var confList *cnilibrary.NetworkConfigList
181+
if strings.HasSuffix(confFile, ".conflist") {
182+
confList, err = cnilibrary.ConfListFromFile(confFile)
183+
if err != nil {
184+
return errors.Wrapf(ErrInvalidConfig, "failed to load CNI config list file %s: %v", confFile, err)
185+
}
186+
} else {
187+
conf, err := cnilibrary.ConfFromFile(confFile)
188+
if err != nil {
189+
return errors.Wrapf(ErrInvalidConfig, "failed to load CNI config file %s: %v", confFile, err)
190+
}
191+
// Ensure the config has a "type" so we know what plugin to run.
192+
// Also catches the case where somebody put a conflist into a conf file.
193+
if conf.Network.Type == "" {
194+
return errors.Wrapf(ErrInvalidConfig, "network type not found in %s", confFile)
209195
}
210-
if len(confList.Plugins) == 0 {
211-
return errors.Wrapf(ErrInvalidConfig, "CNI config list %s has no networks, skipping", confFile)
212196

197+
confList, err = cnilibrary.ConfListFromConf(conf)
198+
if err != nil {
199+
return errors.Wrapf(ErrInvalidConfig, "failed to convert CNI config file %s to list: %v", confFile, err)
213200
}
214-
c.networks = append(c.networks, &Network{
215-
cni: c.cniConfig,
216-
config: confList,
217-
ifName: getIfName(c.prefix, i),
218-
})
219-
i++
220201
}
221-
if len(c.networks) == 0 {
222-
return errors.Wrapf(ErrCNINotInitialized, "no valid networks found in %s", c.pluginDirs)
202+
if len(confList.Plugins) == 0 {
203+
return errors.Wrapf(ErrInvalidConfig, "CNI config list %s has no networks, skipping", confFile)
204+
223205
}
224-
return nil
206+
c.networks = append(c.networks, &Network{
207+
cni: c.cniConfig,
208+
config: confList,
209+
ifName: getIfName(c.prefix, i),
210+
})
211+
i++
212+
}
213+
if len(c.networks) == 0 {
214+
return errors.Wrapf(ErrCNINotInitialized, "no valid networks found in %s", c.pluginDirs)
225215
}
216+
return nil
226217
}

0 commit comments

Comments
 (0)