From 85e8fbf7f33717ef6a0d6cf9e2143b52c874c2de Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Mon, 16 Aug 2021 16:11:26 +0200 Subject: Wire network interface into libpod Make use of the new network interface in libpod. This commit contains several breaking changes: - podman network create only outputs the new network name and not file path. - podman network ls shows the network driver instead of the cni version and plugins. - podman network inspect outputs the new network struct and not the cni conflist. - The bindings and libpod api endpoints have been changed to use the new network structure. The container network status is stored in a new field in the state. The status should be received with the new `c.getNetworkStatus`. This will migrate the old status to the new format. Therefore old containers should contine to work correctly in all cases even when network connect/ disconnect is used. New features: - podman network reload keeps the ip and mac for more than one network. - podman container restore keeps the ip and mac for more than one network. - The network create compat endpoint can now use more than one ipam config. The man pages and the swagger doc are updated to reflect the latest changes. Signed-off-by: Paul Holzinger --- cmd/podman/networks/create.go | 60 ++++++++++++++++++++++++++++++++++++------- cmd/podman/networks/list.go | 38 ++++++++++----------------- 2 files changed, 64 insertions(+), 34 deletions(-) (limited to 'cmd/podman/networks') diff --git a/cmd/podman/networks/create.go b/cmd/podman/networks/create.go index b5ddd215f..21d720f55 100644 --- a/cmd/podman/networks/create.go +++ b/cmd/podman/networks/create.go @@ -8,7 +8,8 @@ import ( "github.com/containers/podman/v3/cmd/podman/common" "github.com/containers/podman/v3/cmd/podman/parse" "github.com/containers/podman/v3/cmd/podman/registry" - "github.com/containers/podman/v3/libpod/define" + "github.com/containers/podman/v3/libpod/network/types" + "github.com/containers/podman/v3/libpod/network/util" "github.com/containers/podman/v3/pkg/domain/entities" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -38,11 +39,11 @@ func networkCreateFlags(cmd *cobra.Command) { flags := cmd.Flags() driverFlagName := "driver" - flags.StringVarP(&networkCreateOptions.Driver, driverFlagName, "d", "bridge", "driver to manage the network") + flags.StringVarP(&networkCreateOptions.Driver, driverFlagName, "d", types.DefaultNetworkDriver, "driver to manage the network") _ = cmd.RegisterFlagCompletionFunc(driverFlagName, common.AutocompleteNetworkDriver) optFlagName := "opt" - flags.StringArrayVarP(&opts, optFlagName, "o", []string{}, "Set driver specific options (default [])") + flags.StringArrayVarP(&opts, optFlagName, "o", nil, "Set driver specific options (default [])") _ = cmd.RegisterFlagCompletionFunc(optFlagName, completion.AutocompleteNone) gatewayFlagName := "gateway" @@ -55,6 +56,7 @@ func networkCreateFlags(cmd *cobra.Command) { flags.IPNetVar(&networkCreateOptions.Range, ipRangeFlagName, net.IPNet{}, "allocate container IP from range") _ = cmd.RegisterFlagCompletionFunc(ipRangeFlagName, completion.AutocompleteNone) + // TODO consider removing this for 4.0 macvlanFlagName := "macvlan" flags.StringVar(&networkCreateOptions.MacVLAN, macvlanFlagName, "", "create a Macvlan connection based on this device") // This option is deprecated @@ -88,9 +90,6 @@ func networkCreate(cmd *cobra.Command, args []string) error { name string ) if len(args) > 0 { - if !define.NameRegex.MatchString(args[0]) { - return define.RegexError - } name = args[0] } var err error @@ -100,17 +99,60 @@ func networkCreate(cmd *cobra.Command, args []string) error { } networkCreateOptions.Options, err = parse.GetAllLabels([]string{}, opts) if err != nil { - return errors.Wrapf(err, "unable to process options") + return errors.Wrapf(err, "unable to parse options") + } + + network := types.Network{ + Name: name, + Driver: networkCreateOptions.Driver, + Options: networkCreateOptions.Options, + Labels: networkCreateOptions.Labels, + IPv6Enabled: networkCreateOptions.IPv6, + DNSEnabled: !networkCreateOptions.DisableDNS, + Internal: networkCreateOptions.Internal, } + // old --macvlan option if networkCreateOptions.MacVLAN != "" { logrus.Warn("The --macvlan option is deprecated, use `--driver macvlan --opt parent=` instead") + network.Driver = types.MacVLANNetworkDriver + network.NetworkInterface = networkCreateOptions.MacVLAN + } else if networkCreateOptions.Driver == types.MacVLANNetworkDriver { + // new -d macvlan --opt parent=... syntax + if parent, ok := network.Options["parent"]; ok { + network.NetworkInterface = parent + delete(network.Options, "parent") + } + } + + if networkCreateOptions.Subnet.IP != nil { + s := types.Subnet{ + Subnet: types.IPNet{IPNet: networkCreateOptions.Subnet}, + Gateway: networkCreateOptions.Gateway, + } + if networkCreateOptions.Range.IP != nil { + startIP, err := util.FirstIPInSubnet(&networkCreateOptions.Range) + if err != nil { + return errors.Wrap(err, "failed to get first ip in range") + } + lastIP, err := util.LastIPInSubnet(&networkCreateOptions.Range) + if err != nil { + return errors.Wrap(err, "failed to get last ip in range") + } + s.LeaseRange = &types.LeaseRange{ + StartIP: startIP, + EndIP: lastIP, + } + } + network.Subnets = append(network.Subnets, s) + } else if networkCreateOptions.Range.IP != nil || networkCreateOptions.Gateway != nil { + return errors.New("cannot set gateway or range without subnet") } - response, err := registry.ContainerEngine().NetworkCreate(registry.Context(), name, networkCreateOptions) + response, err := registry.ContainerEngine().NetworkCreate(registry.Context(), network) if err != nil { return err } - fmt.Println(response.Filename) + fmt.Println(response.Name) return nil } diff --git a/cmd/podman/networks/list.go b/cmd/podman/networks/list.go index 0ac637ea5..124a17d5d 100644 --- a/cmd/podman/networks/list.go +++ b/cmd/podman/networks/list.go @@ -10,8 +10,8 @@ import ( "github.com/containers/podman/v3/cmd/podman/common" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/cmd/podman/validate" + "github.com/containers/podman/v3/libpod/network/types" "github.com/containers/podman/v3/pkg/domain/entities" - "github.com/containers/podman/v3/pkg/network" "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -90,13 +90,13 @@ func networkList(cmd *cobra.Command, args []string) error { return err } -func quietOut(responses []*entities.NetworkListReport) { +func quietOut(responses []types.Network) { for _, r := range responses { fmt.Println(r.Name) } } -func jsonOut(responses []*entities.NetworkListReport) error { +func jsonOut(responses []types.Network) error { prettyJSON, err := json.MarshalIndent(responses, "", " ") if err != nil { return err @@ -105,7 +105,7 @@ func jsonOut(responses []*entities.NetworkListReport) error { return nil } -func templateOut(responses []*entities.NetworkListReport, cmd *cobra.Command) error { +func templateOut(responses []types.Network, cmd *cobra.Command) error { nlprs := make([]ListPrintReports, 0, len(responses)) for _, r := range responses { nlprs = append(nlprs, ListPrintReports{r}) @@ -113,12 +113,10 @@ func templateOut(responses []*entities.NetworkListReport, cmd *cobra.Command) er // Headers() gets lost resolving the embedded field names so add them headers := report.Headers(ListPrintReports{}, map[string]string{ - "Name": "name", - "CNIVersion": "version", - "Version": "version", - "Plugins": "plugins", - "Labels": "labels", - "ID": "network id", + "Name": "name", + "Driver": "driver", + "Labels": "labels", + "ID": "network id", }) renderHeaders := report.HasTable(networkListOptions.Format) @@ -127,7 +125,7 @@ func templateOut(responses []*entities.NetworkListReport, cmd *cobra.Command) er row = report.NormalizeFormat(networkListOptions.Format) } else { // 'podman network ls' equivalent to 'podman network ls --format="table {{.ID}} {{.Name}} {{.Version}} {{.Plugins}}" ' renderHeaders = true - row = "{{.ID}}\t{{.Name}}\t{{.Version}}\t{{.Plugins}}\n" + row = "{{.ID}}\t{{.Name}}\t{{.Driver}}\n" } format = report.EnforceRange(row) @@ -153,23 +151,13 @@ func templateOut(responses []*entities.NetworkListReport, cmd *cobra.Command) er // ListPrintReports returns the network list report type ListPrintReports struct { - *entities.NetworkListReport -} - -// Version returns the CNI version -func (n ListPrintReports) Version() string { - return n.CNIVersion -} - -// Plugins returns the CNI Plugins -func (n ListPrintReports) Plugins() string { - return network.GetCNIPlugins(n.NetworkConfigList) + types.Network } // Labels returns any labels added to a Network func (n ListPrintReports) Labels() string { - list := make([]string, 0, len(n.NetworkListReport.Labels)) - for k, v := range n.NetworkListReport.Labels { + list := make([]string, 0, len(n.Network.Labels)) + for k, v := range n.Network.Labels { list = append(list, k+"="+v) } return strings.Join(list, ",") @@ -181,5 +169,5 @@ func (n ListPrintReports) ID() string { if noTrunc { length = 64 } - return network.GetNetworkID(n.Name)[:length] + return n.Network.ID[:length] } -- cgit v1.2.3-54-g00ecf