github.com/gaoxiaodong/helm@v3.0.0-beta.3+incompatible/internal/resolver/resolver.go (about)

     1  /*
     2  Copyright The Helm Authors.
     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  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  	"time"
    25  
    26  	"github.com/Masterminds/semver"
    27  	"github.com/pkg/errors"
    28  
    29  	"helm.sh/helm/pkg/chart"
    30  	"helm.sh/helm/pkg/helmpath"
    31  	"helm.sh/helm/pkg/provenance"
    32  	"helm.sh/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  	cachepath string
    39  }
    40  
    41  // New creates a new resolver for a given chart and a given helm home.
    42  func New(chartpath, cachepath string) *Resolver {
    43  	return &Resolver{
    44  		chartpath: chartpath,
    45  		cachepath: cachepath,
    46  	}
    47  }
    48  
    49  // Resolve resolves dependencies and returns a lock file with the resolution.
    50  func (r *Resolver) Resolve(reqs []*chart.Dependency, repoNames map[string]string) (*chart.Lock, error) {
    51  
    52  	// Now we clone the dependencies, locking as we go.
    53  	locked := make([]*chart.Dependency, len(reqs))
    54  	missing := []string{}
    55  	for i, d := range reqs {
    56  		if strings.HasPrefix(d.Repository, "file://") {
    57  
    58  			if _, err := GetLocalPath(d.Repository, r.chartpath); err != nil {
    59  				return nil, err
    60  			}
    61  
    62  			locked[i] = &chart.Dependency{
    63  				Name:       d.Name,
    64  				Repository: d.Repository,
    65  				Version:    d.Version,
    66  			}
    67  			continue
    68  		}
    69  		constraint, err := semver.NewConstraint(d.Version)
    70  		if err != nil {
    71  			return nil, errors.Wrapf(err, "dependency %q has an invalid version/constraint format", d.Name)
    72  		}
    73  
    74  		idx := filepath.Join(r.cachepath, helmpath.CacheIndexFile(repoNames[d.Name]))
    75  
    76  		repoIndex, err := repo.LoadIndexFile(idx)
    77  		if err != nil {
    78  			return nil, errors.Wrapf(err, "no cached repo found. (try 'helm repo update') %s", idx)
    79  		}
    80  
    81  		vs, ok := repoIndex.Entries[d.Name]
    82  		if !ok {
    83  			return nil, errors.Errorf("%s chart not found in repo %s", d.Name, d.Repository)
    84  		}
    85  
    86  		locked[i] = &chart.Dependency{
    87  			Name:       d.Name,
    88  			Repository: d.Repository,
    89  		}
    90  		found := false
    91  		// The version are already sorted and hence the first one to satisfy the constraint is used
    92  		for _, ver := range vs {
    93  			v, err := semver.NewVersion(ver.Version)
    94  			if err != nil || len(ver.URLs) == 0 {
    95  				// Not a legit entry.
    96  				continue
    97  			}
    98  			if constraint.Check(v) {
    99  				found = true
   100  				locked[i].Version = v.Original()
   101  				break
   102  			}
   103  		}
   104  
   105  		if !found {
   106  			missing = append(missing, d.Name)
   107  		}
   108  	}
   109  	if len(missing) > 0 {
   110  		return nil, errors.Errorf("can't get a valid version for repositories %s. Try changing the version constraint in Chart.yaml", strings.Join(missing, ", "))
   111  	}
   112  
   113  	digest, err := HashReq(locked)
   114  	if err != nil {
   115  		return nil, err
   116  	}
   117  
   118  	return &chart.Lock{
   119  		Generated:    time.Now(),
   120  		Digest:       digest,
   121  		Dependencies: locked,
   122  	}, nil
   123  }
   124  
   125  // HashReq generates a hash of the dependencies.
   126  //
   127  // This should be used only to compare against another hash generated by this
   128  // function.
   129  func HashReq(req []*chart.Dependency) (string, error) {
   130  	data, err := json.Marshal(req)
   131  	if err != nil {
   132  		return "", err
   133  	}
   134  	s, err := provenance.Digest(bytes.NewBuffer(data))
   135  	return "sha256:" + s, err
   136  }
   137  
   138  // GetLocalPath generates absolute local path when use
   139  // "file://" in repository of dependencies
   140  func GetLocalPath(repo, chartpath string) (string, error) {
   141  	var depPath string
   142  	var err error
   143  	p := strings.TrimPrefix(repo, "file://")
   144  
   145  	// root path is absolute
   146  	if strings.HasPrefix(p, "/") {
   147  		if depPath, err = filepath.Abs(p); err != nil {
   148  			return "", err
   149  		}
   150  	} else {
   151  		depPath = filepath.Join(chartpath, p)
   152  	}
   153  
   154  	if _, err = os.Stat(depPath); os.IsNotExist(err) {
   155  		return "", errors.Errorf("directory %s not found", depPath)
   156  	} else if err != nil {
   157  		return "", err
   158  	}
   159  
   160  	return depPath, nil
   161  }