summaryrefslogtreecommitdiff
path: root/libpod/kube.go
diff options
context:
space:
mode:
authorUrvashi Mohnani <umohnani@redhat.com>2022-05-10 13:23:46 -0400
committerUrvashi Mohnani <umohnani@redhat.com>2022-07-08 11:21:48 -0400
commit81a19a568f6234be47882b1c2b066a637749fd39 (patch)
treeb0877e3ff18651d297838a21bcd1cd95ce23251d /libpod/kube.go
parent49df3cc5cb7e6a1d9e28cacfa86562abbdf48fd9 (diff)
downloadpodman-81a19a568f6234be47882b1c2b066a637749fd39.tar.gz
podman-81a19a568f6234be47882b1c2b066a637749fd39.tar.bz2
podman-81a19a568f6234be47882b1c2b066a637749fd39.zip
Add ports and hostname correctly in kube yaml
If a pod is created without net sharing, allow adding separate ports for each container to the kube yaml and also set the pod level hostname correctly if the uts namespace is not being shared. Add a warning if the default namespace sharing options have been modified by the user. Signed-off-by: Urvashi Mohnani <umohnani@redhat.com>
Diffstat (limited to 'libpod/kube.go')
-rw-r--r--libpod/kube.go53
1 files changed, 39 insertions, 14 deletions
diff --git a/libpod/kube.go b/libpod/kube.go
index 3cb0489b3..8c09a6bb5 100644
--- a/libpod/kube.go
+++ b/libpod/kube.go
@@ -353,6 +353,7 @@ func (p *Pod) podWithContainers(ctx context.Context, containers []*Container, po
podInitCtrs := []v1.Container{}
podAnnotations := make(map[string]string)
dnsInfo := v1.PodDNSConfig{}
+ var hostname string
// Let's sort the containers in order of created time
// This will ensure that the init containers are defined in the correct order in the kube yaml
@@ -368,6 +369,14 @@ func (p *Pod) podWithContainers(ctx context.Context, containers []*Container, po
podAnnotations[k] = TruncateKubeAnnotation(v)
}
isInit := ctr.IsInitCtr()
+ // Since hostname is only set at pod level, set the hostname to the hostname of the first container we encounter
+ if hostname == "" {
+ // Only set the hostname if it is not set to the truncated container ID, which we do by default if no
+ // hostname is specified for the container
+ if !strings.Contains(ctr.ID(), ctr.Hostname()) {
+ hostname = ctr.Hostname()
+ }
+ }
ctr, volumes, _, annotations, err := containerToV1Container(ctx, ctr)
if err != nil {
@@ -377,17 +386,21 @@ func (p *Pod) podWithContainers(ctx context.Context, containers []*Container, po
podAnnotations[define.BindMountPrefix+k] = TruncateKubeAnnotation(v)
}
// Since port bindings for the pod are handled by the
- // infra container, wipe them here.
- ctr.Ports = nil
-
- // We add the original port declarations from the libpod infra container
- // to the first kubernetes container description because otherwise we loose
- // the original container/port bindings.
- // Add the port configuration to the first regular container or the first
- // init container if only init containers have been created in the pod.
- if first && len(ports) > 0 && (!isInit || len(containers) == 2) {
- ctr.Ports = ports
- first = false
+ // infra container, wipe them here only if we are sharing the net namespace
+ // If the network namespace is not being shared in the pod, then containers
+ // can have their own network configurations
+ if p.SharesNet() {
+ ctr.Ports = nil
+
+ // We add the original port declarations from the libpod infra container
+ // to the first kubernetes container description because otherwise we loose
+ // the original container/port bindings.
+ // Add the port configuration to the first regular container or the first
+ // init container if only init containers have been created in the pod.
+ if first && len(ports) > 0 && (!isInit || len(containers) == 2) {
+ ctr.Ports = ports
+ first = false
+ }
}
if isInit {
podInitCtrs = append(podInitCtrs, ctr)
@@ -430,10 +443,11 @@ func (p *Pod) podWithContainers(ctx context.Context, containers []*Container, po
podContainers,
podVolumes,
&dnsInfo,
- hostNetwork), nil
+ hostNetwork,
+ hostname), nil
}
-func newPodObject(podName string, annotations map[string]string, initCtrs, containers []v1.Container, volumes []v1.Volume, dnsOptions *v1.PodDNSConfig, hostNetwork bool) *v1.Pod {
+func newPodObject(podName string, annotations map[string]string, initCtrs, containers []v1.Container, volumes []v1.Volume, dnsOptions *v1.PodDNSConfig, hostNetwork bool, hostname string) *v1.Pod {
tm := v12.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
@@ -454,6 +468,7 @@ func newPodObject(podName string, annotations map[string]string, initCtrs, conta
}
ps := v1.PodSpec{
Containers: containers,
+ Hostname: hostname,
HostNetwork: hostNetwork,
InitContainers: initCtrs,
Volumes: volumes,
@@ -479,6 +494,7 @@ func simplePodWithV1Containers(ctx context.Context, ctrs []*Container) (*v1.Pod,
podDNS := v1.PodDNSConfig{}
kubeAnnotations := make(map[string]string)
ctrNames := make([]string, 0, len(ctrs))
+ var hostname string
for _, ctr := range ctrs {
ctrNames = append(ctrNames, removeUnderscores(ctr.Name()))
for k, v := range ctr.config.Spec.Annotations {
@@ -491,6 +507,14 @@ func simplePodWithV1Containers(ctx context.Context, ctrs []*Container) (*v1.Pod,
}
isInit := ctr.IsInitCtr()
+ // Since hostname is only set at pod level, set the hostname to the hostname of the first container we encounter
+ if hostname == "" {
+ // Only set the hostname if it is not set to the truncated container ID, which we do by default if no
+ // hostname is specified for the container
+ if !strings.Contains(ctr.ID(), ctr.Hostname()) {
+ hostname = ctr.Hostname()
+ }
+ }
if !ctr.HostNetwork() {
hostNetwork = false
@@ -555,7 +579,8 @@ func simplePodWithV1Containers(ctx context.Context, ctrs []*Container) (*v1.Pod,
kubeCtrs,
kubeVolumes,
&podDNS,
- hostNetwork), nil
+ hostNetwork,
+ hostname), nil
}
// containerToV1Container converts information we know about a libpod container