Skip to content

Commit

Permalink
Use repo URL from the app repository (no longer requiring Details.Rep…
Browse files Browse the repository at this point in the history
…oURL). (#1150)

* Use repo URL from the app repository (no longer requiring Details.RepoURL).

* Use const for repo name
  • Loading branch information
absoludity authored Sep 5, 2019
1 parent ad5ea56 commit 272c971
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 30 deletions.
12 changes: 7 additions & 5 deletions pkg/chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,16 @@ type Chart struct {
appRepoClient appRepoClientSet.Interface
load LoadChart
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{
kubeClient,
appRepoClient,
load,
userAgent,
kubeClient: kubeClient,
appRepoClient: appRepoClient,
load: load,
userAgent: userAgent,
}
}

Expand Down Expand Up @@ -306,6 +307,7 @@ func (c *Chart) InitNetClient(details *Details) (HTTPClient, error) {
if err != nil {
return nil, fmt.Errorf("unable to get app repository %q: %v", details.AppRepositoryResourceName, err)
}
c.appRepo = appRepo
auth = &appRepo.Spec.Auth
} else {
auth = &details.Auth
Expand Down Expand Up @@ -353,7 +355,7 @@ func (c *Chart) InitNetClient(details *Details) (HTTPClient, error) {

// GetChart retrieves and loads a Chart from a registry
func (c *Chart) GetChart(details *Details, netClient HTTPClient) (*chart.Chart, error) {
repoURL := details.RepoURL
repoURL := c.appRepo.Spec.URL
if repoURL == "" {
// FIXME: Make configurable
repoURL = defaultRepoURL
Expand Down
65 changes: 40 additions & 25 deletions pkg/chart/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ func TestInitNetClient(t *testing.T) {
},
}
}
appRepoClient := fakeAppRepo.NewSimpleClientset(&appRepov1.AppRepository{
expectedAppRepo := &appRepov1.AppRepository{
ObjectMeta: metav1.ObjectMeta{
Name: tc.details.AppRepositoryResourceName,
Namespace: metav1.NamespaceSystem,
Expand All @@ -459,7 +459,8 @@ func TestInitNetClient(t *testing.T) {
CustomCA: customCA,
},
},
})
}
appRepoClient := fakeAppRepo.NewSimpleClientset(expectedAppRepo)
chUtils := Chart{
kubeClient: kubeClient,
appRepoClient: appRepoClient,
Expand Down Expand Up @@ -502,13 +503,20 @@ func TestInitNetClient(t *testing.T) {
t.Errorf("got: %q, want: %q", got, want)
}
}

// If an app repo name was sent, the client is initialized with the expected app repo
if tc.details.AppRepositoryResourceName != "" {
if got, want := chUtils.appRepo, expectedAppRepo; !cmp.Equal(got, want) {
t.Errorf(cmp.Diff(got, want))
}
}
})
}
}

