this is split for easy test-ability
(clientset corev1client.CoreV1Interface, object, options runtime.Object, timeout time.Duration, allContainers bool, allPods bool)
| 62 | |
| 63 | // this is split for easy test-ability |
| 64 | func logsForObjectWithClient(clientset corev1client.CoreV1Interface, object, options runtime.Object, timeout time.Duration, allContainers bool, allPods bool) (map[corev1.ObjectReference]rest.ResponseWrapper, error) { |
| 65 | opts, ok := options.(*corev1.PodLogOptions) |
| 66 | if !ok { |
| 67 | return nil, errors.New("provided options object is not a PodLogOptions") |
| 68 | } |
| 69 | |
| 70 | switch t := object.(type) { |
| 71 | case *corev1.PodList: |
| 72 | ret := make(map[corev1.ObjectReference]rest.ResponseWrapper) |
| 73 | for i := range t.Items { |
| 74 | currRet, err := logsForObjectWithClient(clientset, &t.Items[i], options, timeout, allContainers, allPods) |
| 75 | if err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | for k, v := range currRet { |
| 79 | ret[k] = v |
| 80 | } |
| 81 | } |
| 82 | return ret, nil |
| 83 | |
| 84 | case *corev1.Pod: |
| 85 | // if allContainers is true, then we're going to locate all containers and then iterate through them. At that point, "allContainers" is false |
| 86 | if !allContainers { |
| 87 | currOpts := new(corev1.PodLogOptions) |
| 88 | if opts != nil { |
| 89 | opts.DeepCopyInto(currOpts) |
| 90 | } |
| 91 | // in case the "kubectl.kubernetes.io/default-container" annotation is present, we preset the opts.Containers to default to selected |
| 92 | // container. This gives users ability to preselect the most interesting container in pod. |
| 93 | if annotations := t.GetAnnotations(); annotations != nil && currOpts.Container == "" { |
| 94 | var defaultContainer string |
| 95 | if len(annotations[podcmd.DefaultContainerAnnotationName]) > 0 { |
| 96 | defaultContainer = annotations[podcmd.DefaultContainerAnnotationName] |
| 97 | } |
| 98 | if len(defaultContainer) > 0 { |
| 99 | if exists, _ := podcmd.FindContainerByName(t, defaultContainer); exists == nil { |
| 100 | fmt.Fprintf(os.Stderr, "Default container name %q not found in pod %s\n", defaultContainer, t.Name) |
| 101 | } else { |
| 102 | currOpts.Container = defaultContainer |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if currOpts.Container == "" { |
| 108 | // Default to the first container name(aligning behavior with `kubectl exec'). |
| 109 | currOpts.Container = t.Spec.Containers[0].Name |
| 110 | if len(t.Spec.Containers) > 1 || len(t.Spec.InitContainers) > 0 || len(t.Spec.EphemeralContainers) > 0 { |
| 111 | if !allPods { |
| 112 | fmt.Fprintf(os.Stderr, "Defaulted container %q out of: %s\n", currOpts.Container, podcmd.AllContainerNames(t)) |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | container, fieldPath := podcmd.FindContainerByName(t, currOpts.Container) |
| 118 | if container == nil { |
| 119 | return nil, fmt.Errorf("container %s is not valid for pod %s out of: %s", currOpts.Container, t.Name, podcmd.AllContainerNames(t)) |
| 120 | } |
| 121 | ref, err := reference.GetPartialReference(scheme.Scheme, t, fieldPath) |
searching dependent graphs…