Skip to content

Commit

Permalink
Merge pull request #4757 from baude/networkrefactor
Browse files Browse the repository at this point in the history
refactor network commands
  • Loading branch information
openshift-merge-robot authored Dec 31, 2019
2 parents 9e03aa1 + 93350b9 commit 6a370cb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 50 deletions.
64 changes: 15 additions & 49 deletions pkg/adapter/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,10 @@ func (r *LocalRuntime) NetworkInspect(cli *cliconfig.NetworkInspectValues) error
rawCNINetworks []map[string]interface{}
)
for _, name := range cli.InputArgs {
b, err := network.ReadRawCNIConfByName(name)
rawList, err := network.InspectNetwork(name)
if err != nil {
return err
}
rawList := make(map[string]interface{})
if err := json.Unmarshal(b, &rawList); err != nil {
return fmt.Errorf("error parsing configuration list: %s", err)
}
rawCNINetworks = append(rawCNINetworks, rawList)
}
out, err := json.MarshalIndent(rawCNINetworks, "", "\t")
Expand All @@ -98,7 +94,20 @@ func (r *LocalRuntime) NetworkRemove(ctx context.Context, cli *cliconfig.Network
if err != nil {
return networkRmSuccesses, networkRmErrors, err
}
if err := r.removeNetwork(ctx, name, containers, cli.Force); err != nil {
// We need to iterate containers looking to see if they belong to the given network
for _, c := range containers {
if util.StringInSlice(name, c.Config().Networks) {
// if user passes force, we nuke containers
if !cli.Force {
// Without the force option, we return an error
return nil, nil, errors.Errorf("%q has associated containers with it. Use -f to forcibly delete containers", name)
}
if err := r.RemoveContainer(ctx, c.Container, true, true); err != nil {
return nil, nil, err
}
}
}
if err := network.RemoveNetwork(name); err != nil {
if lastError != nil {
networkRmErrors[name] = lastError
}
Expand All @@ -110,49 +119,6 @@ func (r *LocalRuntime) NetworkRemove(ctx context.Context, cli *cliconfig.Network
return networkRmSuccesses, networkRmErrors, lastError
}

// removeNetwork removes a single network and its containers given a force bool
func (r *LocalRuntime) removeNetwork(ctx context.Context, name string, containers []*Container, force bool) error {
cniPath, err := network.GetCNIConfigPathByName(name)
if err != nil {
return err
}
// We need to iterate containers looking to see if they belong to the given network
for _, c := range containers {
if util.StringInSlice(name, c.Config().Networks) {
// if user passes force, we nuke containers
if force {
if err := r.RemoveContainer(ctx, c.Container, true, true); err != nil {
return err
}
} else {
// Without the the force option, we return an error
return errors.Errorf("%q has associated containers with it. use -f to forcibly delete containers", name)
}

}
}
// 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 := network.GetInterfaceNameFromConfig(cniPath)
if err != nil {
return errors.Wrapf(err, "failed to find network interface name in %q", cniPath)
}
liveNetworkNames, err := network.GetLiveNetworkNames()
if err != nil {
return errors.Wrapf(err, "failed to get live network names")
}
if util.StringInSlice(interfaceName, liveNetworkNames) {
if err := network.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
}

// NetworkCreateBridge creates a CNI network
func (r *LocalRuntime) NetworkCreateBridge(cli *cliconfig.NetworkCreateValues) (string, error) {
isGateway := true
Expand Down
44 changes: 43 additions & 1 deletion pkg/network/network.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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
}

0 comments on commit 6a370cb

Please sign in to comment.