@@ -28,10 +28,10 @@ const (
2828)
2929
3030// NetworkBackend returns the network backend name and interface
31- // It returns either the CNI or netavark backend depending on what is set in the config.
31+ // It returns netavark backend depending on what is set in the config.
3232// If the backend is set to "" we will automatically assign the backend on the following conditions:
3333// 1. read ${graphroot}/defaultNetworkBackend
34- // 2. find netavark binary (if not installed use CNI)
34+ // 2. find netavark binary
3535// 3. check containers, images and CNI networks and if there are some we have an existing install and should continue to use CNI
3636//
3737// revive does not like the name because the package is already called network
@@ -41,7 +41,7 @@ func NetworkBackend(store storage.Store, conf *config.Config, syslog bool) (type
4141 backend := types .NetworkBackend (conf .Network .NetworkBackend )
4242 if backend == "" {
4343 var err error
44- backend , err = defaultNetworkBackend (store , conf )
44+ backend , err = defaultNetworkBackend (store )
4545 if err != nil {
4646 return "" , nil , fmt .Errorf ("failed to get default network backend: %w" , err )
4747 }
@@ -50,6 +50,19 @@ func NetworkBackend(store storage.Store, conf *config.Config, syslog bool) (type
5050 return backendFromType (backend , store , conf , syslog )
5151}
5252
53+ func backendFromType (backend types.NetworkBackend , store storage.Store , conf * config.Config , syslog bool ) (types.NetworkBackend , types.ContainerNetwork , error ) {
54+ switch backend {
55+ case types .Netavark :
56+ netInt , err := netavarkBackendFromConf (store , conf , syslog )
57+ if err != nil {
58+ return "" , nil , err
59+ }
60+ return types .Netavark , netInt , err
61+ default :
62+ return "" , nil , fmt .Errorf ("unsupported network backend %q, check network_backend in containers.conf" , backend )
63+ }
64+ }
65+
5366func netavarkBackendFromConf (store storage.Store , conf * config.Config , syslog bool ) (types.ContainerNetwork , error ) {
5467 netavarkBin , err := conf .FindHelperBinary (netavarkBinary , false )
5568 if err != nil {
@@ -83,7 +96,7 @@ func netavarkBackendFromConf(store storage.Store, conf *config.Config, syslog bo
8396 return netInt , err
8497}
8598
86- func defaultNetworkBackend (store storage.Store , conf * config. Config ) (backend types.NetworkBackend , err error ) {
99+ func defaultNetworkBackend (store storage.Store ) (backend types.NetworkBackend , err error ) {
87100 err = nil
88101
89102 file := filepath .Join (store .GraphRoot (), defaultNetworkBackendFileName )
@@ -102,39 +115,21 @@ func defaultNetworkBackend(store storage.Store, conf *config.Config) (backend ty
102115 if err == nil {
103116 val := string (b )
104117
105- // if the network backend has been already set previously,
106- // handle the values depending on whether CNI is supported and
107- // whether the network backend is explicitly configured
108- if val == string (types .Netavark ) {
118+ // set network backend to netavark if not already.
119+ if val != string (types .Netavark ) {
109120 // netavark is always good
110- return types .Netavark , nil
111- } else if val == string (types .CNI ) {
112- if cniSupported {
113- return types .CNI , nil
114- }
115- // the user has *not* configured a network
116- // backend explicitly but used CNI in the past
117- // => we upgrade them in this case to netavark only
118121 writeBackendToFile (types .Netavark )
119122 logrus .Info ("Migrating network backend to netavark as no backend has been configured previously" )
120- return types .Netavark , nil
121123 }
122- return "" , fmt . Errorf ( "unknown network backend value %q in %q" , val , file )
124+ return types . Netavark , nil
123125 }
124126
125127 // fail for all errors except ENOENT
126128 if ! errors .Is (err , os .ErrNotExist ) {
127129 return "" , fmt .Errorf ("could not read network backend value: %w" , err )
128130 }
129131
130- backend , err = networkBackendFromStore (store , conf )
131- if err != nil {
132- return "" , err
133- }
134- // cache the network backend to make sure always the same one will be used
135- writeBackendToFile (backend )
136-
137- return backend , nil
132+ return "" , nil
138133}
139134
140135// getDefaultNetavarkConfigDir return the netavark config dir. For rootful it will
0 commit comments