Skip to content

Commit bc5f981

Browse files
committed
Bug 1371511 - add namespace awareness to oadm prune commands
1 parent 949be1e commit bc5f981

File tree

5 files changed

+70
-12
lines changed

5 files changed

+70
-12
lines changed

pkg/cmd/admin/prune/builds.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ func (o *PruneBuildsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command,
8484
return kcmdutil.UsageError(cmd, "no arguments are allowed to this command")
8585
}
8686

87+
namespace := kapi.NamespaceAll
88+
if cmd.Flags().Lookup("namespace").Changed {
89+
var err error
90+
namespace, _, err = f.DefaultNamespace()
91+
if err != nil {
92+
return err
93+
}
94+
}
8795
o.Out = out
8896

8997
osClient, _, err := f.Clients()
@@ -92,7 +100,7 @@ func (o *PruneBuildsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command,
92100
}
93101
o.Client = osClient
94102

95-
buildConfigList, err := osClient.BuildConfigs(kapi.NamespaceAll).List(kapi.ListOptions{})
103+
buildConfigList, err := osClient.BuildConfigs(namespace).List(kapi.ListOptions{})
96104
if err != nil {
97105
return err
98106
}
@@ -101,7 +109,7 @@ func (o *PruneBuildsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command,
101109
buildConfigs = append(buildConfigs, &buildConfigList.Items[i])
102110
}
103111

104-
buildList, err := osClient.Builds(kapi.NamespaceAll).List(kapi.ListOptions{})
112+
buildList, err := osClient.Builds(namespace).List(kapi.ListOptions{})
105113
if err != nil {
106114
return err
107115
}

pkg/cmd/admin/prune/deployments.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ func (o *PruneDeploymentsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Comm
8585
return kcmdutil.UsageError(cmd, "no arguments are allowed to this command")
8686
}
8787

88+
namespace := kapi.NamespaceAll
89+
if cmd.Flags().Lookup("namespace").Changed {
90+
var err error
91+
namespace, _, err = f.DefaultNamespace()
92+
if err != nil {
93+
return err
94+
}
95+
}
8896
o.Out = out
8997

9098
osClient, kClient, err := f.Clients()
@@ -93,7 +101,7 @@ func (o *PruneDeploymentsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Comm
93101
}
94102
o.Client = kClient
95103

96-
deploymentConfigList, err := osClient.DeploymentConfigs(kapi.NamespaceAll).List(kapi.ListOptions{})
104+
deploymentConfigList, err := osClient.DeploymentConfigs(namespace).List(kapi.ListOptions{})
97105
if err != nil {
98106
return err
99107
}
@@ -102,7 +110,7 @@ func (o *PruneDeploymentsOptions) Complete(f *clientcmd.Factory, cmd *cobra.Comm
102110
deploymentConfigs = append(deploymentConfigs, &deploymentConfigList.Items[i])
103111
}
104112

105-
deploymentList, err := kClient.ReplicationControllers(kapi.NamespaceAll).List(kapi.ListOptions{})
113+
deploymentList, err := kClient.ReplicationControllers(namespace).List(kapi.ListOptions{})
106114
if err != nil {
107115
return err
108116
}

