github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/pkg/lint/rules/values.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 18 19 import ( 20 "io/ioutil" 21 "os" 22 "path/filepath" 23 24 "github.com/pkg/errors" 25 26 "github.com/stefanmcshane/helm/pkg/chartutil" 27 "github.com/stefanmcshane/helm/pkg/lint/support" 28 ) 29 30 // Values lints a chart's values.yaml file. 31 // 32 // This function is deprecated and will be removed in Helm 4. 33 func Values(linter *support.Linter) { 34 ValuesWithOverrides(linter, map[string]interface{}{}) 35 } 36 37 // ValuesWithOverrides tests the values.yaml file. 38 // 39 // If a schema is present in the chart, values are tested against that. Otherwise, 40 // they are only tested for well-formedness. 41 // 42 // If additional values are supplied, they are coalesced into the values in values.yaml. 43 func ValuesWithOverrides(linter *support.Linter, values map[string]interface{}) { 44 file := "values.yaml" 45 vf := filepath.Join(linter.ChartDir, file) 46 fileExists := linter.RunLinterRule(support.InfoSev, file, validateValuesFileExistence(vf)) 47 48 if !fileExists { 49 return 50 } 51 52 linter.RunLinterRule(support.ErrorSev, file, validateValuesFile(vf, values)) 53 } 54 55 func validateValuesFileExistence(valuesPath string) error { 56 _, err := os.Stat(valuesPath) 57 if err != nil { 58 return errors.Errorf("file does not exist") 59 } 60 return nil 61 } 62 63 func validateValuesFile(valuesPath string, overrides map[string]interface{}) error { 64 values, err := chartutil.ReadValuesFile(valuesPath) 65 if err != nil { 66 return errors.Wrap(err, "unable to parse YAML") 67 } 68 69 // Helm 3.0.0 carried over the values linting from Helm 2.x, which only tests the top 70 // level values against the top-level expectations. Subchart values are not linted. 71 // We could change that. For now, though, we retain that strategy, and thus can 72 // coalesce tables (like reuse-values does) instead of doing the full chart 73 // CoalesceValues 74 coalescedValues := chartutil.CoalesceTables(make(map[string]interface{}, len(overrides)), overrides) 75 coalescedValues = chartutil.CoalesceTables(coalescedValues, values) 76 77 ext := filepath.Ext(valuesPath) 78 schemaPath := valuesPath[:len(valuesPath)-len(ext)] + ".schema.json" 79 schema, err := ioutil.ReadFile(schemaPath) 80 if len(schema) == 0 { 81 return nil 82 } 83 if err != nil { 84 return err 85 } 86 return chartutil.ValidateAgainstSingleSchema(coalescedValues, schema) 87 }