@@ -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 }
0 commit comments