1
0
mirror of https://github.com/helm/chart-testing.git synced 2026-02-05 09:45:14 +01:00

Add list-changed command (#98)

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>
This commit is contained in:
Reinhard Nägele
2019-01-31 17:17:25 +01:00
committed by GitHub
parent 1d3db5408c
commit d69c43e71b
13 changed files with 119 additions and 19 deletions

View File

@@ -72,7 +72,7 @@ func addInstallFlags(flags *flag.FlagSet) {
func install(cmd *cobra.Command, args []string) {
fmt.Println("Installing charts...")
configuration, err := config.LoadConfiguration(cfgFile, cmd)
configuration, err := config.LoadConfiguration(cfgFile, cmd, true)
if err != nil {
fmt.Printf("Error loading configuration: %s\n", err)
os.Exit(1)

View File

@@ -75,7 +75,7 @@ func addLintFlags(flags *flag.FlagSet) {
func lint(cmd *cobra.Command, args []string) {
fmt.Println("Linting charts...")
configuration, err := config.LoadConfiguration(cfgFile, cmd)
configuration, err := config.LoadConfiguration(cfgFile, cmd, true)
if err != nil {
fmt.Printf("Error loading configuration: %s\n", err)
os.Exit(1)

View File

@@ -43,7 +43,7 @@ func newLintAndInstallCmd() *cobra.Command {
func lintAndInstall(cmd *cobra.Command, args []string) {
fmt.Println("Linting and installing charts...")
configuration, err := config.LoadConfiguration(cfgFile, cmd)
configuration, err := config.LoadConfiguration(cfgFile, cmd, true)
if err != nil {
fmt.Printf("Error loading configuration: %s\n", err)
os.Exit(1)

59
app/cmd/listChanged.go Normal file
View File

@@ -0,0 +1,59 @@
// 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 cmd
import (
"fmt"
"github.com/MakeNowJust/heredoc"
"os"
"github.com/helm/chart-testing/pkg/chart"
"github.com/helm/chart-testing/pkg/config"
"github.com/spf13/cobra"
)
func newListChangedCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list-changed",
Aliases: []string{"ls-changed", "lsc"},
Short: "List changed charts",
Long: heredoc.Doc(`
"List changed charts based on configured charts directories,
"remote, and target branch`),
Run: listChanged,
}
flags := cmd.Flags()
addCommonFlags(flags)
return cmd
}
func listChanged(cmd *cobra.Command, args []string) {
configuration, err := config.LoadConfiguration(cfgFile, cmd, false)
if err != nil {
fmt.Printf("Error loading configuration: %s\n", err)
os.Exit(1)
}
testing := chart.NewTesting(*configuration)
chartDirs, err := testing.ComputeChangedChartDirectories()
if err != nil {
os.Exit(1)
}
for _, dir := range chartDirs {
fmt.Println(dir)
}
}

View File

@@ -44,6 +44,7 @@ func NewRootCmd() *cobra.Command {
cmd.AddCommand(newLintCmd())
cmd.AddCommand(newInstallCmd())
cmd.AddCommand(newLintAndInstallCmd())
cmd.AddCommand(newListChangedCmd())
cmd.AddCommand(newVersionCmd())
cmd.AddCommand(newGenerateDocsCmd())
@@ -58,10 +59,20 @@ func Execute() {
}
}
func addCommonLintAndInstallFlags(flags *pflag.FlagSet) {
func addCommonFlags(flags *pflag.FlagSet) {
flags.StringVar(&cfgFile, "config", "", "Config file")
flags.String("remote", "origin", "The name of the Git remote used to identify changed charts")
flags.String("target-branch", "master", "The name of the target branch used to identify changed charts")
flags.StringSlice("chart-dirs", []string{"charts"}, heredoc.Doc(`
Directories containing Helm charts. May be specified multiple times
or separate values with commas`))
flags.StringSlice("excluded-charts", []string{}, heredoc.Doc(`
Charts that should be skipped. May be specified multiple times
or separate values with commas`))
}
func addCommonLintAndInstallFlags(flags *pflag.FlagSet) {
addCommonFlags(flags)
flags.Bool("all", false, heredoc.Doc(`
Process all charts except those explicitly excluded.
Disables changed charts detection and version increment checking`))
@@ -69,9 +80,6 @@ func addCommonLintAndInstallFlags(flags *pflag.FlagSet) {
Specific charts to test. Disables changed charts detection and
version increment checking. May be specified multiple times
or separate values with commas`))
flags.StringSlice("chart-dirs", []string{"charts"}, heredoc.Doc(`
Directories containing Helm charts. May be specified multiple times
or separate values with commas`))
flags.StringSlice("chart-repos", []string{}, heredoc.Doc(`
Additional chart repositories for dependency resolutions.
Repositories should be formatted as 'name=url' (ex: local=http://127.0.0.1:8879/charts).
@@ -81,9 +89,6 @@ func addCommonLintAndInstallFlags(flags *pflag.FlagSet) {
specified on a per-repo basis with an equals sign as delimiter
(e.g. 'myrepo=--username test --password secret'). May be specified
multiple times or separate values with commas`))
flags.StringSlice("excluded-charts", []string{}, heredoc.Doc(`
Charts that should be skipped. May be specified multiple times
or separate values with commas`))
flags.Bool("debug", false, heredoc.Doc(`
Print CLI calls of external tools to stdout (Note: depending on helm-extra-args
passed, this may reveal sensitive data)`))

View File

@@ -23,6 +23,7 @@ in given chart directories.
* [ct install](ct_install.md) - Install and test a chart
* [ct lint](ct_lint.md) - Lint and validate a chart
* [ct lint-and-install](ct_lint-and-install.md) - Lint, install, and test a chart
* [ct list-changed](ct_list-changed.md) - List changed charts
* [ct version](ct_version.md) - Print version information
###### Auto generated by spf13/cobra on 18-Jan-2019
###### Auto generated by spf13/cobra on 31-Jan-2019

View File

@@ -62,4 +62,4 @@ ct install [flags]
* [ct](ct.md) - The Helm chart testing tool
###### Auto generated by spf13/cobra on 18-Jan-2019
###### Auto generated by spf13/cobra on 31-Jan-2019

View File

@@ -61,4 +61,4 @@ ct lint-and-install [flags]
* [ct](ct.md) - The Helm chart testing tool
###### Auto generated by spf13/cobra on 18-Jan-2019
###### Auto generated by spf13/cobra on 31-Jan-2019

View File

@@ -65,4 +65,4 @@ ct lint [flags]
* [ct](ct.md) - The Helm chart testing tool
###### Auto generated by spf13/cobra on 18-Jan-2019
###### Auto generated by spf13/cobra on 31-Jan-2019

31
doc/ct_list-changed.md Normal file
View File

@@ -0,0 +1,31 @@
## ct list-changed
List changed charts
### Synopsis
"List changed charts based on configured charts directories,
"remote, and target branch
```
ct list-changed [flags]
```
### Options
```
--chart-dirs strings Directories containing Helm charts. May be specified multiple times
or separate values with commas (default [charts])
--config string Config file
--excluded-charts strings Charts that should be skipped. May be specified multiple times
or separate values with commas
-h, --help help for list-changed
--remote string The name of the Git remote used to identify changed charts (default "origin")
--target-branch string The name of the target branch used to identify changed charts (default "master")
```
### SEE ALSO
* [ct](ct.md) - The Helm chart testing tool
###### Auto generated by spf13/cobra on 31-Jan-2019

View File

@@ -20,4 +20,4 @@ ct version [flags]
* [ct](ct.md) - The Helm chart testing tool
###### Auto generated by spf13/cobra on 18-Jan-2019
###### Auto generated by spf13/cobra on 31-Jan-2019

View File

@@ -60,7 +60,7 @@ type Configuration struct {
ReleaseLabel string `mapstructure:"release-label"`
}
func LoadConfiguration(cfgFile string, cmd *cobra.Command) (*Configuration, error) {
func LoadConfiguration(cfgFile string, cmd *cobra.Command, printConfig bool) (*Configuration, error) {
v := viper.New()
cmd.Flags().VisitAll(func(flag *flag.Flag) {
@@ -92,7 +92,9 @@ func LoadConfiguration(cfgFile string, cmd *cobra.Command) (*Configuration, erro
return nil, errors.Wrap(err, "Error loading config file")
}
} else {
fmt.Println("Using config file: ", v.ConfigFileUsed())
if printConfig {
fmt.Println("Using config file: ", v.ConfigFileUsed())
}
}
cfg := &Configuration{}
@@ -134,7 +136,9 @@ func LoadConfiguration(cfgFile string, cmd *cobra.Command) (*Configuration, erro
cfg.CheckVersionIncrement = false
}
printCfg(cfg)
if printConfig {
printCfg(cfg)
}
return cfg, nil
}

View File

@@ -30,7 +30,7 @@ func TestUnmarshalJson(t *testing.T) {
}
func loadAndAssertConfigFromFile(t *testing.T, configFile string) {
cfg, _ := LoadConfiguration(configFile, &cobra.Command{})
cfg, _ := LoadConfiguration(configFile, &cobra.Command{}, true)
require.Equal(t, "origin", cfg.Remote)
require.Equal(t, "master", cfg.TargetBranch)