Skip to content

Commit

Permalink
Add chart Debug method
Browse files Browse the repository at this point in the history
  • Loading branch information
mallardduck committed Dec 17, 2024
1 parent cf096ea commit eb474f7
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ dev/
kubeconfig*.yaml
get_helm.sh
Dockerfile.dapper*
!Dockerfile.dapper
!Dockerfile.dapper
/.debug
2 changes: 2 additions & 0 deletions cmd/prometheus-federator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -84,5 +85,6 @@ func main() {
Version: version.FriendlyVersion(),
})
cmd = command.AddDebug(cmd, &debugConfig)
cmd.AddCommand(debug.ChartDebugSubCommand(base64TgzChart))
command.Main(cmd)
}
106 changes: 106 additions & 0 deletions pkg/debug/chart.go
Original file line number Diff line number Diff line change
@@ -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)
},
}
}

0 comments on commit eb474f7

Please sign in to comment.