Skip to content

containerd.WithNewSpec does not work with containerd.WithDefaultNamespace #6039

@strideynet

Description

@strideynet

Description

Creating a container with the WithNewSpec() option in combination with a client created with the WithDefaultNamespace() option does not work.

This appears to be caused by the fact that the WithDefaultNamespace() option functions by adding an interceptor, which adds the default namespace to the context on outgoing GRPC requests. However, the WithNewSpec() option is evaluated locally (and therefore the GRPC interceptors are not called), meaning that the context it has does not have the default value attached.

Steps to reproduce the issue

  1. Set a default namespace when instantiating your client.
  2. Call NewContainer with the WithNewSpec option.
  3. Observe namespace is required: failed precondition
package main

import (
	"context"
	"log"
	"time"

	"github.com/containerd/containerd"
	"github.com/containerd/containerd/oci"
)

func main() {
	ctx := context.TODO()
	client, err := containerd.New(
		"/run/containerd/containerd.sock",
		containerd.WithDefaultNamespace("korulet"),
	)
	if err != nil {
		log.Fatalf("connect: %s", err)
	}
	defer client.Close()

	image, err := client.Pull(
		ctx,
		"registry.hub.docker.com/library/nginx:1.21.3",
		containerd.WithPullUnpack,
	)
	if err != nil {
		log.Fatalf("pull: %s", err)
	}
	log.Printf("Pulled image %s\n", image.Name())

	container, err := client.NewContainer(
		ctx,
		"nginx-example",
		containerd.WithNewSnapshot("nginx-snapshot", image),
		containerd.WithNewSpec(oci.WithImageConfig(image)),
	)
	if err != nil {
		log.Fatalf("create container: %s", err)
	}
	defer container.Delete(ctx, containerd.WithSnapshotCleanup)
	log.Printf("created container %s\n", container.ID())

	time.Sleep(time.Second * 60)
}

Describe the results you received and expected

Received: an error namespace is required: failed precondition

Expected: It to like, not error ? I would expect that the WithDefaultNamespace() actually sets a default namespace that works with other options.

What version of containerd are you using?

v1.5.4

Any other relevant information

Client:
Version: v1.5.4
Revision: 69107e4
Go version: go1.16.6

Server:
Version: v1.5.4
Revision: 69107e4
UUID: 687a287e-20b3-4141-a8dc-ccdf5c2198a0

Show configuration if it is related to CRI plugin.

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions