mirror of
https://github.com/containers/podman.git
synced 2025-08-06 19:44:14 +08:00
Merge pull request #3836 from chenzhiwei/hostname
Allow customizing pod hostname
This commit is contained in:
@ -300,6 +300,7 @@ type PodCreateValues struct {
|
||||
LabelFile []string
|
||||
Labels []string
|
||||
Name string
|
||||
Hostname string
|
||||
PodIDFile string
|
||||
Publish []string
|
||||
Share string
|
||||
|
@ -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")
|
||||
|
@ -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.
|
||||
func WithPodLabels(labels map[string]string) PodCreateOption {
|
||||
return func(pod *Pod) error {
|
||||
|
@ -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
|
||||
|
@ -31,8 +31,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()
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -256,6 +256,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, ","))
|
||||
@ -476,6 +480,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
|
||||
|
@ -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})
|
||||
|
Reference in New Issue
Block a user