Skip to content

Commit

Permalink
ex: dockergc: add dry-run mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sjenning committed Dec 11, 2017
1 parent 329b4d0 commit 45c72bd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions contrib/completions/bash/oc
Original file line number Diff line number Diff line change
Expand Up @@ -12056,6 +12056,8 @@ _oc_ex_dockergc()
flags_with_completion=()
flags_completion=()

flags+=("--dry-run")
local_nonpersistent_flags+=("--dry-run")
flags+=("--image-gc-high-threshold=")
local_nonpersistent_flags+=("--image-gc-high-threshold=")
flags+=("--image-gc-low-threshold=")
Expand Down
2 changes: 2 additions & 0 deletions contrib/completions/zsh/oc
Original file line number Diff line number Diff line change
Expand Up @@ -12198,6 +12198,8 @@ _oc_ex_dockergc()
flags_with_completion=()
flags_completion=()

flags+=("--dry-run")
local_nonpersistent_flags+=("--dry-run")
flags+=("--image-gc-high-threshold=")
local_nonpersistent_flags+=("--image-gc-high-threshold=")
flags+=("--image-gc-low-threshold=")
Expand Down
23 changes: 20 additions & 3 deletions pkg/oc/experimental/dockergc/dockergc.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ var (

// DockerGCConfigCmdOptions are options supported by the dockergc admin command.
type dockerGCConfigCmdOptions struct {
// DryRun is true if the command was invoked with --dry-run=true
DryRun bool
// MinimumGCAge is the minimum age for a container or unused image before
// it is garbage collected.
MinimumGCAge metav1.Duration
Expand Down Expand Up @@ -66,6 +68,7 @@ var (

func NewCmdDockerGCConfig(f *clientcmd.Factory, parentName, name string, out, errout io.Writer) *cobra.Command {
options := &dockerGCConfigCmdOptions{
DryRun: false,
MinimumGCAge: DefaultMinimumGCAge,
ImageGCHighThresholdPercent: DefaultImageGCHighThresholdPercent,
ImageGCLowThresholdPercent: DefaultImageGCLowThresholdPercent,
Expand All @@ -87,6 +90,7 @@ func NewCmdDockerGCConfig(f *clientcmd.Factory, parentName, name string, out, er
cmd.Flags().DurationVar(&options.MinimumGCAge.Duration, "minimum-ttl-duration", options.MinimumGCAge.Duration, "Minimum age for a container or unused image before it is garbage collected. Examples: '300ms', '10s' or '2h45m'.")
cmd.Flags().Int32Var(&options.ImageGCHighThresholdPercent, "image-gc-high-threshold", options.ImageGCHighThresholdPercent, "The percent of disk usage after which image garbage collection is always run.")
cmd.Flags().Int32Var(&options.ImageGCLowThresholdPercent, "image-gc-low-threshold", options.ImageGCLowThresholdPercent, "The percent of disk usage before which image garbage collection is never run. Lowest disk usage to garbage collect to.")
cmd.Flags().BoolVar(&options.DryRun, "dry-run", options.DryRun, "Run in single-pass mode with no effect.")

return cmd
}
Expand Down Expand Up @@ -183,7 +187,10 @@ func doGarbageCollection(ctx context.Context, client *dockerapi.Client, options
break
}
glog.Infof("removing container %v (size: %v, age: %v)", c.ID, c.SizeRw, age)
err := client.ContainerRemove(ctx, c.ID, dockertypes.ContainerRemoveOptions{RemoveVolumes: true})
var err error
if !options.DryRun {
err = client.ContainerRemove(ctx, c.ID, dockertypes.ContainerRemoveOptions{RemoveVolumes: true})
}
if err != nil {
glog.Infof("unable to remove container: %v", err)
} else {
Expand All @@ -200,6 +207,7 @@ func doGarbageCollection(ctx context.Context, client *dockerapi.Client, options
return err
}
sort.Sort(oldestImagesFirst(images))
glog.Infof("%d images found", len(images))
for _, i := range images {
if freedBytes > attemptToFreeBytes {
glog.Infof("usage is below low threshold, freed %vMB", bytesToMB(freedBytes))
Expand All @@ -220,20 +228,27 @@ func doGarbageCollection(ctx context.Context, client *dockerapi.Client, options
break
}
glog.Infof("removing image %v (size: %v, age: %v)", i.ID, i.Size, age)
_, err := client.ImageRemove(ctx, i.ID, dockertypes.ImageRemoveOptions{PruneChildren: true})
var err error
if !options.DryRun {
_, err = client.ImageRemove(ctx, i.ID, dockertypes.ImageRemoveOptions{PruneChildren: true})
}
if err != nil {
glog.Infof("unable to remove image: %v", err)
} else {
freedBytes += i.Size
}
}
glog.Infof("unable to get below low threshold, %vMB freed", bytesToMB(freedBytes))

return nil
}

// Run runs the dockergc command.
func Run(f *clientcmd.Factory, options *dockerGCConfigCmdOptions, cmd *cobra.Command, args []string) error {
glog.Infof("docker build garbage collection daemon")
if options.DryRun {
glog.Infof("Running in dry-run mode")
}
glog.Infof("MinimumGCAge: %v, ImageGCHighThresholdPercent: %v, ImageGCLowThresholdPercent: %v", options.MinimumGCAge, options.ImageGCHighThresholdPercent, options.ImageGCLowThresholdPercent)
client, err := dockerapi.NewEnvClient()
if err != nil {
Expand All @@ -259,7 +274,9 @@ func Run(f *clientcmd.Factory, options *dockerGCConfigCmdOptions, cmd *cobra.Com
if err != nil {
return err
}
if options.DryRun {
return nil
}
<-time.After(time.Minute)
return nil
}
}

0 comments on commit 45c72bd

Please sign in to comment.