github.com/doitroot/helm@v3.0.0-beta.3+incompatible/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  	"helm.sh/helm/pkg/chartutil"
    27  	"helm.sh/helm/pkg/lint/support"
    28  )
    29  
    30  // Values lints a chart's values.yaml file.
    31  func Values(linter *support.Linter) {
    32  	file := "values.yaml"
    33  	vf := filepath.Join(linter.ChartDir, file)
    34  	fileExists := linter.RunLinterRule(support.InfoSev, file, validateValuesFileExistence(vf))
    35  
    36  	if !fileExists {
    37  		return
    38  	}
    39  
    40  	linter.RunLinterRule(support.ErrorSev, file, validateValuesFile(vf))
    41  }
    42  
    43  func validateValuesFileExistence(valuesPath string) error {
    44  	_, err := os.Stat(valuesPath)
    45  	if err != nil {
    46  		return errors.Errorf("file does not exist")
    47  	}
    48  	return nil
    49  }
    50  
    51  func validateValuesFile(valuesPath string) error {
    52  	values, err := chartutil.ReadValuesFile(valuesPath)
    53  	if err != nil {
    54  		return errors.Wrap(err, "unable to parse YAML")
    55  	}
    56  
    57  	ext := filepath.Ext(valuesPath)
    58  	schemaPath := valuesPath[:len(valuesPath)-len(ext)] + ".schema.json"
    59  	schema, err := ioutil.ReadFile(schemaPath)
    60  	if len(schema) == 0 {
    61  		return nil
    62  	}
    63  	if err != nil {
    64  		return err
    65  	}
    66  	return chartutil.ValidateAgainstSingleSchema(values, schema)
    67  }