Skip to content

Commit

Permalink
Use ctr directly to import images (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
neoaggelos authored Oct 23, 2023
1 parent b306c39 commit 664ceaa
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 33 deletions.
1 change: 1 addition & 0 deletions cmd/cluster-agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)

Expand Down
1 change: 1 addition & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
s := snap.NewSnap(
os.Getenv("SNAP"),
os.Getenv("SNAP_DATA"),
os.Getenv("SNAP_COMMON"),
)
l := k8sinit.NewLauncher(s, initPreInit)

Expand Down
31 changes: 22 additions & 9 deletions pkg/snap/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/snap/snap_addons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/snap/snap_containerd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/snap/snap_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions pkg/snap/snap_images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,27 @@ 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"))
g.Expect(err).To(BeNil())

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())
Expand Down
6 changes: 3 additions & 3 deletions pkg/snap/snap_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion pkg/snap/snap_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/snap/snap_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/snap/snap_sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
14 changes: 7 additions & 7 deletions pkg/snap/snap_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/snap/snap_upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -71,5 +71,4 @@ func TestRunUpgrade(t *testing.T) {
})
}
})

}

0 comments on commit 664ceaa

Please sign in to comment.