github.com/cloudposse/helm@v2.2.3+incompatible/pkg/resolver/resolver.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package resolver
    17  
    18  import (
    19  	"bytes"
    20  	"encoding/json"
    21  	"fmt"
    22  	"os"
    23  	"path/filepath"
    24  	"strings"
    25  	"time"
    26  
    27  	"github.com/Masterminds/semver"
    28  
    29  	"k8s.io/helm/cmd/helm/helmpath"
    30  	"k8s.io/helm/pkg/chartutil"
    31  	"k8s.io/helm/pkg/provenance"
    32  	"k8s.io/helm/pkg/repo"
    33  )
    34  
    35  // Resolver resolves dependencies from semantic version ranges to a particular version.
    36  type Resolver struct {
    37  	chartpath string
    38  	helmhome  helmpath.Home
    39  }
    40  
    41  // New creates a new resolver for a given chart and a given helm home.
    42  func New(chartpath string, helmhome helmpath.Home) *Resolver {
    43  	return &Resolver{
    44  		chartpath: chartpath,
    45  		helmhome:  helmhome,
    46  	}
    47  }
    48  
    49  // Resolve resolves dependencies and returns a lock file with the resolution.
    50  func (r *Resolver) Resolve(reqs *chartutil.Requirements, repoNames map[string]string) (*chartutil.RequirementsLock, error) {
    51  	d, err := HashReq(reqs)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	// Now we clone the dependencies, locking as we go.
    57  	locked := make([]*chartutil.Dependency, len(reqs.Dependencies))
    58  	missing := []string{}
    59  	for i, d := range reqs.Dependencies {
    60  		if strings.HasPrefix(d.Repository, "file://") {
    61  			depPath, err := filepath.Abs(strings.TrimPrefix(d.Repository, "file://"))
    62  			if err != nil {
    63  				return nil, err
    64  			}
    65  
    66  			if _, err = os.Stat(depPath); os.IsNotExist(err) {
    67  				return nil, fmt.Errorf("directory %s not found", depPath)
    68  			} else if err != nil {
    69  				return nil, err
    70  			}
    71  
    72  			locked[i] = &chartutil.Dependency{
    73  				Name:       d.Name,
    74  				Repository: d.Repository,
    75  				Version:    d.Version,
    76  			}
    77  			continue
    78  		}
    79  		constraint, err := semver.NewConstraint(d.Version)
    80  		if err != nil {
    81  			return nil, fmt.Errorf("dependency %q has an invalid version/constraint format: %s", d.Name, err)
    82  		}
    83  
    84  		repoIndex, err := repo.LoadIndexFile(r.helmhome.CacheIndex(repoNames[d.Name]))
    85  		if err != nil {
    86  			return nil, fmt.Errorf("no cached repo found. (try 'helm repo update'). %s", err)
    87  		}
    88  
    89  		vs, ok := repoIndex.Entries[d.Name]
    90  		if !ok {
    91  			return nil, fmt.Errorf("%s chart not found in repo %s", d.Name, d.Repository)
    92  		}
    93  
    94  		locked[i] = &chartutil.Dependency{
    95  			Name:       d.Name,
    96  			Repository: d.Repository,
    97  		}
    98  		found := false
    99  		// The version are already sorted and hence the first one to satisfy the constraint is used
   100  		for _, ver := range vs {
   101  			v, err := semver.NewVersion(ver.Version)
   102  			if err != nil || len(ver.URLs) == 0 {
   103  				// Not a legit entry.
   104  				continue
   105  			}
   106  			if constraint.Check(v) {
   107  				found = true
   108  				locked[i].Version = v.Original()
   109  				break
   110  			}
   111  		}
   112  
   113  		if !found {
   114  			missing = append(missing, d.Name)
   115  		}
   116  	}
   117  	if len(missing) > 0 {
   118  		return nil, fmt.Errorf("Can't get a valid version for repositories %s. Try changing the version constraint in requirements.yaml", strings.Join(missing, ", "))
   119  	}
   120  	return &chartutil.RequirementsLock{
   121  		Generated:    time.Now(),
   122  		Digest:       d,
   123  		Dependencies: locked,
   124  	}, nil
   125  }
   126  
   127  // HashReq generates a hash of the requirements.
   128  //
   129  // This should be used only to compare against another hash generated by this
   130  // function.
   131  func HashReq(req *chartutil.Requirements) (string, error) {
   132  	data, err := json.Marshal(req)
   133  	if err != nil {
   134  		return "", err
   135  	}
   136  	s, err := provenance.Digest(bytes.NewBuffer(data))
   137  	return "sha256:" + s, err
   138  }