github.com/darkowlzz/helm@v2.5.1-0.20171213183701-6707fe0468d4+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/pkg/chartutil" 30 "k8s.io/helm/pkg/helm/helmpath" 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, d string) (*chartutil.RequirementsLock, error) { 51 52 // Now we clone the dependencies, locking as we go. 53 locked := make([]*chartutil.Dependency, len(reqs.Dependencies)) 54 missing := []string{} 55 for i, d := range reqs.Dependencies { 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] = &chartutil.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, fmt.Errorf("dependency %q has an invalid version/constraint format: %s", d.Name, err) 72 } 73 74 repoIndex, err := repo.LoadIndexFile(r.helmhome.CacheIndex(repoNames[d.Name])) 75 if err != nil { 76 return nil, fmt.Errorf("no cached repo found. (try 'helm repo update'). %s", err) 77 } 78 79 vs, ok := repoIndex.Entries[d.Name] 80 if !ok { 81 return nil, fmt.Errorf("%s chart not found in repo %s", d.Name, d.Repository) 82 } 83 84 locked[i] = &chartutil.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, fmt.Errorf("Can't get a valid version for repositories %s. Try changing the version constraint in requirements.yaml", strings.Join(missing, ", ")) 109 } 110 return &chartutil.RequirementsLock{ 111 Generated: time.Now(), 112 Digest: d, 113 Dependencies: locked, 114 }, nil 115 } 116 117 // HashReq generates a hash of the requirements. 118 // 119 // This should be used only to compare against another hash generated by this 120 // function. 121 func HashReq(req *chartutil.Requirements) (string, error) { 122 data, err := json.Marshal(req) 123 if err != nil { 124 return "", err 125 } 126 s, err := provenance.Digest(bytes.NewBuffer(data)) 127 return "sha256:" + s, err 128 } 129 130 // GetLocalPath generates absolute local path when use 131 // "file://" in repository of requirements 132 func GetLocalPath(repo string, chartpath string) (string, error) { 133 var depPath string 134 var err error 135 p := strings.TrimPrefix(repo, "file://") 136 137 // root path is absolute 138 if strings.HasPrefix(p, "/") { 139 if depPath, err = filepath.Abs(p); err != nil { 140 return "", err 141 } 142 } else { 143 depPath = filepath.Join(chartpath, p) 144 } 145 146 if _, err = os.Stat(depPath); os.IsNotExist(err) { 147 return "", fmt.Errorf("directory %s not found", depPath) 148 } else if err != nil { 149 return "", err 150 } 151 152 return depPath, nil 153 }