diff options
author | Daniel J Walsh <dwalsh@redhat.com> | 2021-12-09 09:52:03 -0500 |
---|---|---|
committer | Daniel J Walsh <dwalsh@redhat.com> | 2021-12-09 13:09:02 -0500 |
commit | 593d0907c5c11e933be7dbdce98022f76183ea3c (patch) | |
tree | be7e40888a198e58bd9ac64ffe6c4189eb119309 | |
parent | c7ed2be8d2745eca7a9bcabf7f40e869baa8c5ec (diff) | |
download | podman-593d0907c5c11e933be7dbdce98022f76183ea3c.tar.gz podman-593d0907c5c11e933be7dbdce98022f76183ea3c.tar.bz2 podman-593d0907c5c11e933be7dbdce98022f76183ea3c.zip |
--hostname should be set when using --pod new:foobar
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=2030599
When you create pod, it shares the UTS namespace with Containers.
Currently the --hostname is not passed to the pod created when
you create a container and pod in the same command.
Also fix error message on supported --share flags
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
-rw-r--r-- | cmd/podman/containers/create.go | 8 | ||||
-rw-r--r-- | cmd/podman/containers/run.go | 2 | ||||
-rw-r--r-- | pkg/domain/entities/types.go | 5 | ||||
-rw-r--r-- | pkg/specgen/generate/namespaces.go | 2 | ||||
-rw-r--r-- | test/system/200-pod.bats | 20 |
5 files changed, 31 insertions, 6 deletions
diff --git a/cmd/podman/containers/create.go b/cmd/podman/containers/create.go index 9b53a730f..a17fcaa1c 100644 --- a/cmd/podman/containers/create.go +++ b/cmd/podman/containers/create.go @@ -141,7 +141,7 @@ func create(cmd *cobra.Command, args []string) error { } s.RawImageName = rawImageName - if _, err := createPodIfNecessary(s, cliVals.Net); err != nil { + if _, err := createPodIfNecessary(cmd, s, cliVals.Net); err != nil { return err } @@ -335,7 +335,7 @@ func PullImage(imageName string, cliVals entities.ContainerCreateOptions) (strin // createPodIfNecessary automatically creates a pod when requested. if the pod name // has the form new:ID, the pod ID is created and the name in the spec generator is replaced // with ID. -func createPodIfNecessary(s *specgen.SpecGenerator, netOpts *entities.NetOptions) (*entities.PodCreateReport, error) { +func createPodIfNecessary(cmd *cobra.Command, s *specgen.SpecGenerator, netOpts *entities.NetOptions) (*entities.PodCreateReport, error) { if !strings.HasPrefix(s.Pod, "new:") { return nil, nil } @@ -379,6 +379,10 @@ func createPodIfNecessary(s *specgen.SpecGenerator, netOpts *entities.NetOptions infraOpts := entities.NewInfraContainerCreateOptions() infraOpts.Net = netOpts infraOpts.Quiet = true + infraOpts.Hostname, err = cmd.Flags().GetString("hostname") + if err != nil { + return nil, err + } imageName := config.DefaultInfraImage podGen.InfraImage = imageName podGen.InfraContainerSpec = specgen.NewSpecGenerator(imageName, false) diff --git a/cmd/podman/containers/run.go b/cmd/podman/containers/run.go index 071708b76..9d1b040cc 100644 --- a/cmd/podman/containers/run.go +++ b/cmd/podman/containers/run.go @@ -193,7 +193,7 @@ func run(cmd *cobra.Command, args []string) error { s.RawImageName = rawImageName runOpts.Spec = s - if _, err := createPodIfNecessary(s, cliVals.Net); err != nil { + if _, err := createPodIfNecessary(cmd, s, cliVals.Net); err != nil { return err } diff --git a/pkg/domain/entities/types.go b/pkg/domain/entities/types.go index ec4d4a902..e062b9442 100644 --- a/pkg/domain/entities/types.go +++ b/pkg/domain/entities/types.go @@ -29,8 +29,6 @@ type PodDeleteReport struct{ Report } type VolumeDeleteOptions struct{} type VolumeDeleteReport struct{ Report } -// NetOptions reflect the shared network options between -// pods and containers type NetFlags struct { AddHosts []string `json:"add-host,omitempty"` DNS []string `json:"dns,omitempty"` @@ -43,6 +41,9 @@ type NetFlags struct { Network string `json:"network,omitempty"` NetworkAlias []string `json:"network-alias,omitempty"` } + +// NetOptions reflect the shared network options between +// pods and containers type NetOptions struct { AddHosts []string `json:"hostadd,omitempty"` Aliases []string `json:"network_alias,omitempty"` diff --git a/pkg/specgen/generate/namespaces.go b/pkg/specgen/generate/namespaces.go index 5349e224f..7d63fc10f 100644 --- a/pkg/specgen/generate/namespaces.go +++ b/pkg/specgen/generate/namespaces.go @@ -474,7 +474,7 @@ func GetNamespaceOptions(ns []string, netnsIsHost bool) ([]libpod.PodCreateOptio case "none": return erroredOptions, nil default: - return erroredOptions, errors.Errorf("Invalid kernel namespace to share: %s. Options are: net, pid, ipc, uts or none", toShare) + return erroredOptions, errors.Errorf("Invalid kernel namespace to share: %s. Options are: cgroup, ipc, net, pid, uts or none", toShare) } } return options, nil diff --git a/test/system/200-pod.bats b/test/system/200-pod.bats index b166914db..57d052ce2 100644 --- a/test/system/200-pod.bats +++ b/test/system/200-pod.bats @@ -254,6 +254,8 @@ EOF run_podman run --rm --pod mypod $IMAGE hostname is "$output" "$hostname" "--hostname set the hostname" + run_podman 125 run --rm --pod mypod --hostname foobar $IMAGE hostname + is "$output" ".*invalid config provided: cannot set hostname when joining the pod UTS namespace: invalid configuration" "--hostname should not be allowed in share UTS pod" run_podman run --rm --pod $pod_id $IMAGE cat /etc/hosts is "$output" ".*$add_host_ip $add_host_n" "--add-host was added" @@ -337,4 +339,22 @@ EOF run_podman 1 image exists k8s.gcr.io/pause:3.5 } +@test "podman pod create --share" { + local pod_name="$(random_string 10 | tr A-Z a-z)" + run_podman 125 pod create --share bogus --name $pod_name + is "$output" ".*Invalid kernel namespace to share: bogus. Options are: cgroup, ipc, net, pid, uts or none" \ + "pod test for bogus --share option" + run_podman pod create --share cgroup,ipc --name $pod_name + run_podman run --rm --pod $pod_name --hostname foobar $IMAGE hostname + is "$output" "foobar" "--hostname should work with non share UTS namespace" +} + +@test "podman pod create --pod new:$POD --hostname" { + local pod_name="$(random_string 10 | tr A-Z a-z)" + run_podman run --rm --pod "new:$pod_name" --hostname foobar $IMAGE hostname + is "$output" "foobar" "--hostname should work when creating a new:pod" + run_podman pod rm $pod_name + run_podman run --rm --pod "new:$pod_name" $IMAGE hostname + is "$output" "$pod_name" "new:POD should have hostname name set to podname" +} # vim: filetype=sh |