Set NetNS mode instead of value

when HostNetwork is true in the pod spec.
Also propagate whether host network namespace should be used for containers.

Add test for HostNetwork setting in kubeYaml.
The infra configuration should reflect the setting.

Signed-off-by: Benedikt Ziemons <ben@rs485.network>
This commit is contained in:
Benedikt Ziemons
2020-12-23 19:28:32 +01:00
parent 54b82a175f
commit 14439b9869
3 changed files with 36 additions and 3 deletions

View File

@ -226,7 +226,7 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
return nil, err return nil, err
} }
specGen, err := kube.ToSpecGen(ctx, container, container.Image, newImage, volumes, pod.ID(), podName, podInfraID, configMaps, seccompPaths, ctrRestartPolicy) specGen, err := kube.ToSpecGen(ctx, container, container.Image, newImage, volumes, pod.ID(), podName, podInfraID, configMaps, seccompPaths, ctrRestartPolicy, p.NetNS.IsHost())
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -30,7 +30,7 @@ func ToPodGen(ctx context.Context, podName string, podYAML *v1.PodTemplateSpec)
p.Hostname = podName p.Hostname = podName
} }
if podYAML.Spec.HostNetwork { if podYAML.Spec.HostNetwork {
p.NetNS.Value = "host" p.NetNS.NSMode = specgen.Host
} }
if podYAML.Spec.HostAliases != nil { if podYAML.Spec.HostAliases != nil {
hosts := make([]string, 0, len(podYAML.Spec.HostAliases)) hosts := make([]string, 0, len(podYAML.Spec.HostAliases))
@ -47,7 +47,7 @@ func ToPodGen(ctx context.Context, podName string, podYAML *v1.PodTemplateSpec)
return p, nil return p, nil
} }
func ToSpecGen(ctx context.Context, containerYAML v1.Container, iid string, newImage *image.Image, volumes map[string]*KubeVolume, podID, podName, infraID string, configMaps []v1.ConfigMap, seccompPaths *KubeSeccompPaths, restartPolicy string) (*specgen.SpecGenerator, error) { func ToSpecGen(ctx context.Context, containerYAML v1.Container, iid string, newImage *image.Image, volumes map[string]*KubeVolume, podID, podName, infraID string, configMaps []v1.ConfigMap, seccompPaths *KubeSeccompPaths, restartPolicy string, hostNet bool) (*specgen.SpecGenerator, error) {
s := specgen.NewSpecGenerator(iid, false) s := specgen.NewSpecGenerator(iid, false)
// podName should be non-empty for Deployment objects to be able to create // podName should be non-empty for Deployment objects to be able to create
@ -214,6 +214,10 @@ func ToSpecGen(ctx context.Context, containerYAML v1.Container, iid string, newI
s.RestartPolicy = restartPolicy s.RestartPolicy = restartPolicy
if hostNet {
s.NetNS.NSMode = specgen.Host
}
return s, nil return s, nil
} }

View File

@ -62,6 +62,7 @@ metadata:
spec: spec:
restartPolicy: {{ .RestartPolicy }} restartPolicy: {{ .RestartPolicy }}
hostname: {{ .Hostname }} hostname: {{ .Hostname }}
hostNetwork: {{ .HostNetwork }}
hostAliases: hostAliases:
{{ range .HostAliases }} {{ range .HostAliases }}
- hostnames: - hostnames:
@ -220,6 +221,7 @@ spec:
spec: spec:
restartPolicy: {{ .RestartPolicy }} restartPolicy: {{ .RestartPolicy }}
hostname: {{ .Hostname }} hostname: {{ .Hostname }}
hostNetwork: {{ .HostNetwork }}
containers: containers:
{{ with .Ctrs }} {{ with .Ctrs }}
{{ range . }} {{ range . }}
@ -376,6 +378,7 @@ type Pod struct {
Name string Name string
RestartPolicy string RestartPolicy string
Hostname string Hostname string
HostNetwork bool
HostAliases []HostAlias HostAliases []HostAlias
Ctrs []*Ctr Ctrs []*Ctr
Volumes []*Volume Volumes []*Volume
@ -396,6 +399,7 @@ func getPod(options ...podOption) *Pod {
Name: defaultPodName, Name: defaultPodName,
RestartPolicy: "Never", RestartPolicy: "Never",
Hostname: "", Hostname: "",
HostNetwork: false,
HostAliases: nil, HostAliases: nil,
Ctrs: make([]*Ctr, 0), Ctrs: make([]*Ctr, 0),
Volumes: make([]*Volume, 0), Volumes: make([]*Volume, 0),
@ -464,6 +468,12 @@ func withVolume(v *Volume) podOption {
} }
} }
func withHostNetwork() podOption {
return func(pod *Pod) {
pod.HostNetwork = true
}
}
// Deployment describes the options a kube yaml can be configured at deployment level // Deployment describes the options a kube yaml can be configured at deployment level
type Deployment struct { type Deployment struct {
Name string Name string
@ -1587,4 +1597,23 @@ MemoryReservation: {{ .HostConfig.MemoryReservation }}`})
Expect(inspect.ExitCode()).To(Equal(0)) Expect(inspect.ExitCode()).To(Equal(0))
Expect(inspect.OutputToString()).To(Equal("false")) Expect(inspect.OutputToString()).To(Equal("false"))
}) })
It("podman play kube test with HostNetwork", func() {
if !strings.Contains(podmanTest.OCIRuntime, "crun") {
Skip("Test only works on crun")
}
pod := getPod(withHostNetwork())
err := generateKubeYaml("pod", pod, kubeYaml)
Expect(err).To(BeNil())
kube := podmanTest.Podman([]string{"play", "kube", kubeYaml})
kube.WaitWithDefaultTimeout()
Expect(kube.ExitCode()).To(Equal(0))
inspect := podmanTest.Podman([]string{"inspect", pod.Name, "--format", "{{ .InfraConfig.HostNetwork }}"})
inspect.WaitWithDefaultTimeout()
Expect(inspect.ExitCode()).To(Equal(0))
Expect(inspect.OutputToString()).To(Equal("true"))
})
}) })