Skip to content

Commit 84c57f9

Browse files
authored
Merge pull request #98 from thin-edge/feat-container-group-prune
feat(container-group): prune images during container-group finalize hook
2 parents 5b80c58 + 3f3156d commit 84c57f9

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

cli/container_group/finalize.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ Copyright © 2024 thin-edge.io <[email protected]>
44
package container_group
55

66
import (
7+
"context"
78
"log/slog"
89

10+
"github.com/docker/docker/api/types/filters"
11+
"github.com/docker/go-units"
912
"github.com/spf13/cobra"
1013
"github.com/thin-edge/tedge-container-plugin/pkg/cli"
14+
"github.com/thin-edge/tedge-container-plugin/pkg/container"
1115
)
1216

1317
func NewFinalizeCommand(ctx cli.Cli) *cobra.Command {
@@ -16,6 +20,25 @@ func NewFinalizeCommand(ctx cli.Cli) *cobra.Command {
1620
Short: "Finalize container install/remove operation",
1721
RunE: func(cmd *cobra.Command, args []string) error {
1822
slog.Info("Executing", "cmd", cmd.CalledAs(), "args", args)
23+
24+
pruneImages := ctx.GetBool("container.pruneImages")
25+
if !pruneImages {
26+
return nil
27+
}
28+
cli, err := container.NewContainerClient()
29+
if err != nil {
30+
return err
31+
}
32+
slog.Info("Pruning images")
33+
ctx := context.Background()
34+
resp, err := cli.Client.ImagesPrune(ctx, filters.Args{})
35+
if err != nil {
36+
return err
37+
}
38+
for _, image := range resp.ImagesDeleted {
39+
slog.Info("Deleted image.", "deleted", image.Deleted, "untagged", image.Untagged)
40+
}
41+
slog.Info("Reclaimed space.", "size", units.HumanSizeWithPrecision(float64(resp.SpaceReclaimed), 3))
1942
return nil
2043
},
2144
}

0 commit comments

Comments
 (0)