61 lines
2.2 KiB
Go
61 lines
2.2 KiB
Go
/*
|
|
Copyright 2019 The Kubernetes Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package kubectl
|
|
|
|
import (
|
|
"strings"
|
|
|
|
v1 "k8s.io/api/core/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
clientset "k8s.io/client-go/kubernetes"
|
|
"k8s.io/kubernetes/test/e2e/framework"
|
|
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
|
|
testutils "k8s.io/kubernetes/test/utils"
|
|
)
|
|
|
|
// LogFailedContainers runs `kubectl logs` on a failed containers.
|
|
func LogFailedContainers(c clientset.Interface, ns string, logFunc func(ftm string, args ...interface{})) {
|
|
podList, err := c.CoreV1().Pods(ns).List(metav1.ListOptions{})
|
|
if err != nil {
|
|
logFunc("Error getting pods in namespace '%s': %v", ns, err)
|
|
return
|
|
}
|
|
logFunc("Running kubectl logs on non-ready containers in %v", ns)
|
|
for _, pod := range podList.Items {
|
|
if res, err := testutils.PodRunningReady(&pod); !res || err != nil {
|
|
kubectlLogPod(c, pod, "", framework.Logf)
|
|
}
|
|
}
|
|
}
|
|
|
|
func kubectlLogPod(c clientset.Interface, pod v1.Pod, containerNameSubstr string, logFunc func(ftm string, args ...interface{})) {
|
|
for _, container := range pod.Spec.Containers {
|
|
if strings.Contains(container.Name, containerNameSubstr) {
|
|
// Contains() matches all strings if substr is empty
|
|
logs, err := e2epod.GetPodLogs(c, pod.Namespace, pod.Name, container.Name)
|
|
if err != nil {
|
|
logs, err = e2epod.GetPreviousPodLogs(c, pod.Namespace, pod.Name, container.Name)
|
|
if err != nil {
|
|
logFunc("Failed to get logs of pod %v, container %v, err: %v", pod.Name, container.Name, err)
|
|
}
|
|
}
|
|
logFunc("Logs of %v/%v:%v on node %v", pod.Namespace, pod.Name, container.Name, pod.Spec.NodeName)
|
|
logFunc("%s : STARTLOG\n%s\nENDLOG for container %v:%v:%v", containerNameSubstr, logs, pod.Namespace, pod.Name, container.Name)
|
|
}
|
|
}
|
|
}
|