From 33b9235312b249122eeb6ef7764657c8a1c068c8 Mon Sep 17 00:00:00 2001 From: Janet Kuo Date: Wed, 14 Oct 2015 15:06:06 -0700 Subject: [PATCH] Better error handling for watching a list of resources specified in a file --- hack/test-cmd.sh | 8 ++++++-- pkg/kubectl/cmd/get.go | 2 +- pkg/kubectl/resource/result.go | 2 +- pkg/kubectl/resource/visitor.go | 3 +++ 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/hack/test-cmd.sh b/hack/test-cmd.sh index 85586ecea9d..ef6053ee6b0 100755 --- a/hack/test-cmd.sh +++ b/hack/test-cmd.sh @@ -804,7 +804,6 @@ __EOF__ ###################### kube::log::status "Testing kubectl(${version}:multiple resources)" - # TODO: add test for types like ReplicationControllerList, ServiceList FILES="hack/testdata/multi-resource-yaml hack/testdata/multi-resource-list @@ -862,8 +861,13 @@ __EOF__ fi fi # Command - # kubectl create -f "${file}" "${kube_flags[@]}" # test fails here now kubectl get -f "${file}" "${kube_flags[@]}" + # Command: watching multiple resources should return "not supported" error + WATCH_ERROR_FILE="${KUBE_TEMP}/kubectl-watch-error" + kubectl get -f "${file}" "${kube_flags[@]}" "--watch" 2> ${WATCH_ERROR_FILE} || true + if ! grep -q "watch is only supported on individual resources and resource collections" "${WATCH_ERROR_FILE}"; then + kube::log::error_exit "kubectl watch multiple resource returns unexpected error or non-error: $(cat ${WATCH_ERROR_FILE})" "1" + fi kubectl describe -f "${file}" "${kube_flags[@]}" # Command kubectl replace -f $replace_file --force "${kube_flags[@]}" diff --git a/pkg/kubectl/cmd/get.go b/pkg/kubectl/cmd/get.go index 2b434cb0b02..b990104427c 100644 --- a/pkg/kubectl/cmd/get.go +++ b/pkg/kubectl/cmd/get.go @@ -145,7 +145,7 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string return err } if len(infos) != 1 { - return fmt.Errorf("watch is only supported on a single resource - %d resources were found", len(infos)) + return fmt.Errorf("watch is only supported on individual resources and resource collections - %d resources were found", len(infos)) } info := infos[0] mapping := info.ResourceMapping() diff --git a/pkg/kubectl/resource/result.go b/pkg/kubectl/resource/result.go index 6eda37a171a..224fb7afa34 100644 --- a/pkg/kubectl/resource/result.go +++ b/pkg/kubectl/resource/result.go @@ -199,7 +199,7 @@ func (r *Result) Watch(resourceVersion string) (watch.Interface, error) { return nil, err } if len(info) != 1 { - return nil, fmt.Errorf("watch is only supported on a single resource - %d resources were found", len(info)) + return nil, fmt.Errorf("watch is only supported on individual resources and resource collections - %d resources were found", len(info)) } return info[0].Watch(resourceVersion) } diff --git a/pkg/kubectl/resource/visitor.go b/pkg/kubectl/resource/visitor.go index e2491821e08..90d150eb4cf 100644 --- a/pkg/kubectl/resource/visitor.go +++ b/pkg/kubectl/resource/visitor.go @@ -564,6 +564,9 @@ func RetrieveLatest(info *Info, err error) error { if err != nil { return err } + if runtime.IsListType(info.Object) { + return fmt.Errorf("watch is only supported on individual resources and resource collections, but a list of resources is found") + } if len(info.Name) == 0 { return nil }