diff --git a/hack/make-rules/test-cmd.sh b/hack/make-rules/test-cmd.sh index d5084a47ca1..26dfbcb9fb8 100755 --- a/hack/make-rules/test-cmd.sh +++ b/hack/make-rules/test-cmd.sh @@ -1581,9 +1581,9 @@ __EOF__ # Clean up kubectl delete namespace my-namespace - ############## + ###################### # Pods in Namespaces # - ############## + ###################### ### Create a new namespace # Pre-condition: the other namespace does not exist @@ -1602,6 +1602,9 @@ __EOF__ kube::test::get_object_assert 'pods --namespace=other' "{{range.items}}{{$id_field}}:{{end}}" 'valid-pod:' # Post-condition: verify shorthand `-n other` has the same results as `--namespace=other` kube::test::get_object_assert 'pods -n other' "{{range.items}}{{$id_field}}:{{end}}" 'valid-pod:' + # Post-condition: a resource cannot be retrieved by name across all namespaces + output_message=$(! kubectl get "${kube_flags[@]}" pod valid-pod --all-namespaces 2>&1) + kube::test::if_has_string "${output_message}" "a resource cannot be retrieved by name across all namespaces" ### Delete POD valid-pod in specific namespace # Pre-condition: valid-pod POD exists @@ -1613,9 +1616,9 @@ __EOF__ # Clean up kubectl delete namespace other - ############## + ########### # Secrets # - ############## + ########### ### Create a new namespace # Pre-condition: the test-secrets namespace does not exist diff --git a/pkg/kubectl/resource/builder.go b/pkg/kubectl/resource/builder.go index afb64e97f1f..49ce6aa7481 100644 --- a/pkg/kubectl/resource/builder.go +++ b/pkg/kubectl/resource/builder.go @@ -55,8 +55,9 @@ type Builder struct { resources []string - namespace string - names []string + namespace string + allNamespace bool + names []string resourceTuples []resourceTuple @@ -296,6 +297,7 @@ func (b *Builder) AllNamespaces(allNamespace bool) *Builder { if allNamespace { b.namespace = api.NamespaceAll } + b.allNamespace = allNamespace return b } @@ -644,7 +646,11 @@ func (b *Builder) visitByResource() *Result { selectorNamespace = "" } else { if len(b.namespace) == 0 { - return &Result{singular: isSingular, err: fmt.Errorf("namespace may not be empty when retrieving a resource by name")} + errMsg := "namespace may not be empty when retrieving a resource by name" + if b.allNamespace { + errMsg = "a resource cannot be retrieved by name across all namespaces" + } + return &Result{singular: isSingular, err: fmt.Errorf(errMsg)} } } @@ -690,7 +696,11 @@ func (b *Builder) visitByName() *Result { selectorNamespace = "" } else { if len(b.namespace) == 0 { - return &Result{singular: isSingular, err: fmt.Errorf("namespace may not be empty when retrieving a resource by name")} + errMsg := "namespace may not be empty when retrieving a resource by name" + if b.allNamespace { + errMsg = "a resource cannot be retrieved by name across all namespaces" + } + return &Result{singular: isSingular, err: fmt.Errorf(errMsg)} } }