Skip to content

Commit

Permalink
Cleans up all containerd-related resources on snap removal
Browse files Browse the repository at this point in the history
Currently, when removing the snap, the /var/run/containerd folder is not
properly removed, as it is a folder. This fixes this issue.

Additionally removes other containerd-related folders: /etc/containerd
and /var/lib/containerd.

We're also removing /opt/cni/bin on snap removal, which is created when
bootstrapping the node. As we're removing the k8s snap, we no longer
need this folder either.
  • Loading branch information
claudiubelu committed Nov 21, 2024
1 parent 8d20f34 commit a3eb526
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
8 changes: 5 additions & 3 deletions k8s/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 19 additions & 3 deletions src/k8s/pkg/k8sd/setup/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
17 changes: 13 additions & 4 deletions src/k8s/pkg/k8sd/setup/containerd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
})
}

0 comments on commit a3eb526

Please sign in to comment.