github.com/felipejfc/helm@v2.1.2+incompatible/pkg/lint/rules/template.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 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 "errors" 21 "fmt" 22 "os" 23 "path/filepath" 24 25 "github.com/ghodss/yaml" 26 "k8s.io/helm/pkg/chartutil" 27 "k8s.io/helm/pkg/engine" 28 "k8s.io/helm/pkg/lint/support" 29 "k8s.io/helm/pkg/timeconv" 30 ) 31 32 // Templates lints the templates in the Linter. 33 func Templates(linter *support.Linter) { 34 path := "templates/" 35 templatesPath := filepath.Join(linter.ChartDir, path) 36 37 templatesDirExist := linter.RunLinterRule(support.WarningSev, path, validateTemplatesDir(templatesPath)) 38 39 // Templates directory is optional for now 40 if !templatesDirExist { 41 return 42 } 43 44 // Load chart and parse templates, based on tiller/release_server 45 chart, err := chartutil.Load(linter.ChartDir) 46 47 chartLoaded := linter.RunLinterRule(support.ErrorSev, path, err) 48 49 if !chartLoaded { 50 return 51 } 52 53 options := chartutil.ReleaseOptions{Name: "testRelease", Time: timeconv.Now(), Namespace: "testNamespace"} 54 valuesToRender, err := chartutil.ToRenderValues(chart, chart.Values, options) 55 if err != nil { 56 // FIXME: This seems to generate a duplicate, but I can't find where the first 57 // error is coming from. 58 //linter.RunLinterRule(support.ErrorSev, err) 59 return 60 } 61 renderedContentMap, err := engine.New().Render(chart, valuesToRender) 62 63 renderOk := linter.RunLinterRule(support.ErrorSev, path, err) 64 65 if !renderOk { 66 return 67 } 68 69 /* Iterate over all the templates to check: 70 - It is a .yaml file 71 - All the values in the template file is defined 72 - {{}} include | quote 73 - Generated content is a valid Yaml file 74 - Metadata.Namespace is not set 75 */ 76 for _, template := range chart.Templates { 77 fileName, _ := template.Name, template.Data 78 path = fileName 79 80 linter.RunLinterRule(support.ErrorSev, path, validateAllowedExtension(fileName)) 81 82 // We only apply the following lint rules to yaml files 83 if filepath.Ext(fileName) != ".yaml" { 84 continue 85 } 86 87 // NOTE: disabled for now, Refs https://github.com/kubernetes/helm/issues/1463 88 // Check that all the templates have a matching value 89 //linter.RunLinterRule(support.WarningSev, path, validateNoMissingValues(templatesPath, valuesToRender, preExecutedTemplate)) 90 91 // NOTE: disabled for now, Refs https://github.com/kubernetes/helm/issues/1037 92 // linter.RunLinterRule(support.WarningSev, path, validateQuotes(string(preExecutedTemplate))) 93 94 renderedContent := renderedContentMap[filepath.Join(chart.GetMetadata().Name, fileName)] 95 var yamlStruct K8sYamlStruct 96 // Even though K8sYamlStruct only defines Metadata namespace, an error in any other 97 // key will be raised as well 98 err := yaml.Unmarshal([]byte(renderedContent), &yamlStruct) 99 100 validYaml := linter.RunLinterRule(support.ErrorSev, path, validateYamlContent(err)) 101 102 if !validYaml { 103 continue 104 } 105 } 106 } 107 108 // Validation functions 109 func validateTemplatesDir(templatesPath string) error { 110 if fi, err := os.Stat(templatesPath); err != nil { 111 return errors.New("directory not found") 112 } else if err == nil && !fi.IsDir() { 113 return errors.New("not a directory") 114 } 115 return nil 116 } 117 118 func validateAllowedExtension(fileName string) error { 119 ext := filepath.Ext(fileName) 120 validExtensions := []string{".yaml", ".tpl", ".txt"} 121 122 for _, b := range validExtensions { 123 if b == ext { 124 return nil 125 } 126 } 127 128 return fmt.Errorf("file extension '%s' not valid. Valid extensions are .yaml, .tpl, or .txt", ext) 129 } 130 131 func validateYamlContent(err error) error { 132 if err != nil { 133 return fmt.Errorf("unable to parse YAML\n\t%s", err) 134 } 135 return nil 136 } 137 138 // K8sYamlStruct stubs a Kubernetes YAML file. 139 // Need to access for now to Namespace only 140 type K8sYamlStruct struct { 141 Metadata struct { 142 Namespace string 143 } 144 }