Skip to content

Commit 6d7b509

Browse files
committed
Add WithAllConf, and change WithDefaultConf to only load default.
Signed-off-by: Lantao Liu <[email protected]>
1 parent ef9a3f8 commit 6d7b509

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

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(WithAllConf)
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(WithAllConf)
104104
assert.NoError(t, err)
105105

106106
err = l.Status()

opts.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,27 @@ func WithConfListFile(fileName string) CNIOpt {
146146
}
147147
}
148148

149-
// WithDefaultConf can be used to detect network config
149+
// WithDefaultConf can be used to detect the default network
150+
// config file from the configured cni config directory and load
151+
// it.
152+
// Since the CNI spec does not specify a way to detect default networks,
153+
// the convention chosen is - the first network configuration in the sorted
154+
// list of network conf files as the default network.
155+
func WithDefaultConf(c *libcni) error {
156+
return loadFromConfDir(c, 1)
157+
}
158+
159+
// WithAllConf can be used to detect all network config
150160
// files from the configured cni config directory and load
151161
// them.
152-
func WithDefaultConf(c *libcni) error {
162+
func WithAllConf(c *libcni) error {
163+
return loadFromConfDir(c, 0)
164+
}
165+
166+
// loadFromConfDir detects network config files from the
167+
// configured cni config directory and load them. max is
168+
// the maximum network config to load (max i<= 0 means no limit).
169+
func loadFromConfDir(c *libcni, max int) error {
153170
files, err := cnilibrary.ConfFiles(c.pluginConfDir, []string{".conf", ".conflist", ".json"})
154171
switch {
155172
case err != nil:
@@ -167,6 +184,7 @@ func WithDefaultConf(c *libcni) error {
167184
// interface provided during init as the network interface for this default
168185
// network. For every other network use a generated interface id.
169186
i := 0
187+
var networks []*Network
170188
for _, confFile := range files {
171189
var confList *cnilibrary.NetworkConfigList
172190
if strings.HasSuffix(confFile, ".conflist") {
@@ -194,15 +212,19 @@ func WithDefaultConf(c *libcni) error {
194212
return errors.Wrapf(ErrInvalidConfig, "CNI config list %s has no networks, skipping", confFile)
195213

196214
}
197-
c.networks = append(c.networks, &Network{
215+
networks = append(networks, &Network{
198216
cni: c.cniConfig,
199217
config: confList,
200218
ifName: getIfName(c.prefix, i),
201219
})
202220
i++
221+
if i == max {
222+
break
223+
}
203224
}
204-
if len(c.networks) == 0 {
225+
if len(networks) == 0 {
205226
return errors.Wrapf(ErrCNINotInitialized, "no valid networks found in %s", c.pluginDirs)
206227
}
228+
c.networks = append(c.networks, networks...)
207229
return nil
208230
}

0 commit comments

Comments
 (0)