Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 1.27] Use ctr directly to import images (#52) #54

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 23 additions & 9 deletions pkg/snap/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
Expand All @@ -19,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 @@ -33,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 @@ -55,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 @@ -339,7 +345,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", "-")
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 @@ -41,7 +41,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
15 changes: 10 additions & 5 deletions pkg/snap/snap_images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package snap_test
import (
"bytes"
"context"
"fmt"
"os"
"runtime"
"strings"
"testing"

Expand All @@ -22,24 +24,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))

err := s.ImportImage(context.Background(), bytes.NewBufferString("IMAGEDATA"))
if err != nil {
t.Fatalf("Expected no error but got %q instead", err)
}

expectedCmd := "testdata/microk8s-ctr.wrapper image import -"
if cmd, err := util.ReadFile("testdata/arguments"); err != nil || strings.TrimSpace(cmd) != "testdata/microk8s-ctr.wrapper image import -" {
expectedCmd := fmt.Sprintf("testdata/bin/ctr --namespace k8s.io --address testdata/common/run/containerd.sock image import --platform %s -", runtime.GOARCH)
if cmd, err := util.ReadFile("testdata/arguments"); err != nil || strings.TrimSpace(cmd) != expectedCmd {
t.Fatalf("Expected command to be %q but it was %q instead", expectedCmd, cmd)
}

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 @@ -90,7 +90,7 @@ func TestSignCert(t *testing.T) {
t.Fatalf("Failed to write test certificate key: %s", err)
}

s := snap.NewSnap("testdata", "testdata")
s := snap.NewSnap("testdata", "testdata", "testdata")

certificate, err := s.SignCertificate(context.Background(), []byte(csrPEM))
if err != nil {
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) {
})
}
})

}
Loading