From 2a41c2a28162b317157e9348596a3867b5f28e68 Mon Sep 17 00:00:00 2001 From: Gabriel Meghnagi Date: Sun, 5 Jan 2025 19:18:20 +0100 Subject: [PATCH] [RFE] close #173 --- cmd/use/helpers.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++ cmd/use/use.go | 41 ++++++++++++++++++++++++++++++---- 2 files changed, 92 insertions(+), 4 deletions(-) diff --git a/cmd/use/helpers.go b/cmd/use/helpers.go index 01e7c098..00447d24 100644 --- a/cmd/use/helpers.go +++ b/cmd/use/helpers.go @@ -3,6 +3,7 @@ package use import ( "archive/tar" "archive/zip" + "bufio" "compress/gzip" "fmt" "io" @@ -437,3 +438,57 @@ func extractTarXZ(xzFile string, destinationdir string) (string, error) { } return ExtractTarStream(xzReader, destinationdir) } + +func extractClientVersion(mustGatherLogsFilePath string) string { + filePath := mustGatherLogsFilePath + clientVersion := "" + // Open the file + file, err := os.Open(filePath) + if err != nil { + return "" + } + defer file.Close() + + // Initialize a scanner to read the file line by line + scanner := bufio.NewScanner(file) + + // Variable to store the matching line + var clientVersionLine string + + // Counter for the first 20 lines + lineCount := 0 + + // Read the file line by line + for scanner.Scan() { + line := scanner.Text() + lineCount++ + + // Check if the line starts with "ClientVersion: " + if strings.HasPrefix(line, "ClientVersion: ") { + clientVersionLine = line + break // Exit the loop as we found the line + } + + // Stop checking after 20 lines as it should be at line 4 + if lineCount >= 20 { + break + } + } + + // Handle potential scanning error + if err := scanner.Err(); err != nil { + fmt.Println("Error reading file:", err) + return "" + } + + // Check if we found the line and print the result + if clientVersionLine != "" { + parts := strings.Split(clientVersionLine, ":") + if len(parts) == 2 { + // Trim spaces and get the version part + clientVersion = strings.TrimSpace(parts[1]) + return clientVersion + } + } + return "" +} diff --git a/cmd/use/use.go b/cmd/use/use.go index 37b6ffb5..b8d7e8a7 100644 --- a/cmd/use/use.go +++ b/cmd/use/use.go @@ -33,6 +33,8 @@ import ( "sigs.k8s.io/yaml" ) +var singleNamespaceInMustGather bool + func useContext(path string, omcConfigFile string, idFlag string) error { if path != "" { _path, err := findMustGatherIn(path) @@ -82,6 +84,7 @@ func useContext(path string, omcConfigFile string, idFlag string) error { if len(namespaces) == 1 { NewContexts = append(NewContexts, types.Context{Id: ctxId, Path: path, Current: "*", Project: namespaces[0]}) vars.Namespace = namespaces[0] + singleNamespaceInMustGather = true } else { NewContexts = append(NewContexts, types.Context{Id: ctxId, Path: path, Current: "*", Project: defaultProject}) vars.Namespace = defaultProject @@ -149,7 +152,12 @@ func findMustGatherIn(path string) (string, error) { } func MustGatherInfo() { - fmt.Printf("Must-Gather : %s\nProject : %s\n", vars.MustGatherRootPath, vars.Namespace) + fmt.Printf("Must-Gather : %s\n", vars.MustGatherRootPath) + if singleNamespaceInMustGather { + fmt.Printf("Project : %s (single project)\n", vars.Namespace) + } else { + fmt.Printf("Project : %s\n", vars.Namespace) + } InfrastrctureFilePathExists, _ := helpers.Exists(vars.MustGatherRootPath + "/cluster-scoped-resources/config.openshift.io/infrastructures.yaml") if InfrastrctureFilePathExists { _file, _ := os.ReadFile(vars.MustGatherRootPath + "/cluster-scoped-resources/config.openshift.io/infrastructures.yaml") @@ -158,8 +166,8 @@ func MustGatherInfo() { fmt.Println("Error when trying to unmarshal file: " + vars.MustGatherRootPath + "/cluster-scoped-resources/config.openshift.io/infrastructures.yaml") os.Exit(1) } else { - fmt.Printf("ApiServerURL : %s\n", infrastructureList.Items[0].Status.APIServerURL) - fmt.Printf("Platform : %s\n", infrastructureList.Items[0].Status.PlatformStatus.Type) + fmt.Printf("ApiServerURL : %s\n", infrastructureList.Items[0].Status.APIServerURL) + fmt.Printf("Platform : %s\n", infrastructureList.Items[0].Status.PlatformStatus.Type) } } clusterversionFilePathExists, _ := helpers.Exists(vars.MustGatherRootPath + "/cluster-scoped-resources/config.openshift.io/clusterversions/version.yaml") @@ -170,9 +178,34 @@ func MustGatherInfo() { fmt.Println("Error when trying to unmarshal file: " + vars.MustGatherRootPath + "/cluster-scoped-resources/config.openshift.io/clusterversions/version.yaml") os.Exit(1) } else { - fmt.Printf("ClusterID : %s\n", ClusterVersion.Spec.ClusterID) + clusterversion := "" + versionHistory := ClusterVersion.Status.History + for _, version := range versionHistory { + if version.State == "Completed" { + clusterversion = version.Version + break + } + } + + fmt.Printf("ClusterID : %s\n", ClusterVersion.Spec.ClusterID) + fmt.Printf("ClusterVersion : %s\n", clusterversion) } } + mustGatherSplitPath := strings.Split(vars.MustGatherRootPath, "/") + mustGatherParentPath := strings.Join(mustGatherSplitPath[0:(len(mustGatherSplitPath)-1)], "/") + clientVersion := extractClientVersion(mustGatherParentPath + "/must-gather.logs") + if clientVersion != "" { + fmt.Printf("ClientVersion : %s\n", clientVersion) + } + parts := strings.Split(vars.MustGatherRootPath, "/") + if len(parts) > 0 { + lastPart := parts[len(parts)-1] + if strings.Contains(lastPart, "-sha256") { + mustGatherImage := strings.Split(lastPart, "-sha256")[0] + fmt.Printf("Image : %s\n", mustGatherImage) + } + } + } // useCmd represents the use command