summaryrefslogtreecommitdiff
path: root/pkg/network/network.go
diff options
context:
space:
mode:
authorbaude <bbaude@redhat.com>2019-12-28 13:48:16 -0600
committerbaude <bbaude@redhat.com>2019-12-29 11:18:49 -0600
commit93350b9e9f0313fe100dcc3ae4cdc02d1af0872e (patch)
tree9b9461835dabece51d648679498c5c9aa52c2e1e /pkg/network/network.go
parent24b4921508be011b8fc4707d262116f77c8a176b (diff)
downloadpodman-93350b9e9f0313fe100dcc3ae4cdc02d1af0872e.tar.gz
podman-93350b9e9f0313fe100dcc3ae4cdc02d1af0872e.tar.bz2
podman-93350b9e9f0313fe100dcc3ae4cdc02d1af0872e.zip
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 <bbaude@redhat.com>
Diffstat (limited to 'pkg/network/network.go')
-rw-r--r--pkg/network/network.go44
1 files changed, 43 insertions, 1 deletions
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
+}