Skip to content

Commit 5f41ffd

Browse files
Merge pull request #11322 from Luap99/network-libpod
Wire network interface into libpod
2 parents 505c971 + 5e83094 commit 5f41ffd

File tree

101 files changed

+1232
-8300
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+1232
-8300
lines changed

cmd/podman/common/completion.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/containers/image/v5/pkg/sysregistriesv2"
1212
"github.com/containers/podman/v3/cmd/podman/registry"
1313
"github.com/containers/podman/v3/libpod/define"
14+
"github.com/containers/podman/v3/libpod/network/types"
1415
"github.com/containers/podman/v3/pkg/domain/entities"
1516
"github.com/containers/podman/v3/pkg/network"
1617
"github.com/containers/podman/v3/pkg/rootless"
@@ -1108,9 +1109,9 @@ func AutocompleteManifestFormat(cmd *cobra.Command, args []string, toComplete st
11081109
}
11091110

11101111
// AutocompleteNetworkDriver - Autocomplete network driver option.
1111-
// -> "bridge"
1112+
// -> "bridge", "macvlan"
11121113
func AutocompleteNetworkDriver(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
1113-
drivers := []string{"bridge"}
1114+
drivers := []string{types.BridgeNetworkDriver, types.MacVLANNetworkDriver}
11141115
return drivers, cobra.ShellCompDirectiveNoFileComp
11151116
}
11161117

