From 4b7e4b71bd497efd268c281d2f47e71a301cc2e4 Mon Sep 17 00:00:00 2001 From: Matt Butcher Date: Thu, 20 Oct 2016 15:49:51 -0600 Subject: [PATCH] fix(helm): use 127.0.0.1 instead of localhost This switches the local chart repo to use 127.0.0.1 instead of localhost so that the net library does not negotiate localhost to an IPv6 address, which is known to cause issues in some Docker containers. Breaking Change: When testing on a system that does NOT have IPv4 configured, this will break 'helm serve'. We estimate this will impact none of the current user base. Closes #1410 --- cmd/helm/init.go | 4 +++- cmd/helm/serve.go | 2 +- pkg/repo/local.go | 14 ++++---------- pkg/repo/local_test.go | 8 ++++++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/cmd/helm/init.go b/cmd/helm/init.go index fe8b5c03c..1c1713211 100644 --- a/cmd/helm/init.go +++ b/cmd/helm/init.go @@ -40,7 +40,9 @@ const ( stableRepository = "stable" localRepository = "local" stableRepositoryURL = "http://storage.googleapis.com/kubernetes-charts" - localRepositoryURL = "http://localhost:8879/charts" + // This is the IPv4 loopback, not localhost, because we have to force IPv4 + // for Dockerized Helm: https://github.com/kubernetes/helm/issues/1410 + localRepositoryURL = "http://127.0.0.1:8879/charts" ) type initCmd struct { diff --git a/cmd/helm/serve.go b/cmd/helm/serve.go index 0d27eb4d9..8b1d83a73 100644 --- a/cmd/helm/serve.go +++ b/cmd/helm/serve.go @@ -51,7 +51,7 @@ func newServeCmd(out io.Writer) *cobra.Command { f := cmd.Flags() f.StringVar(&srv.repoPath, "repo-path", helmpath.Home(homePath()).LocalRepository(), "local directory path from which to serve charts") - f.StringVar(&srv.address, "address", "localhost:8879", "address to listen on") + f.StringVar(&srv.address, "address", "127.0.0.1:8879", "address to listen on") return cmd } diff --git a/pkg/repo/local.go b/pkg/repo/local.go index 9ef8a5cad..f13a4d0ac 100644 --- a/pkg/repo/local.go +++ b/pkg/repo/local.go @@ -51,10 +51,6 @@ const indexHTMLTemplate = ` ` -const indexFile = ` -Welcome to the Kubernetes Package manager!\nBrowse charts on localhost:8879/charts! -` - // RepositoryServer is an HTTP handler for serving a chart repository. type RepositoryServer struct { RepoPath string @@ -64,10 +60,8 @@ type RepositoryServer struct { func (s *RepositoryServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { uri := r.URL.Path switch uri { - case "/": - w.Header().Set("Content-Type", "text/plain; charset=utf-8") - fmt.Fprintf(w, indexFile) - case "/charts/", "/charts/index.html", "/charts/index": + case "/", "/charts/", "/charts/index.html", "/charts/index": + w.Header().Set("Content-Type", "text/html; charset=utf-8") s.htmlIndex(w, r) default: file := strings.TrimPrefix(uri, "/charts/") @@ -78,7 +72,7 @@ func (s *RepositoryServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { // StartLocalRepo starts a web server and serves files from the given path func StartLocalRepo(path, address string) error { if address == "" { - address = ":8879" + address = "127.0.0.1:8879" } s := &RepositoryServer{RepoPath: path} return http.ListenAndServe(address, s) @@ -130,7 +124,7 @@ func Reindex(ch *chart.Chart, path string) error { return err } - y.Add(ch.Metadata, name+".tgz", "http://localhost:8879/charts", "sha256:"+dig) + y.Add(ch.Metadata, name+".tgz", "http://127.0.0.1:8879/charts", "sha256:"+dig) out, err := yaml.Marshal(y) if err != nil { diff --git a/pkg/repo/local_test.go b/pkg/repo/local_test.go index e6fdb4f4d..3e2ecbbcf 100644 --- a/pkg/repo/local_test.go +++ b/pkg/repo/local_test.go @@ -25,16 +25,20 @@ import ( ) func TestRepositoryServer(t *testing.T) { + expectedIndexYAML, err := ioutil.ReadFile("testdata/server/index.yaml") + if err != nil { + t.Fatal(err) + } tests := []struct { name string path string expect string }{ - {"index YAML", "/charts/index.yaml", "apiVersion: v1"}, + {"index YAML", "/charts/index.yaml", string(expectedIndexYAML)}, {"index HTML", "/charts/index.html", ""}, {"charts root", "/charts/", ""}, - {"root", "/", "Welcome"}, + {"root", "/", ""}, {"file", "/test.txt", "Hello World"}, }