-
Notifications
You must be signed in to change notification settings - Fork 705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Load chart and provide access in both v2 and v3 structs. #1362
Changes from 3 commits
755257d
fc975bb
62746f9
5dd2caa
287d876
22164c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,9 +35,12 @@ import ( | |
"github.com/ghodss/yaml" | ||
appRepov1 "github.com/kubeapps/kubeapps/cmd/apprepository-controller/pkg/apis/apprepository/v1alpha1" | ||
appRepoClientSet "github.com/kubeapps/kubeapps/cmd/apprepository-controller/pkg/client/clientset/versioned" | ||
helm3chart "helm.sh/helm/v3/pkg/chart" | ||
helm3loader "helm.sh/helm/v3/pkg/chart/loader" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/helm/pkg/proto/hapi/chart" | ||
helm2loader "k8s.io/helm/pkg/chartutil" | ||
helm2chart "k8s.io/helm/pkg/proto/hapi/chart" | ||
"k8s.io/helm/pkg/repo" | ||
) | ||
|
||
|
@@ -78,31 +81,37 @@ type HTTPClient interface { | |
Do(req *http.Request) (*http.Response, error) | ||
} | ||
|
||
// LoadChart should return a Chart struct from an IOReader | ||
type LoadChart func(in io.Reader) (*chart.Chart, error) | ||
type ChartMultiVersion struct { | ||
Helm2Chart *helm2chart.Chart | ||
Helm3Chart *helm3chart.Chart | ||
} | ||
|
||
// LoadHelm2Chart should return a helm2 Chart struct from an IOReader | ||
type LoadHelm2Chart func(in io.Reader) (*helm2chart.Chart, error) | ||
|
||
// LoadHelm3Chart returns a helm3 Chart struct from an IOReader | ||
type LoadHelm3Chart func(in io.Reader) (*helm3chart.Chart, error) | ||
|
||
// Resolver for exposed funcs | ||
type Resolver interface { | ||
ParseDetails(data []byte) (*Details, error) | ||
GetChart(details *Details, netClient HTTPClient) (*chart.Chart, error) | ||
GetChart(details *Details, netClient HTTPClient) (*ChartMultiVersion, error) | ||
InitNetClient(details *Details) (HTTPClient, error) | ||
} | ||
|
||
// Chart struct contains the clients required to retrieve charts info | ||
type Chart struct { | ||
// ChartClient struct contains the clients required to retrieve charts info | ||
type ChartClient struct { | ||
kubeClient kubernetes.Interface | ||
appRepoClient appRepoClientSet.Interface | ||
load LoadChart | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAICT, the I initially packaged the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it was only for testing purposes. What I don't like about using real data is that you cannot modify the input (so you are only testing the happy path). Since here we are only testing the happy path anyway I am okay with the change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is possible to both use real data and modify the input - I've just not done so here because it wasn't necessary for my simple test. But if we want to in the future, we can extend this with something like the helm tests for the archive loader, in https://github.com/helm/helm/blob/master/pkg/chart/loader/archive_test.go where you generate the archive with the setup for your test. |
||
userAgent string | ||
appRepo *appRepov1.AppRepository | ||
} | ||
|
||
// NewChart returns a new Chart | ||
func NewChart(kubeClient kubernetes.Interface, appRepoClient appRepoClientSet.Interface, load LoadChart, userAgent string) *Chart { | ||
return &Chart{ | ||
// NewChartClient returns a new ChartClient | ||
func NewChartClient(kubeClient kubernetes.Interface, appRepoClient appRepoClientSet.Interface, userAgent string) *ChartClient { | ||
return &ChartClient{ | ||
kubeClient: kubeClient, | ||
appRepoClient: appRepoClient, | ||
load: load, | ||
userAgent: userAgent, | ||
} | ||
} | ||
|
@@ -225,7 +234,7 @@ func findChartInRepoIndex(repoIndex *repo.IndexFile, repoURL, chartName, chartVe | |
} | ||
|
||
// fetchChart returns the Chart content given an URL | ||
func fetchChart(netClient *HTTPClient, chartURL string, load LoadChart) (*chart.Chart, error) { | ||
func fetchChart(netClient *HTTPClient, chartURL string) (*ChartMultiVersion, error) { | ||
req, err := getReq(chartURL) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -239,11 +248,19 @@ func fetchChart(netClient *HTTPClient, chartURL string, load LoadChart) (*chart. | |
if err != nil { | ||
return nil, err | ||
} | ||
return load(bytes.NewReader(data)) | ||
helm2Chart, err := helm2loader.LoadArchive(bytes.NewReader(data)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
helm3Chart, err := helm3loader.LoadArchive(bytes.NewReader(data)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &ChartMultiVersion{Helm2Chart: helm2Chart, Helm3Chart: helm3Chart}, nil | ||
} | ||
|
||
// ParseDetails return Chart details | ||
func (c *Chart) ParseDetails(data []byte) (*Details, error) { | ||
func (c *ChartClient) ParseDetails(data []byte) (*Details, error) { | ||
details := &Details{} | ||
err := json.Unmarshal(data, details) | ||
if err != nil { | ||
|
@@ -278,7 +295,7 @@ func (c *clientWithDefaultHeaders) Do(req *http.Request) (*http.Response, error) | |
|
||
// InitNetClient returns an HTTP client based on the chart details loading a | ||
// custom CA if provided (as a secret) | ||
func (c *Chart) InitNetClient(details *Details) (HTTPClient, error) { | ||
func (c *ChartClient) InitNetClient(details *Details) (HTTPClient, error) { | ||
// Require the SystemCertPool unless the env var is explicitly set. | ||
caCertPool, err := x509.SystemCertPool() | ||
if err != nil { | ||
|
@@ -342,8 +359,9 @@ func (c *Chart) InitNetClient(details *Details) (HTTPClient, error) { | |
}, nil | ||
} | ||
|
||
// GetChart retrieves and loads a Chart from a registry | ||
func (c *Chart) GetChart(details *Details, netClient HTTPClient) (*chart.Chart, error) { | ||
// GetChart retrieves and loads a Chart from a registry in both | ||
// v2 and v3 formats. | ||
func (c *ChartClient) GetChart(details *Details, netClient HTTPClient) (*ChartMultiVersion, error) { | ||
repoURL := c.appRepo.Spec.URL | ||
if repoURL == "" { | ||
// FIXME: Make configurable | ||
|
@@ -363,9 +381,10 @@ func (c *Chart) GetChart(details *Details, netClient HTTPClient) (*chart.Chart, | |
} | ||
|
||
log.Printf("Downloading %s ...", chartURL) | ||
chartRequested, err := fetchChart(&netClient, chartURL, c.load) | ||
chart, err := fetchChart(&netClient, chartURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return chartRequested, nil | ||
|
||
return chart, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,13 +32,15 @@ func (f *FakeChart) ParseDetails(data []byte) (*chartUtils.Details, error) { | |
return details, err | ||
} | ||
|
||
func (f *FakeChart) GetChart(details *chartUtils.Details, netClient chartUtils.HTTPClient) (*chart.Chart, error) { | ||
return &chart.Chart{ | ||
Metadata: &chart.Metadata{ | ||
Name: details.ChartName, | ||
}, | ||
Values: &chart.Config{ | ||
Raw: details.Values, | ||
func (f *FakeChart) GetChart(details *chartUtils.Details, netClient chartUtils.HTTPClient) (*chartUtils.ChartMultiVersion, error) { | ||
return &chartUtils.ChartMultiVersion{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should there be no There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently the |
||
Helm2Chart: &chart.Chart{ | ||
Metadata: &chart.Metadata{ | ||
Name: details.ChartName, | ||
}, | ||
Values: &chart.Config{ | ||
Raw: details.Values, | ||
}, | ||
}, | ||
}, nil | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I've mentioned this before, but I found it confusing that we had a struct called
Chart
which wasn't a chart, but a struct containing clients for getting chart details. Hence the rename.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's way less ambiguous