Skip to content

Commit 15428de

Browse files
authored
Use ctr directly to import images (#53)
1 parent 3f08166 commit 15428de

13 files changed

+50
-33
lines changed

cmd/cluster-agent.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ lifecycle of a MicroK8s cluster.`,
3939
s := snap.NewSnap(
4040
os.Getenv("SNAP"),
4141
os.Getenv("SNAP_DATA"),
42+
os.Getenv("SNAP_COMMON"),
4243
snap.WithRetryApplyCNI(20, 3*time.Second),
4344
)
4445

cmd/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var (
2222
s := snap.NewSnap(
2323
os.Getenv("SNAP"),
2424
os.Getenv("SNAP_DATA"),
25+
os.Getenv("SNAP_COMMON"),
2526
)
2627
l := k8sinit.NewLauncher(s, initPreInit)
2728

pkg/snap/snap.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ import (
2020

2121
// snap implements the Snap interface.
2222
type snap struct {
23-
snapDir string
24-
snapDataDir string
25-
runCommand func(context.Context, ...string) error
23+
snapDir string
24+
snapDataDir string
25+
snapCommonDir string
26+
runCommand func(context.Context, ...string) error
2627

2728
clusterTokensMu sync.Mutex
2829
certTokensMu sync.Mutex
@@ -34,12 +35,13 @@ type snap struct {
3435
}
3536

3637
// NewSnap creates a new interface with the MicroK8s snap.
37-
// NewSnap accepts the $SNAP and $SNAP_DATA directories, and a number of options.
38-
func NewSnap(snapDir, snapDataDir string, options ...func(s *snap)) Snap {
38+
// NewSnap accepts the $SNAP, $SNAP_DATA and $SNAP_COMMON, directories, and a number of options.
39+
func NewSnap(snapDir, snapDataDir, snapCommonDir string, options ...func(s *snap)) Snap {
3940
s := &snap{
40-
snapDir: snapDir,
41-
snapDataDir: snapDataDir,
42-
runCommand: util.RunCommand,
41+
snapDir: snapDir,
42+
snapDataDir: snapDataDir,
43+
snapCommonDir: snapCommonDir,
44+
runCommand: util.RunCommand,
4345
}
4446

4547
for _, opt := range options {
@@ -56,6 +58,9 @@ func (s *snap) snapPath(parts ...string) string {
5658
func (s *snap) snapDataPath(parts ...string) string {
5759
return filepath.Join(append([]string{s.snapDataDir}, parts...)...)
5860
}
61+
func (s *snap) snapCommonPath(parts ...string) string {
62+
return filepath.Join(append([]string{s.snapCommonDir}, parts...)...)
63+
}
5964

6065
func (s *snap) GetGroupName() string {
6166
if s.isStrict() {
@@ -336,7 +341,15 @@ func (s *snap) SignCertificate(ctx context.Context, csrPEM []byte) ([]byte, erro
336341
}
337342

338343
func (s *snap) ImportImage(ctx context.Context, reader io.Reader) error {
339-
importCmd := exec.CommandContext(ctx, s.snapPath("microk8s-ctr.wrapper"), "image", "import", "--platform", runtime.GOARCH, "-")
344+
importCmd := exec.CommandContext(ctx,
345+
s.snapPath("bin", "ctr"),
346+
"--namespace", "k8s.io",
347+
"--address", s.snapCommonPath("run", "containerd.sock"),
348+
"image",
349+
"import",
350+
"--platform", runtime.GOARCH,
351+
"-",
352+
)
340353
importCmd.Stdin = reader
341354
importCmd.Stdout = os.Stdout
342355
importCmd.Stdout = os.Stderr

pkg/snap/snap_addons_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
func TestAddons(t *testing.T) {
1313
t.Run("EnableDisable", func(t *testing.T) {
1414
runner := &utiltest.MockRunner{}
15-
s := snap.NewSnap("testdata", "testdata", snap.WithCommandRunner(runner.Run))
15+
s := snap.NewSnap("testdata", "testdata", "testdata", snap.WithCommandRunner(runner.Run))
1616

1717
s.EnableAddon(context.Background(), "dns")
1818
s.EnableAddon(context.Background(), "dns", "10.0.0.2")
@@ -32,7 +32,7 @@ func TestAddons(t *testing.T) {
3232

3333
t.Run("AddRepository", func(t *testing.T) {
3434
runner := &utiltest.MockRunner{}
35-
s := snap.NewSnap("testdata", "testdata", snap.WithCommandRunner(runner.Run))
35+
s := snap.NewSnap("testdata", "testdata", "testdata", snap.WithCommandRunner(runner.Run))
3636

3737
s.AddAddonsRepository(context.Background(), "core", "/snap/microk8s/current/addons/core", "", false)
3838
s.AddAddonsRepository(context.Background(), "core", "/snap/microk8s/current/addons/core", "", true)

pkg/snap/snap_containerd_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestUpdateContainerdRegistryConfigs(t *testing.T) {
1414
}
1515
defer os.RemoveAll("testdata/args")
1616

17-
s := snap.NewSnap("testdata", "testdata")
17+
s := snap.NewSnap("testdata", "testdata", "testdata")
1818

1919
t.Run("Mirror", func(t *testing.T) {
2020
g := NewWithT(t)

pkg/snap/snap_files_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestFiles(t *testing.T) {
4747
defer os.RemoveAll(filepath.Dir(file))
4848
}
4949

50-
s := snap.NewSnap("testdata", "testdata")
50+
s := snap.NewSnap("testdata", "testdata", "testdata")
5151

5252
for _, tc := range []struct {
5353
name string

pkg/snap/snap_images_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,27 @@ cat > testdata/stdin
2525
`
2626

2727
func TestImportImage(t *testing.T) {
28-
if err := os.WriteFile("testdata/microk8s-ctr.wrapper", []byte(mockCtr), 0755); err != nil {
28+
if err := os.MkdirAll("testdata/bin", 0700); err != nil {
29+
t.Fatalf("Failed to intialize mock bin dir: %v", err)
30+
}
31+
if err := os.WriteFile("testdata/bin/ctr", []byte(mockCtr), 0755); err != nil {
2932
t.Fatalf("Failed to initialize mock ctr command: %v", err)
3033
}
3134
defer func() {
32-
os.Remove("testdata/microk8s-ctr.wrapper")
35+
os.RemoveAll("testdata/bin")
3336
os.Remove("testdata/stdin")
3437
os.Remove("testdata/arguments")
3538
}()
3639
mockRunner := &utiltest.MockRunner{}
37-
s := snap.NewSnap("testdata", "testdata", snap.WithCommandRunner(mockRunner.Run))
40+
s := snap.NewSnap("testdata", "testdata", "testdata/common", snap.WithCommandRunner(mockRunner.Run))
3841

3942
g := NewWithT(t)
4043
err := s.ImportImage(context.Background(), bytes.NewBufferString("IMAGEDATA"))
4144
g.Expect(err).To(BeNil())
4245

4346
cmd, err := util.ReadFile("testdata/arguments")
4447
g.Expect(err).To(BeNil())
45-
g.Expect(strings.TrimSpace(cmd)).To(Equal(fmt.Sprintf("testdata/microk8s-ctr.wrapper image import --platform %s -", runtime.GOARCH)))
48+
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)))
4649

