From 93350b9e9f0313fe100dcc3ae4cdc02d1af0872e Mon Sep 17 00:00:00 2001 From: baude Date: Sat, 28 Dec 2019 13:48:16 -0600 Subject: refactor network commands move core of network commands from pkg/adapter to pkg/network to assist with api development and remote podman commands. Signed-off-by: baude --- pkg/network/network.go | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'pkg/network') 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 +} -- cgit v1.2.3-54-g00ecf