Include init containers in error messages

fixes: #27040
pull/6/head
Łukasz Oleś 2016-06-09 14:08:43 +02:00
parent 875a60d030
commit 038bb130b8
2 changed files with 32 additions and 6 deletions

View File

@ -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) {

View File

@ -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{