Skip to content

Commit

Permalink
Add list-changed command (#98)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
unguiculus authored Jan 31, 2019
1 parent 1d3db54 commit d69c43e
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/cmd/lintAndInstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
59 changes: 59 additions & 0 deletions app/cmd/listChanged.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
19 changes: 12 additions & 7 deletions app/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand All @@ -58,20 +59,27 @@ 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`))
flags.StringSlice("charts", []string{}, heredoc.Doc(`
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).
Expand All @@ -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)`))
Expand Down
3 changes: 2 additions & 1 deletion doc/ct.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion doc/ct_install.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion doc/ct_lint-and-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion doc/ct_lint.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 31 additions & 0 deletions doc/ct_list-changed.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion doc/ct_version.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 7 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d69c43e

Please sign in to comment.