Skip to content

Commit

Permalink
Changing from pure deprecated to DeprecatedIn. Enable yaml and json f…
Browse files Browse the repository at this point in the history
…or helm detection (#45)

* Changing from pure deprecated to deprecatedIn

* Fixing static files functional tests

* Updating README with new output

* Fix helm3 functional tests

* Adding cli validation test. Updating helm2

* Review suggestion. Also make output flag available on all detection types
  • Loading branch information
Andrew Suderman authored Apr 17, 2020
1 parent 7f7808a commit e8c636d
Show file tree
Hide file tree
Showing 16 changed files with 290 additions and 118 deletions.
46 changes: 35 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ You should see an output something like:

```
$ pluto detect-files -d pkg/finder/testdata
KIND VERSION DEPRECATED FILE
Deployment extensions/v1beta1 true pkg/finder/testdata/deployment-extensions-v1beta1.json
Deployment extensions/v1beta1 true pkg/finder/testdata/deployment-extensions-v1beta1.yaml
KIND VERSION DEPRECATED DEPRECATED IN RESOURCE NAME
Deployment extensions/v1beta1 true v1.16.0 utilities
Deployment extensions/v1beta1 true v1.16.0 utilities
```

This indicates that we have two files in our directory that have deprecated apiVersions. This will need to be fixed prior to a 1.16 upgrade.
Expand All @@ -82,8 +82,8 @@ This indicates that we have two files in our directory that have deprecated apiV

```
$ pluto detect-helm --helm-version 3
KIND VERSION DEPRECATED RESOURCE NAME
StatefulSet apps/v1beta1 true audit-dashboard-prod-rabbitmq-ha
KIND VERSION DEPRECATED DEPRECATED IN RESOURCE NAME
StatefulSet apps/v1beta1 true v1.16.0 audit-dashboard-prod-rabbitmq-ha
```

This indicates that the StatefulSet audit-dashboard-prod-rabbitmq-ha was deployed with apps/v1beta1 which is deprecated in 1.16
Expand All @@ -92,9 +92,9 @@ You can also use Pluto with helm 2:

```
$ pluto detect-helm --helm-version=2 -A
KIND VERSION DEPRECATED RESOURCE NAME
Deployment extensions/v1beta1 true invincible-zebu-metrics-server
Deployment apps/v1 false lunging-bat-metrics-server
KIND VERSION DEPRECATED DEPRECATED IN RESOURCE NAME
Deployment extensions/v1beta1 true v1.16.0 invincible-zebu-metrics-server
Deployment apps/v1 false n/a lunging-bat-metrics-server
```

### Helm Chart Checking (local files)
Expand All @@ -104,7 +104,31 @@ You can run `helm template <chart-dir> | pluto detect --show-all -`
This will output something like so:

```
KIND VERSION DEPRECATED RESOURCE NAME
Deployment apps/v1 false RELEASE-NAME-goldilocks-controller
Deployment apps/v1 false RELEASE-NAME-goldilocks-dashboard
$ helm template e2e/tests/assets/helm3chart | pluto detect --show-all -
KIND VERSION DEPRECATED DEPRECATED IN RESOURCE NAME
Deployment extensions/v1beta1 true v1.16.0 RELEASE-NAME-helm3chart-v1beta1
Deployment apps/v1 false n/a RELEASE-NAME-helm3chart
```

## Other Usage Options

### CI Pipelines

Pluto will exit with a non-zero code if there are any deprecations detected. This can be used in a CI pipeline to make sure no deprecated versions are introduced into your code.

### Target Versions

By default, Pluto was designed with deprecations related to Kubernetes v1.16.0. However, as more deprecations are introduced, we will try to keep it updated.

You can target the version you are concerned with by using the `--target-version` or `-t` flag. For example:

```
$ pluto detect-helm --target-version "v1.15.0" --show-all
KIND VERSION DEPRECATED DEPRECATED IN RESOURCE NAME
StatefulSet apps/v1beta1 false v1.16.0 audit-dashboard-prod-rabbitmq-ha
$ echo $?
0
```

Notice that there is a deprecated version, but it was reported as non-deprecated because it has not yet been deprecated in v1.15.0. This particular run exited 0.
34 changes: 25 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import (
"fmt"
"io/ioutil"
"os"
"unicode/utf8"

"github.com/fairwindsops/pluto/pkg/api"
"github.com/fairwindsops/pluto/pkg/finder"
"github.com/fairwindsops/pluto/pkg/helm"
"github.com/rogpeppe/go-internal/semver"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand All @@ -37,15 +39,17 @@ var (
showNonDeprecated bool
helmVersion string
ignoreErrors bool
targetVersion string
)

func init() {
rootCmd.AddCommand(detectFilesCmd)
rootCmd.PersistentFlags().BoolVarP(&showNonDeprecated, "show-all", "A", false, "If enabled, will show files that have non-deprecated apiVersion. Only applies to tabular output.")
rootCmd.PersistentFlags().BoolVar(&ignoreErrors, "ignore-errors", false, "Default behavior is to exit non-zero if deprecations are found. This will force a return of zero.")
rootCmd.PersistentFlags().StringVarP(&targetVersion, "target-version", "t", "v1.16.0", "The version of Kubernetes you wish to check deprecations for.")
rootCmd.PersistentFlags().StringVarP(&outputFormat, "output", "o", "tabular", "The output format to use. (tabular|json|yaml)")

detectFilesCmd.PersistentFlags().StringVarP(&directory, "directory", "d", "", "The directory to scan. If blank, defaults to current workding dir.")
detectFilesCmd.PersistentFlags().StringVarP(&outputFormat, "output", "o", "tabular", "The output format to use. (tabular|json|yaml)")

rootCmd.AddCommand(detectHelmCmd)
detectHelmCmd.PersistentFlags().StringVar(&helmVersion, "helm-version", "3", "Helm version in current cluster (2|3)")
Expand Down Expand Up @@ -74,6 +78,18 @@ var rootCmd = &cobra.Command{
}
os.Exit(1)
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
c, _ := utf8.DecodeRuneInString(targetVersion)
if c != 'v' {
fmt.Printf("Your --target-version must begin with a 'v'. Got '%s'\n", targetVersion)
os.Exit(2)
}

if !semver.IsValid(targetVersion) {
fmt.Printf("You must pass a valid semver to --target-version. Got '%s'\n", targetVersion)
os.Exit(2)
}
},
}

var detectFilesCmd = &cobra.Command{
Expand All @@ -93,12 +109,12 @@ var detectFilesCmd = &cobra.Command{
os.Exit(0)
}

err = api.DisplayOutput(dir.Outputs, outputFormat, showNonDeprecated)
err = api.DisplayOutput(dir.Outputs, outputFormat, showNonDeprecated, targetVersion)
if err != nil {
fmt.Println("Error Parsing Output:", err)
os.Exit(1)
}
retCode := api.GetReturnCode(dir.Outputs, ignoreErrors)
retCode := api.GetReturnCode(dir.Outputs, ignoreErrors, targetVersion)
klog.V(5).Infof("retCode: %d", retCode)
os.Exit(retCode)
},
Expand All @@ -115,12 +131,12 @@ var detectHelmCmd = &cobra.Command{
fmt.Println("Error running helm-detect:", err)
os.Exit(1)
}
err = api.DisplayOutput(h.Outputs, outputFormat, showNonDeprecated)
err = api.DisplayOutput(h.Outputs, outputFormat, showNonDeprecated, targetVersion)
if err != nil {
fmt.Println("Error Parsing Output:", err)
os.Exit(1)
}
retCode := api.GetReturnCode(h.Outputs, ignoreErrors)
retCode := api.GetReturnCode(h.Outputs, ignoreErrors, targetVersion)
klog.V(5).Infof("retCode: %d", retCode)
os.Exit(retCode)
},
Expand Down Expand Up @@ -154,12 +170,12 @@ var detectCmd = &cobra.Command{
fmt.Println("Error checking for versions:", err)
os.Exit(1)
}
err = api.DisplayOutput(output, outputFormat, showNonDeprecated)
err = api.DisplayOutput(output, outputFormat, showNonDeprecated, targetVersion)
if err != nil {
fmt.Println("Error parsing output:", err)
os.Exit(1)
}
retCode := api.GetReturnCode(output, ignoreErrors)
retCode := api.GetReturnCode(output, ignoreErrors, targetVersion)
klog.V(5).Infof("retCode: %d", retCode)
os.Exit(retCode)
}
Expand All @@ -168,12 +184,12 @@ var detectCmd = &cobra.Command{
fmt.Println("Error reading file:", err)
os.Exit(1)
}
err = api.DisplayOutput(output, outputFormat, showNonDeprecated)
err = api.DisplayOutput(output, outputFormat, showNonDeprecated, targetVersion)
if err != nil {
fmt.Println("Error parsing output:", err)
os.Exit(1)
}
retCode := api.GetReturnCode(output, ignoreErrors)
retCode := api.GetReturnCode(output, ignoreErrors, targetVersion)
klog.V(5).Infof("retCode: %d", retCode)
os.Exit(retCode)
},
Expand Down
37 changes: 22 additions & 15 deletions e2e/tests/00_static_files.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,36 @@ testcases:
- script: pluto detect-files -d assets/ -A
assertions:
- result.code ShouldEqual 1
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment apps/v1 false utilities"
- result.systemout ShouldContainSubstring "Deployment extensions/v1beta1 true utilities"
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED DEPRECATED IN RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment extensions/v1beta1 true v1.16.0 utilities"
- result.systemout ShouldContainSubstring "Deployment apps/v1 false n/a utilities"

- name: static files
steps:
- script: pluto detect-files -d assets/
assertions:
- result.code ShouldEqual 1
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED RESOURCE NAME"
- result.systemout ShouldNotContainSubstring "Deployment apps/v1 false utilities"
- result.systemout ShouldContainSubstring "Deployment extensions/v1beta1 true utilities"
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED DEPRECATED IN RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment extensions/v1beta1 true v1.16.0 utilities"
- result.systemout ShouldNotContainSubstring "Deployment apps/v1 false n/a utilities"

- name: helm template show all
steps:
- script: helm template assets/helm3chart | pluto detect -A -
assertions:
- result.code ShouldEqual 1
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment extensions/v1beta1 true RELEASE-NAME-helm3chart"
- result.systemout ShouldContainSubstring "Deployment apps/v1 false RELEASE-NAME-helm3chart"
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED DEPRECATED IN RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment extensions/v1beta1 true v1.16.0 RELEASE-NAME-helm3chart-v1beta1"
- result.systemout ShouldContainSubstring "Deployment apps/v1 false n/a RELEASE-NAME-helm3chart"

- name: helm template
steps:
- script: helm template assets/helm3chart | pluto detect -
assertions:
- result.code ShouldEqual 1
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment extensions/v1beta1 true RELEASE-NAME-helm3chart"
- result.systemout ShouldNotContainSubstring "Deployment apps/v1 false RELEASE-NAME-helm3chart"
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED DEPRECATED IN RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment extensions/v1beta1 true v1.16.0 RELEASE-NAME-helm3chart-v1beta1"
- result.systemout ShouldNotContainSubstring "Deployment apps/v1 false n/a RELEASE-NAME-helm3chart"

