Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kubectl --prune -n <namespace> doesn't visit the CLI-specified namespace. #85357

Closed
kinghajj opened this issue Nov 15, 2019 · 2 comments · Fixed by #85613
Closed

kubectl --prune -n <namespace> doesn't visit the CLI-specified namespace. #85357

kinghajj opened this issue Nov 15, 2019 · 2 comments · Fixed by #85613
Labels
kind/bug Categorizes issue or PR as related to a bug. sig/cli Categorizes an issue or PR as relevant to SIG CLI.

Comments

@kinghajj
Copy link

What happened:

While testing out the --prune flag, I created a manifest with the following contents:

apiVersion: v1
kind: Namespace
metadata:
  labels:
    app: woot
    env: woot
  name: woot
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: woot
    env: woot
  name: woot
  namespace: woot
spec:
  replicas: 1
  selector:
    matchLabels:
      app: woot
      env: woot
  template:
    metadata:
      labels:
        app: woot
        env: woot
    spec:
      containers:
      - command:
        - bash
        - -c
        - while true; do date; sleep 1; done
        image: bitnami/minideb:buster
        name: woot

I ran kubectl -n woot -R -f ./manifests --prune -l env=woot, and the resources appeared on my minikube cluster:

NAME                    READY   STATUS    RESTARTS   AGE
woot-5dbccd5689-h78pg   1/1     Running   0          9s

Then, I removed the deployment from the manifest:

apiVersion: v1
kind: Namespace
metadata:
  labels:
    app: woot
    env: woot
  name: woot

And re-ran the same command, kubectl -n woot -R -f ./manifests --prune -l env=woot. To my surprise, however, the deployment wasn't removed; the only output from kubectl was namespace/woot unchanged.

What you expected to happen:

I expected the pruner to find the deployment with the matching label, env=woot, and prune it.

How to reproduce it (as minimally and precisely as possible):

I believe my example above is minimal and easily reproducible.

Anything else we need to know?:

This small change to the code for kubectl apply resolves the issue:

diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go
index 915f055ea43..67dde192a04 100644
--- a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go
+++ b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go
@@ -369,6 +369,10 @@ func (o *ApplyOptions) Run() error {
        visitedUids := sets.NewString()
        visitedNamespaces := sets.NewString()
 
+       if o.Namespace != metav1.NamespaceNone {
+               visitedNamespaces.Insert(o.Namespace)
+       }
+
        var objs []runtime.Object
 
        count := 0

Environment:

  • Kubernetes version (use kubectl version): v1.16.3
  • Cloud provider or hardware configuration: N/A
  • OS (e.g: cat /etc/os-release): N/A
  • Kernel (e.g. uname -a): N/A
  • Install tools: N/A
  • Network plugin and version (if this is a network-related bug): N/A
  • Others: N/A
@kinghajj kinghajj added the kind/bug Categorizes issue or PR as related to a bug. label Nov 15, 2019
@k8s-ci-robot k8s-ci-robot added the needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. label Nov 15, 2019
@kinghajj
Copy link
Author

/sig cli

@k8s-ci-robot k8s-ci-robot added sig/cli Categorizes an issue or PR as relevant to SIG CLI. and removed needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Nov 15, 2019
@MartinKaburu
Copy link
Contributor

MartinKaburu commented Nov 25, 2019

/assign @MartinKaburu
cc @soltysh @kinghajj @seans3 @mengqiy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. sig/cli Categorizes an issue or PR as relevant to SIG CLI.
Projects
None yet
3 participants