mirror of https://github.com/containers/podman.git
				
				
				
			Use the libpod.conf cni_config_dir option for inspect and delete
The pkg/network/files.go methods currently use the constant '/etc/cni/net.d' for network handling. This results in the unability of podman-network-inspect and podman-network-rm to locate the cni network configuration files. This commit propagates the libpod.Runtime through the networking methods and finally makes use of its configuration (config.Network.NetworkConfigDir). Closes #6212 Signed-off-by: Maximilian Müller <maxm123@techie.com>
This commit is contained in:
		
							parent
							
								
									0d9625152b
								
							
						
					
					
						commit
						48d83482db
					
				|  | @ -15,26 +15,15 @@ import ( | ||||||
| 	"github.com/pkg/errors" | 	"github.com/pkg/errors" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func getCNIConfDir(r *libpod.Runtime) (string, error) { |  | ||||||
| 	config, err := r.GetConfig() |  | ||||||
| 	if err != nil { |  | ||||||
| 		return "", err |  | ||||||
| 	} |  | ||||||
| 	configPath := config.Network.NetworkConfigDir |  | ||||||
| 
 |  | ||||||
| 	if len(config.Network.NetworkConfigDir) < 1 { |  | ||||||
| 		configPath = network.CNIConfigDir |  | ||||||
| 	} |  | ||||||
| 	return configPath, nil |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| func (ic *ContainerEngine) NetworkList(ctx context.Context, options entities.NetworkListOptions) ([]*entities.NetworkListReport, error) { | func (ic *ContainerEngine) NetworkList(ctx context.Context, options entities.NetworkListOptions) ([]*entities.NetworkListReport, error) { | ||||||
| 	var reports []*entities.NetworkListReport | 	var reports []*entities.NetworkListReport | ||||||
| 	cniConfigPath, err := getCNIConfDir(ic.Libpod) | 
 | ||||||
|  | 	config, err := ic.Libpod.GetConfig() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	networks, err := network.LoadCNIConfsFromDir(cniConfigPath) | 
 | ||||||
|  | 	networks, err := network.LoadCNIConfsFromDir(network.GetCNIConfDir(config)) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  | @ -49,8 +38,14 @@ func (ic *ContainerEngine) NetworkInspect(ctx context.Context, namesOrIds []stri | ||||||
| 	var ( | 	var ( | ||||||
| 		rawCNINetworks []entities.NetworkInspectReport | 		rawCNINetworks []entities.NetworkInspectReport | ||||||
| 	) | 	) | ||||||
|  | 
 | ||||||
|  | 	config, err := ic.Libpod.GetConfig() | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	for _, name := range namesOrIds { | 	for _, name := range namesOrIds { | ||||||
| 		rawList, err := network.InspectNetwork(name) | 		rawList, err := network.InspectNetwork(config, name) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return nil, err | 			return nil, err | ||||||
| 		} | 		} | ||||||
|  | @ -61,6 +56,12 @@ func (ic *ContainerEngine) NetworkInspect(ctx context.Context, namesOrIds []stri | ||||||
| 
 | 
 | ||||||
| func (ic *ContainerEngine) NetworkRm(ctx context.Context, namesOrIds []string, options entities.NetworkRmOptions) ([]*entities.NetworkRmReport, error) { | func (ic *ContainerEngine) NetworkRm(ctx context.Context, namesOrIds []string, options entities.NetworkRmOptions) ([]*entities.NetworkRmReport, error) { | ||||||
| 	var reports []*entities.NetworkRmReport | 	var reports []*entities.NetworkRmReport | ||||||
|  | 
 | ||||||
|  | 	config, err := ic.Libpod.GetConfig() | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	for _, name := range namesOrIds { | 	for _, name := range namesOrIds { | ||||||
| 		report := entities.NetworkRmReport{Name: name} | 		report := entities.NetworkRmReport{Name: name} | ||||||
| 		containers, err := ic.Libpod.GetAllContainers() | 		containers, err := ic.Libpod.GetAllContainers() | ||||||
|  | @ -80,7 +81,7 @@ func (ic *ContainerEngine) NetworkRm(ctx context.Context, namesOrIds []string, o | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 		if err := network.RemoveNetwork(name); err != nil { | 		if err := network.RemoveNetwork(config, name); err != nil { | ||||||
| 			report.Err = err | 			report.Err = err | ||||||
| 		} | 		} | ||||||
| 		reports = append(reports, &report) | 		reports = append(reports, &report) | ||||||
|  | @ -117,10 +118,10 @@ func createBridge(r *libpod.Runtime, name string, options entities.NetworkCreate | ||||||
| 	// if range is provided, make sure it is "in" network
 | 	// if range is provided, make sure it is "in" network
 | ||||||
| 	if subnet.IP != nil { | 	if subnet.IP != nil { | ||||||
| 		// if network is provided, does it conflict with existing CNI or live networks
 | 		// if network is provided, does it conflict with existing CNI or live networks
 | ||||||
| 		err = network.ValidateUserNetworkIsAvailable(subnet) | 		err = network.ValidateUserNetworkIsAvailable(runtimeConfig, subnet) | ||||||
| 	} else { | 	} else { | ||||||
| 		// if no network is provided, figure out network
 | 		// if no network is provided, figure out network
 | ||||||
| 		subnet, err = network.GetFreeNetwork() | 		subnet, err = network.GetFreeNetwork(runtimeConfig) | ||||||
| 	} | 	} | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return "", err | 		return "", err | ||||||
|  | @ -158,13 +159,13 @@ func createBridge(r *libpod.Runtime, name string, options entities.NetworkCreate | ||||||
| 			return "", errors.Errorf("the ip range %s does not fall within the subnet range %s", options.Range.String(), subnet.String()) | 			return "", errors.Errorf("the ip range %s does not fall within the subnet range %s", options.Range.String(), subnet.String()) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	bridgeDeviceName, err := network.GetFreeDeviceName() | 	bridgeDeviceName, err := network.GetFreeDeviceName(runtimeConfig) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return "", err | 		return "", err | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if len(name) > 0 { | 	if len(name) > 0 { | ||||||
| 		netNames, err := network.GetNetworkNamesFromFileSystem() | 		netNames, err := network.GetNetworkNamesFromFileSystem(runtimeConfig) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return "", err | 			return "", err | ||||||
| 		} | 		} | ||||||
|  | @ -205,11 +206,7 @@ func createBridge(r *libpod.Runtime, name string, options entities.NetworkCreate | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return "", err | 		return "", err | ||||||
| 	} | 	} | ||||||
| 	cniConfigPath, err := getCNIConfDir(r) | 	cniPathName := filepath.Join(network.GetCNIConfDir(runtimeConfig), fmt.Sprintf("%s.conflist", name)) | ||||||
| 	if err != nil { |  | ||||||
| 		return "", err |  | ||||||
| 	} |  | ||||||
| 	cniPathName := filepath.Join(cniConfigPath, fmt.Sprintf("%s.conflist", name)) |  | ||||||
| 	err = ioutil.WriteFile(cniPathName, b, 0644) | 	err = ioutil.WriteFile(cniPathName, b, 0644) | ||||||
| 	return cniPathName, err | 	return cniPathName, err | ||||||
| } | } | ||||||
|  | @ -222,12 +219,18 @@ func createMacVLAN(r *libpod.Runtime, name string, options entities.NetworkCreat | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return "", err | 		return "", err | ||||||
| 	} | 	} | ||||||
|  | 
 | ||||||
|  | 	config, err := r.GetConfig() | ||||||
|  | 	if err != nil { | ||||||
|  | 		return "", err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	// Make sure the host-device exists
 | 	// Make sure the host-device exists
 | ||||||
| 	if !util.StringInSlice(options.MacVLAN, liveNetNames) { | 	if !util.StringInSlice(options.MacVLAN, liveNetNames) { | ||||||
| 		return "", errors.Errorf("failed to find network interface %q", options.MacVLAN) | 		return "", errors.Errorf("failed to find network interface %q", options.MacVLAN) | ||||||
| 	} | 	} | ||||||
| 	if len(name) > 0 { | 	if len(name) > 0 { | ||||||
| 		netNames, err := network.GetNetworkNamesFromFileSystem() | 		netNames, err := network.GetNetworkNamesFromFileSystem(config) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return "", err | 			return "", err | ||||||
| 		} | 		} | ||||||
|  | @ -235,7 +238,7 @@ func createMacVLAN(r *libpod.Runtime, name string, options entities.NetworkCreat | ||||||
| 			return "", errors.Errorf("the network name %s is already used", name) | 			return "", errors.Errorf("the network name %s is already used", name) | ||||||
| 		} | 		} | ||||||
| 	} else { | 	} else { | ||||||
| 		name, err = network.GetFreeDeviceName() | 		name, err = network.GetFreeDeviceName(config) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return "", err | 			return "", err | ||||||
| 		} | 		} | ||||||
|  | @ -248,11 +251,7 @@ func createMacVLAN(r *libpod.Runtime, name string, options entities.NetworkCreat | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return "", err | 		return "", err | ||||||
| 	} | 	} | ||||||
| 	cniConfigPath, err := getCNIConfDir(r) | 	cniPathName := filepath.Join(network.GetCNIConfDir(config), fmt.Sprintf("%s.conflist", name)) | ||||||
| 	if err != nil { |  | ||||||
| 		return "", err |  | ||||||
| 	} |  | ||||||
| 	cniPathName := filepath.Join(cniConfigPath, fmt.Sprintf("%s.conflist", name)) |  | ||||||
| 	err = ioutil.WriteFile(cniPathName, b, 0644) | 	err = ioutil.WriteFile(cniPathName, b, 0644) | ||||||
| 	return cniPathName, err | 	return cniPathName, err | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -4,6 +4,7 @@ import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"os/exec" | 	"os/exec" | ||||||
| 
 | 
 | ||||||
|  | 	"github.com/containers/common/pkg/config" | ||||||
| 	"github.com/containers/libpod/pkg/util" | 	"github.com/containers/libpod/pkg/util" | ||||||
| 	"github.com/containers/libpod/utils" | 	"github.com/containers/libpod/utils" | ||||||
| 	"github.com/sirupsen/logrus" | 	"github.com/sirupsen/logrus" | ||||||
|  | @ -11,12 +12,12 @@ import ( | ||||||
| 
 | 
 | ||||||
| // GetFreeDeviceName returns a device name that is unused; used when no network
 | // GetFreeDeviceName returns a device name that is unused; used when no network
 | ||||||
| // name is provided by user
 | // name is provided by user
 | ||||||
| func GetFreeDeviceName() (string, error) { | func GetFreeDeviceName(config *config.Config) (string, error) { | ||||||
| 	var ( | 	var ( | ||||||
| 		deviceNum  uint | 		deviceNum  uint | ||||||
| 		deviceName string | 		deviceName string | ||||||
| 	) | 	) | ||||||
| 	networkNames, err := GetNetworkNamesFromFileSystem() | 	networkNames, err := GetNetworkNamesFromFileSystem(config) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return "", err | 		return "", err | ||||||
| 	} | 	} | ||||||
|  | @ -24,7 +25,7 @@ func GetFreeDeviceName() (string, error) { | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return "", err | 		return "", err | ||||||
| 	} | 	} | ||||||
| 	bridgeNames, err := GetBridgeNamesFromFileSystem() | 	bridgeNames, err := GetBridgeNamesFromFileSystem(config) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return "", err | 		return "", err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -9,9 +9,17 @@ import ( | ||||||
| 
 | 
 | ||||||
| 	"github.com/containernetworking/cni/libcni" | 	"github.com/containernetworking/cni/libcni" | ||||||
| 	"github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator" | 	"github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator" | ||||||
|  | 	"github.com/containers/common/pkg/config" | ||||||
| 	"github.com/pkg/errors" | 	"github.com/pkg/errors" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | func GetCNIConfDir(config *config.Config) string { | ||||||
|  | 	if len(config.Network.NetworkConfigDir) < 1 { | ||||||
|  | 		return CNIConfigDir | ||||||
|  | 	} | ||||||
|  | 	return config.Network.NetworkConfigDir | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // LoadCNIConfsFromDir loads all the CNI configurations from a dir
 | // LoadCNIConfsFromDir loads all the CNI configurations from a dir
 | ||||||
| func LoadCNIConfsFromDir(dir string) ([]*libcni.NetworkConfigList, error) { | func LoadCNIConfsFromDir(dir string) ([]*libcni.NetworkConfigList, error) { | ||||||
| 	var configs []*libcni.NetworkConfigList | 	var configs []*libcni.NetworkConfigList | ||||||
|  | @ -33,8 +41,8 @@ func LoadCNIConfsFromDir(dir string) ([]*libcni.NetworkConfigList, error) { | ||||||
| 
 | 
 | ||||||
| // GetCNIConfigPathByName finds a CNI network by name and
 | // GetCNIConfigPathByName finds a CNI network by name and
 | ||||||
| // returns its configuration file path
 | // returns its configuration file path
 | ||||||
| func GetCNIConfigPathByName(name string) (string, error) { | func GetCNIConfigPathByName(config *config.Config, name string) (string, error) { | ||||||
| 	files, err := libcni.ConfFiles(CNIConfigDir, []string{".conflist"}) | 	files, err := libcni.ConfFiles(GetCNIConfDir(config), []string{".conflist"}) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return "", err | 		return "", err | ||||||
| 	} | 	} | ||||||
|  | @ -52,8 +60,8 @@ func GetCNIConfigPathByName(name string) (string, error) { | ||||||
| 
 | 
 | ||||||
| // ReadRawCNIConfByName reads the raw CNI configuration for a CNI
 | // ReadRawCNIConfByName reads the raw CNI configuration for a CNI
 | ||||||
| // network by name
 | // network by name
 | ||||||
| func ReadRawCNIConfByName(name string) ([]byte, error) { | func ReadRawCNIConfByName(config *config.Config, name string) ([]byte, error) { | ||||||
| 	confFile, err := GetCNIConfigPathByName(name) | 	confFile, err := GetCNIConfigPathByName(config, name) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  | @ -73,9 +81,10 @@ func GetCNIPlugins(list *libcni.NetworkConfigList) string { | ||||||
| 
 | 
 | ||||||
| // GetNetworksFromFilesystem gets all the networks from the cni configuration
 | // GetNetworksFromFilesystem gets all the networks from the cni configuration
 | ||||||
| // files
 | // files
 | ||||||
| func GetNetworksFromFilesystem() ([]*allocator.Net, error) { | func GetNetworksFromFilesystem(config *config.Config) ([]*allocator.Net, error) { | ||||||
| 	var cniNetworks []*allocator.Net | 	var cniNetworks []*allocator.Net | ||||||
| 	networks, err := LoadCNIConfsFromDir(CNIConfigDir) | 
 | ||||||
|  | 	networks, err := LoadCNIConfsFromDir(GetCNIConfDir(config)) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  | @ -96,9 +105,10 @@ func GetNetworksFromFilesystem() ([]*allocator.Net, error) { | ||||||
| 
 | 
 | ||||||
| // GetNetworkNamesFromFileSystem gets all the names from the cni network
 | // GetNetworkNamesFromFileSystem gets all the names from the cni network
 | ||||||
| // configuration files
 | // configuration files
 | ||||||
| func GetNetworkNamesFromFileSystem() ([]string, error) { | func GetNetworkNamesFromFileSystem(config *config.Config) ([]string, error) { | ||||||
| 	var networkNames []string | 	var networkNames []string | ||||||
| 	networks, err := LoadCNIConfsFromDir(CNIConfigDir) | 
 | ||||||
|  | 	networks, err := LoadCNIConfsFromDir(GetCNIConfDir(config)) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  | @ -133,9 +143,10 @@ func GetInterfaceNameFromConfig(path string) (string, error) { | ||||||
| 
 | 
 | ||||||
| // GetBridgeNamesFromFileSystem is a convenience function to get all the bridge
 | // GetBridgeNamesFromFileSystem is a convenience function to get all the bridge
 | ||||||
| // names from the configured networks
 | // names from the configured networks
 | ||||||
| func GetBridgeNamesFromFileSystem() ([]string, error) { | func GetBridgeNamesFromFileSystem(config *config.Config) ([]string, error) { | ||||||
| 	var bridgeNames []string | 	var bridgeNames []string | ||||||
| 	networks, err := LoadCNIConfsFromDir(CNIConfigDir) | 
 | ||||||
|  | 	networks, err := LoadCNIConfsFromDir(GetCNIConfDir(config)) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -7,6 +7,7 @@ import ( | ||||||
| 
 | 
 | ||||||
| 	"github.com/containernetworking/cni/pkg/types" | 	"github.com/containernetworking/cni/pkg/types" | ||||||
| 	"github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator" | 	"github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator" | ||||||
|  | 	"github.com/containers/common/pkg/config" | ||||||
| 	"github.com/containers/libpod/pkg/util" | 	"github.com/containers/libpod/pkg/util" | ||||||
| 	"github.com/pkg/errors" | 	"github.com/pkg/errors" | ||||||
| 	"github.com/sirupsen/logrus" | 	"github.com/sirupsen/logrus" | ||||||
|  | @ -56,8 +57,8 @@ func GetLiveNetworkNames() ([]string, error) { | ||||||
| 
 | 
 | ||||||
| // GetFreeNetwork looks for a free network according to existing cni configuration
 | // GetFreeNetwork looks for a free network according to existing cni configuration
 | ||||||
| // files and network interfaces.
 | // files and network interfaces.
 | ||||||
| func GetFreeNetwork() (*net.IPNet, error) { | func GetFreeNetwork(config *config.Config) (*net.IPNet, error) { | ||||||
| 	networks, err := GetNetworksFromFilesystem() | 	networks, err := GetNetworksFromFilesystem(config) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  | @ -131,8 +132,8 @@ func networkIntersect(n1, n2 *net.IPNet) bool { | ||||||
| 
 | 
 | ||||||
| // ValidateUserNetworkIsAvailable returns via an error if a network is available
 | // ValidateUserNetworkIsAvailable returns via an error if a network is available
 | ||||||
| // to be used
 | // to be used
 | ||||||
| func ValidateUserNetworkIsAvailable(userNet *net.IPNet) error { | func ValidateUserNetworkIsAvailable(config *config.Config, userNet *net.IPNet) error { | ||||||
| 	networks, err := GetNetworksFromFilesystem() | 	networks, err := GetNetworksFromFilesystem(config) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  | @ -153,8 +154,8 @@ func ValidateUserNetworkIsAvailable(userNet *net.IPNet) error { | ||||||
| 
 | 
 | ||||||
| // RemoveNetwork removes a given network by name.  If the network has container associated with it, that
 | // RemoveNetwork removes a given network by name.  If the network has container associated with it, that
 | ||||||
| // must be handled outside the context of this.
 | // must be handled outside the context of this.
 | ||||||
| func RemoveNetwork(name string) error { | func RemoveNetwork(config *config.Config, name string) error { | ||||||
| 	cniPath, err := GetCNIConfigPathByName(name) | 	cniPath, err := GetCNIConfigPathByName(config, name) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  | @ -181,8 +182,8 @@ func RemoveNetwork(name string) error { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // InspectNetwork reads a CNI config and returns its configuration
 | // InspectNetwork reads a CNI config and returns its configuration
 | ||||||
| func InspectNetwork(name string) (map[string]interface{}, error) { | func InspectNetwork(config *config.Config, name string) (map[string]interface{}, error) { | ||||||
| 	b, err := ReadRawCNIConfByName(name) | 	b, err := ReadRawCNIConfByName(config, name) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue