From 664ceaa4b5a4942ac51ae3cd9ad8e25a322d88b6 Mon Sep 17 00:00:00 2001 From: Angelos Kolaitis Date: Mon, 23 Oct 2023 13:37:45 +0300 Subject: [PATCH] Use ctr directly to import images (#52) --- cmd/cluster-agent.go | 1 + cmd/init.go | 1 + pkg/snap/snap.go | 31 ++++++++++++++++++++++--------- pkg/snap/snap_addons_test.go | 4 ++-- pkg/snap/snap_containerd_test.go | 2 +- pkg/snap/snap_files_test.go | 2 +- pkg/snap/snap_images_test.go | 11 +++++++---- pkg/snap/snap_join_test.go | 6 +++--- pkg/snap/snap_lock_test.go | 2 +- pkg/snap/snap_service_test.go | 2 +- pkg/snap/snap_sign_test.go | 2 +- pkg/snap/snap_token_test.go | 14 +++++++------- pkg/snap/snap_upgrade_test.go | 5 ++--- 13 files changed, 50 insertions(+), 33 deletions(-) diff --git a/cmd/cluster-agent.go b/cmd/cluster-agent.go index fbeabb4..28181f7 100644 --- a/cmd/cluster-agent.go +++ b/cmd/cluster-agent.go @@ -39,6 +39,7 @@ lifecycle of a MicroK8s cluster.`, s := snap.NewSnap( os.Getenv("SNAP"), os.Getenv("SNAP_DATA"), + os.Getenv("SNAP_COMMON"), snap.WithRetryApplyCNI(20, 3*time.Second), ) diff --git a/cmd/init.go b/cmd/init.go index 7b7668e..0a0661e 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -22,6 +22,7 @@ var ( s := snap.NewSnap( os.Getenv("SNAP"), os.Getenv("SNAP_DATA"), + os.Getenv("SNAP_COMMON"), ) l := k8sinit.NewLauncher(s, initPreInit) diff --git a/pkg/snap/snap.go b/pkg/snap/snap.go index 7137bf5..95bb8e7 100644 --- a/pkg/snap/snap.go +++ b/pkg/snap/snap.go @@ -20,9 +20,10 @@ import ( // snap implements the Snap interface. type snap struct { - snapDir string - snapDataDir string - runCommand func(context.Context, ...string) error + snapDir string + snapDataDir string + snapCommonDir string + runCommand func(context.Context, ...string) error clusterTokensMu sync.Mutex certTokensMu sync.Mutex @@ -34,12 +35,13 @@ type snap struct { } // NewSnap creates a new interface with the MicroK8s snap. -// NewSnap accepts the $SNAP and $SNAP_DATA directories, and a number of options. -func NewSnap(snapDir, snapDataDir string, options ...func(s *snap)) Snap { +// NewSnap accepts the $SNAP, $SNAP_DATA and $SNAP_COMMON, directories, and a number of options. +func NewSnap(snapDir, snapDataDir, snapCommonDir string, options ...func(s *snap)) Snap { s := &snap{ - snapDir: snapDir, - snapDataDir: snapDataDir, - runCommand: util.RunCommand, + snapDir: snapDir, + snapDataDir: snapDataDir, + snapCommonDir: snapCommonDir, + runCommand: util.RunCommand, } for _, opt := range options { @@ -56,6 +58,9 @@ func (s *snap) snapPath(parts ...string) string { func (s *snap) snapDataPath(parts ...string) string { return filepath.Join(append([]string{s.snapDataDir}, parts...)...) } +func (s *snap) snapCommonPath(parts ...string) string { + return filepath.Join(append([]string{s.snapCommonDir}, parts...)...) +} func (s *snap) GetGroupName() string { if s.isStrict() { @@ -336,7 +341,15 @@ func (s *snap) SignCertificate(ctx context.Context, csrPEM []byte) ([]byte, erro } func (s *snap) ImportImage(ctx context.Context, reader io.Reader) error { - importCmd := exec.CommandContext(ctx, s.snapPath("microk8s-ctr.wrapper"), "image", "import", "--platform", runtime.GOARCH, "-") + importCmd := exec.CommandContext(ctx, + s.snapPath("bin", "ctr"), + "--namespace", "k8s.io", + "--address", s.snapCommonPath("run", "containerd.sock"), + "image", + "import", + "--platform", runtime.GOARCH, + "-", + ) importCmd.Stdin = reader importCmd.Stdout = os.Stdout importCmd.Stdout = os.Stderr diff --git a/pkg/snap/snap_addons_test.go b/pkg/snap/snap_addons_test.go index 8f327d4..f2bd073 100644 --- a/pkg/snap/snap_addons_test.go +++ b/pkg/snap/snap_addons_test.go @@ -12,7 +12,7 @@ import ( func TestAddons(t *testing.T) { t.Run("EnableDisable", func(t *testing.T) { runner := &utiltest.MockRunner{} - s := snap.NewSnap("testdata", "testdata", snap.WithCommandRunner(runner.Run)) + s := snap.NewSnap("testdata", "testdata", "testdata", snap.WithCommandRunner(runner.Run)) s.EnableAddon(context.Background(), "dns") s.EnableAddon(context.Background(), "dns", "10.0.0.2") @@ -32,7 +32,7 @@ func TestAddons(t *testing.T) { t.Run("AddRepository", func(t *testing.T) { runner := &utiltest.MockRunner{} - s := snap.NewSnap("testdata", "testdata", snap.WithCommandRunner(runner.Run)) + s := snap.NewSnap("testdata", "testdata", "testdata", snap.WithCommandRunner(runner.Run)) s.AddAddonsRepository(context.Background(), "core", "/snap/microk8s/current/addons/core", "", false) s.AddAddonsRepository(context.Background(), "core", "/snap/microk8s/current/addons/core", "", true) diff --git a/pkg/snap/snap_containerd_test.go b/pkg/snap/snap_containerd_test.go index 09d49f7..39826de 100644 --- a/pkg/snap/snap_containerd_test.go +++ b/pkg/snap/snap_containerd_test.go @@ -14,7 +14,7 @@ func TestUpdateContainerdRegistryConfigs(t *testing.T) { } defer os.RemoveAll("testdata/args") - s := snap.NewSnap("testdata", "testdata") + s := snap.NewSnap("testdata", "testdata", "testdata") t.Run("Mirror", func(t *testing.T) { g := NewWithT(t) diff --git a/pkg/snap/snap_files_test.go b/pkg/snap/snap_files_test.go index 2c3b0ea..ba9b606 100644 --- a/pkg/snap/snap_files_test.go +++ b/pkg/snap/snap_files_test.go @@ -47,7 +47,7 @@ func TestFiles(t *testing.T) { defer os.RemoveAll(filepath.Dir(file)) } - s := snap.NewSnap("testdata", "testdata") + s := snap.NewSnap("testdata", "testdata", "testdata") for _, tc := range []struct { name string diff --git a/pkg/snap/snap_images_test.go b/pkg/snap/snap_images_test.go index 54ccc5e..fe72bf8 100644 --- a/pkg/snap/snap_images_test.go +++ b/pkg/snap/snap_images_test.go @@ -25,16 +25,19 @@ cat > testdata/stdin ` func TestImportImage(t *testing.T) { - if err := os.WriteFile("testdata/microk8s-ctr.wrapper", []byte(mockCtr), 0755); err != nil { + if err := os.MkdirAll("testdata/bin", 0700); err != nil { + t.Fatalf("Failed to intialize mock bin dir: %v", err) + } + if err := os.WriteFile("testdata/bin/ctr", []byte(mockCtr), 0755); err != nil { t.Fatalf("Failed to initialize mock ctr command: %v", err) } defer func() { - os.Remove("testdata/microk8s-ctr.wrapper") + os.RemoveAll("testdata/bin") os.Remove("testdata/stdin") os.Remove("testdata/arguments") }() mockRunner := &utiltest.MockRunner{} - s := snap.NewSnap("testdata", "testdata", snap.WithCommandRunner(mockRunner.Run)) + s := snap.NewSnap("testdata", "testdata", "testdata/common", snap.WithCommandRunner(mockRunner.Run)) g := NewWithT(t) err := s.ImportImage(context.Background(), bytes.NewBufferString("IMAGEDATA")) @@ -42,7 +45,7 @@ func TestImportImage(t *testing.T) { cmd, err := util.ReadFile("testdata/arguments") g.Expect(err).To(BeNil()) - g.Expect(strings.TrimSpace(cmd)).To(Equal(fmt.Sprintf("testdata/microk8s-ctr.wrapper image import --platform %s -", runtime.GOARCH))) + g.Expect(strings.TrimSpace(cmd)).To(Equal(fmt.Sprintf("testdata/bin/ctr --namespace k8s.io --address testdata/common/run/containerd.sock image import --platform %s -", runtime.GOARCH))) stdin, err := util.ReadFile("testdata/stdin") g.Expect(err).To(BeNil()) diff --git a/pkg/snap/snap_join_test.go b/pkg/snap/snap_join_test.go index 80a9dbc..1d311ed 100644 --- a/pkg/snap/snap_join_test.go +++ b/pkg/snap/snap_join_test.go @@ -15,7 +15,7 @@ func TestJoinCluster(t *testing.T) { t.Run("PropagateError", func(t *testing.T) { g := NewWithT(t) runner := &utiltest.MockRunner{} - s := snap.NewSnap("testdata", "testdata", snap.WithCommandRunner(runner.Run)) + s := snap.NewSnap("testdata", "testdata", "testdata", snap.WithCommandRunner(runner.Run)) runner.Err = fmt.Errorf("some error") err := s.JoinCluster(context.Background(), "some-url", false) @@ -26,7 +26,7 @@ func TestJoinCluster(t *testing.T) { t.Run("ControlPlane", func(t *testing.T) { g := NewWithT(t) runner := &utiltest.MockRunner{} - s := snap.NewSnap("testdata", "testdata", snap.WithCommandRunner(runner.Run)) + s := snap.NewSnap("testdata", "testdata", "testdata", snap.WithCommandRunner(runner.Run)) err := s.JoinCluster(context.Background(), "10.10.10.10:25000/token/hash", false) g.Expect(err).To(BeNil()) @@ -36,7 +36,7 @@ func TestJoinCluster(t *testing.T) { t.Run("Worker", func(t *testing.T) { g := NewWithT(t) runner := &utiltest.MockRunner{} - s := snap.NewSnap("testdata", "testdata", snap.WithCommandRunner(runner.Run)) + s := snap.NewSnap("testdata", "testdata", "testdata", snap.WithCommandRunner(runner.Run)) err := s.JoinCluster(context.Background(), "10.10.10.10:25000/token/hash", true) g.Expect(err).To(BeNil()) diff --git a/pkg/snap/snap_lock_test.go b/pkg/snap/snap_lock_test.go index d897225..a38a2f9 100644 --- a/pkg/snap/snap_lock_test.go +++ b/pkg/snap/snap_lock_test.go @@ -9,7 +9,7 @@ import ( ) func TestLock(t *testing.T) { - s := snap.NewSnap("testdata", "testdata") + s := snap.NewSnap("testdata", "testdata", "testdata") if err := os.MkdirAll("testdata/var/lock", 0755); err != nil { t.Fatalf("Failed to create directory: %s", err) } diff --git a/pkg/snap/snap_service_test.go b/pkg/snap/snap_service_test.go index 7da49d7..e4ecc50 100644 --- a/pkg/snap/snap_service_test.go +++ b/pkg/snap/snap_service_test.go @@ -11,7 +11,7 @@ import ( func TestServiceRestart(t *testing.T) { mockRunner := &utiltest.MockRunner{} - s := snap.NewSnap("testdata", "testdata", snap.WithCommandRunner(mockRunner.Run)) + s := snap.NewSnap("testdata", "testdata", "testdata", snap.WithCommandRunner(mockRunner.Run)) t.Run("NoKubelite", func(t *testing.T) { for _, tc := range []struct { diff --git a/pkg/snap/snap_sign_test.go b/pkg/snap/snap_sign_test.go index 221a361..20f9941 100644 --- a/pkg/snap/snap_sign_test.go +++ b/pkg/snap/snap_sign_test.go @@ -35,7 +35,7 @@ func TestSignCertificate(t *testing.T) { os.Remove("testdata/arguments") }() mockRunner := &utiltest.MockRunner{} - s := snap.NewSnap("testdata", "testdata", snap.WithCommandRunner(mockRunner.Run)) + s := snap.NewSnap("testdata", "testdata", "testdata", snap.WithCommandRunner(mockRunner.Run)) g := NewWithT(t) b, err := s.SignCertificate(context.Background(), []byte("MOCK CSR")) diff --git a/pkg/snap/snap_token_test.go b/pkg/snap/snap_token_test.go index 3597330..cfe163d 100644 --- a/pkg/snap/snap_token_test.go +++ b/pkg/snap/snap_token_test.go @@ -13,7 +13,7 @@ import ( func TestClusterTokens(t *testing.T) { os.RemoveAll("testdata/credentials") - s := snap.NewSnap("testdata", "testdata") + s := snap.NewSnap("testdata", "testdata", "testdata") t.Run("MissingTokensFile", func(t *testing.T) { if s.ConsumeClusterToken("token1") { t.Fatal("Expected token1 to not be valid, but it is") @@ -94,7 +94,7 @@ func TestPersistentClusterToken(t *testing.T) { t.Fatalf("Failed to create test directory: %s", err) } defer os.RemoveAll("testdata/credentials") - s := snap.NewSnap("testdata", "testdata") + s := snap.NewSnap("testdata", "testdata", "testdata") if err := s.AddPersistentClusterToken("my-token"); err != nil { t.Fatalf("Failed to add persistent cluster token: %s", err) } @@ -117,7 +117,7 @@ func TestCertificateRequestTokens(t *testing.T) { t.Fatalf("Failed to create test directory: %s", err) } defer os.RemoveAll("testdata/credentials") - s := snap.NewSnap("testdata", "testdata") + s := snap.NewSnap("testdata", "testdata", "testdata") if err := s.AddCertificateRequestToken("my-token"); err != nil { t.Fatalf("Failed to add certificate request token: %s", err) } @@ -151,7 +151,7 @@ func TestCallbackTokens(t *testing.T) { t.Fatalf("Failed to create test directory: %s", err) } defer os.RemoveAll("testdata/credentials") - s := snap.NewSnap("testdata", "testdata") + s := snap.NewSnap("testdata", "testdata", "testdata") if err := s.AddCallbackToken("ip:port", "my-token"); err != nil { t.Fatalf("Failed to add certificate request token: %s", err) } @@ -169,7 +169,7 @@ func TestSelfCallbackToken(t *testing.T) { t.Fatalf("Failed to create test directory: %s", err) } defer os.RemoveAll("testdata/credentials") - s := snap.NewSnap("testdata", "testdata") + s := snap.NewSnap("testdata", "testdata", "testdata") token, err := s.GetOrCreateSelfCallbackToken() if err != nil { t.Fatalf("Failed to configure callback token: %q", err) @@ -194,7 +194,7 @@ func TestKnownTokens(t *testing.T) { t.Fatalf("Failed to create test directory: %s", err) } defer os.RemoveAll("testdata/credentials") - s := snap.NewSnap("testdata", "testdata") + s := snap.NewSnap("testdata", "testdata", "testdata") if token, err := s.GetKnownToken("user"); token != "" || err == nil { t.Fatalf("Expected an empty token and an error, but found token %s and error %s", token, err) } @@ -273,7 +273,7 @@ func TestStrictGroup(t *testing.T) { if err := os.WriteFile("testdata/meta/snapcraft.yaml", []byte(fmt.Sprintf("confinement: %s", tc.confinement)), 0660); err != nil { t.Fatalf("Failed to create test file: %s", err) } - group := snap.NewSnap("testdata", "testdata").GetGroupName() + group := snap.NewSnap("testdata", "testdata", "testdata").GetGroupName() if tc.group != group { t.Fatalf("Expected group to be %q but it was %q instead", tc.group, group) } diff --git a/pkg/snap/snap_upgrade_test.go b/pkg/snap/snap_upgrade_test.go index 2435edd..5363cf4 100644 --- a/pkg/snap/snap_upgrade_test.go +++ b/pkg/snap/snap_upgrade_test.go @@ -31,7 +31,7 @@ func TestRunUpgrade(t *testing.T) { defer os.RemoveAll("testdata/upgrade-scripts") runner := &utiltest.MockRunner{} - s := snap.NewSnap("testdata", "testdata", snap.WithCommandRunner(runner.Run)) + s := snap.NewSnap("testdata", "testdata", "testdata", snap.WithCommandRunner(runner.Run)) t.Run("Invalid", func(t *testing.T) { for _, tc := range []struct { @@ -58,7 +58,7 @@ func TestRunUpgrade(t *testing.T) { t.Run(phase, func(t *testing.T) { runner := &utiltest.MockRunner{} - s := snap.NewSnap("testdata", "testdata", snap.WithCommandRunner(runner.Run)) + s := snap.NewSnap("testdata", "testdata", "testdata", snap.WithCommandRunner(runner.Run)) err := s.RunUpgrade(context.Background(), "001-custom-upgrade", phase) if err != nil { @@ -71,5 +71,4 @@ func TestRunUpgrade(t *testing.T) { }) } }) - }