Skip to content

Commit 43d8b34

Browse files
committed
Add manually invalidate cache documentation into delete
When CRDs are deleted, discovery local cache is not invalidated. This brings about `resource not found` error when new CRD with same name is created with different fields(ie. changing scope from cluster-wide to namespaced). Because this already deleted CRD still stays in serverresources.json and kubectl tries to use it. This local cached files have 10 minutes TTL. After deletion, if user waits 10 minutes, files will be expired and deleted and there will be no errors. However, 10 minutes is a long time and cache needs to be invalidated after deletion occurs. This PR adds a document into delete command by noting that there might be a need to invalidate discovery cache when CRD is deleted. In addition to that this PR adds a test to catch this behavior.
1 parent cd117ab commit 43d8b34

File tree

7 files changed

+103
-1
lines changed

7 files changed

+103
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: test.com/v1
2+
kind: Example
3+
metadata:
4+
name: test
5+
spec:
6+
test: test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: apiextensions.k8s.io/v1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: examples.test.com
5+
spec:
6+
group: test.com
7+
scope: Cluster
8+
versions:
9+
- name: v1
10+
served: true
11+
storage: true
12+
schema:
13+
openAPIV3Schema:
14+
type: object
15+
properties:
16+
spec:
17+
type: object
18+
properties:
19+
test:
20+
type: string
21+
names:
22+
plural: examples
23+
singular: example
24+
kind: Example
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: test.com/v1
2+
kind: Example
3+
metadata:
4+
name: test
5+
namespace: default
6+
spec:
7+
test: test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: apiextensions.k8s.io/v1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: examples.test.com
5+
spec:
6+
group: test.com
7+
scope: Namespaced
8+
versions:
9+
- name: v1
10+
served: true
11+
storage: true
12+
schema:
13+
openAPIV3Schema:
14+
type: object
15+
properties:
16+
spec:
17+
type: object
18+
properties:
19+
test:
20+
type: string
21+
names:
22+
plural: examples
23+
singular: example
24+
kind: Example

staging/src/k8s.io/kubectl/pkg/cmd/delete/delete.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ var (
6969
7070
Note that the delete command does NOT do resource version checks, so if someone submits an
7171
update to a resource right when you submit a delete, their update will be lost along with the
72-
rest of the resource.`))
72+
rest of the resource.
73+
74+
After a CustomResourceDefinition is deleted, invalidation of discovery cache may take up
75+
to 10 minutes. If you don't want to wait, you might want to run "kubectl api-resources"
76+
to refresh the discovery cache.`))
7377

7478
deleteExample = templates.Examples(i18n.T(`
7579
# Delete a pod using the type and name specified in pod.json

test/cmd/discovery.sh

+30
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,36 @@ run_resource_aliasing_tests() {
9696
set +o errexit
9797
}
9898

99+
run_crd_deletion_recreation_tests() {
100+
set -o nounset
101+
set -o errexit
102+
103+
create_and_use_new_namespace
104+
kube::log::status "Testing resource creation, deletion, and re-creation"
105+
106+
output_message=$(kubectl apply -f hack/testdata/CRD/example-crd-1-cluster-scoped.yaml)
107+
kube::test::if_has_string "${output_message}" 'created'
108+
output_message=$(kubectl apply -f hack/testdata/CRD/example-crd-1-cluster-scoped-resource.yaml)
109+
kube::test::if_has_string "${output_message}" 'created'
110+
output_message=$(kubectl delete -f hack/testdata/CRD/example-crd-1-cluster-scoped.yaml)
111+
kube::test::if_has_string "${output_message}" 'deleted'
112+
# Invalidate local cache because cluster scoped CRD in cache is stale.
113+
# Invalidation of cache may take up to 10 minutes and we are manually
114+
# invalidate cache and expect that scope changed CRD should be created without problem.
115+
kubectl api-resources
116+
output_message=$(kubectl apply -f hack/testdata/CRD/example-crd-1-namespaced.yaml)
117+
kube::test::if_has_string "${output_message}" 'created'
118+
output_message=$(kubectl apply -f hack/testdata/CRD/example-crd-1-namespaced-resource.yaml)
119+
kube::test::if_has_string "${output_message}" 'created'
120+
121+
# Cleanup
122+
kubectl delete -f hack/testdata/CRD/example-crd-1-namespaced-resource.yaml
123+
kubectl delete -f hack/testdata/CRD/example-crd-1-namespaced.yaml
124+
125+
set +o nounset
126+
set +o errexit
127+
}
128+
99129
run_kubectl_explain_tests() {
100130
set -o nounset
101131
set -o errexit

test/cmd/legacy-script.sh

+7
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,13 @@ runTests() {
892892
record_command run_kubectl_explain_tests
893893
fi
894894

895+
##############################
896+
# CRD Deletion / Re-creation #
897+
##############################
898+
899+
if kube::test::if_supports_resource "${namespaces}" ; then
900+
record_command run_crd_deletion_recreation_tests
901+
fi
895902

896903
###########
897904
# Swagger #

0 commit comments

Comments
 (0)