Add shortname ambiguity warning in shortcut expander (#117668)

* Add warning handler callback function in shortcut expander

Currently, errors in client-go are propagated back to the callers via
function returns. However, there is no elegant way for just warning users.
For example, when user wants to get a resource with it's short name format
and if there are multiple resources belonging to this short name, we need to
warn user about this ambugity which one is picked and which ones are discarded.

Not only to overcome this particular case mentioned above, but also propose a
way for the possible warnings in the future, this commit adds a warningHandler
callback function in shortcutExpander.

* Add warningPrinter functionality in ConfigFlags

ConfigFlags has neither warning user in a standardized
format functionality nor passing warning callback functions to other upper level
libraries such as client-go.

This commit adds an ability that user can set warningPrinters
according to their IOStreams and this warningPrinters will be used
to raise possible warnings happening not only in cli-runtime but
also in client-go.

* Pass warning callback function in ConfigFlags to shortcutExpander

This commit passes warning callback function to print possible
warnings happened in shortcut expander to warn user in a
standardized format.

* Add integration test for CRDs having ambiguous short names

This commit adds integration test to assure that warning message
related to this ambiguity is printed when resources are being retrieved via their short name
representations in cases where multiple resources have same
short names.

This integration test also ensures that the logic behind which resource
will be selected hasn't been changed which may cause disperancies in
clusters.

* Remove defaultConfigFlag global variable

* Move default config flags initialization into function

* Skip warning for versions of same group/resource

* Run update-vendor

* Warn only once when there are multiple versions registered for ambiguous resource

* Apply gocritic review

* Add multi-resource multi-version ambiguity unit test
This commit is contained in:
Arda Güçlü
2023-10-11 18:04:11 +03:00
committed by GitHub
parent 86a0b1c57a
commit a504aed54d
14 changed files with 431 additions and 46 deletions

View File

@@ -303,3 +303,107 @@ run_swagger_tests() {
set +o nounset
set +o errexit
}
run_ambiguous_shortname_tests() {
set -o nounset
set -o errexit
create_and_use_new_namespace
kube::log::status "Testing ambiguous short name"
kubectl create -f - << __EOF__
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: foos.bar.com
spec:
group: bar.com
scope: Namespaced
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
test:
type: string
names:
plural: foos
singular: foo
shortNames:
- exmp
kind: Foo
categories:
- all
__EOF__
# Test that we can list this new custom resource
kube::test::wait_object_assert customresourcedefinitions "{{range.items}}{{if eq ${id_field:?} \"foos.bar.com\"}}{{$id_field}}:{{end}}{{end}}" 'foos.bar.com:'
kubectl create -f - << __EOF__
apiVersion: bar.com/v1
kind: Foo
metadata:
name: test-crd-foo
spec:
test: test
__EOF__
# Test that we can list this new custom resource
kube::test::wait_object_assert foos "{{range.items}}{{$id_field}}:{{end}}" 'test-crd-foo:'
output_message=$(kubectl get exmp)
kube::test::if_has_string "${output_message}" "test-crd-foo"
kubectl create -f - << __EOF__
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: examples.test.com
spec:
group: test.com
scope: Namespaced
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
test:
type: string
names:
plural: examples
singular: example
shortNames:
- exmp
kind: Example
__EOF__
# Test that we can list this new custom resource
kube::test::wait_object_assert customresourcedefinitions "{{range.items}}{{if eq ${id_field:?} \"examples.test.com\"}}{{$id_field}}:{{end}}{{end}}" 'examples.test.com:'
output_message=$(kubectl get examples 2>&1 "${kube_flags[@]}")
kube::test::if_has_string "${output_message}" 'No resources found'
output_message=$(kubectl get exmp 2>&1)
kube::test::if_has_string "${output_message}" "test-crd-foo"
kube::test::if_has_string "${output_message}" "short name \"exmp\" could also match lower priority resource examples.test.com"
# Cleanup
kubectl delete foos/test-crd-foo
kubectl delete customresourcedefinition foos.bar.com
kubectl delete customresourcedefinition examples.test.com
set +o nounset
set +o errexit
}

View File

@@ -518,6 +518,14 @@ runTests() {
record_command run_assert_singular_name_tests
fi
#########################
# Ambiguous short name #
#########################
if kube::test::if_supports_resource "${customresourcedefinitions}" ; then
record_command run_ambiguous_shortname_tests
fi
#########################
# Assert categories #
#########################