aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel J Walsh <dwalsh@redhat.com>2022-09-26 14:48:37 -0400
committerDaniel J Walsh <dwalsh@redhat.com>2022-09-27 14:04:24 -0400
commitf5fc0960e5eddf64a7d8fbf8fbfd3652b4ee5db3 (patch)
treeb72484a5ace3b59ff23c67a659700bc160f42288
parentcbc7185d1fc30fdb24466ccaaa5680aa10f67dd1 (diff)
downloadpodman-f5fc0960e5eddf64a7d8fbf8fbfd3652b4ee5db3.tar.gz
podman-f5fc0960e5eddf64a7d8fbf8fbfd3652b4ee5db3.tar.bz2
podman-f5fc0960e5eddf64a7d8fbf8fbfd3652b4ee5db3.zip
Default missing hostPort to containerPort is defined in kube.yaml
If user does not specify hostPort in a kube.yml file but does specify a containerPort, then the hostPort should default to the containerPort. Fixes: https://github.com/containers/podman/issues/15942 Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
-rw-r--r--pkg/specgen/generate/kube/kube.go3
-rw-r--r--test/e2e/play_kube_test.go6
-rw-r--r--test/system/700-play.bats24
3 files changed, 27 insertions, 6 deletions
diff --git a/pkg/specgen/generate/kube/kube.go b/pkg/specgen/generate/kube/kube.go
index 7d85fd2f3..bc9013afc 100644
--- a/pkg/specgen/generate/kube/kube.go
+++ b/pkg/specgen/generate/kube/kube.go
@@ -908,6 +908,9 @@ func getPodPorts(containers []v1.Container) []types.PortMapping {
if p.HostPort != 0 && p.ContainerPort == 0 {
p.ContainerPort = p.HostPort
}
+ if p.HostPort == 0 && p.ContainerPort != 0 {
+ p.HostPort = p.ContainerPort
+ }
if p.Protocol == "" {
p.Protocol = "tcp"
}
diff --git a/test/e2e/play_kube_test.go b/test/e2e/play_kube_test.go
index 67c88953a..651cb1074 100644
--- a/test/e2e/play_kube_test.go
+++ b/test/e2e/play_kube_test.go
@@ -133,8 +133,6 @@ spec:
containers:
- name: podnameEqualsContainerNameYaml
image: quay.io/libpod/alpine:latest
- ports:
- - containerPort: 80
`
var podWithoutAName = `
@@ -239,8 +237,6 @@ spec:
- "1.5"
name: alpine
image: quay.io/libpod/alpine:latest
- ports:
- - containerPort: 80
livenessProbe:
exec:
command:
@@ -274,8 +270,6 @@ spec:
- "1.5"
name: alpine
image: quay.io/libpod/alpine:latest
- ports:
- - containerPort: 80
livenessProbe:
exec:
command:
diff --git a/test/system/700-play.bats b/test/system/700-play.bats
index bad9544ff..578d28394 100644
--- a/test/system/700-play.bats
+++ b/test/system/700-play.bats
@@ -386,3 +386,27 @@ status: {}
run_podman rm -a -f
run_podman rm -f -t0 myyaml
}
+
+@test "podman kube play - hostport" {
+ HOST_PORT=$(random_free_port)
+ echo "
+apiVersion: v1
+kind: Pod
+metadata:
+ labels:
+ app: test
+ name: test_pod
+spec:
+ containers:
+ - name: server
+ image: $IMAGE
+ ports:
+ - name: hostp
+ containerPort: $HOST_PORT
+" > $PODMAN_TMPDIR/testpod.yaml
+
+ run_podman kube play $PODMAN_TMPDIR/testpod.yaml
+ run_podman pod inspect test_pod --format "{{.InfraConfig.PortBindings}}"
+ assert "$output" = "map[$HOST_PORT/tcp:[{ $HOST_PORT}]]"
+ run_podman kube down $PODMAN_TMPDIR/testpod.yaml
+}