- name: static files no deprecated
steps:
Expand All @@ -44,10 +44,17 @@ testcases:
- result.code ShouldEqual 0
- result.systemout ShouldContainSubstring "APIVersions were found, but none were deprecated. Try --show-all."

- name: static files no deprecated
- name: static files no deprecated show all
steps:
- script: pluto detect-files -d assets/non-deprecated -A
assertions:
- result.code ShouldEqual 0
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment apps/v1 false utilities"
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED DEPRECATED IN RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment apps/v1 false n/a utilities"

- name: static files target v1.15.0 non-deprecated
steps:
- script: pluto detect-files -d assets/deprecated116 -t "v1.15.0"
assertions:
- result.code ShouldEqual 0
- result.systemout ShouldContainSubstring "APIVersions were found, but none were deprecated. Try --show-all."
23 changes: 13 additions & 10 deletions e2e/tests/01_helm-detect-3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,37 @@ testcases:
- script: pluto detect-helm --helm-version=3 -A
assertions:
- result.code ShouldEqual 1
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment extensions/v1beta1 true test-helm3chart-v1beta1"
- result.systemout ShouldContainSubstring "Deployment apps/v1 false test-helm3chart"
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED DEPRECATED IN RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment extensions/v1beta1 true v1.16.0 test-helm3chart-v1beta1"
- result.systemout ShouldContainSubstring "Deployment apps/v1 false n/a test-helm3chart"

