about summary refs log tree commit diff
path: root/users/fcuny/exp/containerd-to-vm/cmd/c2vm
diff options
context:
space:
mode:
Diffstat (limited to 'users/fcuny/exp/containerd-to-vm/cmd/c2vm')
-rw-r--r--users/fcuny/exp/containerd-to-vm/cmd/c2vm/main.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/users/fcuny/exp/containerd-to-vm/cmd/c2vm/main.go b/users/fcuny/exp/containerd-to-vm/cmd/c2vm/main.go
new file mode 100644
index 0000000..4d0968b
--- /dev/null
+++ b/users/fcuny/exp/containerd-to-vm/cmd/c2vm/main.go
@@ -0,0 +1,56 @@
+package main
+
+import (
+	"context"
+	"flag"
+	"fmt"
+	"log"
+
+	"github.com/containerd/containerd"
+	"github.com/containerd/containerd/namespaces"
+	"github.com/containerd/containerd/platforms"
+	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
+)
+
+const (
+	containerdSock   = "/run/containerd/containerd.sock"
+	defaultNamespace = "c2vm"
+)
+
+var (
+	platform = platforms.Only(ocispec.Platform{
+		OS:           "linux",
+		Architecture: "amd64",
+	})
+)
+
+func main() {
+	var (
+		containerName = flag.String("container", "", "Name of the container")
+	)
+
+	flag.Parse()
+
+	if *containerName == "" {
+		log.Fatal("a container is required")
+	}
+
+	client, err := containerd.New(containerdSock)
+	if err != nil {
+		log.Fatalf("failed to create a client for containerd: %v", err)
+	}
+	defer client.Close()
+
+	ctx := namespaces.WithNamespace(context.Background(), defaultNamespace)
+	image, err := client.Pull(ctx, *containerName, containerd.WithPlatformMatcher(platform))
+	if err != nil {
+		log.Fatalf("failed to pull the container %s: %v\n", *containerName, err)
+	}
+
+	imageSize, err := image.Usage(ctx, containerd.WithUsageManifestLimit(1))
+	if err != nil {
+		log.Fatalf("failed to get the size of the image: %v", err)
+	}
+
+	fmt.Printf("pulled %s (%d bytes)\n", image.Name(), imageSize)
+}