// Fake server for repositories and charts
type fakeHTTPClient struct {
repoURLs []string
repoURL string
chartURLs []string
index *repo.IndexFile
userAgent string
Expand All @@ -522,15 +530,13 @@ func (f *fakeHTTPClient) Do(h *http.Request) (*http.Response, error) {
if f.userAgent != "" && h.Header.Get("User-Agent") != f.userAgent {
return nil, fmt.Errorf("Wrong user agent: %s", h.Header.Get("User-Agent"))
}
for _, repoURL := range f.repoURLs {
if h.URL.String() == fmt.Sprintf("%sindex.yaml", repoURL) {
// Return fake chart index (not customizable per repo)
body, err := json.Marshal(*f.index)
if err != nil {
return nil, err
}
return &http.Response{StatusCode: 200, Body: ioutil.NopCloser(bytes.NewReader(body))}, nil
if h.URL.String() == fmt.Sprintf("%sindex.yaml", f.repoURL) {
// Return fake chart index
body, err := json.Marshal(*f.index)
if err != nil {
return nil, err
}
return &http.Response{StatusCode: 200, Body: ioutil.NopCloser(bytes.NewReader(body))}, nil
}
for _, chartURL := range f.chartURLs {
if h.URL.String() == chartURL {
Expand All @@ -539,18 +545,16 @@ func (f *fakeHTTPClient) Do(h *http.Request) (*http.Response, error) {
}
}
// Unexpected path
return &http.Response{StatusCode: 404}, fmt.Errorf("Unexpected path")
return &http.Response{StatusCode: 404}, fmt.Errorf("Unexpected path %q for chartURLs %+v", h.URL.String(), f.chartURLs)
}

func newHTTPClient(charts []Details, userAgent string) HTTPClient {
var repoURLs []string
func newHTTPClient(repoURL string, charts []Details, userAgent string) HTTPClient {
var chartURLs []string
entries := map[string]repo.ChartVersions{}
// Populate Chart registry with content of the given helmReleases
for _, ch := range charts {
repoURLs = append(repoURLs, ch.RepoURL)
chartMeta := chart.Metadata{Name: ch.ChartName, Version: ch.Version}
chartURL := fmt.Sprintf("%s%s-%s.tgz", ch.RepoURL, ch.ChartName, ch.Version)
chartURL := fmt.Sprintf("%s%s-%s.tgz", repoURL, ch.ChartName, ch.Version)
chartURLs = append(chartURLs, chartURL)
chartVersion := repo.ChartVersion{Metadata: &chartMeta, URLs: []string{chartURL}}
chartVersions := []*repo.ChartVersion{&chartVersion}
Expand All @@ -559,7 +563,7 @@ func newHTTPClient(charts []Details, userAgent string) HTTPClient {
index := &repo.IndexFile{APIVersion: "v1", Generated: time.Now(), Entries: entries}
return &clientWithDefaultHeaders{
client: &fakeHTTPClient{
repoURLs: repoURLs,
repoURL: repoURL,
chartURLs: chartURLs,
index: index,
userAgent: userAgent,
Expand All @@ -582,11 +586,12 @@ func getFakeClientRequests(t *testing.T, c HTTPClient) []*http.Request {
}

func TestGetChart(t *testing.T) {
const repoName = "foo-repo"
target := Details{
RepoURL: "http://foo.com/",
ChartName: "test",
ReleaseName: "foo",
Version: "1.0.0",
AppRepositoryResourceName: repoName,
ChartName: "test",
ReleaseName: "foo",
Version: "1.0.0",
}
testCases := []struct {
name string
Expand All @@ -602,19 +607,29 @@ func TestGetChart(t *testing.T) {
},
}

const repoURL = "http://foo.com/"
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
httpClient := newHTTPClient([]Details{target}, tc.userAgent)
httpClient := newHTTPClient(repoURL, []Details{target}, tc.userAgent)
kubeClient := fakeK8s.NewSimpleClientset()
chUtils := Chart{
kubeClient: kubeClient,
load: fakeLoadChart,
userAgent: tc.userAgent,
appRepo: &appRepov1.AppRepository{
ObjectMeta: metav1.ObjectMeta{
Name: repoName,
Namespace: metav1.NamespaceSystem,
},
Spec: appRepov1.AppRepositorySpec{
URL: repoURL,
},
},
}
ch, err := chUtils.GetChart(&target, httpClient)

if err != nil {
t.Errorf("Unexpected error: %v", err)
t.Fatalf("Unexpected error: %v", err)
}
// Currently tests return an empty chart object.
if got, want := ch, &(chart.Chart{}); !cmp.Equal(got, want) {
Expand All @@ -628,8 +643,8 @@ func TestGetChart(t *testing.T) {
}

for i, url := range []string{
target.RepoURL + "index.yaml",
fmt.Sprintf("%s%s-%s.tgz", target.RepoURL, target.ChartName, target.Version),
chUtils.appRepo.Spec.URL + "index.yaml",
fmt.Sprintf("%s%s-%s.tgz", chUtils.appRepo.Spec.URL, target.ChartName, target.Version),
} {
if got, want := requests[i].URL.String(), url; got != want {
t.Errorf("got: %q, want: %q", got, want)
Expand Down

0 comments on commit 272c971

Please sign in to comment.