github.com/azure-devops-engineer/helm@v3.0.0-alpha.2+incompatible/pkg/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 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 []*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 repoIndex, err := repo.LoadIndexFile(r.helmhome.CacheIndex(repoNames[d.Name])) 75 if err != nil { 76 return nil, errors.Wrap(err, "no cached repo found. (try 'helm repo update')") 77 } 78 79 vs, ok := repoIndex.Entries[d.Name] 80 if !ok { 81 return nil, errors.Errorf("%s chart not found in repo %s", d.Name, d.Repository) 82 } 83 84 locked[i] = &chart.Dependency{ 85 Name: d.Name, 86 Repository: d.Repository, 87 } 88 found := false 89 // The version are already sorted and hence the first one to satisfy the constraint is used 90 for _, ver := range vs { 91 v, err := semver.NewVersion(ver.Version) 92 if err != nil || len(ver.URLs) == 0 { 93 // Not a legit entry. 94 continue 95 } 96 if constraint.Check(v) { 97 found = true 98 locked[i].Version = v.Original() 99 break 100 } 101 } 102 103 if !found { 104 missing = append(missing, d.Name) 105 } 106 } 107 if len(missing) > 0 { 108 return nil, errors.Errorf("can't get a valid version for repositories %s. Try changing the version constraint in Chart.yaml", strings.Join(missing, ", ")) 109 } 110 111 digest, err := HashReq(locked) 112 if err != nil { 113 return nil, err 114 } 115 116 return &chart.Lock{ 117 Generated: time.Now(), 118 Digest: digest, 119 Dependencies: locked, 120 }, nil 121 } 122 123 // HashReq generates a hash of the dependencies. 124 // 125 // This should be used only to compare against another hash generated by this 126 // function. 127 func HashReq(req []*chart.Dependency) (string, error) { 128 data, err := json.Marshal(req) 129 if err != nil { 130 return "", err 131 } 132 s, err := provenance.Digest(bytes.NewBuffer(data)) 133 return "sha256:" + s, err 134 } 135 136 // GetLocalPath generates absolute local path when use 137 // "file://" in repository of dependencies 138 func GetLocalPath(repo, chartpath string) (string, error) { 139 var depPath string 140 var err error 141 p := strings.TrimPrefix(repo, "file://") 142 143 // root path is absolute 144 if strings.HasPrefix(p, "/") { 145 if depPath, err = filepath.Abs(p); err != nil { 146 return "", err 147 } 148 } else { 149 depPath = filepath.Join(chartpath, p) 150 } 151 152 if _, err = os.Stat(depPath); os.IsNotExist(err) { 153 return "", errors.Errorf("directory %s not found", depPath) 154 } else if err != nil { 155 return "", err 156 } 157 158 return depPath, nil 159 }