github.com/latiif/helm@v2.15.0+incompatible/pkg/repo/index.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package repo
    18  
    19  import (
    20  	"encoding/json"
    21  	"errors"
    22  	"fmt"
    23  	"io/ioutil"
    24  	"os"
    25  	"path"
    26  	"path/filepath"
    27  	"sort"
    28  	"strings"
    29  	"time"
    30  
    31  	"github.com/Masterminds/semver"
    32  	"github.com/ghodss/yaml"
    33  
    34  	"k8s.io/helm/pkg/chartutil"
    35  	"k8s.io/helm/pkg/proto/hapi/chart"
    36  	"k8s.io/helm/pkg/provenance"
    37  	"k8s.io/helm/pkg/urlutil"
    38  )
    39  
    40  var indexPath = "index.yaml"
    41  
    42  // APIVersionV1 is the v1 API version for index and repository files.
    43  const APIVersionV1 = "v1"
    44  
    45  var (
    46  	// ErrNoAPIVersion indicates that an API version was not specified.
    47  	ErrNoAPIVersion = errors.New("no API version specified")
    48  	// ErrNoChartVersion indicates that a chart with the given version is not found.
    49  	ErrNoChartVersion = errors.New("no chart version found")
    50  	// ErrNoChartName indicates that a chart with the given name is not found.
    51  	ErrNoChartName = errors.New("no chart name found")
    52  )
    53  
    54  // ChartVersions is a list of versioned chart references.
    55  // Implements a sorter on Version.
    56  type ChartVersions []*ChartVersion
    57  
    58  // Len returns the length.
    59  func (c ChartVersions) Len() int { return len(c) }
    60  
    61  // Swap swaps the position of two items in the versions slice.
    62  func (c ChartVersions) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
    63  
    64  // Less returns true if the version of entry a is less than the version of entry b.
    65  func (c ChartVersions) Less(a, b int) bool {
    66  	// Failed parse pushes to the back.
    67  	i, err := semver.NewVersion(c[a].Version)
    68  	if err != nil {
    69  		return true
    70  	}
    71  	j, err := semver.NewVersion(c[b].Version)
    72  	if err != nil {
    73  		return false
    74  	}
    75  	return i.LessThan(j)
    76  }
    77  
    78  // IndexFile represents the index file in a chart repository
    79  type IndexFile struct {
    80  	APIVersion string                   `json:"apiVersion"`
    81  	Generated  time.Time                `json:"generated"`
    82  	Entries    map[string]ChartVersions `json:"entries"`
    83  	PublicKeys []string                 `json:"publicKeys,omitempty"`
    84  }
    85  
    86  // NewIndexFile initializes an index.
    87  func NewIndexFile() *IndexFile {
    88  	return &IndexFile{
    89  		APIVersion: APIVersionV1,
    90  		Generated:  time.Now(),
    91  		Entries:    map[string]ChartVersions{},
    92  		PublicKeys: []string{},
    93  	}
    94  }
    95  
    96  // LoadIndexFile takes a file at the given path and returns an IndexFile object
    97  func LoadIndexFile(path string) (*IndexFile, error) {
    98  	b, err := ioutil.ReadFile(path)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  	return loadIndex(b)
   103  }
   104  
   105  // Add adds a file to the index
   106  // This can leave the index in an unsorted state
   107  func (i IndexFile) Add(md *chart.Metadata, filename, baseURL, digest string) {
   108  	u := filename
   109  	if baseURL != "" {
   110  		var err error
   111  		_, file := filepath.Split(filename)
   112  		u, err = urlutil.URLJoin(baseURL, file)
   113  		if err != nil {
   114  			u = path.Join(baseURL, file)
   115  		}
   116  	}
   117  	cr := &ChartVersion{
   118  		URLs:     []string{u},
   119  		Metadata: md,
   120  		Digest:   digest,
   121  		Created:  time.Now(),
   122  	}
   123  	if ee, ok := i.Entries[md.Name]; !ok {
   124  		i.Entries[md.Name] = ChartVersions{cr}
   125  	} else {
   126  		i.Entries[md.Name] = append(ee, cr)
   127  	}
   128  }
   129  
   130  // Has returns true if the index has an entry for a chart with the given name and exact version.
   131  func (i IndexFile) Has(name, version string) bool {
   132  	_, err := i.Get(name, version)
   133  	return err == nil
   134  }
   135  
   136  // SortEntries sorts the entries by version in descending order.
   137  //
   138  // In canonical form, the individual version records should be sorted so that
   139  // the most recent release for every version is in the 0th slot in the
   140  // Entries.ChartVersions array. That way, tooling can predict the newest
   141  // version without needing to parse SemVers.
   142  func (i IndexFile) SortEntries() {
   143  	for _, versions := range i.Entries {
   144  		sort.Sort(sort.Reverse(versions))
   145  	}
   146  }
   147  
   148  // Get returns the ChartVersion for the given name.
   149  //
   150  // If version is empty, this will return the chart with the highest version.
   151  func (i IndexFile) Get(name, version string) (*ChartVersion, error) {
   152  	vs, ok := i.Entries[name]
   153  	if !ok {
   154  		return nil, ErrNoChartName
   155  	}
   156  	if len(vs) == 0 {
   157  		return nil, ErrNoChartVersion
   158  	}
   159  
   160  	var constraint *semver.Constraints
   161  	if len(version) == 0 {
   162  		constraint, _ = semver.NewConstraint("*")
   163  	} else {
   164  		var err error
   165  		constraint, err = semver.NewConstraint(version)
   166  		if err != nil {
   167  			return nil, err
   168  		}
   169  	}
   170  
   171  	// when customer input exact version, check whether have exact match one first
   172  	if len(version) != 0 {
   173  		for _, ver := range vs {
   174  			if version == ver.Version {
   175  				return ver, nil
   176  			}
   177  		}
   178  	}
   179  
   180  	for _, ver := range vs {
   181  		test, err := semver.NewVersion(ver.Version)
   182  		if err != nil {
   183  			continue
   184  		}
   185  
   186  		if constraint.Check(test) {
   187  			return ver, nil
   188  		}
   189  	}
   190  	return nil, fmt.Errorf("No chart version found for %s-%s", name, version)
   191  }
   192  
   193  // WriteFile writes an index file to the given destination path.
   194  //
   195  // The mode on the file is set to 'mode'.
   196  func (i IndexFile) WriteFile(dest string, mode os.FileMode) error {
   197  	b, err := yaml.Marshal(i)
   198  	if err != nil {
   199  		return err
   200  	}
   201  	return ioutil.WriteFile(dest, b, mode)
   202  }
   203  
   204  // Merge merges the given index file into this index.
   205  //
   206  // This merges by name and version.
   207  //
   208  // If one of the entries in the given index does _not_ already exist, it is added.
   209  // In all other cases, the existing record is preserved.
   210  //
   211  // This can leave the index in an unsorted state
   212  func (i *IndexFile) Merge(f *IndexFile) {
   213  	for _, cvs := range f.Entries {
   214  		for _, cv := range cvs {
   215  			if !i.Has(cv.Name, cv.Version) {
   216  				e := i.Entries[cv.Name]
   217  				i.Entries[cv.Name] = append(e, cv)
   218  			}
   219  		}
   220  	}
   221  }
   222  
   223  // Need both JSON and YAML annotations until we get rid of gopkg.in/yaml.v2
   224  
   225  // ChartVersion represents a chart entry in the IndexFile
   226  type ChartVersion struct {
   227  	*chart.Metadata
   228  	URLs    []string  `json:"urls"`
   229  	Created time.Time `json:"created,omitempty"`
   230  	Removed bool      `json:"removed,omitempty"`
   231  	Digest  string    `json:"digest,omitempty"`
   232  }
   233  
   234  // IndexDirectory reads a (flat) directory and generates an index.
   235  //
   236  // It indexes only charts that have been packaged (*.tgz).
   237  //
   238  // The index returned will be in an unsorted state
   239  func IndexDirectory(dir, baseURL string) (*IndexFile, error) {
   240  	archives, err := filepath.Glob(filepath.Join(dir, "*.tgz"))
   241  	if err != nil {
   242  		return nil, err
   243  	}
   244  	moreArchives, err := filepath.Glob(filepath.Join(dir, "**/*.tgz"))
   245  	if err != nil {
   246  		return nil, err
   247  	}
   248  	archives = append(archives, moreArchives...)
   249  
   250  	index := NewIndexFile()
   251  	for _, arch := range archives {
   252  		fname, err := filepath.Rel(dir, arch)
   253  		if err != nil {
   254  			return index, err
   255  		}
   256  
   257  		var parentDir string
   258  		parentDir, fname = filepath.Split(fname)
   259  		// filepath.Split appends an extra slash to the end of parentDir. We want to strip that out.
   260  		parentDir = strings.TrimSuffix(parentDir, string(os.PathSeparator))
   261  		parentURL, err := urlutil.URLJoin(baseURL, parentDir)
   262  		if err != nil {
   263  			parentURL = path.Join(baseURL, parentDir)
   264  		}
   265  
   266  		c, err := chartutil.Load(arch)
   267  		if err != nil {
   268  			// Assume this is not a chart.
   269  			continue
   270  		}
   271  		hash, err := provenance.DigestFile(arch)
   272  		if err != nil {
   273  			return index, err
   274  		}
   275  		index.Add(c.Metadata, fname, parentURL, hash)
   276  	}
   277  	return index, nil
   278  }
   279  
   280  // loadIndex loads an index file and does minimal validity checking.
   281  //
   282  // This will fail if API Version is not set (ErrNoAPIVersion) or if the unmarshal fails.
   283  func loadIndex(data []byte) (*IndexFile, error) {
   284  	i := &IndexFile{}
   285  	if err := yaml.Unmarshal(data, i); err != nil {
   286  		return i, err
   287  	}
   288  	i.SortEntries()
   289  	if i.APIVersion == "" {
   290  		// When we leave Beta, we should remove legacy support and just
   291  		// return this error:
   292  		//return i, ErrNoAPIVersion
   293  		return loadUnversionedIndex(data)
   294  	}
   295  	return i, nil
   296  }
   297  
   298  // unversionedEntry represents a deprecated pre-Alpha.5 format.
   299  //
   300  // This will be removed prior to v2.0.0
   301  type unversionedEntry struct {
   302  	Checksum  string          `json:"checksum"`
   303  	URL       string          `json:"url"`
   304  	Chartfile *chart.Metadata `json:"chartfile"`
   305  }
   306  
   307  // loadUnversionedIndex loads a pre-Alpha.5 index.yaml file.
   308  //
   309  // This format is deprecated. This function will be removed prior to v2.0.0.
   310  func loadUnversionedIndex(data []byte) (*IndexFile, error) {
   311  	fmt.Fprintln(os.Stderr, "WARNING: Deprecated index file format. Try 'helm repo update'")
   312  	i := map[string]unversionedEntry{}
   313  
   314  	// This gets around an error in the YAML parser. Instead of parsing as YAML,
   315  	// we convert to JSON, and then decode again.
   316  	var err error
   317  	data, err = yaml.YAMLToJSON(data)
   318  	if err != nil {
   319  		return nil, err
   320  	}
   321  	if err := json.Unmarshal(data, &i); err != nil {
   322  		return nil, err
   323  	}
   324  
   325  	if len(i) == 0 {
   326  		return nil, ErrNoAPIVersion
   327  	}
   328  	ni := NewIndexFile()
   329  	for n, item := range i {
   330  		if item.Chartfile == nil || item.Chartfile.Name == "" {
   331  			parts := strings.Split(n, "-")
   332  			ver := ""
   333  			if len(parts) > 1 {
   334  				ver = strings.TrimSuffix(parts[1], ".tgz")
   335  			}
   336  			item.Chartfile = &chart.Metadata{Name: parts[0], Version: ver}
   337  		}
   338  		ni.Add(item.Chartfile, item.URL, "", item.Checksum)
   339  	}
   340  	return ni, nil
   341  }