Skip to content

Commit

Permalink
Output init containers in subctl gather
Browse files Browse the repository at this point in the history
Refactored "gatherPodLogs" to take the desired container names. If none
specified, gather logs for all init and normal containers confgiured in
the pod.

Also, if a pod fails, it's useful to see the pod details so gather the
YAML for pod resources for daemonsets and deployments.

Signed-off-by: Tom Pantelis <[email protected]>
  • Loading branch information
tpantelis committed Nov 21, 2024
1 parent 715e490 commit c1d11cb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
4 changes: 2 additions & 2 deletions internal/gather/connectivity.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ func gatherGatewayPodLogs(info *Info) {
}

func gatherMetricsProxyPodLogs(info *Info) {
gatherPodLogsByContainer(metricsProxyPodLabel, "gateway-metrics-proxy", info)
gatherPodLogs(metricsProxyPodLabel, info, "gateway-metrics-proxy")

if info.Submariner.Spec.GlobalCIDR != "" {
gatherPodLogsByContainer(metricsProxyPodLabel, "globalnet-metrics-proxy", info)
gatherPodLogs(metricsProxyPodLabel, info, "globalnet-metrics-proxy")
}
}

Expand Down
51 changes: 34 additions & 17 deletions internal/gather/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ import (
"k8s.io/client-go/kubernetes"
)

func gatherPodLogs(podLabelSelector string, info *Info) {
gatherPodLogsByContainer(podLabelSelector, "", info)
}

