github.com/felipejfc/helm@v2.1.2+incompatible/cmd/helm/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 "strings" 23 "time" 24 25 "github.com/Masterminds/semver" 26 27 "k8s.io/helm/cmd/helm/helmpath" 28 "k8s.io/helm/pkg/chartutil" 29 "k8s.io/helm/pkg/provenance" 30 "k8s.io/helm/pkg/repo" 31 ) 32 33 // Resolver resolves dependencies from semantic version ranges to a particular version. 34 type Resolver struct { 35 chartpath string 36 helmhome helmpath.Home 37 } 38 39 // New creates a new resolver for a given chart and a given helm home. 40 func New(chartpath string, helmhome helmpath.Home) *Resolver { 41 return &Resolver{ 42 chartpath: chartpath, 43 helmhome: helmhome, 44 } 45 } 46 47 // Resolve resolves dependencies and returns a lock file with the resolution. 48 func (r *Resolver) Resolve(reqs *chartutil.Requirements, repoNames map[string]string) (*chartutil.RequirementsLock, error) { 49 d, err := HashReq(reqs) 50 if err != nil { 51 return nil, err 52 } 53 54 // Now we clone the dependencies, locking as we go. 55 locked := make([]*chartutil.Dependency, len(reqs.Dependencies)) 56 missing := []string{} 57 for i, d := range reqs.Dependencies { 58 constraint, err := semver.NewConstraint(d.Version) 59 if err != nil { 60 return nil, fmt.Errorf("dependency %q has an invalid version/constraint format: %s", d.Name, err) 61 } 62 63 repoIndex, err := repo.LoadIndexFile(r.helmhome.CacheIndex(repoNames[d.Name])) 64 if err != nil { 65 return nil, fmt.Errorf("no cached repo found. (try 'helm repo update'). %s", err) 66 } 67 68 vs, ok := repoIndex.Entries[d.Name] 69 if !ok { 70 return nil, fmt.Errorf("%s chart not found in repo %s", d.Name, d.Repository) 71 } 72 73 locked[i] = &chartutil.Dependency{ 74 Name: d.Name, 75 Repository: d.Repository, 76 } 77 found := false 78 // The version are already sorted and hence the first one to satisfy the constraint is used 79 for _, ver := range vs { 80 v, err := semver.NewVersion(ver.Version) 81 if err != nil || len(ver.URLs) == 0 { 82 // Not a legit entry. 83 continue 84 } 85 if constraint.Check(v) { 86 found = true 87 locked[i].Version = v.Original() 88 break 89 } 90 } 91 92 if !found { 93 missing = append(missing, d.Name) 94 } 95 } 96 if len(missing) > 0 { 97 return nil, fmt.Errorf("Can't get a valid version for repositories %s. Try changing the version constraint in requirements.yaml", strings.Join(missing, ", ")) 98 } 99 return &chartutil.RequirementsLock{ 100 Generated: time.Now(), 101 Digest: d, 102 Dependencies: locked, 103 }, nil 104 } 105 106 // HashReq generates a hash of the requirements. 107 // 108 // This should be used only to compare against another hash generated by this 109 // function. 110 func HashReq(req *chartutil.Requirements) (string, error) { 111 data, err := json.Marshal(req) 112 if err != nil { 113 return "", err 114 } 115 s, err := provenance.Digest(bytes.NewBuffer(data)) 116 return "sha256:" + s, err 117 }