4750
stdin, err := util.ReadFile("testdata/stdin")
4851
g.Expect(err).To(BeNil())

pkg/snap/snap_join_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestJoinCluster(t *testing.T) {
1515
t.Run("PropagateError", func(t *testing.T) {
1616
g := NewWithT(t)
1717
runner := &utiltest.MockRunner{}
18-
s := snap.NewSnap("testdata", "testdata", snap.WithCommandRunner(runner.Run))
18+
s := snap.NewSnap("testdata", "testdata", "testdata", snap.WithCommandRunner(runner.Run))
1919
runner.Err = fmt.Errorf("some error")
2020

2121
err := s.JoinCluster(context.Background(), "some-url", false)
@@ -26,7 +26,7 @@ func TestJoinCluster(t *testing.T) {
2626
t.Run("ControlPlane", func(t *testing.T) {
2727
g := NewWithT(t)
2828
runner := &utiltest.MockRunner{}
29-
s := snap.NewSnap("testdata", "testdata", snap.WithCommandRunner(runner.Run))
29+
s := snap.NewSnap("testdata", "testdata", "testdata", snap.WithCommandRunner(runner.Run))
3030

3131
err := s.JoinCluster(context.Background(), "10.10.10.10:25000/token/hash", false)
3232
g.Expect(err).To(BeNil())
@@ -36,7 +36,7 @@ func TestJoinCluster(t *testing.T) {
3636
t.Run("Worker", func(t *testing.T) {
3737
g := NewWithT(t)
3838
runner := &utiltest.MockRunner{}
39-
s := snap.NewSnap("testdata", "testdata", snap.WithCommandRunner(runner.Run))
39+
s := snap.NewSnap("testdata", "testdata", "testdata", snap.WithCommandRunner(runner.Run))
4040

4141
err := s.JoinCluster(context.Background(), "10.10.10.10:25000/token/hash", true)
4242
g.Expect(err).To(BeNil())

pkg/snap/snap_lock_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func TestLock(t *testing.T) {
12-
s := snap.NewSnap("testdata", "testdata")
12+
s := snap.NewSnap("testdata", "testdata", "testdata")
1313
if err := os.MkdirAll("testdata/var/lock", 0755); err != nil {
1414
t.Fatalf("Failed to create directory: %s", err)
1515
}

pkg/snap/snap_service_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func TestServiceRestart(t *testing.T) {
1313
mockRunner := &utiltest.MockRunner{}
14-
s := snap.NewSnap("testdata", "testdata", snap.WithCommandRunner(mockRunner.Run))
14+
s := snap.NewSnap("testdata", "testdata", "testdata", snap.WithCommandRunner(mockRunner.Run))
1515

1616
t.Run("NoKubelite", func(t *testing.T) {
1717
for _, tc := range []struct {

0 commit comments

Comments
 (0)