Skip to content

Commit c08ae87

Browse files
authored
Merge pull request kubernetes#90016 from seans3/automated-cherry-pick-of-#89551-upstream-release-1.18-1586456731
Automated cherry pick of kubernetes#89551 upstream release 1.18
2 parents f76728f + 4e644d0 commit c08ae87

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
@@ -240,6 +240,30 @@ __EOF__
240240
# cleanup
241241
kubectl delete svc prune-svc 2>&1 "${kube_flags[@]:?}"
242242

243+
## kubectl apply --prune can prune resources not in the defaulted namespace
244+
# Pre-Condition: namespace nsb exists; no POD exists
245+
kubectl create ns nsb
246+
kube::test::get_object_assert pods "{{range.items}}{{${id_field:?}}}:{{end}}" ''
247+
# apply a into namespace nsb
248+
kubectl apply --namespace nsb -f hack/testdata/prune/a.yaml "${kube_flags[@]:?}"
249+
kube::test::get_object_assert 'pods a -n nsb' "{{${id_field:?}}}" 'a'
250+
# apply b with namespace
251+
kubectl apply --namespace nsb -f hack/testdata/prune/b.yaml "${kube_flags[@]:?}"
252+
kube::test::get_object_assert 'pods b -n nsb' "{{${id_field:?}}}" 'b'
253+
# apply --prune must prune a
254+
kubectl apply --prune --all -f hack/testdata/prune/b.yaml
255+
# check wrong pod doesn't exist
256+
output_message=$(! kubectl get pods a -n nsb 2>&1 "${kube_flags[@]:?}")
257+
kube::test::if_has_string "${output_message}" 'pods "a" not found'
258+
# check right pod exists
259+
kube::test::get_object_assert 'pods b -n nsb' "{{${id_field:?}}}" 'b'
260+
261+
# cleanup
262+
kubectl delete ns nsb
263+
264+
## kubectl apply -n must fail if input file contains namespace other than the one given in -n
265+
output_message=$(! kubectl apply -n foo -f hack/testdata/prune/b.yaml 2>&1 "${kube_flags[@]:?}")
266+
kube::test::if_has_string "${output_message}" 'the namespace from the provided object "nsb" does not match the namespace "foo".'
243267

244268
## kubectl apply -f some.yml --force
245269
# Pre-condition: no service exists

0 commit comments

Comments
 (0)