Skip to content

Commit

Permalink
Support server side context path
Browse files Browse the repository at this point in the history
  • Loading branch information
shizhMSFT committed Aug 8, 2018
1 parent 547fa24 commit 0e757f0
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cmd/helmpush/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,15 @@ func (p *pushCmd) push() error {
return err
}

// update context path if not overrided
if p.contextPath == "" {
index, err := helm.GetIndexByRepo(repo, getIndexDownloader(client))
if err != nil {
return err
}
client.Option(cm.ContextPath(index.ContextPath))
}

tmp, err := ioutil.TempDir("", "helm-push-")
if err != nil {
return err
Expand Down Expand Up @@ -335,6 +344,24 @@ func getChartmuseumError(b []byte, code int) error {
return fmt.Errorf("%d: %s", code, er.Error)
}

func getIndexDownloader(client *cm.Client) helm.IndexDownloader {
return func() ([]byte, error) {
resp, err := client.DownloadFile("index.yaml")
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, getChartmuseumError(b, resp.StatusCode)
}
return b, nil
}
}

func main() {
cmd := newPushCmd(os.Args[1:])
if err := cmd.Execute(); err != nil {
Expand Down
48 changes: 48 additions & 0 deletions pkg/helm/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package helm

import (
"io/ioutil"

"github.com/ghodss/yaml"
"k8s.io/helm/pkg/repo"
)

type (
// Index represents the index file in a chart repository
Index struct {
*repo.IndexFile
ContextPath string `json:"contextPath"`
}

// IndexDownloader is a function to download the index
IndexDownloader func() ([]byte, error)
)

// GetIndexByRepo returns index by repository
func GetIndexByRepo(repo *Repo, downloadIndex IndexDownloader) (*Index, error) {
if repo.Cache != "" {
return GetIndexByDownloader(func() ([]byte, error) {
return ioutil.ReadFile(repo.Cache)
})
}
return GetIndexByDownloader(downloadIndex)
}

// GetIndexByDownloader takes binary data from IndexDownloader and returns an Index object
func GetIndexByDownloader(downloadIndex IndexDownloader) (*Index, error) {
b, err := downloadIndex()
if err != nil {
return nil, err
}
return LoadIndex(b)
}

// LoadIndex loads an index file
func LoadIndex(data []byte) (*Index, error) {
i := &Index{}
if err := yaml.Unmarshal(data, i); err != nil {
return i, err
}
i.SortEntries()
return i, nil
}
25 changes: 25 additions & 0 deletions pkg/helm/index_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package helm

import (
"testing"
)

func TestLoadIndex(t *testing.T) {
// No context path
index, err := LoadIndex([]byte("apiVersion: v1\nentries: {}\ngenerated: \"2018-08-08T08:21:33Z\"\n"))
if err != nil {
t.Error("unexpected error loading index", err)
}
if index.ContextPath != "" {
t.Errorf("expexted empty context path, instead got %s", index.ContextPath)
}

// Has context path
index, err = LoadIndex([]byte("apiVersion: v1\ncontextPath: /helm/v1\nentries: {}\ngenerated: \"2018-08-08T08:21:33Z\"\n"))
if err != nil {
t.Error("unexpected error loading index", err)
}
if index.ContextPath != "/helm/v1" {
t.Errorf("expexted context path to be /helm/v1, instead got %s", index.ContextPath)
}
}

0 comments on commit 0e757f0

Please sign in to comment.