Skip to content

Commit

Permalink
Merge pull request #11322 from Luap99/network-libpod
Browse files Browse the repository at this point in the history
Wire network interface into libpod
  • Loading branch information
openshift-merge-robot authored Sep 15, 2021
2 parents 505c971 + 5e83094 commit 5f41ffd
Show file tree
Hide file tree
Showing 101 changed files with 1,232 additions and 8,300 deletions.
16 changes: 7 additions & 9 deletions cmd/podman/common/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/containers/image/v5/pkg/sysregistriesv2"
"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/pkg/domain/entities"
"github.com/containers/podman/v3/pkg/network"
"github.com/containers/podman/v3/pkg/rootless"
Expand Down Expand Up @@ -1108,9 +1109,9 @@ func AutocompleteManifestFormat(cmd *cobra.Command, args []string, toComplete st
}

// AutocompleteNetworkDriver - Autocomplete network driver option.
// -> "bridge"
// -> "bridge", "macvlan"
func AutocompleteNetworkDriver(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
drivers := []string{"bridge"}
drivers := []string{types.BridgeNetworkDriver, types.MacVLANNetworkDriver}
return drivers, cobra.ShellCompDirectiveNoFileComp
}

Expand Down Expand Up @@ -1252,16 +1253,13 @@ func AutocompletePruneFilters(cmd *cobra.Command, args []string, toComplete stri
// AutocompleteNetworkFilters - Autocomplete network ls --filter options.
func AutocompleteNetworkFilters(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
kv := keyValueCompletion{
"name=": func(s string) ([]string, cobra.ShellCompDirective) { return getNetworks(cmd, s, completeNames) },
"id=": func(s string) ([]string, cobra.ShellCompDirective) { return getNetworks(cmd, s, completeIDs) },
"plugin=": func(_ string) ([]string, cobra.ShellCompDirective) {
return []string{"bridge", "portmap",
"firewall", "tuning", "dnsname", "macvlan"}, cobra.ShellCompDirectiveNoFileComp
},
"name=": func(s string) ([]string, cobra.ShellCompDirective) { return getNetworks(cmd, s, completeNames) },
"id=": func(s string) ([]string, cobra.ShellCompDirective) { return getNetworks(cmd, s, completeIDs) },
"label=": nil,
"driver=": func(_ string) ([]string, cobra.ShellCompDirective) {
return []string{"bridge"}, cobra.ShellCompDirectiveNoFileComp
return []string{types.BridgeNetworkDriver, types.MacVLANNetworkDriver}, cobra.ShellCompDirectiveNoFileComp
},
"until=": nil,
}
return completeKeyValues(toComplete, kv)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/podman/containers/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,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/cri-o/ocicni/pkg/ocicni"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -73,7 +73,7 @@ func port(_ *cobra.Command, args []string) error {
var (
container string
err error
userPort ocicni.PortMapping
userPort types.OCICNIPortMapping
)

if len(args) == 0 && !portOpts.Latest && !portOpts.All {
Expand Down Expand Up @@ -105,7 +105,7 @@ func port(_ *cobra.Command, args []string) error {
if err != nil {
return err
}
userPort = ocicni.PortMapping{
userPort = types.OCICNIPortMapping{
HostPort: 0,
ContainerPort: int32(portNum),
Protocol: fields[1],
Expand Down
18 changes: 9 additions & 9 deletions cmd/podman/containers/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/containers/podman/v3/cmd/podman/registry"
"github.com/containers/podman/v3/cmd/podman/utils"
"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/cri-o/ocicni/pkg/ocicni"
"github.com/docker/go-units"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -469,7 +469,7 @@ func (l psReporter) UTS() string {

// portsToString converts the ports used to a string of the from "port1, port2"
// and also groups a continuous list of ports into a readable format.
func portsToString(ports []ocicni.PortMapping) string {
func portsToString(ports []types.OCICNIPortMapping) string {
if len(ports) == 0 {
return ""
}
Expand All @@ -478,8 +478,8 @@ func portsToString(ports []ocicni.PortMapping) string {
return comparePorts(ports[i], ports[j])
})

portGroups := [][]ocicni.PortMapping{}
currentGroup := []ocicni.PortMapping{}
portGroups := [][]types.OCICNIPortMapping{}
currentGroup := []types.OCICNIPortMapping{}
for i, v := range ports {
var prevPort, nextPort *int32
if i > 0 {
Expand All @@ -492,17 +492,17 @@ func portsToString(ports []ocicni.PortMapping) string {
port := v.ContainerPort

// Helper functions
addToCurrentGroup := func(x ocicni.PortMapping) {
addToCurrentGroup := func(x types.OCICNIPortMapping) {
currentGroup = append(currentGroup, x)
}

addToPortGroup := func(x ocicni.PortMapping) {
portGroups = append(portGroups, []ocicni.PortMapping{x})
addToPortGroup := func(x types.OCICNIPortMapping) {
portGroups = append(portGroups, []types.OCICNIPortMapping{x})
}

finishCurrentGroup := func() {
portGroups = append(portGroups, currentGroup)
currentGroup = []ocicni.PortMapping{}
currentGroup = []types.OCICNIPortMapping{}
}

// Single entry slice
Expand Down Expand Up @@ -600,7 +600,7 @@ func portsToString(ports []ocicni.PortMapping) string {
return strings.Join(portDisplay, ", ")
}

func comparePorts(i, j ocicni.PortMapping) bool {
func comparePorts(i, j types.OCICNIPortMapping) bool {
if i.ContainerPort != j.ContainerPort {
return i.ContainerPort < j.ContainerPort
}
Expand Down
60 changes: 51 additions & 9 deletions cmd/podman/networks/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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=<device>` 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
}
38 changes: 13 additions & 25 deletions cmd/podman/networks/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -105,20 +105,18 @@ 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})
}

// 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)
Expand All @@ -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)

Expand All @@ -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, ",")
Expand All @@ -181,5 +169,5 @@ func (n ListPrintReports) ID() string {
if noTrunc {
length = 64
}
return network.GetNetworkID(n.Name)[:length]
return n.Network.ID[:length]
}
3 changes: 3 additions & 0 deletions contrib/cirrus/setup_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ esac
case "$PRIV_NAME" in
root) ;;
rootless)
# load kernel modules since the rootless user has no permission to do so
modprobe ip6_tables || :
modprobe ip6table_nat || :
# Needs to exist for setup_rootless()
ROOTLESS_USER="${ROOTLESS_USER:-some${RANDOM}dude}"
echo "ROOTLESS_USER=$ROOTLESS_USER" >> /etc/ci_environment
Expand Down
Loading

0 comments on commit 5f41ffd

Please sign in to comment.