@@ -1252,16 +1253,13 @@ func AutocompletePruneFilters(cmd *cobra.Command, args []string, toComplete stri
12521253
// AutocompleteNetworkFilters - Autocomplete network ls --filter options.
12531254
func AutocompleteNetworkFilters(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
12541255
kv := keyValueCompletion{
1255-
"name=": func(s string) ([]string, cobra.ShellCompDirective) { return getNetworks(cmd, s, completeNames) },
1256-
"id=": func(s string) ([]string, cobra.ShellCompDirective) { return getNetworks(cmd, s, completeIDs) },
1257-
"plugin=": func(_ string) ([]string, cobra.ShellCompDirective) {
1258-
return []string{"bridge", "portmap",
1259-
"firewall", "tuning", "dnsname", "macvlan"}, cobra.ShellCompDirectiveNoFileComp
1260-
},
1256+
"name=": func(s string) ([]string, cobra.ShellCompDirective) { return getNetworks(cmd, s, completeNames) },
1257+
"id=": func(s string) ([]string, cobra.ShellCompDirective) { return getNetworks(cmd, s, completeIDs) },
12611258
"label=": nil,
12621259
"driver=": func(_ string) ([]string, cobra.ShellCompDirective) {
1263-
return []string{"bridge"}, cobra.ShellCompDirectiveNoFileComp
1260+
return []string{types.BridgeNetworkDriver, types.MacVLANNetworkDriver}, cobra.ShellCompDirectiveNoFileComp
12641261
},
1262+
"until=": nil,
12651263
}
12661264
return completeKeyValues(toComplete, kv)
12671265
}

cmd/podman/containers/port.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"github.com/containers/podman/v3/cmd/podman/common"
99
"github.com/containers/podman/v3/cmd/podman/registry"
1010
"github.com/containers/podman/v3/cmd/podman/validate"
11+
"github.com/containers/podman/v3/libpod/network/types"
1112
"github.com/containers/podman/v3/pkg/domain/entities"
12-
"github.com/cri-o/ocicni/pkg/ocicni"
1313
"github.com/pkg/errors"
1414
"github.com/spf13/cobra"
1515
"github.com/spf13/pflag"
@@ -73,7 +73,7 @@ func port(_ *cobra.Command, args []string) error {
7373
var (
7474
container string
7575
err error
76-
userPort ocicni.PortMapping
76+
userPort types.OCICNIPortMapping
7777
)
7878

7979
if len(args) == 0 && !portOpts.Latest && !portOpts.All {
@@ -105,7 +105,7 @@ func port(_ *cobra.Command, args []string) error {
105105
if err != nil {
106106
return err
107107
}
108-
userPort = ocicni.PortMapping{
108+
userPort = types.OCICNIPortMapping{
109109
HostPort: 0,
110110
ContainerPort: int32(portNum),
111111
Protocol: fields[1],

cmd/podman/containers/ps.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515
"github.com/containers/podman/v3/cmd/podman/registry"
1616
"github.com/containers/podman/v3/cmd/podman/utils"
1717
"github.com/containers/podman/v3/cmd/podman/validate"
18+
"github.com/containers/podman/v3/libpod/network/types"
1819
"github.com/containers/podman/v3/pkg/domain/entities"
19-
"github.com/cri-o/ocicni/pkg/ocicni"
2020
"github.com/docker/go-units"
2121
"github.com/pkg/errors"
2222
"github.com/sirupsen/logrus"
@@ -469,7 +469,7 @@ func (l psReporter) UTS() string {
469469

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

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

494494
// Helper functions
495-
addToCurrentGroup := func(x ocicni.PortMapping) {
495+
addToCurrentGroup := func(x types.OCICNIPortMapping) {
496496
currentGroup = append(currentGroup, x)
497497
}
498498

499-
addToPortGroup := func(x ocicni.PortMapping) {
500-
portGroups = append(portGroups, []ocicni.PortMapping{x})
499+
addToPortGroup := func(x types.OCICNIPortMapping) {
500+
portGroups = append(portGroups, []types.OCICNIPortMapping{x})
501501
}
502502

503503
finishCurrentGroup := func() {
504504
portGroups = append(portGroups, currentGroup)
505-
currentGroup = []ocicni.PortMapping{}
505+
currentGroup = []types.OCICNIPortMapping{}
506506
}
507507

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

603-
func comparePorts(i, j ocicni.PortMapping) bool {
603+
func comparePorts(i, j types.OCICNIPortMapping) bool {
604604
if i.ContainerPort != j.ContainerPort {
605605
return i.ContainerPort < j.ContainerPort
606606
}

cmd/podman/networks/create.go

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
"github.com/containers/podman/v3/cmd/podman/common"
99
"github.com/containers/podman/v3/cmd/podman/parse"
1010
"github.com/containers/podman/v3/cmd/podman/registry"
11-
"github.com/containers/podman/v3/libpod/define"
11+
"github.com/containers/podman/v3/libpod/network/types"
12+
"github.com/containers/podman/v3/libpod/network/util"
1213
"github.com/containers/podman/v3/pkg/domain/entities"
1314
"github.com/pkg/errors"
1415
"github.com/sirupsen/logrus"
@@ -38,11 +39,11 @@ func networkCreateFlags(cmd *cobra.Command) {
3839
flags := cmd.Flags()
3940

4041
driverFlagName := "driver"
41-
flags.StringVarP(&networkCreateOptions.Driver, driverFlagName, "d", "bridge", "driver to manage the network")
42+
flags.StringVarP(&networkCreateOptions.Driver, driverFlagName, "d", types.DefaultNetworkDriver, "driver to manage the network")
4243
_ = cmd.RegisterFlagCompletionFunc(driverFlagName, common.AutocompleteNetworkDriver)
4344

4445
optFlagName := "opt"
45-
flags.StringArrayVarP(&opts, optFlagName, "o", []string{}, "Set driver specific options (default [])")
46+
flags.StringArrayVarP(&opts, optFlagName, "o", nil, "Set driver specific options (default [])")
4647
_ = cmd.RegisterFlagCompletionFunc(optFlagName, completion.AutocompleteNone)
4748

4849
gatewayFlagName := "gateway"
@@ -55,6 +56,7 @@ func networkCreateFlags(cmd *cobra.Command) {
5556
flags.IPNetVar(&networkCreateOptions.Range, ipRangeFlagName, net.IPNet{}, "allocate container IP from range")
5657
_ = cmd.RegisterFlagCompletionFunc(ipRangeFlagName, completion.AutocompleteNone)
5758

59+
// TODO consider removing this for 4.0
5860
macvlanFlagName := "macvlan"
5961
flags.StringVar(&networkCreateOptions.MacVLAN, macvlanFlagName, "", "create a Macvlan connection based on this device")
6062
// This option is deprecated
@@ -88,9 +90,6 @@ func networkCreate(cmd *cobra.Command, args []string) error {
8890
name string
8991
)
9092
if len(args) > 0 {
91-
if !define.NameRegex.MatchString(args[0]) {
92-
return define.RegexError
93-
}
9493
name = args[0]
9594
}
9695
var err error
@@ -100,17 +99,60 @@ func networkCreate(cmd *cobra.Command, args []string) error {
10099
}
101100
networkCreateOptions.Options, err = parse.GetAllLabels([]string{}, opts)
102101
if err != nil {
103-
return errors.Wrapf(err, "unable to process options")
102+
return errors.Wrapf(err, "unable to parse options")
103+
}
104+
105+
network := types.Network{
106+
Name: name,
107+
Driver: networkCreateOptions.Driver,
108+
Options: networkCreateOptions.Options,
109+
Labels: networkCreateOptions.Labels,
110+
IPv6Enabled: networkCreateOptions.IPv6,
111+
DNSEnabled: !networkCreateOptions.DisableDNS,
112+
Internal: networkCreateOptions.Internal,
104113
}
105114

115+
// old --macvlan option
106116
if networkCreateOptions.MacVLAN != "" {
107117
logrus.Warn("The --macvlan option is deprecated, use `--driver macvlan --opt parent=<device>` instead")
118+
network.Driver = types.MacVLANNetworkDriver
119+
network.NetworkInterface = networkCreateOptions.MacVLAN
120+
} else if networkCreateOptions.Driver == types.MacVLANNetworkDriver {
121+
// new -d macvlan --opt parent=... syntax
122+
if parent, ok := network.Options["parent"]; ok {
123+
network.NetworkInterface = parent
124+
delete(network.Options, "parent")
125+
}
126+
}
127+
128+
if networkCreateOptions.Subnet.IP != nil {
129+
s := types.Subnet{
130+
Subnet: types.IPNet{IPNet: networkCreateOptions.Subnet},
131+
Gateway: networkCreateOptions.Gateway,
132+
}
133+
if networkCreateOptions.Range.IP != nil {
134+
startIP, err := util.FirstIPInSubnet(&networkCreateOptions.Range)
135+
if err != nil {
136+
return errors.Wrap(err, "failed to get first ip in range")
137+
}
138+
lastIP, err := util.LastIPInSubnet(&networkCreateOptions.Range)
139+
if err != nil {
140+
return errors.Wrap(err, "failed to get last ip in range")
141+
}
142+
s.LeaseRange = &types.LeaseRange{
143+
StartIP: startIP,
144+
EndIP: lastIP,
145+
}
146+
}
147+
network.Subnets = append(network.Subnets, s)
148+
} else if networkCreateOptions.Range.IP != nil || networkCreateOptions.Gateway != nil {
149+
return errors.New("cannot set gateway or range without subnet")
108150
}
109151

110-
response, err := registry.ContainerEngine().NetworkCreate(registry.Context(), name, networkCreateOptions)
152+
response, err := registry.ContainerEngine().NetworkCreate(registry.Context(), network)
111153
if err != nil {
112154
return err
113155
}
114-
fmt.Println(response.Filename)
156+
fmt.Println(response.Name)
115157
return nil
116158
}

cmd/podman/networks/list.go

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"github.com/containers/podman/v3/cmd/podman/common"
1111
"github.com/containers/podman/v3/cmd/podman/registry"
1212
"github.com/containers/podman/v3/cmd/podman/validate"
13+
"github.com/containers/podman/v3/libpod/network/types"
1314
"github.com/containers/podman/v3/pkg/domain/entities"
14-
"github.com/containers/podman/v3/pkg/network"
1515
"github.com/pkg/errors"
1616
"github.com/spf13/cobra"
1717
"github.com/spf13/pflag"
@@ -90,13 +90,13 @@ func networkList(cmd *cobra.Command, args []string) error {
9090
return err
9191
}
9292

93-
func quietOut(responses []*entities.NetworkListReport) {
93+
func quietOut(responses []types.Network) {
9494
for _, r := range responses {
9595
fmt.Println(r.Name)
9696
}
9797
}
9898

99-
func jsonOut(responses []*entities.NetworkListReport) error {
99+
func jsonOut(responses []types.Network) error {
100100
prettyJSON, err := json.MarshalIndent(responses, "", " ")
101101
if err != nil {
102102
return err
@@ -105,20 +105,18 @@ func jsonOut(responses []*entities.NetworkListReport) error {
105105
return nil
106106
}
107107

108-
func templateOut(responses []*entities.NetworkListReport, cmd *cobra.Command) error {
108+
func templateOut(responses []types.Network, cmd *cobra.Command) error {
109109
nlprs := make([]ListPrintReports, 0, len(responses))
110110
for _, r := range responses {
111111
nlprs = append(nlprs, ListPrintReports{r})
112112
}
113113

114114
// Headers() gets lost resolving the embedded field names so add them
115115
headers := report.Headers(ListPrintReports{}, map[string]string{
116-
"Name": "name",
117-
"CNIVersion": "version",
118-
"Version": "version",
119-
"Plugins": "plugins",
120-
"Labels": "labels",
121-
"ID": "network id",
116+
"Name": "name",
117+
"Driver": "driver",
118+
"Labels": "labels",
119+
"ID": "network id",
122120
})
123121

124122
renderHeaders := report.HasTable(networkListOptions.Format)
@@ -127,7 +125,7 @@ func templateOut(responses []*entities.NetworkListReport, cmd *cobra.Command) er
127125
row = report.NormalizeFormat(networkListOptions.Format)
128126
} else { // 'podman network ls' equivalent to 'podman network ls --format="table {{.ID}} {{.Name}} {{.Version}} {{.Plugins}}" '
129127
renderHeaders = true
130-
row = "{{.ID}}\t{{.Name}}\t{{.Version}}\t{{.Plugins}}\n"
128+
row = "{{.ID}}\t{{.Name}}\t{{.Driver}}\n"
131129
}
132130
format = report.EnforceRange(row)
133131

@@ -153,23 +151,13 @@ func templateOut(responses []*entities.NetworkListReport, cmd *cobra.Command) er
153151

154152
// ListPrintReports returns the network list report
155153
type ListPrintReports struct {
156-
*entities.NetworkListReport
157-
}
158-
159-
// Version returns the CNI version
160-
func (n ListPrintReports) Version() string {
161-
return n.CNIVersion
162-
}
163-
164-
// Plugins returns the CNI Plugins
165-
func (n ListPrintReports) Plugins() string {
166-
return network.GetCNIPlugins(n.NetworkConfigList)
154+
types.Network
167155
}
168156

169157
// Labels returns any labels added to a Network
170158
func (n ListPrintReports) Labels() string {
171-
list := make([]string, 0, len(n.NetworkListReport.Labels))
172-
for k, v := range n.NetworkListReport.Labels {
159+
list := make([]string, 0, len(n.Network.Labels))
160+
for k, v := range n.Network.Labels {
173161
list = append(list, k+"="+v)
174162
}
175163
return strings.Join(list, ",")
@@ -181,5 +169,5 @@ func (n ListPrintReports) ID() string {
181169
if noTrunc {
182170
length = 64
183171
}
184-
return network.GetNetworkID(n.Name)[:length]
172+
return n.Network.ID[:length]
185173
}

contrib/cirrus/setup_environment.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ esac
173173
case "$PRIV_NAME" in
174174
root) ;;
175175
rootless)
176+
# load kernel modules since the rootless user has no permission to do so
177+
modprobe ip6_tables || :
178+
modprobe ip6table_nat || :
176179
# Needs to exist for setup_rootless()
177180
ROOTLESS_USER="${ROOTLESS_USER:-some${RANDOM}dude}"
178181
echo "ROOTLESS_USER=$ROOTLESS_USER" >> /etc/ci_environment

0 commit comments

Comments
 (0)