Skip to content

Commit 45c72bd

Browse files
committed
ex: dockergc: add dry-run mode
1 parent 329b4d0 commit 45c72bd

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

contrib/completions/bash/oc

+2
Original file line numberDiff line numberDiff line change
@@ -12056,6 +12056,8 @@ _oc_ex_dockergc()
1205612056
flags_with_completion=()
1205712057
flags_completion=()
1205812058

12059+
flags+=("--dry-run")
12060+
local_nonpersistent_flags+=("--dry-run")
1205912061
flags+=("--image-gc-high-threshold=")
1206012062
local_nonpersistent_flags+=("--image-gc-high-threshold=")
1206112063
flags+=("--image-gc-low-threshold=")

contrib/completions/zsh/oc

+2
Original file line numberDiff line numberDiff line change
@@ -12198,6 +12198,8 @@ _oc_ex_dockergc()
1219812198
flags_with_completion=()
1219912199
flags_completion=()
1220012200

12201+
flags+=("--dry-run")
12202+
local_nonpersistent_flags+=("--dry-run")
1220112203
flags+=("--image-gc-high-threshold=")
1220212204
local_nonpersistent_flags+=("--image-gc-high-threshold=")
1220312205
flags+=("--image-gc-low-threshold=")

pkg/oc/experimental/dockergc/dockergc.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ var (
3535

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

6769
func NewCmdDockerGCConfig(f *clientcmd.Factory, parentName, name string, out, errout io.Writer) *cobra.Command {
6870
options := &dockerGCConfigCmdOptions{
71+
DryRun: false,
6972
MinimumGCAge: DefaultMinimumGCAge,
7073
ImageGCHighThresholdPercent: DefaultImageGCHighThresholdPercent,
7174
ImageGCLowThresholdPercent: DefaultImageGCLowThresholdPercent,
@@ -87,6 +90,7 @@ func NewCmdDockerGCConfig(f *clientcmd.Factory, parentName, name string, out, er
8790
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'.")
8891
cmd.Flags().Int32Var(&options.ImageGCHighThresholdPercent, "image-gc-high-threshold", options.ImageGCHighThresholdPercent, "The percent of disk usage after which image garbage collection is always run.")
8992
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.")
93+
cmd.Flags().BoolVar(&options.DryRun, "dry-run", options.DryRun, "Run in single-pass mode with no effect.")
9094

9195
return cmd
9296
}
@@ -183,7 +187,10 @@ func doGarbageCollection(ctx context.Context, client *dockerapi.Client, options
183187
break
184188
}
185189
glog.Infof("removing container %v (size: %v, age: %v)", c.ID, c.SizeRw, age)
186-
err := client.ContainerRemove(ctx, c.ID, dockertypes.ContainerRemoveOptions{RemoveVolumes: true})
190+
var err error
191+
if !options.DryRun {
192+
err = client.ContainerRemove(ctx, c.ID, dockertypes.ContainerRemoveOptions{RemoveVolumes: true})
193+
}
187194
if err != nil {
188195
glog.Infof("unable to remove container: %v", err)
189196
} else {
@@ -200,6 +207,7 @@ func doGarbageCollection(ctx context.Context, client *dockerapi.Client, options
200207
return err
201208
}
202209
sort.Sort(oldestImagesFirst(images))
210+
glog.Infof("%d images found", len(images))
203211
for _, i := range images {
204212
if freedBytes > attemptToFreeBytes {
205213
glog.Infof("usage is below low threshold, freed %vMB", bytesToMB(freedBytes))
@@ -220,20 +228,27 @@ func doGarbageCollection(ctx context.Context, client *dockerapi.Client, options
220228
break
221229
}
222230
glog.Infof("removing image %v (size: %v, age: %v)", i.ID, i.Size, age)
223-
_, err := client.ImageRemove(ctx, i.ID, dockertypes.ImageRemoveOptions{PruneChildren: true})
231+
var err error
232+
if !options.DryRun {
233+
_, err = client.ImageRemove(ctx, i.ID, dockertypes.ImageRemoveOptions{PruneChildren: true})
234+
}
224235
if err != nil {
225236
glog.Infof("unable to remove image: %v", err)
226237
} else {
227238
freedBytes += i.Size
228239
}
229240
}
241+
glog.Infof("unable to get below low threshold, %vMB freed", bytesToMB(freedBytes))
230242

231243
return nil
232244
}
233245

234246
// Run runs the dockergc command.
235247
func Run(f *clientcmd.Factory, options *dockerGCConfigCmdOptions, cmd *cobra.Command, args []string) error {
236248
glog.Infof("docker build garbage collection daemon")
249+
if options.DryRun {
250+
glog.Infof("Running in dry-run mode")
251+
}
237252
glog.Infof("MinimumGCAge: %v, ImageGCHighThresholdPercent: %v, ImageGCLowThresholdPercent: %v", options.MinimumGCAge, options.ImageGCHighThresholdPercent, options.ImageGCLowThresholdPercent)
238253
client, err := dockerapi.NewEnvClient()
239254
if err != nil {
@@ -259,7 +274,9 @@ func Run(f *clientcmd.Factory, options *dockerGCConfigCmdOptions, cmd *cobra.Com
259274
if err != nil {
260275
return err
261276
}
277+
if options.DryRun {
278+
return nil
279+
}
262280
<-time.After(time.Minute)
263-
return nil
264281
}
265282
}

0 commit comments

Comments
 (0)