summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChen Zhiwei <zhiweik@gmail.com>2019-08-17 02:42:36 +0000
committerChen Zhiwei <zhiweik@gmail.com>2019-08-18 03:48:40 +0000
commit7aefc1ac33ba153f6177e9fe3c4da886de49a9e2 (patch)
treefdd2f01237c17263ea06655321696ac3f9f754e0
parent8eab96ec3afdc04c02b5ea1c2067e4a36c14c783 (diff)
downloadpodman-7aefc1ac33ba153f6177e9fe3c4da886de49a9e2.tar.gz
podman-7aefc1ac33ba153f6177e9fe3c4da886de49a9e2.tar.bz2
podman-7aefc1ac33ba153f6177e9fe3c4da886de49a9e2.zip
Allow customizing pod hostname
* set hostname in pod yaml file * set --hostname in pod create command Signed-off-by: Chen Zhiwei <zhiweik@gmail.com>
-rw-r--r--cmd/podman/cliconfig/config.go1
-rw-r--r--cmd/podman/pod_create.go1
-rw-r--r--libpod/options.go18
-rw-r--r--libpod/pod.go2
-rw-r--r--libpod/runtime_pod_infra_linux.go4
-rw-r--r--libpod/runtime_pod_linux.go4
-rw-r--r--pkg/adapter/pods.go10
-rw-r--r--test/e2e/play_kube_test.go54
8 files changed, 86 insertions, 8 deletions
diff --git a/cmd/podman/cliconfig/config.go b/cmd/podman/cliconfig/config.go
index f7c78908f..e7ad921da 100644
--- a/cmd/podman/cliconfig/config.go
+++ b/cmd/podman/cliconfig/config.go
@@ -300,6 +300,7 @@ type PodCreateValues struct {
LabelFile []string
Labels []string
Name string
+ Hostname string
PodIDFile string
Publish []string
Share string
diff --git a/cmd/podman/pod_create.go b/cmd/podman/pod_create.go
index d04c85dba..ad3c00aa8 100644
--- a/cmd/podman/pod_create.go
+++ b/cmd/podman/pod_create.go
@@ -52,6 +52,7 @@ func init() {
flags.StringSliceVar(&podCreateCommand.LabelFile, "label-file", []string{}, "Read in a line delimited file of labels")
flags.StringSliceVarP(&podCreateCommand.Labels, "label", "l", []string{}, "Set metadata on pod (default [])")
flags.StringVarP(&podCreateCommand.Name, "name", "n", "", "Assign a name to the pod")
+ flags.StringVarP(&podCreateCommand.Hostname, "hostname", "", "", "Set a hostname to the pod")
flags.StringVar(&podCreateCommand.PodIDFile, "pod-id-file", "", "Write the pod ID to the file")
flags.StringSliceVarP(&podCreateCommand.Publish, "publish", "p", []string{}, "Publish a container's port, or a range of ports, to the host (default [])")
flags.StringVar(&podCreateCommand.Share, "share", shared.DefaultKernelNamespaces, "A comma delimited list of kernel namespaces the pod will share")
diff --git a/libpod/options.go b/libpod/options.go
index 7fbd0016a..bce07f301 100644
--- a/libpod/options.go
+++ b/libpod/options.go
@@ -1488,6 +1488,24 @@ func WithPodName(name string) PodCreateOption {
}
}
+// WithPodHostname sets the hostname of the pod.
+func WithPodHostname(hostname string) PodCreateOption {
+ return func(pod *Pod) error {
+ if pod.valid {
+ return define.ErrPodFinalized
+ }
+
+ // Check the hostname against a regex
+ if !nameRegex.MatchString(hostname) {
+ return regexError
+ }
+
+ pod.config.Hostname = hostname
+
+ return nil
+ }
+}
+
// WithPodLabels sets the labels of a pod.
func WithPodLabels(labels map[string]string) PodCreateOption {
return func(pod *Pod) error {
diff --git a/libpod/pod.go b/libpod/pod.go
index 60626bfd7..3b9bb9c60 100644
--- a/libpod/pod.go
+++ b/libpod/pod.go
@@ -36,6 +36,8 @@ type PodConfig struct {
// Namespace the pod is in
Namespace string `json:"namespace,omitempty"`
+ Hostname string `json:"hostname,omitempty"`
+
// Labels contains labels applied to the pod
Labels map[string]string `json:"labels"`
// CgroupParent contains the pod's CGroup parent
diff --git a/libpod/runtime_pod_infra_linux.go b/libpod/runtime_pod_infra_linux.go
index 24651099f..7cfce8919 100644
--- a/libpod/runtime_pod_infra_linux.go
+++ b/libpod/runtime_pod_infra_linux.go
@@ -30,8 +30,8 @@ func (r *Runtime) makeInfraContainer(ctx context.Context, p *Pod, imgName, imgID
return nil, err
}
- // Set Pod hostname as Pod name
- g.Config.Hostname = p.config.Name
+ // Set Pod hostname
+ g.Config.Hostname = p.config.Hostname
isRootless := rootless.IsRootless()
diff --git a/libpod/runtime_pod_linux.go b/libpod/runtime_pod_linux.go
index f38e6e7c1..073c5054d 100644
--- a/libpod/runtime_pod_linux.go
+++ b/libpod/runtime_pod_linux.go
@@ -52,6 +52,10 @@ func (r *Runtime) NewPod(ctx context.Context, options ...PodCreateOption) (_ *Po
pod.config.Name = name
}
+ if pod.config.Hostname == "" {
+ pod.config.Hostname = pod.config.Name
+ }
+
// Allocate a lock for the pod
lock, err := r.lockManager.AllocateLock()
if err != nil {
diff --git a/pkg/adapter/pods.go b/pkg/adapter/pods.go
index 2743dfdc6..87092b458 100644
--- a/pkg/adapter/pods.go
+++ b/pkg/adapter/pods.go
@@ -255,6 +255,10 @@ func (r *LocalRuntime) CreatePod(ctx context.Context, cli *cliconfig.PodCreateVa
options = append(options, libpod.WithPodName(cli.Name))
}
+ if cli.Flag("hostname").Changed {
+ options = append(options, libpod.WithPodHostname(cli.Hostname))
+ }
+
if cli.Infra {
options = append(options, libpod.WithInfraContainer())
nsOptions, err := shared.GetNamespaceOptions(strings.Split(cli.Share, ","))
@@ -475,6 +479,12 @@ func (r *LocalRuntime) PlayKubeYAML(ctx context.Context, c *cliconfig.KubePlayVa
podOptions = append(podOptions, libpod.WithPodName(podName))
// TODO for now we just used the default kernel namespaces; we need to add/subtract this from yaml
+ hostname := podYAML.Spec.Hostname
+ if hostname == "" {
+ hostname = podName
+ }
+ podOptions = append(podOptions, libpod.WithPodHostname(hostname))
+
nsOptions, err := shared.GetNamespaceOptions(strings.Split(shared.DefaultKernelNamespaces, ","))
if err != nil {
return nil, err
diff --git a/test/e2e/play_kube_test.go b/test/e2e/play_kube_test.go
index b0a9f2ead..af3cab379 100644
--- a/test/e2e/play_kube_test.go
+++ b/test/e2e/play_kube_test.go
@@ -21,6 +21,7 @@ metadata:
app: {{ .Name }}
name: {{ .Name }}
spec:
+ hostname: {{ .Hostname }}
containers:
{{ with .Containers }}
{{ range . }}
@@ -66,6 +67,7 @@ status: {}
type Pod struct {
Name string
+ Hostname string
Containers []Container
}
@@ -78,13 +80,13 @@ type Container struct {
CapDrop []string
}
-func generateKubeYaml(ctrs []Container, fileName string) error {
+func generateKubeYaml(name string, hostname string, ctrs []Container, fileName string) error {
f, err := os.Create(fileName)
if err != nil {
return err
}
defer f.Close()
- testPod := Pod{"test", ctrs}
+ testPod := Pod{name, hostname, ctrs}
t, err := template.New("pod").Parse(yamlTemplate)
if err != nil {
@@ -127,7 +129,7 @@ var _ = Describe("Podman generate kube", func() {
testContainer := Container{ctrCmd, ALPINE, ctrName, false, nil, nil}
tempFile := filepath.Join(podmanTest.TempDir, "kube.yaml")
- err := generateKubeYaml([]Container{testContainer}, tempFile)
+ err := generateKubeYaml("test", "", []Container{testContainer}, tempFile)
Expect(err).To(BeNil())
kube := podmanTest.Podman([]string{"play", "kube", tempFile})
@@ -146,7 +148,7 @@ var _ = Describe("Podman generate kube", func() {
testContainer := Container{ctrCmd, ALPINE, ctrName, false, nil, nil}
tempFile := filepath.Join(podmanTest.TempDir, "kube.yaml")
- err := generateKubeYaml([]Container{testContainer}, tempFile)
+ err := generateKubeYaml("test", "", []Container{testContainer}, tempFile)
Expect(err).To(BeNil())
kube := podmanTest.Podman([]string{"play", "kube", tempFile})
@@ -164,6 +166,46 @@ var _ = Describe("Podman generate kube", func() {
Expect(inspect.OutputToString()).To(ContainSubstring("hello"))
})
+ It("podman play kube test hostname", func() {
+ podName := "test"
+ ctrName := "testCtr"
+ ctrCmd := []string{"top"}
+ testContainer := Container{ctrCmd, ALPINE, ctrName, false, nil, nil}
+ tempFile := filepath.Join(podmanTest.TempDir, "kube.yaml")
+
+ err := generateKubeYaml(podName, "", []Container{testContainer}, tempFile)
+ Expect(err).To(BeNil())
+
+ kube := podmanTest.Podman([]string{"play", "kube", tempFile})
+ kube.WaitWithDefaultTimeout()
+ Expect(kube.ExitCode()).To(Equal(0))
+
+ inspect := podmanTest.Podman([]string{"inspect", ctrName, "--format", "{{ .Config.Hostname }}"})
+ inspect.WaitWithDefaultTimeout()
+ Expect(inspect.ExitCode()).To(Equal(0))
+ Expect(inspect.OutputToString()).To(Equal(podName))
+ })
+
+ It("podman play kube test with customized hostname", func() {
+ hostname := "myhostname"
+ ctrName := "testCtr"
+ ctrCmd := []string{"top"}
+ testContainer := Container{ctrCmd, ALPINE, ctrName, false, nil, nil}
+ tempFile := filepath.Join(podmanTest.TempDir, "kube.yaml")
+
+ err := generateKubeYaml("test", hostname, []Container{testContainer}, tempFile)
+ Expect(err).To(BeNil())
+
+ kube := podmanTest.Podman([]string{"play", "kube", tempFile})
+ kube.WaitWithDefaultTimeout()
+ Expect(kube.ExitCode()).To(Equal(0))
+
+ inspect := podmanTest.Podman([]string{"inspect", ctrName, "--format", "{{ .Config.Hostname }}"})
+ inspect.WaitWithDefaultTimeout()
+ Expect(inspect.ExitCode()).To(Equal(0))
+ Expect(inspect.OutputToString()).To(Equal(hostname))
+ })
+
It("podman play kube cap add", func() {
ctrName := "testCtr"
ctrCmd := []string{"cat", "/proc/self/status"}
@@ -171,7 +213,7 @@ var _ = Describe("Podman generate kube", func() {
testContainer := Container{ctrCmd, ALPINE, ctrName, true, []string{capAdd}, nil}
tempFile := filepath.Join(podmanTest.TempDir, "kube.yaml")
- err := generateKubeYaml([]Container{testContainer}, tempFile)
+ err := generateKubeYaml("test", "", []Container{testContainer}, tempFile)
Expect(err).To(BeNil())
kube := podmanTest.Podman([]string{"play", "kube", tempFile})
@@ -191,7 +233,7 @@ var _ = Describe("Podman generate kube", func() {
testContainer := Container{ctrCmd, ALPINE, ctrName, true, []string{capDrop}, nil}
tempFile := filepath.Join(podmanTest.TempDir, "kube.yaml")
- err := generateKubeYaml([]Container{testContainer}, tempFile)
+ err := generateKubeYaml("test", "", []Container{testContainer}, tempFile)
Expect(err).To(BeNil())
kube := podmanTest.Podman([]string{"play", "kube", tempFile})