Skip to content

Commit 9146ef6

Browse files
Merge pull request #27302 from arsenalzp/podman_26396
Add option to remove Pod name prefix in resulting container name
2 parents 2487242 + bb4fa06 commit 9146ef6

File tree

10 files changed

+58
-1
lines changed

10 files changed

+58
-1
lines changed

cmd/podman/kube/play.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ func playFlags(cmd *cobra.Command) {
176176
flags.BoolVar(&playOptions.UseLongAnnotations, noTruncFlagName, false, "Use annotations that are not truncated to the Kubernetes maximum length of 63 characters")
177177
_ = flags.MarkHidden(noTruncFlagName)
178178

179+
noPodPrefix := "no-pod-prefix"
180+
flags.BoolVar(&playOptions.NoPodPrefix, noPodPrefix, false, "Do not prefix container name with pod name")
181+
179182
if !registry.IsRemote() {
180183
certDirFlagName := "cert-dir"
181184
flags.StringVar(&playOptions.CertDir, certDirFlagName, "", "`Pathname` of a directory containing TLS certificates and keys")

docs/source/markdown/podman-kube-play.1.md.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ When no network option is specified and *host* network mode is not configured in
260260

261261
This option conflicts with host added in the Kubernetes YAML.
262262

263+
#### **--no-pod-prefix**
264+
265+
Do not prefix container name with pod name.
266+
263267
#### **--publish**=*[[ip:][hostPort]:]containerPort[/protocol]*
264268

265269
Define or override a port definition in the YAML file.

pkg/api/handlers/libpod/kube.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ func KubePlay(w http.ResponseWriter, r *http.Request) {
123123
Userns string `schema:"userns"`
124124
Wait bool `schema:"wait"`
125125
Build bool `schema:"build"`
126+
NoPodPrefix bool `schema:"noPodPrefix"`
126127
}{
127128
TLSVerify: true,
128129
Start: true,
@@ -198,6 +199,7 @@ func KubePlay(w http.ResponseWriter, r *http.Request) {
198199
Userns: query.Userns,
199200
Wait: query.Wait,
200201
ContextDir: contextDirectory,
202+
NoPodPrefix: query.NoPodPrefix,
201203
}
202204
if _, found := r.URL.Query()["build"]; found {
203205
options.Build = types.NewOptionalBool(query.Build)

pkg/bindings/kube/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type PlayOptions struct {
6363
// Wait - indicates whether to return after having created the pods
6464
Wait *bool
6565
ServiceContainer *bool
66+
NoPodPrefix *bool
6667
}
6768

6869
// ApplyOptions are optional options for applying kube YAML files to a k8s cluster

pkg/bindings/kube/types_play_options.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/domain/entities/play.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ type PlayKubeOptions struct {
8181
Wait bool
8282
// SystemContext - used when building the image
8383
SystemContext *types.SystemContext
84+
// Do not prefix container name with pod name
85+
NoPodPrefix bool
8486
}
8587

8688
// PlayKubePod represents a single pod and associated containers created by play kube

pkg/domain/infra/abi/play.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,7 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
10891089
VolumesFrom: volumesFrom,
10901090
ImageVolumes: automountImages,
10911091
UtsNSIsHost: p.UtsNs.IsHost(),
1092+
NoPodPrefix: options.NoPodPrefix,
10921093
}
10931094

10941095
if podYAML.Spec.TerminationGracePeriodSeconds != nil {

pkg/domain/infra/tunnel/kube.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func (ic *ContainerEngine) PlayKube(_ context.Context, body io.Reader, opts enti
7575
options.WithPublishPorts(opts.PublishPorts)
7676
options.WithPublishAllPorts(opts.PublishAllPorts)
7777
options.WithNoTrunc(opts.UseLongAnnotations)
78+
options.WithNoPodPrefix(opts.NoPodPrefix)
7879
return play.KubeWithBody(ic.ClientCtx, body, options)
7980
}
8081

pkg/specgen/generate/kube/kube.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ type CtrSpecGenOptions struct {
185185
PodSecurityContext *v1.PodSecurityContext
186186
// TerminationGracePeriodSeconds is the grace period given to a container to stop before being forcefully killed
187187
TerminationGracePeriodSeconds *int64
188+
// Don't use pod name as prefix in resulting container name.
189+
NoPodPrefix bool
188190
}
189191

190192
func ToSpecGen(ctx context.Context, opts *CtrSpecGenOptions) (*specgen.SpecGenerator, error) {
@@ -217,7 +219,11 @@ func ToSpecGen(ctx context.Context, opts *CtrSpecGenOptions) (*specgen.SpecGener
217219
return nil, errors.New("got empty pod name on container creation when playing kube")
218220
}
219221

220-
s.Name = fmt.Sprintf("%s-%s", opts.PodName, opts.Container.Name)
222+
if opts.NoPodPrefix {
223+
s.Name = opts.Container.Name
224+
} else {
225+
s.Name = fmt.Sprintf("%s-%s", opts.PodName, opts.Container.Name)
226+
}
221227

222228
s.Terminal = &opts.Container.TTY
223229

test/e2e/play_kube_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,19 @@ spec:
222222
- sleep
223223
- "3600"`
224224

225+
var simpleWithoutPodPrefixYaml = `
226+
apiVersion: v1
227+
kind: Pod
228+
metadata:
229+
name: libpod-test
230+
spec:
231+
containers:
232+
- name: simpleWithoutPodPrefix
233+
image: ` + CITEST_IMAGE + `
234+
command:
235+
- sleep
236+
- "3600"`
237+
225238
var unknownKindYaml = `
226239
apiVersion: v1
227240
kind: UnknownKind
@@ -6420,4 +6433,13 @@ spec:
64206433

64216434
Expect(testfile).ToNot(BeAnExistingFile(), "file should never be created on the host")
64226435
})
6436+
6437+
It("test container name without Pod name prefix", func() {
6438+
err := writeYaml(simpleWithoutPodPrefixYaml, kubeYaml)
6439+
Expect(err).ToNot(HaveOccurred())
6440+
6441+
podmanTest.PodmanExitCleanly("kube", "play", "--no-pod-prefix", kubeYaml)
6442+
inspect := podmanTest.PodmanExitCleanly("inspect", "simpleWithoutPodPrefix")
6443+
Expect(inspect.InspectContainerToJSON()[0].Name).Should(Equal("simpleWithoutPodPrefix"))
6444+
})
64236445
})

0 commit comments

Comments
 (0)