mirror of
https://github.com/containers/podman.git
synced 2025-07-15 03:02:52 +08:00
Merge pull request #4542 from mheon/static_ip_single_net_allowed
Allow --ip and --mac to be set when joining a CNI net
This commit is contained in:
@ -29,19 +29,40 @@ import (
|
|||||||
|
|
||||||
// Get an OCICNI network config
|
// Get an OCICNI network config
|
||||||
func (r *Runtime) getPodNetwork(id, name, nsPath string, networks []string, ports []ocicni.PortMapping, staticIP net.IP, staticMAC net.HardwareAddr) ocicni.PodNetwork {
|
func (r *Runtime) getPodNetwork(id, name, nsPath string, networks []string, ports []ocicni.PortMapping, staticIP net.IP, staticMAC net.HardwareAddr) ocicni.PodNetwork {
|
||||||
defaultNetwork := r.netPlugin.GetDefaultNetworkName()
|
var networkKey string
|
||||||
|
if len(networks) > 0 {
|
||||||
|
// This is inconsistent for >1 network, but it's probably the
|
||||||
|
// best we can do.
|
||||||
|
networkKey = networks[0]
|
||||||
|
} else {
|
||||||
|
networkKey = r.netPlugin.GetDefaultNetworkName()
|
||||||
|
}
|
||||||
network := ocicni.PodNetwork{
|
network := ocicni.PodNetwork{
|
||||||
Name: name,
|
Name: name,
|
||||||
Namespace: name, // TODO is there something else we should put here? We don't know about Kube namespaces
|
Namespace: name, // TODO is there something else we should put here? We don't know about Kube namespaces
|
||||||
ID: id,
|
ID: id,
|
||||||
NetNS: nsPath,
|
NetNS: nsPath,
|
||||||
RuntimeConfig: map[string]ocicni.RuntimeConfig{
|
RuntimeConfig: map[string]ocicni.RuntimeConfig{
|
||||||
defaultNetwork: {PortMappings: ports},
|
networkKey: {PortMappings: ports},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we have extra networks, add them
|
||||||
|
if len(networks) > 0 {
|
||||||
|
network.Networks = make([]ocicni.NetAttachment, len(networks))
|
||||||
|
for i, netName := range networks {
|
||||||
|
network.Networks[i].Name = netName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if staticIP != nil || staticMAC != nil {
|
if staticIP != nil || staticMAC != nil {
|
||||||
network.Networks = []ocicni.NetAttachment{{Name: defaultNetwork}}
|
// For static IP or MAC, we need to populate networks even if
|
||||||
|
// it's just the default.
|
||||||
|
if len(networks) == 0 {
|
||||||
|
// If len(networks) == 0 this is guaranteed to be the
|
||||||
|
// default network.
|
||||||
|
network.Networks = []ocicni.NetAttachment{{Name: networkKey}}
|
||||||
|
}
|
||||||
var rt ocicni.RuntimeConfig = ocicni.RuntimeConfig{PortMappings: ports}
|
var rt ocicni.RuntimeConfig = ocicni.RuntimeConfig{PortMappings: ports}
|
||||||
if staticIP != nil {
|
if staticIP != nil {
|
||||||
rt.IP = staticIP.String()
|
rt.IP = staticIP.String()
|
||||||
@ -50,12 +71,7 @@ func (r *Runtime) getPodNetwork(id, name, nsPath string, networks []string, port
|
|||||||
rt.MAC = staticMAC.String()
|
rt.MAC = staticMAC.String()
|
||||||
}
|
}
|
||||||
network.RuntimeConfig = map[string]ocicni.RuntimeConfig{
|
network.RuntimeConfig = map[string]ocicni.RuntimeConfig{
|
||||||
defaultNetwork: rt,
|
networkKey: rt,
|
||||||
}
|
|
||||||
} else {
|
|
||||||
network.Networks = make([]ocicni.NetAttachment, len(networks))
|
|
||||||
for i, netName := range networks {
|
|
||||||
network.Networks[i].Name = netName
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1041,8 +1041,8 @@ func WithStaticIP(ip net.IP) CtrCreateOption {
|
|||||||
return errors.Wrapf(define.ErrInvalidArg, "cannot set a static IP if the container is not creating a network namespace")
|
return errors.Wrapf(define.ErrInvalidArg, "cannot set a static IP if the container is not creating a network namespace")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(ctr.config.Networks) != 0 {
|
if len(ctr.config.Networks) > 1 {
|
||||||
return errors.Wrapf(define.ErrInvalidArg, "cannot set a static IP if joining additional CNI networks")
|
return errors.Wrapf(define.ErrInvalidArg, "cannot set a static IP if joining more than 1 CNI network")
|
||||||
}
|
}
|
||||||
|
|
||||||
ctr.config.StaticIP = ip
|
ctr.config.StaticIP = ip
|
||||||
@ -1066,8 +1066,8 @@ func WithStaticMAC(mac net.HardwareAddr) CtrCreateOption {
|
|||||||
return errors.Wrapf(define.ErrInvalidArg, "cannot set a static MAC if the container is not creating a network namespace")
|
return errors.Wrapf(define.ErrInvalidArg, "cannot set a static MAC if the container is not creating a network namespace")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(ctr.config.Networks) != 0 {
|
if len(ctr.config.Networks) > 1 {
|
||||||
return errors.Wrapf(define.ErrInvalidArg, "cannot set a static MAC if joining additional CNI networks")
|
return errors.Wrapf(define.ErrInvalidArg, "cannot set a static MAC if joining more than 1 CNI network")
|
||||||
}
|
}
|
||||||
|
|
||||||
ctr.config.StaticMAC = mac
|
ctr.config.StaticMAC = mac
|
||||||
|
@ -232,4 +232,18 @@ var _ = Describe("Podman run networking", func() {
|
|||||||
Expect(session).To(ExitWithError())
|
Expect(session).To(ExitWithError())
|
||||||
Expect(session.ErrorToString()).To(ContainSubstring("stat /run/netns/xxy: no such file or directory"))
|
Expect(session.ErrorToString()).To(ContainSubstring("stat /run/netns/xxy: no such file or directory"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("podman run in custom CNI network with --static-ip", func() {
|
||||||
|
SkipIfRootless()
|
||||||
|
netName := "podmantestnetwork"
|
||||||
|
ipAddr := "10.20.30.128"
|
||||||
|
create := podmanTest.Podman([]string{"network", "create", "--subnet", "10.20.30.0/24", netName})
|
||||||
|
create.WaitWithDefaultTimeout()
|
||||||
|
Expect(create.ExitCode()).To(BeZero())
|
||||||
|
|
||||||
|
run := podmanTest.Podman([]string{"run", "-t", "-i", "--rm", "--net", netName, "--ip", ipAddr, ALPINE, "ip", "addr"})
|
||||||
|
run.WaitWithDefaultTimeout()
|
||||||
|
Expect(run.ExitCode()).To(BeZero())
|
||||||
|
Expect(run.OutputToString()).To(ContainSubstring(ipAddr))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user