Add debug logging for helm pull command

Implemented debug output feature for HTTP-level logs
in the helm pull command. This addresses the issue
where the --debug flag was ignored during helm pull.

Fixes #31098

Signed-off-by: Sowmith Kuppa <soumith.odu@gmail.com>
pull/31108/head
Sowmith Kuppa 2 months ago
parent 89f391e674
commit df457d5c56

@ -20,9 +20,9 @@ import (
"fmt" "fmt"
"io" "io"
"io/fs" "io/fs"
"net/url"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -80,32 +80,37 @@ type ChartDownloader struct {
type debugTransport struct { type debugTransport struct {
*http.Transport *http.Transport
out io.Writer out io.Writer
debug bool
} }
func (t *debugTransport) RoundTrip(req *http.Request) (*http.Response, error) { func (t *debugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if t.debug {
fmt.Fprintf(t.out, "DEBUG: HTTP request to %s\n", req.URL.String()) fmt.Fprintf(t.out, "DEBUG: HTTP request to %s\n", req.URL.String())
// Log the request // Log the request
reqDump, err := httputil.DumpRequestOut(req, false) reqDump, err := httputil.DumpRequestOut(req, false)
if err == nil { if err == nil {
fmt.Fprintf(t.out, "%s\n", reqDump) fmt.Fprintf(t.out, "%s\n", reqDump)
} }
}
// Perform the request // Perform the request
resp, err := t.Transport.RoundTrip(req) resp, err := t.Transport.RoundTrip(req)
if err != nil { if err != nil {
if t.debug {
fmt.Fprintf(t.out, "HTTP request failed: %v\n", err) fmt.Fprintf(t.out, "HTTP request failed: %v\n", err)
}
return nil, err return nil, err
} }
// Log the response // Log the response
if t.debug {
respDump, err := httputil.DumpResponse(resp, false) respDump, err := httputil.DumpResponse(resp, false)
if err == nil { if err == nil {
fmt.Fprintf(t.out, "HTTP Response: \n%s\n", respDump) fmt.Fprintf(t.out, "HTTP Response: \n%s\n", respDump)
} }
if resp.StatusCode >= 300 && resp.StatusCode < 400 { if resp.StatusCode >= 300 && resp.StatusCode < 400 {
location, _ := resp.Header["Location"] location := resp.Header["Location"]
fmt.Fprintf(t.out, "DEBUG: Redirect to: %v\n", location) fmt.Fprintf(t.out, "DEBUG: Redirect to: %v\n", location)
} }
}
return resp, err return resp, err
} }
@ -123,17 +128,24 @@ func (t *debugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *provenance.Verification, error) { func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *provenance.Verification, error) {
u, err := c.ResolveChartVersion(ref, version) u, err := c.ResolveChartVersion(ref, version)
if err != nil { if err != nil {
if c.Debug {
fmt.Fprintf(c.Out, "DEBUG: Failed to resolve chart version: %v\n", err) fmt.Fprintf(c.Out, "DEBUG: Failed to resolve chart version: %v\n", err)
}
return "", nil, err return "", nil, err
} }
if c.Debug {
fmt.Fprintf(c.Out, "DEBUG: Resolved chart URL: %s\n", u.String()) fmt.Fprintf(c.Out, "DEBUG: Resolved chart URL: %s\n", u.String())
}
g, err := c.Getters.ByScheme(u.Scheme) g, err := c.Getters.ByScheme(u.Scheme)
if err != nil { if err != nil {
if c.Debug {
fmt.Fprintf(c.Out, "DEBUG: Failed to get getter for scheme %s: %v\n", u.Scheme, err) fmt.Fprintf(c.Out, "DEBUG: Failed to get getter for scheme %s: %v\n", u.Scheme, err)
}
return "", nil, err return "", nil, err
} }
if c.Debug {
fmt.Fprintf(c.Out, "DEBUG: Using getter for scheme: %s\n", u.Scheme) fmt.Fprintf(c.Out, "DEBUG: Using getter for scheme: %s\n", u.Scheme)
}
c.Options = append(c.Options, getter.WithAcceptHeader("application/gzip,application/octet-stream")) c.Options = append(c.Options, getter.WithAcceptHeader("application/gzip,application/octet-stream"))
// If debug is enabled, wrap the getter's HTTP client with a debug transport // If debug is enabled, wrap the getter's HTTP client with a debug transport
@ -141,19 +153,24 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven
dt := &debugTransport{ dt := &debugTransport{
Transport: http.DefaultTransport.(*http.Transport).Clone(), Transport: http.DefaultTransport.(*http.Transport).Clone(),
out: c.Out, out: c.Out,
debug: c.Debug,
} }
dt.Transport.DisableKeepAlives = true dt.DisableKeepAlives = true
c.Options = append(c.Options, getter.WithClient(&http.Client{ c.Options = append(c.Options, getter.WithClient(&http.Client{
Transport: dt, Transport: dt,
CheckRedirect: func(req *http.Request, via []*http.Request) error { CheckRedirect: func(req *http.Request, _ []*http.Request) error {
if c.Debug {
fmt.Fprintf(c.Out, "DEBUG: Following redirect to %s\n", req.URL.String()) fmt.Fprintf(c.Out, "DEBUG: Following redirect to %s\n", req.URL.String())
}
return nil return nil
}, },
})) }))
} }
data, err := g.Get(u.String(), c.Options...) data, err := g.Get(u.String(), c.Options...)
if err != nil { if err != nil {
if c.Debug {
fmt.Fprintf(c.Out, "DEBUG: Failed to fetch chart: %v\n", err) fmt.Fprintf(c.Out, "DEBUG: Failed to fetch chart: %v\n", err)
}
return "", nil, err return "", nil, err
} }
@ -177,11 +194,13 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven
Transport: http.DefaultTransport.(*http.Transport).Clone(), Transport: http.DefaultTransport.(*http.Transport).Clone(),
out: c.Out, out: c.Out,
} }
dt.Transport.DisableKeepAlives = true dt.DisableKeepAlives = true
c.Options = append(c.Options, getter.WithClient(&http.Client{ c.Options = append(c.Options, getter.WithClient(&http.Client{
Transport: dt, Transport: dt,
CheckRedirect: func(req *http.Request, via []*http.Request) error { CheckRedirect: func(req *http.Request, _ []*http.Request) error {
if c.Debug {
fmt.Fprintf(c.Out, "DEBUG: Following redirect to %s\n", req.URL.String()) fmt.Fprintf(c.Out, "DEBUG: Following redirect to %s\n", req.URL.String())
}
return nil return nil
}, },
})) }))

@ -35,7 +35,6 @@ type HTTPGetter struct {
once sync.Once once sync.Once
} }
// Get performs a Get from repo.Getter and returns the body. // Get performs a Get from repo.Getter and returns the body.
func (g *HTTPGetter) Get(href string, options ...Option) (*bytes.Buffer, error) { func (g *HTTPGetter) Get(href string, options ...Option) (*bytes.Buffer, error) {
for _, opt := range options { for _, opt := range options {

Loading…
Cancel
Save