Merge pull request #27302 from arsenalzp/podman_26396

Add option to remove Pod name prefix in resulting container name
This commit is contained in:
openshift-merge-bot[bot]
2025-10-20 23:47:40 +00:00
committed by GitHub
10 changed files with 58 additions and 1 deletions

View File

@@ -176,6 +176,9 @@ func playFlags(cmd *cobra.Command) {
flags.BoolVar(&playOptions.UseLongAnnotations, noTruncFlagName, false, "Use annotations that are not truncated to the Kubernetes maximum length of 63 characters")
_ = flags.MarkHidden(noTruncFlagName)
noPodPrefix := "no-pod-prefix"
flags.BoolVar(&playOptions.NoPodPrefix, noPodPrefix, false, "Do not prefix container name with pod name")
if !registry.IsRemote() {
certDirFlagName := "cert-dir"
flags.StringVar(&playOptions.CertDir, certDirFlagName, "", "`Pathname` of a directory containing TLS certificates and keys")

View File

@@ -260,6 +260,10 @@ When no network option is specified and *host* network mode is not configured in
This option conflicts with host added in the Kubernetes YAML.
#### **--no-pod-prefix**
Do not prefix container name with pod name.
#### **--publish**=*[[ip:][hostPort]:]containerPort[/protocol]*
Define or override a port definition in the YAML file.

View File

@@ -123,6 +123,7 @@ func KubePlay(w http.ResponseWriter, r *http.Request) {
Userns string `schema:"userns"`
Wait bool `schema:"wait"`
Build bool `schema:"build"`
NoPodPrefix bool `schema:"noPodPrefix"`
}{
TLSVerify: true,
Start: true,
@@ -198,6 +199,7 @@ func KubePlay(w http.ResponseWriter, r *http.Request) {
Userns: query.Userns,
Wait: query.Wait,
ContextDir: contextDirectory,
NoPodPrefix: query.NoPodPrefix,
}
if _, found := r.URL.Query()["build"]; found {
options.Build = types.NewOptionalBool(query.Build)

View File

@@ -63,6 +63,7 @@ type PlayOptions struct {
// Wait - indicates whether to return after having created the pods
Wait *bool
ServiceContainer *bool
NoPodPrefix *bool
}
// ApplyOptions are optional options for applying kube YAML files to a k8s cluster

View File

@@ -407,3 +407,18 @@ func (o *PlayOptions) GetServiceContainer() bool {
}
return *o.ServiceContainer
}
// WithNoPodPrefix set field NoPodPrefix to given value
func (o *PlayOptions) WithNoPodPrefix(value bool) *PlayOptions {
o.NoPodPrefix = &value
return o
}
// GetNoPodPrefix returns value of field NoPodPrefix
func (o *PlayOptions) GetNoPodPrefix() bool {
if o.NoPodPrefix == nil {
var z bool
return z
}
return *o.NoPodPrefix
}

View File

@@ -81,6 +81,8 @@ type PlayKubeOptions struct {
Wait bool
// SystemContext - used when building the image
SystemContext *types.SystemContext
// Do not prefix container name with pod name
NoPodPrefix bool
}
// PlayKubePod represents a single pod and associated containers created by play kube

View File

@@ -1089,6 +1089,7 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
VolumesFrom: volumesFrom,
ImageVolumes: automountImages,
UtsNSIsHost: p.UtsNs.IsHost(),
NoPodPrefix: options.NoPodPrefix,
}
if podYAML.Spec.TerminationGracePeriodSeconds != nil {

View File

@@ -75,6 +75,7 @@ func (ic *ContainerEngine) PlayKube(_ context.Context, body io.Reader, opts enti
options.WithPublishPorts(opts.PublishPorts)
options.WithPublishAllPorts(opts.PublishAllPorts)
options.WithNoTrunc(opts.UseLongAnnotations)
options.WithNoPodPrefix(opts.NoPodPrefix)
return play.KubeWithBody(ic.ClientCtx, body, options)
}

View File

@@ -185,6 +185,8 @@ type CtrSpecGenOptions struct {
PodSecurityContext *v1.PodSecurityContext
// TerminationGracePeriodSeconds is the grace period given to a container to stop before being forcefully killed
TerminationGracePeriodSeconds *int64
// Don't use pod name as prefix in resulting container name.
NoPodPrefix bool
}
func ToSpecGen(ctx context.Context, opts *CtrSpecGenOptions) (*specgen.SpecGenerator, error) {
@@ -217,7 +219,11 @@ func ToSpecGen(ctx context.Context, opts *CtrSpecGenOptions) (*specgen.SpecGener
return nil, errors.New("got empty pod name on container creation when playing kube")
}
s.Name = fmt.Sprintf("%s-%s", opts.PodName, opts.Container.Name)
if opts.NoPodPrefix {
s.Name = opts.Container.Name
} else {
s.Name = fmt.Sprintf("%s-%s", opts.PodName, opts.Container.Name)
}
s.Terminal = &opts.Container.TTY

View File

@@ -222,6 +222,19 @@ spec:
- sleep
- "3600"`
var simpleWithoutPodPrefixYaml = `
apiVersion: v1
kind: Pod
metadata:
name: libpod-test
spec:
containers:
- name: simpleWithoutPodPrefix
image: ` + CITEST_IMAGE + `
command:
- sleep
- "3600"`
var unknownKindYaml = `
apiVersion: v1
kind: UnknownKind
@@ -6420,4 +6433,13 @@ spec:
Expect(testfile).ToNot(BeAnExistingFile(), "file should never be created on the host")
})
It("test container name without Pod name prefix", func() {
err := writeYaml(simpleWithoutPodPrefixYaml, kubeYaml)
Expect(err).ToNot(HaveOccurred())
podmanTest.PodmanExitCleanly("kube", "play", "--no-pod-prefix", kubeYaml)
inspect := podmanTest.PodmanExitCleanly("inspect", "simpleWithoutPodPrefix")
Expect(inspect.InspectContainerToJSON()[0].Name).Should(Equal("simpleWithoutPodPrefix"))
})
})