Skip to content

Commit 8cafefb

Browse files
authored
Merge pull request containerd#4289 from apostasie/2025-05-empty-names
Prevent --name="" for container
2 parents 76b786b + dcbf14b commit 8cafefb

File tree

3 files changed

+17
-25
lines changed

3 files changed

+17
-25
lines changed

cmd/nerdctl/container/container_create.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,6 @@ func createOptions(cmd *cobra.Command) (types.ContainerCreateOptions, error) {
371371
// #endregion
372372

373373
// #region for metadata flags
374-
opt.NameChanged = cmd.Flags().Changed("name")
375374
opt.Name, err = cmd.Flags().GetString("name")
376375
if err != nil {
377376
return opt, err

pkg/api/types/container_types.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,6 @@ type ContainerCreateOptions struct {
237237
// #endregion
238238

239239
// #region for metadata flags
240-
// NameChanged specifies whether the name has been changed
241-
NameChanged bool
242240
// Name assign a name to the container
243241
Name string
244242
// Label set meta data on a container

pkg/cmd/container/create.go

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
339339
cOpts = append(cOpts, lCOpts...)
340340

341341
var containerNameStore namestore.NameStore
342-
if options.Name == "" && !options.NameChanged {
343-
// Automatically set the container name, unless `--name=""` was explicitly specified.
342+
if options.Name == "" {
344343
var imageRef string
345344
if ensuredImage != nil {
346345
imageRef = ensuredImage.Ref
@@ -352,15 +351,15 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
352351
}
353352
options.Name = parsedReference.SuggestContainerName(id)
354353
}
355-
if options.Name != "" {
356-
containerNameStore, err = namestore.New(dataStore, options.GOptions.Namespace)
357-
if err != nil {
358-
return nil, generateRemoveOrphanedDirsFunc(ctx, id, dataStore, internalLabels), err
359-
}
360-
if err := containerNameStore.Acquire(options.Name, id); err != nil {
361-
return nil, generateRemoveOrphanedDirsFunc(ctx, id, dataStore, internalLabels), err
362-
}
354+
355+
containerNameStore, err = namestore.New(dataStore, options.GOptions.Namespace)
356+
if err != nil {
357+
return nil, generateRemoveOrphanedDirsFunc(ctx, id, dataStore, internalLabels), err
363358
}
359+
if err := containerNameStore.Acquire(options.Name, id); err != nil {
360+
return nil, generateRemoveOrphanedDirsFunc(ctx, id, dataStore, internalLabels), err
361+
}
362+
364363
internalLabels.name = options.Name
365364
internalLabels.pidFile = options.PidFile
366365

@@ -714,9 +713,7 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
714713
var hostConfigLabel dockercompat.HostConfigLabel
715714
var dnsSettings dockercompat.DNSSettings
716715
m[labels.Namespace] = internalLabels.namespace
717-
if internalLabels.name != "" {
718-
m[labels.Name] = internalLabels.name
719-
}
716+
m[labels.Name] = internalLabels.name
720717
m[labels.Hostname] = internalLabels.hostname
721718
m[labels.Domainname] = internalLabels.domainname
722719
extraHostsJSON, err := json.Marshal(internalLabels.extraHosts)
@@ -1024,15 +1021,13 @@ func generateGcFunc(ctx context.Context, container containerd.Container, ns, id,
10241021
log.G(ctx).WithError(rmErr).Warnf("failed to remove container %q state dir %q", id, internalLabels.stateDir)
10251022
}
10261023

1027-
if name != "" {
1028-
var errE error
1029-
if containerNameStore, errE = namestore.New(dataStore, ns); errE != nil {
1030-
log.G(ctx).WithError(errE).Warnf("failed to instantiate container name store during cleanup for container %q", id)
1031-
}
1032-
// Double-releasing may happen with containers started with --rm, so, ignore NotFound errors
1033-
if errE := containerNameStore.Release(name, id); errE != nil && !errors.Is(errE, store.ErrNotFound) {
1034-
log.G(ctx).WithError(errE).Warnf("failed to release container name store for container %q (%s)", name, id)
1035-
}
1024+
var errE error
1025+
if containerNameStore, errE = namestore.New(dataStore, ns); errE != nil {
1026+
log.G(ctx).WithError(errE).Warnf("failed to instantiate container name store during cleanup for container %q", id)
1027+
}
1028+
// Double-releasing may happen with containers started with --rm, so, ignore NotFound errors
1029+
if errE := containerNameStore.Release(name, id); errE != nil && !errors.Is(errE, store.ErrNotFound) {
1030+
log.G(ctx).WithError(errE).Warnf("failed to release container name store for container %q (%s)", name, id)
10361031
}
10371032
}
10381033
}

0 commit comments

Comments
 (0)