mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-03 19:58:17 +00:00 
			
		
		
		
	Merge pull request #27122 from lukaszo/uxlogs
Automatic merge from submit-queue Include init containers in error messages
This commit is contained in:
		@@ -246,9 +246,9 @@ func ResourceLocation(getter ResourceGetter, rt http.RoundTripper, ctx api.Conte
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// getContainerNames returns a formatted string containing the container names
 | 
					// getContainerNames returns a formatted string containing the container names
 | 
				
			||||||
func getContainerNames(pod *api.Pod) string {
 | 
					func getContainerNames(containers []api.Container) string {
 | 
				
			||||||
	names := []string{}
 | 
						names := []string{}
 | 
				
			||||||
	for _, c := range pod.Spec.Containers {
 | 
						for _, c := range containers {
 | 
				
			||||||
		names = append(names, c.Name)
 | 
							names = append(names, c.Name)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return strings.Join(names, " ")
 | 
						return strings.Join(names, " ")
 | 
				
			||||||
@@ -278,8 +278,13 @@ func LogLocation(
 | 
				
			|||||||
		case 0:
 | 
							case 0:
 | 
				
			||||||
			return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s", name))
 | 
								return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s", name))
 | 
				
			||||||
		default:
 | 
							default:
 | 
				
			||||||
			containerNames := getContainerNames(pod)
 | 
								containerNames := getContainerNames(pod.Spec.Containers)
 | 
				
			||||||
			return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s, choose one of: [%s]", name, containerNames))
 | 
								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 {
 | 
						} else {
 | 
				
			||||||
		if !podHasContainerWithName(pod, container) {
 | 
							if !podHasContainerWithName(pod, container) {
 | 
				
			||||||
@@ -424,8 +429,13 @@ func streamLocation(
 | 
				
			|||||||
		case 0:
 | 
							case 0:
 | 
				
			||||||
			return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s", name))
 | 
								return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s", name))
 | 
				
			||||||
		default:
 | 
							default:
 | 
				
			||||||
			containerNames := getContainerNames(pod)
 | 
								containerNames := getContainerNames(pod.Spec.Containers)
 | 
				
			||||||
			return nil, nil, errors.NewBadRequest(fmt.Sprintf("a container name must be specified for pod %s, choose one of: [%s]", name, containerNames))
 | 
								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 {
 | 
						} else {
 | 
				
			||||||
		if !podHasContainerWithName(pod, container) {
 | 
							if !podHasContainerWithName(pod, container) {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -192,6 +192,22 @@ func TestCheckLogLocation(t *testing.T) {
 | 
				
			|||||||
			opts:        &api.PodLogOptions{},
 | 
								opts:        &api.PodLogOptions{},
 | 
				
			||||||
			expectedErr: errors.NewBadRequest("a container name must be specified for pod test, choose one of: [container1 container2]"),
 | 
								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{
 | 
								in: &api.Pod{
 | 
				
			||||||
				Spec: api.PodSpec{
 | 
									Spec: api.PodSpec{
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user