Commit b2f7a874 authored by Michelle Noorali's avatar Michelle Noorali

ref(helm): fix helm update

parent df4dc3e1
......@@ -47,7 +47,7 @@ func runRepoAdd(cmd *cobra.Command, args []string) error {
}
name, url := args[0], args[1]
if err := downloadIndexFile(name, url); err != nil {
if err := repo.DownloadIndexFile(name, url, cacheDirectory(name, "-index.yaml")); err != nil {
return errors.New("Oops! Looks like " + url + " is not a valid chart repository or cannot be reached\n")
}
......
......@@ -3,15 +3,9 @@ package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"sync"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"github.com/kubernetes/helm/pkg/repo"
)
......@@ -52,7 +46,8 @@ func updateCharts(repos map[string]string, verbose bool) {
wg.Add(1)
go func(n, u string) {
defer wg.Done()
err := downloadIndexFile(n, u)
indexFileName := cacheDirectory(n + "-index.yaml")
err := repo.DownloadIndexFile(n, u, indexFileName)
if err != nil {
updateErr := "...Unable to get an update from the " + n + " chart repository"
if verbose {
......@@ -67,37 +62,3 @@ func updateCharts(repos map[string]string, verbose bool) {
wg.Wait()
fmt.Println("Update Complete. Happy Helming!")
}
func downloadIndexFile(name, url string) error {
var indexURL string
indexURL = strings.TrimSuffix(url, "/") + "/index.yaml"
resp, err := http.Get(indexURL)
if err != nil {
return err
}
defer resp.Body.Close()
var indexFile *os.File
var r repo.RepoFile
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if err := yaml.Unmarshal(b, &r); err != nil {
return err
}
indexFile, err = os.Create(cacheDirectory(name + "-index.yaml"))
if err != nil {
return err
}
if _, err := io.Copy(indexFile, resp.Body); err != nil {
return err
}
return nil
}
package repo
import (
"io/ioutil"
"net/http"
"strings"
"gopkg.in/yaml.v2"
)
// IndexFile represents the index file in a chart repository
type IndexFile struct {
Entries map[string]*ChartRef
}
// ChartRef represents a chart entry in the IndexFile
type ChartRef struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
Keywords []string `yaml:"keywords"`
Removed bool `yaml:"removed,omitempty"`
}
// DownloadIndexFile uses
func DownloadIndexFile(repoName, url, indexFileName string) error {
var indexURL string
indexURL = strings.TrimSuffix(url, "/") + "/index.yaml"
resp, err := http.Get(indexURL)
if err != nil {
return err
}
defer resp.Body.Close()
var r IndexFile
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if err := yaml.Unmarshal(b, &r); err != nil {
return err
}
if err := ioutil.WriteFile(indexFileName, b, 0644); err != nil {
return err
}
return nil
}
package repo
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"gopkg.in/yaml.v2"
)
var (
testRepo = "test-repo"
)
func TestDownloadIndexFile(t *testing.T) {
fileBytes, err := ioutil.ReadFile("testdata/local-index.yaml")
if err != nil {
t.Errorf("%#v", err)
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "binary/octet-stream")
fmt.Fprintln(w, string(fileBytes))
}))
dirName, err := ioutil.TempDir("testdata", "tmp")
path := filepath.Join(dirName, testRepo+"-index.yaml")
if err := DownloadIndexFile(testRepo, ts.URL, path); err != nil {
t.Errorf("%#v", err)
}
if _, err := os.Stat(path); err != nil {
t.Errorf("error finding created index file: %#v", err)
}
b, err := ioutil.ReadFile(path)
if err != nil {
t.Errorf("error reading index file: %#v", err)
}
var i IndexFile
if err = yaml.Unmarshal(b, &i); err != nil {
t.Errorf("error unmarshaling index file: %#v", err)
}
numEntries := len(i.Entries)
if numEntries != 2 {
t.Errorf("Expected 2 entries in index file but got %v", numEntries)
}
os.Remove(path)
}
......@@ -13,19 +13,6 @@ import (
var localRepoPath string
// IndexFile represents the index file in a chart repository
type IndexFile struct {
Entries map[string]*ChartRef
}
// ChartRef represents a chart entry in the IndexFile
type ChartRef struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
Keywords []string `yaml:"keywords"`
Removed bool `yaml:"removed,omitempty"`
}
// StartLocalRepo starts a web server and serves files from the given path
func StartLocalRepo(path string) {
fmt.Println("Now serving you on localhost:8879...")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment