Commit 737d3c74 authored by Matt Butcher's avatar Matt Butcher Committed by GitHub
Browse files

Merge pull request #1406 from technosophos/fix/1397-fix-index

fix(helm): finish repo index.html
parents 58be8c9f abf1ddc3
main Release add-codeql dependabot/go_modules/github.com/docker/distribution-2.8.2incompatible dependabot/go_modules/github.com/lib/pq-1.10.9 dependabot/go_modules/github.com/rubenv/sql-migrate-1.4.0 dependabot/go_modules/golang.org/x/crypto-0.9.0 dependabot/go_modules/golang.org/x/term-0.8.0 dependabot/go_modules/k8s.io/klog/v2-2.100.1 dev-v2 feat-v3/event-emitter-lua release-2.0 release-2.1 release-2.10 release-2.11 release-2.12 release-2.13 release-2.14 release-2.15 release-2.16 release-2.17 release-2.2 release-2.3 release-2.4 release-2.5 release-2.6 release-2.7 release-2.8 release-2.9 release-3.0 release-3.1 release-3.10 release-3.11 release-3.12 release-3.2 release-3.3 release-3.4 release-3.5 release-3.6 release-3.6.1 release-3.6.2 release-3.7 release-3.8 release-3.9 release-v3.0.0-beta.4 v3.12.0 v3.12.0-rc.1 v3.12.0-dev.1 v3.11.3 v3.11.2 v3.11.1 v3.11.0 v3.11.0-rc.2 v3.11.0-rc.1 v3.10.3 v3.10.2 v3.10.1 v3.10.0 v3.10.0-rc.1 v3.9.4 v3.9.3 v3.9.2 v3.9.1 v3.9.0 v3.9.0-rc.1 v3.8.2 v3.8.1 v3.8.0 v3.8.0-rc.2 v3.8.0-rc.1 v3.7.2 v3.7.1 v3.7.0 v3.7.0-rc.3 v3.7.0-rc.2 v3.7.0-rc.1 v3.6.3 v3.6.2 v3.6.1 v3.6.0 v3.6.0-rc.1 v3.5.4 v3.5.3 v3.5.2 v3.5.1 v3.5.0 v3.5.0-rc.2 v3.5.0-rc.1 v3.4.2 v3.4.1 v3.4.0 v3.4.0-rc.1 v3.3.4 v3.3.3 v3.3.2 v3.3.1 v3.3.0 v3.3.0-rc.2 v3.3.0-rc.1 v3.2.4 v3.2.3 v3.2.2 v3.2.1 v3.2.0 v3.2.0-rc.1 v3.1.3 v3.1.2 v3.1.1 v3.1.0 v3.1.0-rc.3 v3.1.0-rc.2 v3.1.0-rc.1 v3.0.3 v3.0.2 v3.0.1 v3.0.0 v3.0.0-rc.4 v3.0.0-rc.3 v3.0.0-rc.2 v3.0.0-rc.1 v3.0.0-beta.5 v3.0.0-beta.4 v3.0.0-beta.3 v3.0.0-beta.2 v3.0.0-beta.1 v3.0.0-alpha.2 v3.0.0-alpha.1 v2.17.0 v2.17.0-rc.1 v2.16.12 v2.16.11 v2.16.10 v2.16.9 v2.16.8 v2.16.7 v2.16.6 v2.16.5 v2.16.4 v2.16.3 v2.16.2 v2.16.1 v2.16.0 v2.16.0-rc.2 v2.16.0-rc.1 v2.15.2 v2.15.1 v2.15.0 v2.15.0-rc.2 v2.15.0-rc.1 v2.14.3 v2.14.2 v2.14.1 v2.14.0 v2.14.0-rc.2 v2.14.0-rc.1 v2.13.1 v2.13.1-rc.1 v2.13.0 v2.13.0-rc.2 v2.13.0-rc.1 v2.12.3 v2.12.2 v2.12.1 v2.12.0 v2.12.0-rc.2 v2.12.0-rc.1 v2.11.0 v2.11.0-rc.4 v2.11.0-rc.3 v2.11.0-rc.2 v2.11.0-rc.1 v2.10.0 v2.10.0-rc.3 v2.10.0-rc.2 v2.10.0-rc.1 v2.9.1 v2.9.0 v2.9.0-rc5 v2.9.0-rc4 v2.9.0-rc3 v2.9.0-rc2 v2.9.0-rc1 v2.8.2 v2.8.2-rc1 v2.8.1 v2.8.0 v2.8.0-rc.1 v2.7.2 v2.7.1 v2.7.0 v2.7.0-rc1 v2.6.2 v2.6.1 v2.6.0 v2.5.1 v2.5.0 v2.4.2 v2.4.1 v2.4.0 v2.3.1 v2.3.0 v2.2.3 v2.2.2 v2.2.1 v2.2.0 v2.1.3 v2.1.2 v2.1.1 v2.1.0 v2.0.2 v2.0.1 v2.0.0 v2.0.0-rc.2 v2.0.0-rc.1 v2.0.0-beta.2
No related merge requests found
Showing with 163 additions and 23 deletions
+163 -23
...@@ -18,6 +18,7 @@ package repo ...@@ -18,6 +18,7 @@ package repo
import ( import (
"fmt" "fmt"
htemplate "html/template"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"path/filepath" "path/filepath"
...@@ -30,37 +31,74 @@ import ( ...@@ -30,37 +31,74 @@ import (
"k8s.io/helm/pkg/provenance" "k8s.io/helm/pkg/provenance"
) )
var localRepoPath string const indexHTMLTemplate = `
<html>
<head>
<title>Helm Repository</title>
</head>
<h1>Helm Charts Repository</h1>
<ul>
{{range $name, $ver := .Index.Entries}}
<li>{{$name}}<ul>{{range $ver}}
<li><a href="{{index .URLs 0}}">{{.Name}}-{{.Version}}</a></li>
{{end}}</ul>
</li>
{{end}}
</ul>
<body>
<p>Last Generated: {{.Index.Generated}}</p>
</body>
</html>
`
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
}
// ServeHTTP implements the http.Handler interface.
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":
s.htmlIndex(w, r)
default:
file := strings.TrimPrefix(uri, "/charts/")
http.ServeFile(w, r, filepath.Join(s.RepoPath, file))
}
}
// StartLocalRepo starts a web server and serves files from the given path // StartLocalRepo starts a web server and serves files from the given path
func StartLocalRepo(path, address string) error { func StartLocalRepo(path, address string) error {
if address == "" { if address == "" {
address = ":8879" address = ":8879"
} }
localRepoPath = path s := &RepositoryServer{RepoPath: path}
http.HandleFunc("/", rootHandler) return http.ListenAndServe(address, s)
http.HandleFunc("/charts/", indexHandler)
return http.ListenAndServe(address, nil)
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintf(w, "Welcome to the Kubernetes Package manager!\nBrowse charts on localhost:8879/charts!")
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
file := r.URL.Path[len("/charts/"):]
if len(strings.Split(file, ".")) > 1 {
serveFile(w, r, file)
} else if file == "" {
fmt.Fprintf(w, "list of charts should be here at some point")
} else if file == "index" {
fmt.Fprintf(w, "index file data should be here at some point")
} else {
fmt.Fprintf(w, "Ummm... Nothing to see here folks")
}
} }
func serveFile(w http.ResponseWriter, r *http.Request, file string) { func (s *RepositoryServer) htmlIndex(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join(localRepoPath, file)) t := htemplate.Must(htemplate.New("index.html").Parse(indexHTMLTemplate))
// load index
lrp := filepath.Join(s.RepoPath, "index.yaml")
i, err := LoadIndexFile(lrp)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
data := map[string]interface{}{
"Index": i,
}
if err := t.Execute(w, data); err != nil {
fmt.Fprintf(w, "Template error: %s", err)
}
} }
// AddChartToLocalRepo saves a chart in the given path and then reindexes the index file // AddChartToLocalRepo saves a chart in the given path and then reindexes the index file
......
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package repo
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestRepositoryServer(t *testing.T) {
tests := []struct {
name string
path string
expect string
}{
{"index YAML", "/charts/index.yaml", "apiVersion: v1"},
{"index HTML", "/charts/index.html", "<html>"},
{"charts root", "/charts/", "<html>"},
{"root", "/", "Welcome"},
{"file", "/test.txt", "Hello World"},
}
s := &RepositoryServer{RepoPath: "testdata/server"}
srv := httptest.NewServer(s)
defer srv.Close()
for _, tt := range tests {
res, err := http.Get(srv.URL + tt.path)
if err != nil {
t.Errorf("%s: error getting %s: %s", tt.name, tt.path, err)
continue
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Errorf("%s: error reading %s: %s", tt.name, tt.path, err)
}
res.Body.Close()
if !strings.Contains(string(body), tt.expect) {
t.Errorf("%s: expected to find %q in %q", tt.name, tt.expect, string(body))
}
}
}
apiVersion: v1
entries:
nginx:
- urls:
- http://storage.googleapis.com/kubernetes-charts/nginx-0.1.0.tgz
name: nginx
description: string
version: 0.1.0
home: https://github.com/something
digest: "sha256:1234567890abcdef"
keywords:
- popular
- web server
- proxy
- urls:
- http://storage.googleapis.com/kubernetes-charts/nginx-0.2.0.tgz
name: nginx
description: string
version: 0.2.0
home: https://github.com/something/else
digest: "sha256:1234567890abcdef"
keywords:
- popular
- web server
- proxy
alpine:
- urls:
- http://storage.googleapis.com/kubernetes-charts/alpine-1.0.0.tgz
- http://storage2.googleapis.com/kubernetes-charts/alpine-1.0.0.tgz
name: alpine
description: string
version: 1.0.0
home: https://github.com/something
keywords:
- linux
- alpine
- small
- sumtin
digest: "sha256:1234567890abcdef"
Hello World
Supports Markdown
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