fix: will logs the default container only even --all-containers is specified

Signed-off-by: pacoxu <paco.xu@daocloud.io>
This commit is contained in:
pacoxu 2021-03-01 11:00:05 +08:00
parent bce5d7a015
commit 71db08d15a

View File

@ -73,6 +73,8 @@ func logsForObjectWithClient(clientset corev1client.CoreV1Interface, object, opt
return ret, nil return ret, nil
case *corev1.Pod: case *corev1.Pod:
// if allContainers is true, then we're going to locate all containers and then iterate through them. At that point, "allContainers" is false
if !allContainers {
// in case the "kubectl.kubernetes.io/default-container" annotation is present, we preset the opts.Containers to default to selected // in case the "kubectl.kubernetes.io/default-container" annotation is present, we preset the opts.Containers to default to selected
// container. This gives users ability to preselect the most interesting container in pod. // container. This gives users ability to preselect the most interesting container in pod.
if annotations := t.GetAnnotations(); annotations != nil && len(opts.Container) == 0 { if annotations := t.GetAnnotations(); annotations != nil && len(opts.Container) == 0 {
@ -91,12 +93,11 @@ func logsForObjectWithClient(clientset corev1client.CoreV1Interface, object, opt
} }
} }
} }
// if allContainers is true, then we're going to locate all containers and then iterate through them. At that point, "allContainers" is false
if !allContainers {
var containerName string var containerName string
if opts == nil || len(opts.Container) == 0 { if opts == nil || len(opts.Container) == 0 {
// We don't know container name. In this case we expect only one container to be present in the pod (ignoring InitContainers). // We don't know container name. In this case we expect only one container to be present in the pod (ignoring InitContainers).
// If there is more than one container we should return an error showing all container names. // If there is more than one container, we should return an error showing all container names.
if len(t.Spec.Containers) != 1 { if len(t.Spec.Containers) != 1 {
containerNames := getContainerNames(t.Spec.Containers) containerNames := getContainerNames(t.Spec.Containers)
initContainerNames := getContainerNames(t.Spec.InitContainers) initContainerNames := getContainerNames(t.Spec.InitContainers)