From 3c8ee11216c11bc1eda4ccaa057c7e7b2f6f194f Mon Sep 17 00:00:00 2001 From: bogdanvbalan <32824086+bogdanvbalan@users.noreply.github.com> Date: Fri, 21 Feb 2025 03:30:43 +0200 Subject: [PATCH] Update name of merged file (#1716) Co-authored-by: bogdan.balan1 Co-authored-by: Alon Girmonsky <1990761+alongir@users.noreply.github.com> --- cmd/pcapDumpRunner.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/cmd/pcapDumpRunner.go b/cmd/pcapDumpRunner.go index 52947a5f8..5aacb7023 100644 --- a/cmd/pcapDumpRunner.go +++ b/cmd/pcapDumpRunner.go @@ -341,13 +341,18 @@ func copyPcapFiles(clientset *kubernetes.Clientset, config *rest.Config, destDir // Remove the original files after merging for _, file := range copiedFiles { - if err := os.Remove(file); err != nil { + if err = os.Remove(file); err != nil { log.Debug().Err(err).Msgf("error removing file %s", file) } } + clusterID, err := getClusterID(clientset) + if err != nil { + return fmt.Errorf("failed to get cluster ID: %w", err) + } + timestamp := time.Now().Format("2006-01-02_15-04") // Rename the temp file to the final name - finalMergedFile := strings.TrimSuffix(tempMergedFile, "_temp") + finalMergedFile := filepath.Join(destDir, fmt.Sprintf("%s-%s.pcap", clusterID, timestamp)) err = os.Rename(tempMergedFile, finalMergedFile) if err != nil { return err @@ -356,3 +361,11 @@ func copyPcapFiles(clientset *kubernetes.Clientset, config *rest.Config, destDir log.Info().Msgf("Merged file created: %s", finalMergedFile) return nil } + +func getClusterID(clientset *kubernetes.Clientset) (string, error) { + namespace, err := clientset.CoreV1().Namespaces().Get(context.TODO(), "kube-system", metav1.GetOptions{}) + if err != nil { + return "", fmt.Errorf("failed to get kube-system namespace UID: %w", err) + } + return string(namespace.UID), nil +}