Re-enable label selector

This commit is contained in:
Arda Güçlü 2023-02-07 11:11:05 +03:00
parent 6e8a1beda7
commit 6c346e6cc9
4 changed files with 70 additions and 65 deletions

View File

@ -1,35 +0,0 @@
apiVersion: v1
kind: Pod
metadata:
name: a
namespace: nsbprune
labels:
prune-group: "true"
spec:
containers:
- name: kubernetes-pause
image: registry.k8s.io/pause:3.9
---
apiVersion: v1
kind: Pod
metadata:
name: b
namespace: nsbprune
labels:
prune-group: "true"
spec:
containers:
- name: kubernetes-pause
image: registry.k8s.io/pause:3.9
---
apiVersion: v1
kind: Pod
metadata:
name: c
namespace: nsbprune
labels:
prune-group: "false"
spec:
containers:
- name: kubernetes-pause
image: registry.k8s.io/pause:3.9

View File

@ -1,23 +0,0 @@
apiVersion: v1
kind: Pod
metadata:
name: a
namespace: nsbprune
labels:
prune-group: "true"
spec:
containers:
- name: kubernetes-pause
image: registry.k8s.io/pause:3.9
---
apiVersion: v1
kind: Pod
metadata:
name: c
namespace: nsbprune
labels:
prune-group: "false"
spec:
containers:
- name: kubernetes-pause
image: registry.k8s.io/pause:3.9

View File

@ -47,7 +47,7 @@ func newPruner(dc dynamic.Interface, m meta.RESTMapper, r []prune.Resource, sele
dynamicClient: dc, dynamicClient: dc,
mapper: m, mapper: m,
resources: r, resources: r,
//labelSelector: selector, labelSelector: selector,
} }
} }

View File

@ -113,25 +113,88 @@ run_kubectl_diff_tests() {
# Cleanup # Cleanup
kubectl delete -f hack/testdata/pod.yaml kubectl delete -f hack/testdata/pod.yaml
kubectl delete -f hack/testdata/prune/b.yaml kubectl delete -f hack/testdata/prune/b.yaml
kubectl delete namespace nsb
## kubectl diff --prune with label selector ## kubectl diff --prune with label selector
kubectl create ns nsbprune kubectl create ns nsbprune
kubectl apply --namespace nsbprune -f hack/testdata/prune/a_selector.yaml kubectl apply --namespace nsbprune -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
name: a
namespace: nsbprune
labels:
prune-group: "true"
spec:
containers:
- name: kubernetes-pause
image: registry.k8s.io/pause:3.9
---
apiVersion: v1
kind: Pod
metadata:
name: b
namespace: nsbprune
labels:
prune-group: "true"
spec:
containers:
- name: kubernetes-pause
image: registry.k8s.io/pause:3.9
---
apiVersion: v1
kind: Pod
metadata:
name: c
namespace: nsbprune
labels:
prune-group: "false"
spec:
containers:
- name: kubernetes-pause
image: registry.k8s.io/pause:3.9
EOF
kube::test::get_object_assert 'pods a -n nsbprune' "{{${id_field:?}}}" 'a' kube::test::get_object_assert 'pods a -n nsbprune' "{{${id_field:?}}}" 'a'
kube::test::get_object_assert 'pods b -n nsbprune' "{{${id_field:?}}}" 'b' kube::test::get_object_assert 'pods b -n nsbprune' "{{${id_field:?}}}" 'b'
kube::test::get_object_assert 'pods c -n nsbprune' "{{${id_field:?}}}" 'c' kube::test::get_object_assert 'pods c -n nsbprune' "{{${id_field:?}}}" 'c'
# Make sure that kubectl diff does not return neither pod 'b' nor pod 'c' without prune flag # Make sure that kubectl diff does not return either pod 'b' or pod 'c' without prune flag
output_message=$(kubectl diff -l prune-group=true -f hack/testdata/prune/a_selector_prune.yaml) PRUNE=$(cat <<EOF
apiVersion: v1
kind: Pod
metadata:
name: a
namespace: nsbprune
labels:
prune-group: "true"
spec:
containers:
- name: kubernetes-pause
image: registry.k8s.io/pause:3.9
---
apiVersion: v1
kind: Pod
metadata:
name: c
namespace: nsbprune
labels:
prune-group: "false"
spec:
containers:
- name: kubernetes-pause
image: registry.k8s.io/pause:3.9
EOF
)
output_message=$(echo "${PRUNE}" | kubectl diff -l prune-group=true -f -)
kube::test::if_has_not_string "${output_message}" "name: b" kube::test::if_has_not_string "${output_message}" "name: b"
kube::test::if_has_not_string "${output_message}" "name: c" kube::test::if_has_not_string "${output_message}" "name: c"
# the exist code for diff is 1 because pod name 'b' is found in the given label selector but not 'c' # the exit code for diff is 1 because pod 'b' is found in the given label selector but not 'c'
output_message=$(kubectl diff --prune -l prune-group=true -f hack/testdata/prune/a_selector_prune.yaml || test $? -eq 1) output_message=$(echo "${PRUNE}" | kubectl diff --prune -l prune-group=true -f - || test $? -eq 1)
# pod 'b' should be in output, it is pruned. On the other hand, 'c' should not be, it's label selector is different # pod 'b' should be in output, it is pruned. On the other hand, 'c' should not be, it's label selector is different
kube::test::if_has_string "${output_message}" 'name: b' kube::test::if_has_string "${output_message}" 'name: b'
kube::test::if_has_not_string "${output_message}" "name: c" kube::test::if_has_not_string "${output_message}" "name: c"
# Cleanup # Cleanup
kubectl delete -f hack/testdata/prune/a_selector.yaml kubectl delete namespace nsbprune
set +o nounset set +o nounset