MCPcopy Create free account
hub / github.com/cloudnative-pg/cloudnative-pg / streamClusterLogsToZip

Function streamClusterLogsToZip

internal/cmd/plugin/report/logs.go:78–120  ·  view source on GitHub ↗

streamClusterLogsToZip streams the logs from the pods in the cluster, one by one, each in a new file, within a folder

(
	ctx context.Context,
	clusterName string,
	namespace string,
	dirname string,
	logTimeStamp bool,
	zipper *zip.Writer,
)

Source from the content-addressed store, hash-verified

76// streamClusterLogsToZip streams the logs from the pods in the cluster, one by
77// one, each in a new file, within a folder
78func streamClusterLogsToZip(
79 ctx context.Context,
80 clusterName string,
81 namespace string,
82 dirname string,
83 logTimeStamp bool,
84 zipper *zip.Writer,
85) error {
86 logsdir := filepath.Join(dirname, "logs")
87 _, err := zipper.Create(logsdir + "/")
88 if err != nil {
89 return fmt.Errorf("could not add '%s' to zip: %w", logsdir, err)
90 }
91
92 matchClusterName := client.MatchingLabels{
93 utils.ClusterLabelName: clusterName,
94 }
95
96 var podList corev1.PodList
97 err = plugin.Client.List(ctx, &podList, matchClusterName, client.InNamespace(namespace))
98 if err != nil {
99 return fmt.Errorf("could not get cluster pods: %w", err)
100 }
101
102 cli := kubernetes.NewForConfigOrDie(ctrl.GetConfigOrDie())
103
104 for idx := range podList.Items {
105 pod := podList.Items[idx]
106 streamPodLogs := podlogs.NewPodLogsWriter(pod, cli)
107 fileNamer := func(containerName string) string {
108 return filepath.Join(logsdir, fmt.Sprintf("%s-%s.jsonl", pod.Name, containerName))
109 }
110 opts := &corev1.PodLogOptions{
111 Timestamps: logTimeStamp,
112 Previous: true,
113 }
114 if err := streamPodLogs.Multiple(ctx, opts, zipper, fileNamer); err != nil {
115 return err
116 }
117 }
118
119 return nil
120}
121
122// streamClusterJobLogsToZip checks for jobs in the cluster, and streams
123// the logs from the pods created by those jobs, one by one, each in a new file

Callers 1

clusterFunction · 0.85

Calls 5

NewPodLogsWriterFunction · 0.92
JoinMethod · 0.80
MultipleMethod · 0.80
CreateMethod · 0.65
ListMethod · 0.45

Tested by

no test coverage detected