Skip to content

Commit

Permalink
fix: images import now unpacks layers, issue with cleanup images (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdand-weka authored Feb 3, 2024
1 parent 5cb6d1c commit 03f2b78
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
20 changes: 16 additions & 4 deletions internal/local/cleanup/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
"golang.org/x/exp/slices"
)

const kubernetesImagesNamespace = "k8s.io"
const (
kubernetesImagesNamespace = "k8s.io"
k3sSocket = "/run/k3s/containerd/containerd.sock"
)

type ImagesArgs struct {
Force bool
Expand All @@ -21,7 +24,7 @@ type ImagesArgs struct {
func Images(ctx context.Context, args ImagesArgs) error {
logger.Info().Msg("Removing unused images")

client, err := containerd.New("/run/k3s/containerd/containerd.sock")
client, err := containerd.New(k3sSocket)
if err != nil {
return err
}
Expand All @@ -37,6 +40,8 @@ func Images(ctx context.Context, args ImagesArgs) error {
return err
}

var deleted int

for _, img := range imgs {
if inUse[img] {
logger.Debug().Str("image", img).Msg("Skipping in-use image")
Expand All @@ -50,14 +55,21 @@ func Images(ctx context.Context, args ImagesArgs) error {

logger.Info().Str("image", img).Msg("Removing image")

deleted++

if !args.DryRun {
err = client.ImageService().Delete(ctx, img)
ctx = namespaces.WithNamespace(ctx, kubernetesImagesNamespace)
err = client.ImageService().Delete(ctx, img, images.SynchronousDelete())
if err != nil {
return err
}
}
}

if deleted == 0 {
logger.Info().Msg("No images to remove")
}

return nil
}

Expand All @@ -71,7 +83,7 @@ func getImages(ctx context.Context, client *containerd.Client) ([]string, error)

for _, img := range list {
logger.Debug().Interface("image", img).Msg("Got image")
imgs = append(imgs, img.Target.Annotations[images.AnnotationImageName])
imgs = append(imgs, img.Name)
}

slices.Sort(imgs)
Expand Down
16 changes: 14 additions & 2 deletions internal/local/k3s/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,23 @@ func ImportImages(ctx context.Context, imgs map[string]string, failFast bool) er
}

logger.Info().Msg("Importing image")
_, err = client.Import(ctx, reader,
imported, err := client.Import(ctx, reader,
containerd.WithImportPlatform(platforms.Any(platform)),
)
if err != nil {
return err
}

return err
for _, img := range imported {
logger.Debug().Msg("Unpacking image")
err := containerd.NewImageWithPlatform(client, img, platforms.Any(platform)).
Unpack(ctx, "")
if err != nil {
return fmt.Errorf("unpack image: %w", err)
}
}

return nil
})
if err != nil {
logger.Warn().Err(err).Msg("Failed to import image")
Expand Down

0 comments on commit 03f2b78

Please sign in to comment.