Skip to content

Commit 7af3b01

Browse files
author
Tatsuhiro Tsujikawa
committed
Restore the ability to kubectl apply --prune without -n flag
Before kubernetes#83084, `kubectl apply --prune` can prune resources in all namespaces specified in config files. After that PR got merged, only a single namespace is considered for pruning. It is OK if namespace is explicitly specified by --namespace option, but what the PR does is use the default namespace (or from kubeconfig) if not overridden by command line flag. That breaks the existing usage of `kubectl apply --prune` without --namespace option. If --namespace is not used, there is no error, and no one notices this issue unless they actually check that pruning happens. This issue also prevents resources in multiple namespaces in config file from being pruned. kubectl 1.16 does not have this bug. Let's see the difference between kubectl 1.16 and kubectl 1.17. Suppose the following config file: ```yaml apiVersion: v1 kind: ConfigMap metadata: creationTimestamp: null name: foo namespace: a labels: pl: foo data: foo: bar --- apiVersion: v1 kind: ConfigMap metadata: creationTimestamp: null name: bar namespace: a labels: pl: foo data: foo: bar ``` Apply it with `kubectl apply -f file`. Then comment out ConfigMap foo in this file. kubectl 1.16 prunes ConfigMap foo with the following command: $ kubectl-1.16 apply -f file -l pl=foo --prune configmap/bar configured configmap/foo pruned But kubectl 1.17 does not prune ConfigMap foo with the same command: $ kubectl-1.17 apply -f file -l pl=foo --prune configmap/bar configured With this patch, kubectl once again can prune the resource as before.
1 parent 4bdb72e commit 7af3b01

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/apply/prune.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ func (p *pruner) pruneAll(o *ApplyOptions) error {
7777
}
7878

7979
for n := range p.visitedNamespaces {
80-
if len(o.Namespace) != 0 && n != o.Namespace {
81-
continue
82-
}
8380
for _, m := range namespacedRESTMappings {
8481
if err := p.prune(n, m); err != nil {
8582
return fmt.Errorf("error pruning namespaced object %v: %v", m.GroupVersionKind, err)

test/cmd/apply.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,30 @@ __EOF__
223223
# cleanup
224224
kubectl delete svc prune-svc 2>&1 "${kube_flags[@]:?}"
225225

226+
## kubectl apply --prune can prune resources not in the defaulted namespace
227+
# Pre-Condition: namespace nsb exists; no POD exists
228+
kubectl create ns nsb
229+
kube::test::get_object_assert pods "{{range.items}}{{${id_field:?}}}:{{end}}" ''
230+
# apply a into namespace nsb
231+
kubectl apply --namespace nsb -f hack/testdata/prune/a.yaml "${kube_flags[@]:?}"
232+
kube::test::get_object_assert 'pods a -n nsb' "{{${id_field:?}}}" 'a'
233+
# apply b with namespace
234+
kubectl apply --namespace nsb -f hack/testdata/prune/b.yaml "${kube_flags[@]:?}"
235+
kube::test::get_object_assert 'pods b -n nsb' "{{${id_field:?}}}" 'b'
236+
# apply --prune must prune a
237+
kubectl apply --prune --all -f hack/testdata/prune/b.yaml
238+
# check wrong pod doesn't exist
239+
output_message=$(! kubectl get pods a -n nsb 2>&1 "${kube_flags[@]:?}")
240+
kube::test::if_has_string "${output_message}" 'pods "a" not found'
241+
# check right pod exists
242+
kube::test::get_object_assert 'pods b -n nsb' "{{${id_field:?}}}" 'b'
243+
244+
# cleanup
245+
kubectl delete ns nsb
246+
247+
## kubectl apply -n must fail if input file contains namespace other than the one given in -n
248+
output_message=$(! kubectl apply -n foo -f hack/testdata/prune/b.yaml 2>&1 "${kube_flags[@]:?}")
249+
kube::test::if_has_string "${output_message}" 'the namespace from the provided object "nsb" does not match the namespace "foo".'
226250

227251
## kubectl apply -f some.yml --force
228252
# Pre-condition: no service exists

0 commit comments

Comments
 (0)