Skip to content

Commit

Permalink
Default missing hostPort to containerPort is defined in kube.yaml
Browse files Browse the repository at this point in the history
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: containers#15942

Signed-off-by: Daniel J Walsh <[email protected]>
  • Loading branch information
rhatdan committed Sep 27, 2022
1 parent cbc7185 commit f5fc096
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
3 changes: 3 additions & 0 deletions pkg/specgen/generate/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
6 changes: 0 additions & 6 deletions test/e2e/play_kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ spec:
containers:
- name: podnameEqualsContainerNameYaml
image: quay.io/libpod/alpine:latest
ports:
- containerPort: 80
`

var podWithoutAName = `
Expand Down Expand Up @@ -239,8 +237,6 @@ spec:
- "1.5"
name: alpine
image: quay.io/libpod/alpine:latest
ports:
- containerPort: 80
livenessProbe:
exec:
command:
Expand Down Expand Up @@ -274,8 +270,6 @@ spec:
- "1.5"
name: alpine
image: quay.io/libpod/alpine:latest
ports:
- containerPort: 80
livenessProbe:
exec:
command:
Expand Down
24 changes: 24 additions & 0 deletions test/system/700-play.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit f5fc096

Please sign in to comment.