- name: helm detect
steps:
- script: pluto detect-helm --helm-version=3
assertions:
- result.code ShouldEqual 1
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment extensions/v1beta1 true test-helm3chart-v1beta1"
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED DEPRECATED IN RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment extensions/v1beta1 true v1.16.0 test-helm3chart-v1beta1"
- result.systemout ShouldNotContainSubstring "Deployment apps/v1 false n/a test-helm3chart"

- name: helm detect ignore errors
steps:
- script: pluto detect-helm --helm-version=3 --ignore-errors
assertions:
- result.code ShouldEqual 0
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment extensions/v1beta1 true test-helm3chart-v1beta1"
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED DEPRECATED IN RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment extensions/v1beta1 true v1.16.0 test-helm3chart-v1beta1"
- result.systemout ShouldNotContainSubstring "Deployment apps/v1 false n/a test-helm3chart"


- name: helm detect show all ignore errors
steps:
- script: pluto detect-helm --helm-version=3 -A --ignore-errors
assertions:
- result.code ShouldEqual 0
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment extensions/v1beta1 true test-helm3chart-v1beta1"
- result.systemout ShouldContainSubstring "Deployment apps/v1 false test-helm3chart"
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED DEPRECATED IN RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment extensions/v1beta1 true v1.16.0 test-helm3chart-v1beta1"
- result.systemout ShouldContainSubstring "Deployment apps/v1 false n/a test-helm3chart"

- name: cleanup
steps:
Expand Down
11 changes: 6 additions & 5 deletions e2e/tests/02_helm-detect-2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ testcases:
- script: pluto detect-helm --helm-version=2 -A
assertions:
- result.code ShouldEqual 1
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment extensions/v1beta1 true invincible-zebu-metrics-server"
- result.systemout ShouldContainSubstring "Deployment apps/v1 false lunging-bat-metrics-server"
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED DEPRECATED IN RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment extensions/v1beta1 true v1.16.0 invincible-zebu-metrics-server"
- result.systemout ShouldContainSubstring "Deployment apps/v1 false n/a lunging-bat-metrics-server"

- name: helm2 detect in-cluster
steps:
- script: pluto detect-helm --helm-version=2
assertions:
- result.code ShouldEqual 1
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment extensions/v1beta1 true invincible-zebu-metrics-server"
- result.systemout ShouldContainSubstring "KIND VERSION DEPRECATED DEPRECATED IN RESOURCE NAME"
- result.systemout ShouldContainSubstring "Deployment extensions/v1beta1 true v1.16.0 invincible-zebu-metrics-server"
- result.systemout ShouldNotContainSubstring "Deployment apps/v1 false n/a lunging-bat-metrics-server"
- name: cleanup
steps:
- script: kubectl delete ns helm-system
15 changes: 15 additions & 0 deletions e2e/tests/03-cli-validation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: "2"
name: "CLI Validation"
testcases:
- name: Pass bad semver with no v
steps:
- script: pluto detect-files -d assets/deprecated116 -t "foo"
assertions:
- result.code ShouldEqual 2
- result.systemout ShouldEqual "Your --target-version must begin with a 'v'. Got 'foo'"
- name: Pass bad semver starting with v
steps:
- script: pluto detect-files -d assets/deprecated116 -t "vfoo"
assertions:
- result.code ShouldEqual 2
- result.systemout ShouldEqual "You must pass a valid semver to --target-version. Got 'vfoo'"
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ require (
github.com/DATA-DOG/go-sqlmock v1.4.1 // indirect
github.com/jmoiron/sqlx v1.2.0 // indirect
github.com/lib/pq v1.3.0 // indirect
github.com/rogpeppe/go-internal v1.4.0
github.com/rubenv/sql-migrate v0.0.0-20200212082348-64f95ea68aa3 // indirect
github.com/spf13/cobra v0.0.6
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.4.0
golang.org/x/mod v0.2.0
gopkg.in/yaml.v2 v2.2.8
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
helm.sh/helm v2.16.5+incompatible
Expand Down
Loading

0 comments on commit e8c636d

Please sign in to comment.