pkg/cmd/admin/prune/images.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,14 @@ func (o *PruneImagesOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command,
124124
if !cmd.Flags().Lookup("prune-over-size-limit").Changed {
125125
o.PruneOverSizeLimit = nil
126126
}
127-
127+
namespace := kapi.NamespaceAll
128+
if cmd.Flags().Lookup("namespace").Changed {
129+
var err error
130+
namespace, _, err = f.DefaultNamespace()
131+
if err != nil {
132+
return err
133+
}
134+
}
128135
o.Out = out
129136

130137
osClient, kClient, registryClient, err := getClients(f, o.CABundle)
@@ -138,41 +145,41 @@ func (o *PruneImagesOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command,
138145
return err
139146
}
140147

141-
allStreams, err := osClient.ImageStreams(kapi.NamespaceAll).List(kapi.ListOptions{})
148+
allStreams, err := osClient.ImageStreams(namespace).List(kapi.ListOptions{})
142149
if err != nil {
143150
return err
144151
}
145152

146-
allPods, err := kClient.Pods(kapi.NamespaceAll).List(kapi.ListOptions{})
153+
allPods, err := kClient.Pods(namespace).List(kapi.ListOptions{})
147154
if err != nil {
148155
return err
149156
}
150157

151-
allRCs, err := kClient.ReplicationControllers(kapi.NamespaceAll).List(kapi.ListOptions{})
158+
allRCs, err := kClient.ReplicationControllers(namespace).List(kapi.ListOptions{})
152159
if err != nil {
153160
return err
154161
}
155162

156-
allBCs, err := osClient.BuildConfigs(kapi.NamespaceAll).List(kapi.ListOptions{})
163+
allBCs, err := osClient.BuildConfigs(namespace).List(kapi.ListOptions{})
157164
// We need to tolerate 'not found' errors for buildConfigs since they may be disabled in Atomic
158165
err = oserrors.TolerateNotFoundError(err)
159166
if err != nil {
160167
return err
161168
}
162169

163-
allBuilds, err := osClient.Builds(kapi.NamespaceAll).List(kapi.ListOptions{})
170+
allBuilds, err := osClient.Builds(namespace).List(kapi.ListOptions{})
164171
// We need to tolerate 'not found' errors for builds since they may be disabled in Atomic
165172
err = oserrors.TolerateNotFoundError(err)
166173
if err != nil {
167174
return err
168175
}
169176

170-
allDCs, err := osClient.DeploymentConfigs(kapi.NamespaceAll).List(kapi.ListOptions{})
177+
allDCs, err := osClient.DeploymentConfigs(namespace).List(kapi.ListOptions{})
171178
if err != nil {
172179
return err
173180
}
174181

175-
limitRangesList, err := kClient.LimitRanges(kapi.NamespaceAll).List(kapi.ListOptions{})
182+
limitRangesList, err := kClient.LimitRanges(namespace).List(kapi.ListOptions{})
176183
if err != nil {
177184
return err
178185
}
@@ -203,6 +210,9 @@ func (o *PruneImagesOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command,
203210
RegistryClient: registryClient,
204211
RegistryURL: o.RegistryUrlOverride,
205212
}
213+
if namespace != kapi.NamespaceAll {
214+
options.Namespace = namespace
215+
}
206216

207217
o.Pruner = prune.NewPruner(options)
208218

pkg/image/prune/prune.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type pruneAlgorithm struct {
5454
keepYoungerThan time.Duration
5555
keepTagRevisions int
5656
pruneOverSizeLimit bool
57+
namespace string
5758
}
5859

5960
// ImageDeleter knows how to remove images from OpenShift.
@@ -102,6 +103,8 @@ type PrunerOptions struct {
102103
// PruneOverSizeLimit indicates that images exceeding defined limits (openshift.io/Image)
103104
// will be considered as candidates for pruning.
104105
PruneOverSizeLimit *bool
106+
// Namespace to be pruned, if specified it should never remove Images.
107+
Namespace string
105108
// Images is the entire list of images in OpenShift. An image must be in this
106109
// list to be a candidate for pruning.
107110
Images *imageapi.ImageList
@@ -256,6 +259,7 @@ func NewPruner(options PrunerOptions) Pruner {
256259
if options.PruneOverSizeLimit != nil {
257260
algorithm.pruneOverSizeLimit = *options.PruneOverSizeLimit
258261
}
262+
algorithm.namespace = options.Namespace
259263

260264
addImagesToGraph(g, options.Images, algorithm)
261265
addImageStreamsToGraph(g, options.Streams, options.LimitRanges, algorithm)
@@ -825,6 +829,10 @@ func (p *pruner) Prune(
825829
errs := []error{}
826830

827831
errs = append(errs, pruneStreams(p.g, prunableImageNodes, streamPruner)...)
832+
// if namespace is specified prune only ImageStreams and nothing more
833+
if len(p.algorithm.namespace) > 0 {
834+
return kerrors.NewAggregate(errs)
835+
}
828836
errs = append(errs, pruneImageComponents(p.g, p.registryClient, registryURL, prunableComponents, layerLinkPruner)...)
829837
errs = append(errs, pruneBlobs(p.g, p.registryClient, registryURL, prunableComponents, blobPruner)...)
830838
errs = append(errs, pruneManifests(p.g, p.registryClient, registryURL, prunableImageNodes, manifestPruner)...)

pkg/image/prune/prune_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ func TestImagePruning(t *testing.T) {
418418
tests := map[string]struct {
419419
pruneOverSizeLimit *bool
420420
registryURLs []string
421+
namespace string
421422
images imageapi.ImageList
422423
pods kapi.PodList
423424
streams imageapi.ImageStreamList
@@ -870,6 +871,28 @@ func TestImagePruning(t *testing.T) {
870871
expectedImageDeletions: []string{},
871872
expectedStreamUpdates: []string{},
872873
},
874+
"image exceeding limits with namespace specified": {
875+
pruneOverSizeLimit: newBool(true),
876+
namespace: "foo",
877+
images: imageList(
878+
unmanagedImage("id", "otherregistry/foo/bar@id", false, "", ""),
879+
sizedImage("id2", registryURL+"/foo/bar@id2", 100, nil),
880+
sizedImage("id3", registryURL+"/foo/bar@id3", 200, nil),
881+
),
882+
streams: streamList(
883+
stream(registryURL, "foo", "bar", tags(
884+
tag("latest",
885+
tagEvent("id", "otherregistry/foo/bar@id"),
886+
tagEvent("id2", registryURL+"/foo/bar@id2"),
887+
tagEvent("id3", registryURL+"/foo/bar@id3"),
888+
),
889+
)),
890+
),
891+
limits: map[string][]*kapi.LimitRange{
892+
"foo": limitList(100, 200),
893+
},
894+
expectedStreamUpdates: []string{"foo/bar|id3"},
895+
},
873896
}
874897

875898
for name, test := range tests {
@@ -879,6 +902,7 @@ func TestImagePruning(t *testing.T) {
879902
}
880903

881904
options := PrunerOptions{
905+
Namespace: test.namespace,
882906
Images: &test.images,
883907
Streams: &test.streams,
884908
Pods: &test.pods,

0 commit comments

Comments
 (0)