Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(retrievePodLogs): fetch logs from pods and add to zip #878

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions test/e2e/support/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package support
import (
"context"
"fmt"
"io"
"log"
"os"
"path/filepath"
Expand All @@ -16,6 +17,7 @@ import (
v12 "k8s.io/api/apps/v1"
v13 "k8s.io/api/batch/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client/config"

Expand Down Expand Up @@ -151,6 +153,12 @@ func DumpNamespace(ctx context.Context, cli client.Client, ns string) {
}
}

// Retrieve logs for all pods (in the namespace)
podLogFiles := retrievePodLogs(ctx, cli, ns)
for k, v := range podLogFiles {
k8s[k] = v
}

// Create the output file
fileName := "k8s-dump-" + ns + ".tar.gz"
outFile, err := os.Create(fileName)
Expand All @@ -163,6 +171,59 @@ func DumpNamespace(ctx context.Context, cli client.Client, ns string) {
}
}

func retrievePodLogs(ctx context.Context, cli client.Client, ns string) map[string]logTarget {
podLogs := make(map[string]logTarget)

podList := &v1.PodList{}
if err := cli.List(ctx, podList, client.InNamespace(ns)); err != nil {
log.Printf("failed to list pods in namespace %q: %v", ns, err)
return podLogs
}

restCfg, err := config.GetConfig()
if err != nil {
log.Printf("failed to retrieve REST configuration: %v", err)
return podLogs
}
clientset, err := kubernetes.NewForConfig(restCfg)
if err != nil {
log.Printf("failed to create Kubernetes clientset: %v", err)
return podLogs
}

for _, pod := range podList.Items {
podLogOptions := v1.PodLogOptions{}
req := clientset.CoreV1().Pods(ns).GetLogs(pod.Name, &podLogOptions)

stream, err := req.Stream(ctx)
if err != nil {
// Ensure we close the stream if it is non-nil
if stream != nil {
_ = stream.Close()
}
log.Printf("failed to open log stream for pod %q: %v", pod.Name, err)
continue
}

logData, err := io.ReadAll(stream)
if closeErr := stream.Close(); closeErr != nil {
log.Printf("failed to close log stream for pod %q: %v", pod.Name, closeErr)
}
if err != nil {
log.Printf("failed to read log stream for pod %q: %v", pod.Name, err)
continue
}

fileKey := "pod-logs/" + pod.Name + ".log"
podLogs[fileKey] = logTarget{
reader: strings.NewReader(string(logData)),
size: int64(len(logData)),
}
}

return podLogs
}

func dumpK8sObjects(ctx context.Context, cli client.Client, list client.ObjectList, namespace string) (string, error) {
var builder strings.Builder

Expand Down
Loading