MCPcopy Create free account
hub / github.com/containerd/nerdctl / GetDefaultNetworkConfig

Method GetDefaultNetworkConfig

pkg/netutil/netutil.go:371–416  ·  view source on GitHub ↗

GetDefaultNetworkConfig checks whether the default network exists by first searching for if any network bears the `labels.NerdctlDefaultNetwork` label, or falls back to checking whether any network bears the `DefaultNetworkName` name.

()

Source from the content-addressed store, hash-verified

369// label, or falls back to checking whether any network bears the
370// `DefaultNetworkName` name.
371func (e *CNIEnv) GetDefaultNetworkConfig() (*NetworkConfig, error) {
372 // Search for networks bearing the `labels.NerdctlDefaultNetwork` label.
373 defaultLabelFilterF := func(nc *NetworkConfig) bool {
374 if nc.NerdctlLabels == nil {
375 return false
376 } else if _, ok := (*nc.NerdctlLabels)[labels.NerdctlDefaultNetwork]; ok {
377 return true
378 }
379 return false
380 }
381 labelMatches, err := e.filterNetworks(defaultLabelFilterF)
382 if err != nil {
383 return nil, err
384 }
385 if len(labelMatches) >= 1 {
386 if len(labelMatches) > 1 {
387 log.L.Warnf("returning the first network bearing the %q label out of the multiple found: %#v", labels.NerdctlDefaultNetwork, labelMatches)
388 }
389 return labelMatches[0], nil
390 }
391
392 // Search for networks bearing the DefaultNetworkName.
393 defaultNameFilterF := func(nc *NetworkConfig) bool {
394 return nc.Name == DefaultNetworkName
395 }
396 nameMatches, err := e.filterNetworks(defaultNameFilterF)
397 if err != nil {
398 return nil, err
399 }
400 if len(nameMatches) >= 1 {
401 if len(nameMatches) > 1 {
402 log.L.Warnf("returning the first network bearing the %q default network name out of the multiple found: %#v", DefaultNetworkName, nameMatches)
403 }
404
405 // Warn the user if the default network was not created by nerdctl.
406 match := nameMatches[0]
407 exists, statErr := fsExists(e, DefaultNetworkName)
408 if match.NerdctlID == nil || statErr != nil || !exists {
409 log.L.Warnf("default network named %q does not have an internal nerdctl ID or nerdctl-managed config file, it was most likely NOT created by nerdctl", DefaultNetworkName)
410 }
411
412 return nameMatches[0], nil
413 }
414
415 return nil, nil
416}
417
418func (e *CNIEnv) ensureDefaultNetworkConfig(bridgeIP string) error {
419 defaultNet, err := e.GetDefaultNetworkConfig()

Calls 2

filterNetworksMethod · 0.95
fsExistsFunction · 0.85