func gatherPodLogsByContainer(podLabelSelector, container string, info *Info) {
func gatherPodLogs(podLabelSelector string, info *Info, fromContainers ...string) {
err := func() error {
pods, err := findPods(info.ClientProducer.ForKubernetes(), podLabelSelector)
if err != nil {
Expand All @@ -44,11 +40,23 @@ func gatherPodLogsByContainer(podLabelSelector, container string, info *Info) {

info.Status.Success("Found %d pods matching label selector %q", len(pods.Items), podLabelSelector)

podLogOptions := corev1.PodLogOptions{
Container: container,
}
for i := range pods.Items {
info.Summary.PodLogs = append(info.Summary.PodLogs, outputPodLogs(&pods.Items[i], podLogOptions, info))
containers := fromContainers
if len(containers) == 0 {
containers = append(getContainerNames(pods.Items[i].Spec.InitContainers),
getContainerNames(pods.Items[i].Spec.Containers)...)
}

for _, container := range containers {
logName := pods.Items[i].Name
if len(containers) > 1 {
logName = logName + "-" + container
}

info.Summary.PodLogs = append(info.Summary.PodLogs, outputPodLogs(&pods.Items[i], corev1.PodLogOptions{
Container: container,
}, logName, info))
}
}

return nil
Expand All @@ -59,19 +67,28 @@ func gatherPodLogsByContainer(podLabelSelector, container string, info *Info) {
}
}

func getContainerNames(containers []corev1.Container) []string {
names := make([]string, len(containers))
for i := range containers {
names[i] = containers[i].Name
}

return names
}

//nolint:gocritic // hugeParam: podLogOptions - purposely passed by value.
func outputPodLogs(pod *corev1.Pod, podLogOptions corev1.PodLogOptions, info *Info) (podLogInfo LogInfo) {
func outputPodLogs(pod *corev1.Pod, podLogOptions corev1.PodLogOptions, logName string, info *Info) (podLogInfo LogInfo) {
podLogInfo.Namespace = pod.Namespace
podLogInfo.PodState = pod.Status.Phase
podLogInfo.PodName = pod.Name
podLogInfo.NodeName = pod.Spec.NodeName

err := outputPreviousPodLog(pod, podLogOptions, info, &podLogInfo)
err := outputPreviousPodLog(pod, podLogOptions, logName, info, &podLogInfo)
if err != nil {
info.Status.Failure("Error outputting previous log for pod %q: %v", pod.Name, err)
}

err = outputCurrentPodLog(pod, podLogOptions, info, &podLogInfo)
err = outputCurrentPodLog(pod, podLogOptions, logName, info, &podLogInfo)
if err != nil {
info.Status.Failure("Error outputting current log for pod %q: %v", pod.Name, err)
}
Expand Down Expand Up @@ -129,7 +146,7 @@ func findPods(clientSet kubernetes.Interface, byLabelSelector string) (*corev1.P
}

//nolint:gocritic // hugeParam: podLogOptions - purposely passed by value.
func outputPreviousPodLog(pod *corev1.Pod, podLogOptions corev1.PodLogOptions, info *Info, podLogInfo *LogInfo) error {
func outputPreviousPodLog(pod *corev1.Pod, podLogOptions corev1.PodLogOptions, logName string, info *Info, podLogInfo *LogInfo) error {
podLogOptions.Previous = true
logRequest := info.ClientProducer.ForKubernetes().CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &podLogOptions)
logStream, _ := logRequest.Stream(context.TODO())
Expand All @@ -138,9 +155,9 @@ func outputPreviousPodLog(pod *corev1.Pod, podLogOptions corev1.PodLogOptions, i

// if no previous pods found, logstream == nil, ignore it
if logStream != nil {
info.Status.Warning("Found logs for previous instances of pod %s", pod.Name)
info.Status.Warning("Found logs for previous instances of pod %q", pod.Name)

fileName, err := writePodLogToFile(logStream, info, pod.Name, ".log.prev")
fileName, err := writePodLogToFile(logStream, info, logName, ".log.prev")
if err != nil {
return err
}
Expand All @@ -158,7 +175,7 @@ func outputPreviousPodLog(pod *corev1.Pod, podLogOptions corev1.PodLogOptions, i
}

//nolint:gocritic // hugeParam: podLogOptions - purposely passed by value.
func outputCurrentPodLog(pod *corev1.Pod, podLogOptions corev1.PodLogOptions, info *Info, podLogInfo *LogInfo) error {
func outputCurrentPodLog(pod *corev1.Pod, podLogOptions corev1.PodLogOptions, logName string, info *Info, podLogInfo *LogInfo) error {
// Running with Previous = false on the same pod
podLogOptions.Previous = false
logRequest := info.ClientProducer.ForKubernetes().CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &podLogOptions)
Expand All @@ -170,7 +187,7 @@ func outputCurrentPodLog(pod *corev1.Pod, podLogOptions corev1.PodLogOptions, in

defer logStream.Close()

fileName, err := writePodLogToFile(logStream, info, pod.Name, ".log")
fileName, err := writePodLogToFile(logStream, info, logName, ".log")
podLogInfo.LogFileName = append(podLogInfo.LogFileName, fileName)

return err
Expand Down
2 changes: 2 additions & 0 deletions internal/gather/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ func ResourcesToYAMLFile(info *Info, ofType schema.GroupVersionResource, namespa
//nolint:gocritic // hugeParam: listOptions - match K8s API.
func gatherDaemonSet(info *Info, namespace string, listOptions metav1.ListOptions) {
ResourcesToYAMLFile(info, appsv1.SchemeGroupVersion.WithResource("daemonsets"), namespace, listOptions)
ResourcesToYAMLFile(info, corev1.SchemeGroupVersion.WithResource("pods"), namespace, listOptions)
}

//nolint:gocritic // hugeParam: listOptions - match K8s API.
func gatherDeployment(info *Info, namespace string, listOptions metav1.ListOptions) {
ResourcesToYAMLFile(info, appsv1.SchemeGroupVersion.WithResource("deployments"), namespace, listOptions)
ResourcesToYAMLFile(info, corev1.SchemeGroupVersion.WithResource("pods"), namespace, listOptions)
}

//nolint:gocritic // hugeParam: listOptions - match K8s API.
Expand Down
2 changes: 1 addition & 1 deletion internal/gather/servicediscovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func gatherServiceDiscoveryPodLogs(info *Info) {

func gatherCoreDNSPodLogs(info *Info) {
if isCoreDNSTypeOcp(info) {
gatherPodLogsByContainer(ocpCoreDNSPodLabel, "dns", info)
gatherPodLogs(ocpCoreDNSPodLabel, info, "dns")
} else {
gatherPodLogs(k8sCoreDNSPodLabel, info)
}
Expand Down

0 comments on commit c1d11cb

Please sign in to comment.