diff --git a/cmd/containerd-shim-nerdbox-v1/main.go b/cmd/containerd-shim-nerdbox-v1/main.go index 50c469a..2cf5d97 100644 --- a/cmd/containerd-shim-nerdbox-v1/main.go +++ b/cmd/containerd-shim-nerdbox-v1/main.go @@ -21,7 +21,7 @@ import ( "github.com/containerd/containerd/v2/pkg/shim" - "github.com/containerd/nerdbox/internal/shim/manager" + "github.com/containerd/nerdbox/pkg/shim/manager" _ "github.com/containerd/nerdbox/plugins/shim/sandbox" _ "github.com/containerd/nerdbox/plugins/shim/streaming" @@ -31,5 +31,5 @@ import ( ) func main() { - shim.RunShim(context.Background(), manager.NewShimManager("io.containerd.nerdbox.v1")) + shim.RunShim(context.Background(), manager.New("io.containerd.nerdbox.v1")) } diff --git a/internal/shim/manager/manager.go b/pkg/shim/manager/manager.go similarity index 88% rename from internal/shim/manager/manager.go rename to pkg/shim/manager/manager.go index c04cadf..6003566 100644 --- a/internal/shim/manager/manager.go +++ b/pkg/shim/manager/manager.go @@ -26,9 +26,11 @@ import ( "github.com/containerd/containerd/v2/pkg/shim" ) -// NewShimManager returns an implementation of the shim manager -// using run_vminitd -func NewShimManager(name string) shim.Shim { +// New returns a shim manager implementation that launches the nerdbox shim +// process. The name is the runtime identifier reported to containerd (for +// example "io.containerd.nerdbox.v1"). External callers building variants +// of the nerdbox shim use this to assemble their own main package. +func New(name string) shim.Shim { return &manager{ name: name, } diff --git a/pkg/shim/manager/manager_test.go b/pkg/shim/manager/manager_test.go new file mode 100644 index 0000000..0985e53 --- /dev/null +++ b/pkg/shim/manager/manager_test.go @@ -0,0 +1,93 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package manager + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" +) + +func TestNewName(t *testing.T) { + const name = "io.containerd.nerdbox.v1" + s := New(name) + if got := s.Name(); got != name { + t.Errorf("Name() = %q, want %q", got, name) + } +} + +func TestInfo(t *testing.T) { + const name = "io.containerd.nerdbox.v1" + s := New(name) + info, err := s.Info(context.Background(), strings.NewReader("")) + if err != nil { + t.Fatalf("Info() error: %v", err) + } + if info.Name != name { + t.Errorf("Info().Name = %q, want %q", info.Name, name) + } + if got := info.Annotations["containerd.io/runtime-allow-mounts"]; got == "" { + t.Errorf("Info().Annotations missing runtime-allow-mounts: %v", info.Annotations) + } +} + +func TestReadSpec(t *testing.T) { + dir := t.TempDir() + configPath := filepath.Join(dir, "config.json") + const body = `{"annotations":{"io.kubernetes.cri.sandbox-id":"abc123","other":"value"}}` + if err := os.WriteFile(configPath, []byte(body), 0o600); err != nil { + t.Fatal(err) + } + + cwd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { os.Chdir(cwd) }) + if err := os.Chdir(dir); err != nil { + t.Fatal(err) + } + + spec, err := readSpec() + if err != nil { + t.Fatalf("readSpec() error: %v", err) + } + if got := spec.Annotations["io.kubernetes.cri.sandbox-id"]; got != "abc123" { + t.Errorf("annotation sandbox-id = %q, want %q", got, "abc123") + } + if got := spec.Annotations["other"]; got != "value" { + t.Errorf("annotation other = %q, want %q", got, "value") + } +} + +func TestReadSpecMissing(t *testing.T) { + dir := t.TempDir() + cwd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { os.Chdir(cwd) }) + if err := os.Chdir(dir); err != nil { + t.Fatal(err) + } + + if _, err := readSpec(); !os.IsNotExist(err) { + t.Errorf("readSpec() error = %v, want os.IsNotExist", err) + } +} diff --git a/internal/shim/manager/manager_unix.go b/pkg/shim/manager/manager_unix.go similarity index 100% rename from internal/shim/manager/manager_unix.go rename to pkg/shim/manager/manager_unix.go diff --git a/internal/shim/manager/manager_windows.go b/pkg/shim/manager/manager_windows.go similarity index 100% rename from internal/shim/manager/manager_windows.go rename to pkg/shim/manager/manager_windows.go diff --git a/internal/shim/manager/mount_linux.go b/pkg/shim/manager/mount_linux.go similarity index 100% rename from internal/shim/manager/mount_linux.go rename to pkg/shim/manager/mount_linux.go diff --git a/internal/shim/manager/mount_other.go b/pkg/shim/manager/mount_other.go similarity index 100% rename from internal/shim/manager/mount_other.go rename to pkg/shim/manager/mount_other.go