github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/pkg/lint/rules/dependencies.go (about) 1 /* 2 Copyright The Helm Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package rules // import "github.com/stefanmcshane/helm/pkg/lint/rules" 18 19 import ( 20 "fmt" 21 "strings" 22 23 "github.com/pkg/errors" 24 25 "github.com/stefanmcshane/helm/pkg/chart" 26 "github.com/stefanmcshane/helm/pkg/chart/loader" 27 "github.com/stefanmcshane/helm/pkg/lint/support" 28 ) 29 30 // Dependencies runs lints against a chart's dependencies 31 // 32 // See https://github.com/helm/helm/issues/7910 33 func Dependencies(linter *support.Linter) { 34 c, err := loader.LoadDir(linter.ChartDir) 35 if !linter.RunLinterRule(support.ErrorSev, "", validateChartFormat(err)) { 36 return 37 } 38 39 linter.RunLinterRule(support.ErrorSev, linter.ChartDir, validateDependencyInMetadata(c)) 40 linter.RunLinterRule(support.WarningSev, linter.ChartDir, validateDependencyInChartsDir(c)) 41 } 42 43 func validateChartFormat(chartError error) error { 44 if chartError != nil { 45 return errors.Errorf("unable to load chart\n\t%s", chartError) 46 } 47 return nil 48 } 49 50 func validateDependencyInChartsDir(c *chart.Chart) (err error) { 51 dependencies := map[string]struct{}{} 52 missing := []string{} 53 for _, dep := range c.Dependencies() { 54 dependencies[dep.Metadata.Name] = struct{}{} 55 } 56 for _, dep := range c.Metadata.Dependencies { 57 if _, ok := dependencies[dep.Name]; !ok { 58 missing = append(missing, dep.Name) 59 } 60 } 61 if len(missing) > 0 { 62 err = fmt.Errorf("chart directory is missing these dependencies: %s", strings.Join(missing, ",")) 63 } 64 return err 65 } 66 67 func validateDependencyInMetadata(c *chart.Chart) (err error) { 68 dependencies := map[string]struct{}{} 69 missing := []string{} 70 for _, dep := range c.Metadata.Dependencies { 71 dependencies[dep.Name] = struct{}{} 72 } 73 for _, dep := range c.Dependencies() { 74 if _, ok := dependencies[dep.Metadata.Name]; !ok { 75 missing = append(missing, dep.Metadata.Name) 76 } 77 } 78 if len(missing) > 0 { 79 err = fmt.Errorf("chart metadata is missing these dependencies: %s", strings.Join(missing, ",")) 80 } 81 return err 82 }