Merge pull request #3836 from chenzhiwei/hostname

Allow customizing pod hostname
This commit is contained in:
OpenShift Merge Robot
2019-08-19 13:33:15 +02:00
committed by GitHub
8 changed files with 86 additions and 8 deletions

View File

@ -300,6 +300,7 @@ type PodCreateValues struct {
LabelFile []string LabelFile []string
Labels []string Labels []string
Name string Name string
Hostname string
PodIDFile string PodIDFile string
Publish []string Publish []string
Share string Share string

View File

@ -52,6 +52,7 @@ func init() {
flags.StringSliceVar(&podCreateCommand.LabelFile, "label-file", []string{}, "Read in a line delimited file of labels") 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.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.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.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.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") flags.StringVar(&podCreateCommand.Share, "share", shared.DefaultKernelNamespaces, "A comma delimited list of kernel namespaces the pod will share")

View File

@ -1497,6 +1497,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. // WithPodLabels sets the labels of a pod.
func WithPodLabels(labels map[string]string) PodCreateOption { func WithPodLabels(labels map[string]string) PodCreateOption {
return func(pod *Pod) error { return func(pod *Pod) error {

View File

@ -36,6 +36,8 @@ type PodConfig struct {
// Namespace the pod is in // Namespace the pod is in
Namespace string `json:"namespace,omitempty"` Namespace string `json:"namespace,omitempty"`
Hostname string `json:"hostname,omitempty"`
// Labels contains labels applied to the pod // Labels contains labels applied to the pod
Labels map[string]string `json:"labels"` Labels map[string]string `json:"labels"`
// CgroupParent contains the pod's CGroup parent // CgroupParent contains the pod's CGroup parent

View File

@ -31,8 +31,8 @@ func (r *Runtime) makeInfraContainer(ctx context.Context, p *Pod, imgName, imgID
return nil, err return nil, err
} }
// Set Pod hostname as Pod name // Set Pod hostname
g.Config.Hostname = p.config.Name g.Config.Hostname = p.config.Hostname
isRootless := rootless.IsRootless() isRootless := rootless.IsRootless()

View File

@ -52,6 +52,10 @@ func (r *Runtime) NewPod(ctx context.Context, options ...PodCreateOption) (_ *Po
pod.config.Name = name pod.config.Name = name
} }
if pod.config.Hostname == "" {
pod.config.Hostname = pod.config.Name
}
// Allocate a lock for the pod // Allocate a lock for the pod
lock, err := r.lockManager.AllocateLock() lock, err := r.lockManager.AllocateLock()
if err != nil { if err != nil {

View File

@ -256,6 +256,10 @@ func (r *LocalRuntime) CreatePod(ctx context.Context, cli *cliconfig.PodCreateVa
options = append(options, libpod.WithPodName(cli.Name)) options = append(options, libpod.WithPodName(cli.Name))
} }
if cli.Flag("hostname").Changed {
options = append(options, libpod.WithPodHostname(cli.Hostname))
}
if cli.Infra { if cli.Infra {
options = append(options, libpod.WithInfraContainer()) options = append(options, libpod.WithInfraContainer())
nsOptions, err := shared.GetNamespaceOptions(strings.Split(cli.Share, ",")) nsOptions, err := shared.GetNamespaceOptions(strings.Split(cli.Share, ","))
@ -476,6 +480,12 @@ func (r *LocalRuntime) PlayKubeYAML(ctx context.Context, c *cliconfig.KubePlayVa
podOptions = append(podOptions, libpod.WithPodName(podName)) podOptions = append(podOptions, libpod.WithPodName(podName))
// TODO for now we just used the default kernel namespaces; we need to add/subtract this from yaml // 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, ",")) nsOptions, err := shared.GetNamespaceOptions(strings.Split(shared.DefaultKernelNamespaces, ","))
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -21,6 +21,7 @@ metadata:
app: {{ .Name }} app: {{ .Name }}
name: {{ .Name }} name: {{ .Name }}
spec: spec:
hostname: {{ .Hostname }}
containers: containers:
{{ with .Containers }} {{ with .Containers }}
{{ range . }} {{ range . }}
@ -66,6 +67,7 @@ status: {}
type Pod struct { type Pod struct {
Name string Name string
Hostname string
Containers []Container Containers []Container
} }
@ -78,13 +80,13 @@ type Container struct {
CapDrop []string 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) f, err := os.Create(fileName)
if err != nil { if err != nil {
return err return err
} }
defer f.Close() defer f.Close()
testPod := Pod{"test", ctrs} testPod := Pod{name, hostname, ctrs}
t, err := template.New("pod").Parse(yamlTemplate) t, err := template.New("pod").Parse(yamlTemplate)
if err != nil { if err != nil {
@ -127,7 +129,7 @@ var _ = Describe("Podman generate kube", func() {
testContainer := Container{ctrCmd, ALPINE, ctrName, false, nil, nil} testContainer := Container{ctrCmd, ALPINE, ctrName, false, nil, nil}
tempFile := filepath.Join(podmanTest.TempDir, "kube.yaml") tempFile := filepath.Join(podmanTest.TempDir, "kube.yaml")
err := generateKubeYaml([]Container{testContainer}, tempFile) err := generateKubeYaml("test", "", []Container{testContainer}, tempFile)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
kube := podmanTest.Podman([]string{"play", "kube", tempFile}) 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} testContainer := Container{ctrCmd, ALPINE, ctrName, false, nil, nil}
tempFile := filepath.Join(podmanTest.TempDir, "kube.yaml") tempFile := filepath.Join(podmanTest.TempDir, "kube.yaml")
err := generateKubeYaml([]Container{testContainer}, tempFile) err := generateKubeYaml("test", "", []Container{testContainer}, tempFile)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
kube := podmanTest.Podman([]string{"play", "kube", tempFile}) kube := podmanTest.Podman([]string{"play", "kube", tempFile})
@ -164,6 +166,46 @@ var _ = Describe("Podman generate kube", func() {
Expect(inspect.OutputToString()).To(ContainSubstring("hello")) 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() { It("podman play kube cap add", func() {
ctrName := "testCtr" ctrName := "testCtr"
ctrCmd := []string{"cat", "/proc/self/status"} 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} testContainer := Container{ctrCmd, ALPINE, ctrName, true, []string{capAdd}, nil}
tempFile := filepath.Join(podmanTest.TempDir, "kube.yaml") tempFile := filepath.Join(podmanTest.TempDir, "kube.yaml")
err := generateKubeYaml([]Container{testContainer}, tempFile) err := generateKubeYaml("test", "", []Container{testContainer}, tempFile)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
kube := podmanTest.Podman([]string{"play", "kube", tempFile}) 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} testContainer := Container{ctrCmd, ALPINE, ctrName, true, []string{capDrop}, nil}
tempFile := filepath.Join(podmanTest.TempDir, "kube.yaml") tempFile := filepath.Join(podmanTest.TempDir, "kube.yaml")
err := generateKubeYaml([]Container{testContainer}, tempFile) err := generateKubeYaml("test", "", []Container{testContainer}, tempFile)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
kube := podmanTest.Podman([]string{"play", "kube", tempFile}) kube := podmanTest.Podman([]string{"play", "kube", tempFile})