From 038bb130b8071dc6dc4df6fbacdd5036cca89c6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Ole=C5=9B?= Date: Thu, 9 Jun 2016 14:08:43 +0200 Subject: [PATCH] Include init containers in error messages fixes: #27040 --- pkg/registry/pod/strategy.go | 22 ++++++++++++++++------ pkg/registry/pod/strategy_test.go | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/pkg/registry/pod/strategy.go b/pkg/registry/pod/strategy.go index 98d551322bb..4846d4e4180 100644 --- a/pkg/registry/pod/strategy.go +++ b/pkg/registry/pod/strategy.go @@ -246,9 +246,9 @@ func ResourceLocation(getter ResourceGetter, rt http.RoundTripper, ctx api.Conte } // getContainerNames returns a formatted string containing the container names -func getContainerNames(pod *api.Pod) string { +func getContainerNames(containers []api.Container) string { names := []string{} - for _, c := range pod.Spec.Containers { + for _, c := range containers { names = append(names, c.Name) } return strings.Join(names, " ") @@ -278,8 +278,13 @@ func LogLocation( case 0: return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s", name)) default: - containerNames := getContainerNames(pod) - return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s, choose one of: [%s]", name, containerNames)) + containerNames := getContainerNames(pod.Spec.Containers) + initContainerNames := getContainerNames(pod.Spec.InitContainers) + err := fmt.Sprintf("a container name must be specified for pod %s, choose one of: [%s]", name, containerNames) + if len(initContainerNames) > 0 { + err += fmt.Sprintf(" or one of the init containers: [%s]", initContainerNames) + } + return nil, nil, errors.NewBadRequest(err) } } else { if !podHasContainerWithName(pod, container) { @@ -424,8 +429,13 @@ func streamLocation( case 0: return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s", name)) default: - containerNames := getContainerNames(pod) - return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s, choose one of: [%s]", name, containerNames)) + containerNames := getContainerNames(pod.Spec.Containers) + initContainerNames := getContainerNames(pod.Spec.InitContainers) + err := fmt.Sprintf("a container name must be specified for pod %s, choose one of: [%s]", name, containerNames) + if len(initContainerNames) > 0 { + err += fmt.Sprintf(" or one of the init containers: [%s]", initContainerNames) + } + return nil, nil, errors.NewBadRequest(err) } } else { if !podHasContainerWithName(pod, container) { diff --git a/pkg/registry/pod/strategy_test.go b/pkg/registry/pod/strategy_test.go index 77d343b5219..7f03fd655e8 100644 --- a/pkg/registry/pod/strategy_test.go +++ b/pkg/registry/pod/strategy_test.go @@ -192,6 +192,22 @@ func TestCheckLogLocation(t *testing.T) { opts: &api.PodLogOptions{}, expectedErr: errors.NewBadRequest("a container name must be specified for pod test, choose one of: [container1 container2]"), }, + { + in: &api.Pod{ + Spec: api.PodSpec{ + Containers: []api.Container{ + {Name: "container1"}, + {Name: "container2"}, + }, + InitContainers: []api.Container{ + {Name: "initcontainer1"}, + }, + }, + Status: api.PodStatus{}, + }, + opts: &api.PodLogOptions{}, + expectedErr: errors.NewBadRequest("a container name must be specified for pod test, choose one of: [container1 container2] or one of the init containers: [initcontainer1]"), + }, { in: &api.Pod{ Spec: api.PodSpec{