From eb474f74a355c67f99e84772c1c117fce624f8ad Mon Sep 17 00:00:00 2001 From: Dan Pock Date: Mon, 16 Dec 2024 18:29:38 -0500 Subject: [PATCH] Add chart Debug method --- .gitignore | 3 +- cmd/prometheus-federator/main.go | 2 + pkg/debug/chart.go | 106 +++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 pkg/debug/chart.go diff --git a/.gitignore b/.gitignore index 0d4b31bd..8c7ea87f 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,5 @@ dev/ kubeconfig*.yaml get_helm.sh Dockerfile.dapper* -!Dockerfile.dapper \ No newline at end of file +!Dockerfile.dapper +/.debug \ No newline at end of file diff --git a/cmd/prometheus-federator/main.go b/cmd/prometheus-federator/main.go index 25ff2550..9941b45a 100644 --- a/cmd/prometheus-federator/main.go +++ b/cmd/prometheus-federator/main.go @@ -9,6 +9,7 @@ import ( _ "net/http/pprof" "os" + "github.com/rancher/prometheus-federator/pkg/debug" "github.com/rancher/prometheus-federator/pkg/helm-project-operator/controllers/common" "github.com/rancher/prometheus-federator/pkg/helm-project-operator/operator" "github.com/rancher/prometheus-federator/pkg/version" @@ -84,5 +85,6 @@ func main() { Version: version.FriendlyVersion(), }) cmd = command.AddDebug(cmd, &debugConfig) + cmd.AddCommand(debug.ChartDebugSubCommand(base64TgzChart)) command.Main(cmd) } diff --git a/pkg/debug/chart.go b/pkg/debug/chart.go new file mode 100644 index 00000000..d6a0dfc3 --- /dev/null +++ b/pkg/debug/chart.go @@ -0,0 +1,106 @@ +package debug + +import ( + "archive/tar" + "bytes" + "compress/gzip" + "encoding/base64" + "fmt" + "github.com/spf13/cobra" + "io" + "os" + "path/filepath" +) + +func ChartDebugSubCommand(base64ChartTarball string) *cobra.Command { + return &cobra.Command{ + Use: "debug-chart", + Short: "This command helps debug the internal chart files", + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("TODO debug") + // TODO: use straing data from base64ChartTarball to: + // Un-base64 the data to get a raw tgz file, + // Prompt for user input - in the future, for now just use local CWD, + // Save the files of the charts tar extracted to the local CWD + chartTarballData, err := base64.StdEncoding.DecodeString(base64ChartTarball) + if err != nil { + fmt.Println("error:", err) + return + } + + gzipReader, err := gzip.NewReader(bytes.NewReader(chartTarballData)) + if err != nil { + fmt.Println("Error creating gzip reader:", err) + return + } + defer gzipReader.Close() + + // Create a tar reader + tarReader := tar.NewReader(gzipReader) + + // Extract files to the current working directory + cwd, err := os.Getwd() + if err != nil { + fmt.Println("Error getting current working directory:", err) + return + } + debugDir := filepath.Join(cwd, ".debug") + // Ensure the .debug directory exists + if err := os.MkdirAll(debugDir, 0755); err != nil { + fmt.Println("Error creating .debug directory:", err) + return + } + + // Extract files to the .debug directory + for { + header, err := tarReader.Next() + if err == io.EOF { + // End of archive + break + } + if err != nil { + fmt.Println("Error reading tarball:", err) + return + } + + // Determine the path to extract the file to + filePath := filepath.Join(debugDir, header.Name) + + switch header.Typeflag { + case tar.TypeDir: + // Create directory + if err := os.MkdirAll(filePath, os.FileMode(header.Mode)); err != nil { + fmt.Println("Error creating directory:", err) + return + } + case tar.TypeReg: + // Ensure the parent directory exists + parentDir := filepath.Dir(filePath) + if err := os.MkdirAll(parentDir, 0755); err != nil { + fmt.Println("Error creating parent directory:", err) + return + } + + // Create regular file + outFile, err := os.Create(filePath) + if err != nil { + fmt.Println("Error creating file:", err) + return + } + + // Copy file content + if _, err := io.Copy(outFile, tarReader); err != nil { + fmt.Println("Error writing file content:", err) + outFile.Close() + return + } + outFile.Close() + default: + fmt.Printf("Skipping unsupported file type: %s\n", header.Name) + } + } + + fmt.Println("Chart files successfully extracted to", debugDir) + }, + } +}