@@ -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