|
|
|
/*
|
|
|
|
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 (
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Masterminds/semver"
|
|
|
|
"github.com/ghodss/yaml"
|
|
|
|
|
|
|
|
"k8s.io/helm/pkg/chartutil"
|
|
|
|
"k8s.io/helm/pkg/proto/hapi/chart"
|
|
|
|
"k8s.io/helm/pkg/provenance"
|
|
|
|
)
|
|
|
|
|
|
|
|
var indexPath = "index.yaml"
|
|
|
|
|
|
|
|
// APIVersionV1 is the v1 API version for index and repository files.
|
|
|
|
const APIVersionV1 = "v1"
|
|
|
|
|
|
|
|
// ErrNoAPIVersion indicates that an API version was not specified.
|
|
|
|
var ErrNoAPIVersion = errors.New("no API version specified")
|
|
|
|
|
|
|
|
// ChartVersions is a list of versioned chart references.
|
|
|
|
// Implements a sorter on Version.
|
|
|
|
type ChartVersions []*ChartVersion
|
|
|
|
|
|
|
|
// Len returns the length.
|
|
|
|
func (c ChartVersions) Len() int { return len(c) }
|
|
|
|
|
|
|
|
// Swap swaps the position of two items in the versions slice.
|
|
|
|
func (c ChartVersions) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
|
|
|
|
|
|
|
|
// Less returns true if the version of entry a is less than the version of entry b.
|
|
|
|
func (c ChartVersions) Less(a, b int) bool {
|
|
|
|
// Failed parse pushes to the back.
|
|
|
|
i, err := semver.NewVersion(c[a].Version)
|
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
j, err := semver.NewVersion(c[b].Version)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return i.LessThan(j)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IndexFile represents the index file in a chart repository
|
|
|
|
type IndexFile struct {
|
|
|
|
APIVersion string `json:"apiVersion"`
|
|
|
|
Generated time.Time `json:"generated"`
|
|
|
|
Entries map[string]ChartVersions `json:"entries"`
|
|
|
|
PublicKeys []string `json:"publicKeys,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewIndexFile initializes an index.
|
|
|
|
func NewIndexFile() *IndexFile {
|
|
|
|
return &IndexFile{
|
|
|
|
APIVersion: APIVersionV1,
|
|
|
|
Generated: time.Now(),
|
|
|
|
Entries: map[string]ChartVersions{},
|
|
|
|
PublicKeys: []string{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds a file to the index
|
|
|
|
func (i IndexFile) Add(md *chart.Metadata, filename, baseURL, digest string) {
|
|
|
|
cr := &ChartVersion{
|
|
|
|
URLs: []string{baseURL + "/" + filename},
|
|
|
|
Metadata: md,
|
|
|
|
Digest: digest,
|
|
|
|
Created: time.Now(),
|
|
|
|
}
|
|
|
|
if ee, ok := i.Entries[md.Name]; !ok {
|
|
|
|
i.Entries[md.Name] = ChartVersions{cr}
|
|
|
|
} else {
|
|
|
|
i.Entries[md.Name] = append(ee, cr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has returns true if the index has an entry for a chart with the given name and exact version.
|
|
|
|
func (i IndexFile) Has(name, version string) bool {
|
|
|
|
vs, ok := i.Entries[name]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, ver := range vs {
|
|
|
|
// TODO: Do we need to normalize the version field with the SemVer lib?
|
|
|
|
if ver.Version == version {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// SortEntries sorts the entries by version in descending order.
|
|
|
|
//
|
|
|
|
// In canonical form, the individual version records should be sorted so that
|
|
|
|
// the most recent release for every version is in the 0th slot in the
|
|
|
|
// Entries.ChartVersions array. That way, tooling can predict the newest
|
|
|
|
// version without needing to parse SemVers.
|
|
|
|
func (i IndexFile) SortEntries() {
|
|
|
|
for _, versions := range i.Entries {
|
|
|
|
sort.Sort(sort.Reverse(versions))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteFile writes an index file to the given destination path.
|
|
|
|
//
|
|
|
|
// The mode on the file is set to 'mode'.
|
|
|
|
func (i IndexFile) WriteFile(dest string, mode os.FileMode) error {
|
|
|
|
b, err := yaml.Marshal(i)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ioutil.WriteFile(dest, b, mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need both JSON and YAML annotations until we get rid of gopkg.in/yaml.v2
|
|
|
|
|
|
|
|
// ChartVersion represents a chart entry in the IndexFile
|
|
|
|
type ChartVersion struct {
|
|
|
|
*chart.Metadata
|
|
|
|
URLs []string `yaml:"url" json:"urls"`
|
|
|
|
Created time.Time `yaml:"created,omitempty" json:"created,omitempty"`
|
|
|
|
Removed bool `yaml:"removed,omitempty" json:"removed,omitempty"`
|
|
|
|
Digest string `yaml:"digest,omitempty" json:"digest,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// IndexDirectory reads a (flat) directory and generates an index.
|
|
|
|
//
|
|
|
|
// It indexes only charts that have been packaged (*.tgz).
|
|
|
|
//
|
|
|
|
// It writes the results to dir/index.yaml.
|
|
|
|
func IndexDirectory(dir, baseURL string) (*IndexFile, error) {
|
|
|
|
archives, err := filepath.Glob(filepath.Join(dir, "*.tgz"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
index := NewIndexFile()
|
|
|
|
for _, arch := range archives {
|
|
|
|
fname := filepath.Base(arch)
|
|
|
|
c, err := chartutil.Load(arch)
|
|
|
|
if err != nil {
|
|
|
|
// Assume this is not a chart.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
hash, err := provenance.DigestFile(arch)
|
|
|
|
if err != nil {
|
|
|
|
return index, err
|
|
|
|
}
|
|
|
|
index.Add(c.Metadata, fname, baseURL, hash)
|
|
|
|
}
|
|
|
|
return index, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownloadIndexFile fetches the index from a repository.
|
|
|
|
func DownloadIndexFile(repoName, url, indexFilePath 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()
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := LoadIndex(b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(indexFilePath, b, 0644)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadIndex loads an index file and does minimal validity checking.
|
|
|
|
//
|
|
|
|
// This will fail if API Version is not set (ErrNoAPIVersion) or if the unmarshal fails.
|
|
|
|
func LoadIndex(data []byte) (*IndexFile, error) {
|
|
|
|
i := &IndexFile{}
|
|
|
|
if err := yaml.Unmarshal(data, i); err != nil {
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
if i.APIVersion == "" {
|
|
|
|
return i, ErrNoAPIVersion
|
|
|
|
}
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadIndexFile takes a file at the given path and returns an IndexFile object
|
|
|
|
func LoadIndexFile(path string) (*IndexFile, error) {
|
|
|
|
b, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return LoadIndex(b)
|
|
|
|
}
|