diff --git a/k8s/lib.sh b/k8s/lib.sh index e31ace822..e81b7aefd 100755 --- a/k8s/lib.sh +++ b/k8s/lib.sh @@ -99,9 +99,11 @@ k8s::remove::containerd() { # only remove containerd if the snap was already bootstrapped. # this is to prevent removing containerd when it is not installed by the snap. - if [ -f "$SNAP_COMMON/lock/containerd-socket-path" ]; then - rm -f $(cat "$SNAP_COMMON/lock/containerd-socket-path") - fi + for file in "containerd-socket-path" "containerd-config-dir" "containerd-root-dir" "containerd-cni-bin-dir"; do + if [ -f "$SNAP_COMMON/lock/$file" ]; then + rm -rf $(cat "$SNAP_COMMON/lock/$file") + fi + done } # Run a ctr command against the local containerd socket diff --git a/src/k8s/pkg/k8sd/setup/containerd.go b/src/k8s/pkg/k8sd/setup/containerd.go index e3213e4c2..9e6a3bd1a 100644 --- a/src/k8s/pkg/k8sd/setup/containerd.go +++ b/src/k8s/pkg/k8sd/setup/containerd.go @@ -159,14 +159,30 @@ func Containerd(snap snap.Snap, extraContainerdConfig map[string]any, extraArgs } } - // Write the containerd socket path to a file to properly clean-up on removal. - if err := utils.WriteFile(filepath.Join(snap.LockFilesDir(), "containerd-socket-path"), []byte(snap.ContainerdSocketDir()), 0o600); err != nil { - return fmt.Errorf("failed to write containerd-socket-path: %w", err) + if err := saveSnapContainerdPaths(snap); err != nil { + return err } return nil } +func saveSnapContainerdPaths(s snap.Snap) error { + // Write the containerd-related paths to files to properly clean-up on removal. + m := map[string]string{ + "containerd-socket-path": s.ContainerdSocketDir(), + "containerd-config-dir": s.ContainerdConfigDir(), + "containerd-root-dir": s.ContainerdSocketDir(), + "containerd-cni-bin-dir": s.CNIBinDir(), + } + + for filename, content := range m { + if err := utils.WriteFile(filepath.Join(s.LockFilesDir(), filename), []byte(content), 0o600); err != nil { + return fmt.Errorf("failed to write %s: %w", filename, err) + } + } + return nil +} + func init() { images.Register(defaultPauseImage) } diff --git a/src/k8s/pkg/k8sd/setup/containerd_test.go b/src/k8s/pkg/k8sd/setup/containerd_test.go index 904bd11ce..2c4460a2b 100644 --- a/src/k8s/pkg/k8sd/setup/containerd_test.go +++ b/src/k8s/pkg/k8sd/setup/containerd_test.go @@ -121,10 +121,19 @@ func TestContainerd(t *testing.T) { }) }) - t.Run("Lockfile", func(t *testing.T) { + t.Run("Lockfiles", func(t *testing.T) { g := NewWithT(t) - b, err := os.ReadFile(filepath.Join(s.LockFilesDir(), "containerd-socket-path")) - g.Expect(err).To(Not(HaveOccurred())) - g.Expect(string(b)).To(Equal(s.ContainerdSocketDir())) + m := map[string]string{ + "containerd-socket-path": s.ContainerdSocketDir(), + "containerd-config-dir": s.ContainerdConfigDir(), + "containerd-root-dir": s.ContainerdSocketDir(), + "containerd-cni-bin-dir": s.CNIBinDir(), + } + for filename, content := range m { + + b, err := os.ReadFile(filepath.Join(s.LockFilesDir(), filename)) + g.Expect(err).To(Not(HaveOccurred())) + g.Expect(string(b)).To(Equal(content)) + } }) }