aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2020-08-28 16:06:11 -0400
committerGitHub <noreply@github.com>2020-08-28 16:06:11 -0400
commit4e3ea01243665259dededa7e3fa924cb5bd2ee3c (patch)
tree5b1b6a575f63465a001f15d2fa7e753ae9ac422b
parent97780a110b58a3bcc983bff75f3665768271631c (diff)
parenta2bb7bd36b22f45dbccabbab9dcca83abfda1682 (diff)
downloadpodman-4e3ea01243665259dededa7e3fa924cb5bd2ee3c.tar.gz
podman-4e3ea01243665259dededa7e3fa924cb5bd2ee3c.tar.bz2
podman-4e3ea01243665259dededa7e3fa924cb5bd2ee3c.zip
Merge pull request #7469 from zhangguanzhang/generate-kube-with-ExtraHosts
fix podman generate kube with HostAliases
-rw-r--r--libpod/kube.go18
-rw-r--r--test/e2e/generate_kube_test.go32
2 files changed, 48 insertions, 2 deletions
diff --git a/libpod/kube.go b/libpod/kube.go
index 0068427a5..5f2c9e0fd 100644
--- a/libpod/kube.go
+++ b/libpod/kube.go
@@ -48,12 +48,22 @@ func (p *Pod) GenerateForKube() (*v1.Pod, []v1.ServicePort, error) {
return nil, servicePorts, errors.Errorf("pod %s only has an infra container", p.ID())
}
+ extraHost := make([]v1.HostAlias, 0)
if p.HasInfraContainer() {
infraContainer, err := p.getInfraContainer()
if err != nil {
return nil, servicePorts, err
}
-
+ for _, host := range infraContainer.config.ContainerNetworkConfig.HostAdd {
+ hostSli := strings.SplitN(host, ":", 2)
+ if len(hostSli) != 2 {
+ return nil, servicePorts, errors.New("invalid hostAdd")
+ }
+ extraHost = append(extraHost, v1.HostAlias{
+ IP: hostSli[1],
+ Hostnames: []string{hostSli[0]},
+ })
+ }
ports, err = ocicniPortMappingToContainerPort(infraContainer.config.PortMappings)
if err != nil {
return nil, servicePorts, err
@@ -61,7 +71,11 @@ func (p *Pod) GenerateForKube() (*v1.Pod, []v1.ServicePort, error) {
servicePorts = containerPortsToServicePorts(ports)
}
pod, err := p.podWithContainers(allContainers, ports)
- return pod, servicePorts, err
+ if err != nil {
+ return nil, servicePorts, err
+ }
+ pod.Spec.HostAliases = extraHost
+ return pod, servicePorts, nil
}
func (p *Pod) getInfraContainer() (*Container, error) {
diff --git a/test/e2e/generate_kube_test.go b/test/e2e/generate_kube_test.go
index 1ec8d51c1..3c3fb5a4d 100644
--- a/test/e2e/generate_kube_test.go
+++ b/test/e2e/generate_kube_test.go
@@ -151,6 +151,38 @@ var _ = Describe("Podman generate kube", func() {
Expect(numContainers).To(Equal(1))
})
+ It("podman generate kube on pod with hostAliases", func() {
+ podName := "testHost"
+ testIP := "127.0.0.1"
+ podSession := podmanTest.Podman([]string{"pod", "create", "--name", podName,
+ "--add-host", "test1.podman.io" + ":" + testIP,
+ "--add-host", "test2.podman.io" + ":" + testIP,
+ })
+ podSession.WaitWithDefaultTimeout()
+ Expect(podSession.ExitCode()).To(Equal(0))
+
+ ctr1Name := "ctr1"
+ ctr1Session := podmanTest.Podman([]string{"create", "--name", ctr1Name, "--pod", podName, ALPINE, "top"})
+ ctr1Session.WaitWithDefaultTimeout()
+ Expect(ctr1Session.ExitCode()).To(Equal(0))
+
+ ctr2Name := "ctr2"
+ ctr2Session := podmanTest.Podman([]string{"create", "--name", ctr2Name, "--pod", podName, ALPINE, "top"})
+ ctr2Session.WaitWithDefaultTimeout()
+ Expect(ctr2Session.ExitCode()).To(Equal(0))
+
+ kube := podmanTest.Podman([]string{"generate", "kube", podName})
+ kube.WaitWithDefaultTimeout()
+ Expect(kube.ExitCode()).To(Equal(0))
+
+ pod := new(v1.Pod)
+ err := yaml.Unmarshal(kube.Out.Contents(), pod)
+ Expect(err).To(BeNil())
+ Expect(len(pod.Spec.HostAliases)).To(Equal(2))
+ Expect(pod.Spec.HostAliases[0].IP).To(Equal(testIP))
+ Expect(pod.Spec.HostAliases[1].IP).To(Equal(testIP))
+ })
+
It("podman generate service kube on pod", func() {
_, rc, _ := podmanTest.CreatePod("toppod")
Expect(rc).To(Equal(0))