mirror of
https://github.com/helm/chart-testing.git
synced 2026-02-05 09:45:14 +01:00
Allows to identify chart changes before actually running lint or install commands. This can be useful in the following cases: * In a CI setup where kind clusters are spun up on the fly, this makes it possible to decide whether a cluster is necessary at all. A PR may only contain changes that are not relevant to any charts. * By knowing upfront which charts have changed, it is possible to load a per-chart CI configuration which would allows us to determine the number of nodes needed in a kind cluster. For most charts, one node is enough, but in certain scenarios, especially for StatefulSets, we may want to test with pod anti-affinity where replicas have to be spread across multiple nodes. Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>
177 lines
5.1 KiB
Go
177 lines
5.1 KiB
Go
// Copyright The Helm Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"path"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"github.com/mitchellh/go-homedir"
|
|
|
|
"github.com/helm/chart-testing/pkg/util"
|
|
"github.com/pkg/errors"
|
|
"github.com/spf13/cobra"
|
|
flag "github.com/spf13/pflag"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
var (
|
|
homeDir, _ = homedir.Dir()
|
|
configSearchLocations = []string{
|
|
".",
|
|
path.Join(homeDir, ".ct"),
|
|
"/etc/ct",
|
|
}
|
|
)
|
|
|
|
type Configuration struct {
|
|
Remote string `mapstructure:"remote"`
|
|
TargetBranch string `mapstructure:"target-branch"`
|
|
BuildId string `mapstructure:"build-id"`
|
|
LintConf string `mapstructure:"lint-conf"`
|
|
ChartYamlSchema string `mapstructure:"chart-yaml-schema"`
|
|
ValidateMaintainers bool `mapstructure:"validate-maintainers"`
|
|
ValidateChartSchema bool `mapstructure:"validate-chart-schema"`
|
|
ValidateYaml bool `mapstructure:"validate-yaml"`
|
|
CheckVersionIncrement bool `mapstructure:"check-version-increment"`
|
|
ProcessAllCharts bool `mapstructure:"all"`
|
|
Charts []string `mapstructure:"charts"`
|
|
ChartRepos []string `mapstructure:"chart-repos"`
|
|
ChartDirs []string `mapstructure:"chart-dirs"`
|
|
ExcludedCharts []string `mapstructure:"excluded-charts"`
|
|
HelmExtraArgs string `mapstructure:"helm-extra-args"`
|
|
HelmRepoExtraArgs []string `mapstructure:"helm-repo-extra-args"`
|
|
Debug bool `mapstructure:"debug"`
|
|
Namespace string `mapstructure:"namespace"`
|
|
ReleaseLabel string `mapstructure:"release-label"`
|
|
}
|
|
|
|
func LoadConfiguration(cfgFile string, cmd *cobra.Command, printConfig bool) (*Configuration, error) {
|
|
v := viper.New()
|
|
|
|
cmd.Flags().VisitAll(func(flag *flag.Flag) {
|
|
flagName := flag.Name
|
|
if flagName != "config" && flagName != "help" {
|
|
if err := v.BindPFlag(flagName, flag); err != nil {
|
|
// can't really happen
|
|
panic(fmt.Sprintln(errors.Wrapf(err, "Error binding flag '%s'", flagName)))
|
|
}
|
|
}
|
|
})
|
|
|
|
v.AutomaticEnv()
|
|
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
|
|
v.SetEnvPrefix("CT")
|
|
|
|
if cfgFile != "" {
|
|
v.SetConfigFile(cfgFile)
|
|
} else {
|
|
v.SetConfigName("ct")
|
|
for _, searchLocation := range configSearchLocations {
|
|
v.AddConfigPath(searchLocation)
|
|
}
|
|
}
|
|
|
|
if err := v.ReadInConfig(); err != nil {
|
|
if cfgFile != "" {
|
|
// Only error out for specified config file. Ignore for default locations.
|
|
return nil, errors.Wrap(err, "Error loading config file")
|
|
}
|
|
} else {
|
|
if printConfig {
|
|
fmt.Println("Using config file: ", v.ConfigFileUsed())
|
|
}
|
|
}
|
|
|
|
cfg := &Configuration{}
|
|
if err := v.Unmarshal(cfg); err != nil {
|
|
return nil, errors.Wrap(err, "Error unmarshaling configuration")
|
|
}
|
|
|
|
if cfg.ProcessAllCharts && len(cfg.Charts) > 0 {
|
|
return nil, errors.New("specifying both, '--all' and '--charts', is not allowed")
|
|
}
|
|
|
|
if cfg.Namespace != "" && cfg.ReleaseLabel == "" {
|
|
return nil, errors.New("specifying '--namespace' without '--release-label' is not allowed")
|
|
}
|
|
|
|
isLint := strings.Contains(cmd.Use, "lint")
|
|
chartYamlSchemaPath := cfg.ChartYamlSchema
|
|
if chartYamlSchemaPath == "" {
|
|
var err error
|
|
cfgFile, err = findConfigFile("chart_schema.yaml")
|
|
if err != nil && isLint && cfg.ValidateChartSchema {
|
|
return nil, errors.New("'chart_schema.yaml' neither specified nor found in default locations")
|
|
}
|
|
cfg.ChartYamlSchema = cfgFile
|
|
}
|
|
|
|
lintConfPath := cfg.LintConf
|
|
if lintConfPath == "" {
|
|
var err error
|
|
cfgFile, err = findConfigFile("lintconf.yaml")
|
|
if err != nil && isLint && cfg.ValidateYaml {
|
|
return nil, errors.New("'lintconf.yaml' neither specified nor found in default locations")
|
|
}
|
|
cfg.LintConf = cfgFile
|
|
}
|
|
|
|
if len(cfg.Charts) > 0 || cfg.ProcessAllCharts {
|
|
fmt.Println("Version increment checking disabled.")
|
|
cfg.CheckVersionIncrement = false
|
|
}
|
|
|
|
if printConfig {
|
|
printCfg(cfg)
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
func printCfg(cfg *Configuration) {
|
|
util.PrintDelimiterLine("-")
|
|
fmt.Println(" Configuration")
|
|
util.PrintDelimiterLine("-")
|
|
|
|
e := reflect.ValueOf(cfg).Elem()
|
|
typeOfCfg := e.Type()
|
|
|
|
for i := 0; i < e.NumField(); i++ {
|
|
var pattern string
|
|
switch e.Field(i).Kind() {
|
|
case reflect.Bool:
|
|
pattern = "%s: %t\n"
|
|
default:
|
|
pattern = "%s: %s\n"
|
|
}
|
|
fmt.Printf(pattern, typeOfCfg.Field(i).Name, e.Field(i).Interface())
|
|
}
|
|
|
|
util.PrintDelimiterLine("-")
|
|
}
|
|
|
|
func findConfigFile(fileName string) (string, error) {
|
|
for _, location := range configSearchLocations {
|
|
filePath := path.Join(location, fileName)
|
|
if util.FileExists(filePath) {
|
|
return filePath, nil
|
|
}
|
|
}
|
|
return "", errors.New(fmt.Sprintf("Config file not found: %s", fileName))
|
|
}
|