MCPcopy Create free account
hub / github.com/containers/common / loadNetworks

Method loadNetworks

libnetwork/cni/network.go:117–198  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

115}
116
117func (n *cniNetwork) loadNetworks() error {
118 // check the mod time of the config dir
119 f, err := os.Stat(n.cniConfigDir)
120 if err != nil {
121 return err
122 }
123 modTime := f.ModTime()
124
125 // skip loading networks if they are already loaded and
126 // if the config dir was not modified since the last call
127 if n.networks != nil && modTime.Equal(n.modTime) {
128 return nil
129 }
130 // make sure the remove all networks before we reload them
131 n.networks = nil
132 n.modTime = modTime
133
134 // FIXME: do we have to support other file types as well, e.g. .conf?
135 files, err := libcni.ConfFiles(n.cniConfigDir, []string{".conflist"})
136 if err != nil {
137 return err
138 }
139 networks := make(map[string]*network, len(files))
140 for _, file := range files {
141 conf, err := libcni.ConfListFromFile(file)
142 if err != nil {
143 // do not log ENOENT errors
144 if !errors.Is(err, os.ErrNotExist) {
145 logrus.Warnf("Error loading CNI config file %s: %v", file, err)
146 }
147 continue
148 }
149
150 if !types.NameRegex.MatchString(conf.Name) {
151 logrus.Warnf("CNI config list %s has invalid name, skipping: %v", file, types.RegexError)
152 continue
153 }
154
155 // podman < v4.0 used the podman-machine cni plugin for podman machine port forwarding
156 // since this is now build into podman we no longer use the plugin
157 // old configs may still contain it so we just remove it here
158 if n.isMachine {
159 conf = removeMachinePlugin(conf)
160 }
161
162 if _, err := n.cniConf.ValidateNetworkList(context.Background(), conf); err != nil {
163 logrus.Warnf("Error validating CNI config file %s: %v", file, err)
164 continue
165 }
166
167 if val, ok := networks[conf.Name]; ok {
168 logrus.Warnf("CNI config list %s has the same network name as %s, skipping", file, val.filename)
169 continue
170 }
171
172 net, err := createNetworkFromCNIConfigList(conf, file)
173 if err != nil {
174 logrus.Errorf("CNI config list %s could not be converted to a libpod config, skipping: %v", file, err)

Callers 6

SetupMethod · 0.95
TeardownMethod · 0.95
NetworkCreateMethod · 0.95
NetworkRemoveMethod · 0.95
NetworkListMethod · 0.95
NetworkInspectMethod · 0.95

Calls 4

createDefaultNetworkMethod · 0.95
removeMachinePluginFunction · 0.85
StatMethod · 0.65

Tested by

no test coverage detected