diff options
Diffstat (limited to 'pkg/network')
-rw-r--r-- | pkg/network/config.go | 6 | ||||
-rw-r--r-- | pkg/network/files.go | 3 | ||||
-rw-r--r-- | pkg/network/network.go | 44 |
3 files changed, 51 insertions, 2 deletions
diff --git a/pkg/network/config.go b/pkg/network/config.go index e47b16143..e5c981419 100644 --- a/pkg/network/config.go +++ b/pkg/network/config.go @@ -2,6 +2,7 @@ package network import ( "encoding/json" + "errors" "net" ) @@ -19,6 +20,10 @@ const ( DefaultPodmanDomainName = "dns.podman" ) +var ( + ErrNetworkNotFound = errors.New("network not found") +) + // GetDefaultPodmanNetwork outputs the default network for podman func GetDefaultPodmanNetwork() (*net.IPNet, error) { _, n, err := net.ParseCIDR("10.88.1.0/24") @@ -90,6 +95,7 @@ func (p PortMapConfig) Bytes() ([]byte, error) { return json.MarshalIndent(p, "", "\t") } +// IPAMDHCP describes the ipamdhcp config type IPAMDHCP struct { DHCP string `json:"type"` } diff --git a/pkg/network/files.go b/pkg/network/files.go index 2f3932974..92cadcf0c 100644 --- a/pkg/network/files.go +++ b/pkg/network/files.go @@ -2,6 +2,7 @@ package network import ( "encoding/json" + "fmt" "io/ioutil" "sort" "strings" @@ -46,7 +47,7 @@ func GetCNIConfigPathByName(name string) (string, error) { return confFile, nil } } - return "", errors.Errorf("unable to find network configuration for %s", name) + return "", errors.Wrap(ErrNetworkNotFound, fmt.Sprintf("unable to find network configuration for %s", name)) } // ReadRawCNIConfByName reads the raw CNI configuration for a CNI diff --git a/pkg/network/network.go b/pkg/network/network.go index b241a66c0..bb6f13579 100644 --- a/pkg/network/network.go +++ b/pkg/network/network.go @@ -1,11 +1,13 @@ package network import ( - "github.com/containers/libpod/pkg/util" + "encoding/json" "net" + "os" "github.com/containernetworking/cni/pkg/types" "github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator" + "github.com/containers/libpod/pkg/util" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -148,3 +150,43 @@ func ValidateUserNetworkIsAvailable(userNet *net.IPNet) error { } return nil } + +// RemoveNetwork removes a given network by name. If the network has container associated with it, that +// must be handled outside the context of this. +func RemoveNetwork(name string) error { + cniPath, err := GetCNIConfigPathByName(name) + if err != nil { + return err + } + // Before we delete the configuration file, we need to make sure we can read and parse + // it to get the network interface name so we can remove that too + interfaceName, err := GetInterfaceNameFromConfig(cniPath) + if err != nil { + return errors.Wrapf(err, "failed to find network interface name in %q", cniPath) + } + liveNetworkNames, err := GetLiveNetworkNames() + if err != nil { + return errors.Wrapf(err, "failed to get live network names") + } + if util.StringInSlice(interfaceName, liveNetworkNames) { + if err := RemoveInterface(interfaceName); err != nil { + return errors.Wrapf(err, "failed to delete the network interface %q", interfaceName) + } + } + // Remove the configuration file + if err := os.Remove(cniPath); err != nil { + return errors.Wrapf(err, "failed to remove network configuration file %q", cniPath) + } + return nil +} + +// InspectNetwork reads a CNI config and returns its configuration +func InspectNetwork(name string) (map[string]interface{}, error) { + b, err := ReadRawCNIConfByName(name) + if err != nil { + return nil, err + } + rawList := make(map[string]interface{}) + err = json.Unmarshal(b, &rawList) + return rawList, err +} |