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