Skip to content

Commit 211eb6d

Browse files
Merge pull request containers#17501 from umohnani8/port
Don't set hostPort when generating a service
2 parents 26abb3a + c0f983d commit 211eb6d

File tree

3 files changed

+55
-16
lines changed

3 files changed

+55
-16
lines changed

libpod/kube.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ import (
3333

3434
// GenerateForKube takes a slice of libpod containers and generates
3535
// one v1.Pod description that includes just a single container.
36-
func GenerateForKube(ctx context.Context, ctrs []*Container) (*v1.Pod, error) {
36+
func GenerateForKube(ctx context.Context, ctrs []*Container, getService bool) (*v1.Pod, error) {
3737
// Generate the v1.Pod yaml description
38-
return simplePodWithV1Containers(ctx, ctrs)
38+
return simplePodWithV1Containers(ctx, ctrs, getService)
3939
}
4040

4141
// GenerateForKube takes a slice of libpod containers and generates
4242
// one v1.Pod description
43-
func (p *Pod) GenerateForKube(ctx context.Context) (*v1.Pod, []v1.ServicePort, error) {
43+
func (p *Pod) GenerateForKube(ctx context.Context, getService bool) (*v1.Pod, []v1.ServicePort, error) {
4444
// Generate the v1.Pod yaml description
4545
var (
4646
ports []v1.ContainerPort
@@ -78,7 +78,7 @@ func (p *Pod) GenerateForKube(ctx context.Context) (*v1.Pod, []v1.ServicePort, e
7878
Hostnames: []string{hostSli[0]},
7979
})
8080
}
81-
ports, err = portMappingToContainerPort(infraContainer.config.PortMappings)
81+
ports, err = portMappingToContainerPort(infraContainer.config.PortMappings, getService)
8282
if err != nil {
8383
return nil, servicePorts, err
8484
}
@@ -90,7 +90,7 @@ func (p *Pod) GenerateForKube(ctx context.Context) (*v1.Pod, []v1.ServicePort, e
9090
hostNetwork = infraContainer.NetworkMode() == string(namespaces.NetworkMode(specgen.Host))
9191
hostUsers = infraContainer.IDMappings().HostUIDMapping && infraContainer.IDMappings().HostGIDMapping
9292
}
93-
pod, err := p.podWithContainers(ctx, allContainers, ports, hostNetwork, hostUsers)
93+
pod, err := p.podWithContainers(ctx, allContainers, ports, hostNetwork, hostUsers, getService)
9494
if err != nil {
9595
return nil, servicePorts, err
9696
}
@@ -350,7 +350,7 @@ func containersToServicePorts(containers []v1.Container) ([]v1.ServicePort, erro
350350
return sps, nil
351351
}
352352

353-
func (p *Pod) podWithContainers(ctx context.Context, containers []*Container, ports []v1.ContainerPort, hostNetwork, hostUsers bool) (*v1.Pod, error) {
353+
func (p *Pod) podWithContainers(ctx context.Context, containers []*Container, ports []v1.ContainerPort, hostNetwork, hostUsers, getService bool) (*v1.Pod, error) {
354354
deDupPodVolumes := make(map[string]*v1.Volume)
355355
first := true
356356
podContainers := make([]v1.Container, 0, len(containers))
@@ -385,7 +385,7 @@ func (p *Pod) podWithContainers(ctx context.Context, containers []*Container, po
385385
}
386386
}
387387

388-
ctr, volumes, _, annotations, err := containerToV1Container(ctx, ctr)
388+
ctr, volumes, _, annotations, err := containerToV1Container(ctx, ctr, getService)
389389
if err != nil {
390390
return nil, err
391391
}
@@ -421,7 +421,7 @@ func (p *Pod) podWithContainers(ctx context.Context, containers []*Container, po
421421
deDupPodVolumes[vol.Name] = &vol
422422
}
423423
} else {
424-
_, _, infraDNS, _, err := containerToV1Container(ctx, ctr)
424+
_, _, infraDNS, _, err := containerToV1Container(ctx, ctr, getService)
425425
if err != nil {
426426
return nil, err
427427
}
@@ -497,7 +497,7 @@ func newPodObject(podName string, annotations map[string]string, initCtrs, conta
497497

498498
// simplePodWithV1Containers is a function used by inspect when kube yaml needs to be generated
499499
// for a single container. we "insert" that container description in a pod.
500-
func simplePodWithV1Containers(ctx context.Context, ctrs []*Container) (*v1.Pod, error) {
500+
func simplePodWithV1Containers(ctx context.Context, ctrs []*Container, getService bool) (*v1.Pod, error) {
501501
kubeCtrs := make([]v1.Container, 0, len(ctrs))
502502
kubeInitCtrs := []v1.Container{}
503503
kubeVolumes := make([]v1.Volume, 0)
@@ -555,7 +555,7 @@ func simplePodWithV1Containers(ctx context.Context, ctrs []*Container) (*v1.Pod,
555555
if !(ctr.IDMappings().HostUIDMapping && ctr.IDMappings().HostGIDMapping) {
556556
hostUsers = false
557557
}
558-
kubeCtr, kubeVols, ctrDNS, annotations, err := containerToV1Container(ctx, ctr)
558+
kubeCtr, kubeVols, ctrDNS, annotations, err := containerToV1Container(ctx, ctr, getService)
559559
if err != nil {
560560
return nil, err
561561
}
@@ -622,7 +622,7 @@ func simplePodWithV1Containers(ctx context.Context, ctrs []*Container) (*v1.Pod,
622622

623623
// containerToV1Container converts information we know about a libpod container
624624
// to a V1.Container specification.
625-
func containerToV1Container(ctx context.Context, c *Container) (v1.Container, []v1.Volume, *v1.PodDNSConfig, map[string]string, error) {
625+
func containerToV1Container(ctx context.Context, c *Container, getService bool) (v1.Container, []v1.Volume, *v1.PodDNSConfig, map[string]string, error) {
626626
kubeContainer := v1.Container{}
627627
kubeVolumes := []v1.Volume{}
628628
annotations := make(map[string]string)
@@ -652,7 +652,7 @@ func containerToV1Container(ctx context.Context, c *Container) (v1.Container, []
652652
if err != nil {
653653
return kubeContainer, kubeVolumes, nil, annotations, err
654654
}
655-
ports, err := portMappingToContainerPort(portmappings)
655+
ports, err := portMappingToContainerPort(portmappings, getService)
656656
if err != nil {
657657
return kubeContainer, kubeVolumes, nil, annotations, err
658658
}
@@ -799,7 +799,7 @@ func containerToV1Container(ctx context.Context, c *Container) (v1.Container, []
799799

800800
// portMappingToContainerPort takes an portmapping and converts
801801
// it to a v1.ContainerPort format for kube output
802-
func portMappingToContainerPort(portMappings []types.PortMapping) ([]v1.ContainerPort, error) {
802+
func portMappingToContainerPort(portMappings []types.PortMapping, getService bool) ([]v1.ContainerPort, error) {
803803
containerPorts := make([]v1.ContainerPort, 0, len(portMappings))
804804
for _, p := range portMappings {
805805
protocols := strings.Split(p.Protocol, ",")
@@ -819,11 +819,13 @@ func portMappingToContainerPort(portMappings []types.PortMapping) ([]v1.Containe
819819
for i := uint16(0); i < p.Range; i++ {
820820
cp := v1.ContainerPort{
821821
// Name will not be supported
822-
HostPort: int32(p.HostPort + i),
823822
HostIP: p.HostIP,
824823
ContainerPort: int32(p.ContainerPort + i),
825824
Protocol: protocol,
826825
}
826+
if !getService {
827+
cp.HostPort = int32(p.HostPort + i)
828+
}
827829
containerPorts = append(containerPorts, cp)
828830
}
829831
}

pkg/domain/infra/abi/generate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (ic *ContainerEngine) GenerateKube(ctx context.Context, nameOrIDs []string,
200200

201201
// Generate the kube pods from containers.
202202
if len(ctrs) >= 1 {
203-
po, err := libpod.GenerateForKube(ctx, ctrs)
203+
po, err := libpod.GenerateForKube(ctx, ctrs, options.Service)
204204
if err != nil {
205205
return nil, err
206206
}
@@ -249,7 +249,7 @@ func getKubePods(ctx context.Context, pods []*libpod.Pod, getService bool) ([][]
249249
svcs := [][]byte{}
250250

251251
for _, p := range pods {
252-
po, sp, err := p.GenerateForKube(ctx)
252+
po, sp, err := p.GenerateForKube(ctx, getService)
253253
if err != nil {
254254
return nil, nil, err
255255
}

test/e2e/generate_kube_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,43 @@ var _ = Describe("Podman kube generate", func() {
150150
Expect(err).ToNot(HaveOccurred())
151151
})
152152

153+
It("podman generate kube on container with and without service", func() {
154+
session := podmanTest.Podman([]string{"create", "--name", "test-ctr", "-p", "3890:3890", ALPINE, "ls"})
155+
session.WaitWithDefaultTimeout()
156+
Expect(session).Should(Exit(0))
157+
158+
kube := podmanTest.Podman([]string{"kube", "generate", "-s", "test-ctr"})
159+
kube.WaitWithDefaultTimeout()
160+
Expect(kube).Should(Exit(0))
161+
162+
// Separate out the Service and Pod yaml
163+
arr := strings.Split(string(kube.Out.Contents()), "---")
164+
Expect(arr).To(HaveLen(2))
165+
166+
svc := new(v1.Service)
167+
err := yaml.Unmarshal([]byte(arr[0]), svc)
168+
Expect(err).ToNot(HaveOccurred())
169+
Expect(svc.Spec.Ports).To(HaveLen(1))
170+
Expect(svc.Spec.Ports[0].TargetPort.IntValue()).To(Equal(3890))
171+
172+
pod := new(v1.Pod)
173+
err = yaml.Unmarshal([]byte(arr[1]), pod)
174+
Expect(err).ToNot(HaveOccurred())
175+
// Since hostPort will not be set in the yaml, when we unmarshal it it will have a value of 0
176+
Expect(pod.Spec.Containers[0].Ports[0].HostPort).To(Equal(int32(0)))
177+
178+
// Now do kube generate without the --service flag
179+
kube = podmanTest.Podman([]string{"kube", "generate", "test-ctr"})
180+
kube.WaitWithDefaultTimeout()
181+
Expect(kube).Should(Exit(0))
182+
183+
// The hostPort in the pod yaml should be set to 3890
184+
pod = new(v1.Pod)
185+
err = yaml.Unmarshal(kube.Out.Contents(), pod)
186+
Expect(err).ToNot(HaveOccurred())
187+
Expect(pod.Spec.Containers[0].Ports[0].HostPort).To(Equal(int32(3890)))
188+
})
189+
153190
It("podman generate kube on pod", func() {
154191
_, rc, _ := podmanTest.CreatePod(map[string][]string{"--name": {"toppod"}})
155192
Expect(rc).To(Equal(0))

0 commit comments

Comments
 (0)