Skip to content

Commit

Permalink
Fix return message when no known versions are parsed. Remove show-all (
Browse files Browse the repository at this point in the history
…#100)

* Change error messaging and remove show-all flag

* Update readme to remove show-all

* Remove comments

* Fixing e2e

* Remove -A

* increase test coverage of namespaces

* Remove unused error output of tabOut function

* Fix test
  • Loading branch information
Andrew Suderman authored Jul 9, 2020
1 parent 88b8501 commit 8c8b057
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 105 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,14 @@ cert-manager/cert-manager-webhook cert-manager MutatingWebhookConfiguration

### Helm Chart Checking (local files)

You can run `helm template <chart-dir> | pluto detect --show-all -`
You can run `helm template <chart-dir> | pluto detect -`

This will output something like so:

```
$ helm template e2e/tests/assets/helm3chart | pluto detect --show-all -
$ helm template e2e/tests/assets/helm3chart | pluto detect -
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
Expand Down Expand Up @@ -218,15 +217,14 @@ You can target the version you are concerned with by using the `--target-version
For example:

```
$ pluto detect-helm --target-version k8s=v1.15.0 --show-all
NAME KIND VERSION REPLACEMENT REMOVED DEPRECATED
cert-manager/cert-manager-webhook MutatingWebhookConfiguration admissionregistration.k8s.io/v1beta1 admissionregistration.k8s.io/v1 false false
$ pluto detect-helm --target-version k8s=v1.15.0
No output to display
$ 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.
Notice that there is no output, despite the fact that we might have recognized apiVersions present in the cluster that are not yet deprecated or removed in v1.15.0. This particular run exited 0.

### Adding Custom Version Checks

Expand Down
8 changes: 0 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ var (
additionalVersionsFile string
directory string
outputFormat string
showAll bool
helmVersion string
helmStore string
ignoreDeprecations bool
Expand All @@ -47,7 +46,6 @@ var (
)

func init() {
rootCmd.PersistentFlags().BoolVarP(&showAll, "show-all", "A", false, "If enabled, will show files that have non-deprecated and non-removed apiVersion. Only applies to tabular output.")
rootCmd.PersistentFlags().BoolVar(&ignoreDeprecations, "ignore-deprecations", false, "Ignore the default behavior to exit 2 if deprecated apiVersions are found.")
rootCmd.PersistentFlags().BoolVar(&ignoreRemovals, "ignore-removals", false, "Ignore the default behavior to exit 3 if removed apiVersions are found.")
rootCmd.PersistentFlags().StringVarP(&additionalVersionsFile, "additional-versions", "f", "", "Additional deprecated versions file to add to the list. Cannot contain any existing versions")
Expand Down Expand Up @@ -150,7 +148,6 @@ var rootCmd = &cobra.Command{
apiInstance = &api.Instance{
TargetVersions: targetVersions,
OutputFormat: outputFormat,
ShowAll: showAll,
IgnoreDeprecations: ignoreDeprecations,
IgnoreRemovals: ignoreRemovals,
DeprecatedVersions: deprecatedVersionList,
Expand All @@ -173,11 +170,6 @@ var detectFilesCmd = &cobra.Command{
os.Exit(1)
}

if dir.Instance.Outputs == nil {
fmt.Println("No api-versioned files found in specified directory.")
os.Exit(0)
}

err = apiInstance.DisplayOutput()
if err != nil {
fmt.Println("Error Parsing Output:", err)
Expand Down
4 changes: 2 additions & 2 deletions e2e/tests/00_static_files.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ testcases:
- script: pluto detect-files -d assets/non-deprecated --target-versions k8s=v1.16.0
assertions:
- result.code ShouldEqual 0
- result.systemout ShouldContainSubstring "No api-versioned files found in specified directory."
- result.systemout ShouldContainSubstring "There were no resources found with known deprecated apiVersions."

- name: static files target v1.8.0 not removed or deprecated
steps:
- script: pluto detect-files -d assets/deprecated116 --target-versions k8s=v1.8.0
assertions:
- result.code ShouldEqual 0
- result.systemout ShouldContainSubstring "APIVersions were found, but none were deprecated. Try --show-all."
- result.systemout ShouldContainSubstring "No output to display"

- name: static files json
steps:
Expand Down
4 changes: 2 additions & 2 deletions e2e/tests/02_helm-detect-2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ testcases:
- result.systemout ShouldContainSubstring "invincible-zebu/invincible-zebu-metrics-server Deployment extensions/v1beta1 apps/v1 true true"
- result.systemout ShouldNotContainSubstring "lunging-bat/lunging-bat-metrics-server Deployment apps/v1 false false"

- name: helm2 detect show all wide namespaced secrets
- name: helm2 detect wide namespaced secrets
steps:
- script: pluto detect-helm --helm-version=2 --helm-store secrets -A --target-versions k8s=v1.16.0 -owide
- script: pluto detect-helm --helm-version=2 --helm-store secrets --target-versions k8s=v1.16.0 -owide
assertions:
- result.code ShouldEqual 3
- result.systemout ShouldContainSubstring "NAME NAMESPACE KIND VERSION REPLACEMENT DEPRECATED DEPRECATED IN REMOVED REMOVED IN"
Expand Down
24 changes: 8 additions & 16 deletions pkg/api/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type Instance struct {
IgnoreDeprecations bool `json:"-" yaml:"-"`
IgnoreRemovals bool `json:"-" yaml:"-"`
OutputFormat string `json:"-" yaml:"-"`
ShowAll bool `json:"show-all,omitempty" yaml:"show-all,omitempty"`
TargetVersions map[string]string `json:"target-versions,omitempty" yaml:"target-versions,omitempty"`
DeprecatedVersions []Version `json:"-" yaml:"-"`
}
Expand All @@ -42,20 +41,14 @@ func (instance *Instance) DisplayOutput() error {
var outData []byte
switch instance.OutputFormat {
case "normal":
t, err := instance.tabOut()
if err != nil {
return err
}
t := instance.tabOut()
err = t.Flush()
if err != nil {
return err
}
return nil
case "wide":
t, err := instance.tabOut()
if err != nil {
return err
}
t := instance.tabOut()
err = t.Flush()
if err != nil {
return err
Expand Down Expand Up @@ -84,23 +77,22 @@ func (instance *Instance) filterOutput() {
for _, output := range instance.Outputs {
output.Deprecated = output.APIVersion.isDeprecatedIn(instance.TargetVersions)
output.Removed = output.APIVersion.isRemovedIn(instance.TargetVersions)
if instance.ShowAll {
usableOutputs = append(usableOutputs, output)
} else if output.APIVersion.isDeprecatedIn(instance.TargetVersions) || output.APIVersion.isRemovedIn(instance.TargetVersions) {

if output.Deprecated || output.Removed {
usableOutputs = append(usableOutputs, output)
}
}
instance.Outputs = usableOutputs

}

func (instance *Instance) tabOut() (*tabwriter.Writer, error) {
func (instance *Instance) tabOut() *tabwriter.Writer {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 15, 2, padChar, 0)

if len(instance.Outputs) == 0 {
_, _ = fmt.Fprintln(w, "APIVersions were found, but none were deprecated. Try --show-all.")
return w, nil
_, _ = fmt.Fprintln(w, "No output to display")
return w
}

if instance.OutputFormat == "normal" {
Expand Down Expand Up @@ -142,7 +134,7 @@ func (instance *Instance) tabOut() (*tabwriter.Writer, error) {
}

}
return w, nil
return w
}

// GetReturnCode checks for deprecated versions and returns a code.
Expand Down
96 changes: 31 additions & 65 deletions pkg/api/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ var testOutput1 = &Output{
Name: "some name one",
Namespace: "pluto-namespace",
APIVersion: &Version{
Name: "apps/v1",
Name: "extensions/v1beta1",
Kind: "Deployment",
DeprecatedIn: "",
RemovedIn: "",
ReplacementAPI: "",
DeprecatedIn: "v1.9.0",
RemovedIn: "v1.16.0",
ReplacementAPI: "apps/v1",
Component: "foo",
},
}
Expand All @@ -44,48 +44,20 @@ var testOutput2 = &Output{
},
}

func init() {
padChar = byte('-')
}

func ExampleInstance_DisplayOutput_showAll_normal() {
instance := &Instance{
TargetVersions: map[string]string{
"foo": "v1.15.0",
},
Outputs: []*Output{
testOutput1,
testOutput2,
},
OutputFormat: "normal",
ShowAll: true,
}
_ = instance.DisplayOutput()

// Output:
// NAME----------- KIND-------- VERSION------------- REPLACEMENT-- REMOVED-- DEPRECATED--
// some name one-- Deployment-- apps/v1------------- ------------- false---- false-------
// some name two-- Deployment-- extensions/v1beta1-- apps/v1------ false---- true--------
var testOutputNoOutput = &Output{
Name: "not a deprecated object",
APIVersion: &Version{
Name: "apps/v1",
Kind: "Deployment",
DeprecatedIn: "",
RemovedIn: "",
ReplacementAPI: "",
Component: "foo",
},
}

func ExampleInstance_DisplayOutput_showAll_wide() {
instance := &Instance{
TargetVersions: map[string]string{
"foo": "v1.16.0",
},
Outputs: []*Output{
testOutput1,
testOutput2,
},
OutputFormat: "wide",
ShowAll: true,
}
_ = instance.DisplayOutput()

// Output:
// NAME----------- NAMESPACE-------- KIND-------- VERSION------------- REPLACEMENT-- DEPRECATED-- DEPRECATED IN-- REMOVED-- REMOVED IN--
// some name one-- pluto-namespace-- Deployment-- apps/v1------------- ------------- false------- --------------- false---- ------------
// some name two-- <UNKNOWN>-------- Deployment-- extensions/v1beta1-- apps/v1------ true-------- v1.9.0--------- true----- v1.16.0-----
func init() {
padChar = byte('-')
}

func ExampleInstance_DisplayOutput_normal() {
Expand All @@ -98,12 +70,12 @@ func ExampleInstance_DisplayOutput_normal() {
testOutput2,
},
OutputFormat: "normal",
ShowAll: false,
}
_ = instance.DisplayOutput()

// Output:
// NAME----------- KIND-------- VERSION------------- REPLACEMENT-- REMOVED-- DEPRECATED--
// some name one-- Deployment-- extensions/v1beta1-- apps/v1------ true----- true--------
// some name two-- Deployment-- extensions/v1beta1-- apps/v1------ true----- true--------
}

Expand All @@ -117,16 +89,16 @@ func ExampleInstance_DisplayOutput_wide() {
testOutput2,
},
OutputFormat: "wide",
ShowAll: false,
}
_ = instance.DisplayOutput()

// Output:
// NAME----------- NAMESPACE-- KIND-------- VERSION------------- REPLACEMENT-- DEPRECATED-- DEPRECATED IN-- REMOVED-- REMOVED IN--
// some name two-- <UNKNOWN>-- Deployment-- extensions/v1beta1-- apps/v1------ true-------- v1.9.0--------- true----- v1.16.0-----
// NAME----------- NAMESPACE-------- KIND-------- VERSION------------- REPLACEMENT-- DEPRECATED-- DEPRECATED IN-- REMOVED-- REMOVED IN--
// some name one-- pluto-namespace-- Deployment-- extensions/v1beta1-- apps/v1------ true-------- v1.9.0--------- true----- v1.16.0-----
// some name two-- <UNKNOWN>-------- Deployment-- extensions/v1beta1-- apps/v1------ true-------- v1.9.0--------- true----- v1.16.0-----
}

func ExampleInstance_DisplayOutput_showAll_json() {
func ExampleInstance_DisplayOutput_json() {
instance := &Instance{
TargetVersions: map[string]string{
"foo": "v1.16.0",
Expand All @@ -136,15 +108,14 @@ func ExampleInstance_DisplayOutput_showAll_json() {
testOutput2,
},
OutputFormat: "json",
ShowAll: true,
}
_ = instance.DisplayOutput()

// Output:
// {"items":[{"name":"some name one","namespace":"pluto-namespace","api":{"version":"apps/v1","kind":"Deployment","deprecated-in":"","removed-in":"","replacement-api":"","component":"foo"},"deprecated":false,"removed":false},{"name":"some name two","api":{"version":"extensions/v1beta1","kind":"Deployment","deprecated-in":"v1.9.0","removed-in":"v1.16.0","replacement-api":"apps/v1","component":"foo"},"deprecated":true,"removed":true}],"show-all":true,"target-versions":{"foo":"v1.16.0"}}
// {"items":[{"name":"some name one","namespace":"pluto-namespace","api":{"version":"extensions/v1beta1","kind":"Deployment","deprecated-in":"v1.9.0","removed-in":"v1.16.0","replacement-api":"apps/v1","component":"foo"},"deprecated":true,"removed":true},{"name":"some name two","api":{"version":"extensions/v1beta1","kind":"Deployment","deprecated-in":"v1.9.0","removed-in":"v1.16.0","replacement-api":"apps/v1","component":"foo"},"deprecated":true,"removed":true}],"target-versions":{"foo":"v1.16.0"}}
}

func ExampleInstance_DisplayOutput_showAll_yaml() {
func ExampleInstance_DisplayOutput_yaml() {
instance := &Instance{
TargetVersions: map[string]string{
"foo": "v1.16.0",
Expand All @@ -154,7 +125,6 @@ func ExampleInstance_DisplayOutput_showAll_yaml() {
testOutput2,
},
OutputFormat: "yaml",
ShowAll: true,
}
_ = instance.DisplayOutput()

Expand All @@ -163,14 +133,14 @@ func ExampleInstance_DisplayOutput_showAll_yaml() {
// - name: some name one
// namespace: pluto-namespace
// api:
// version: apps/v1
// version: extensions/v1beta1
// kind: Deployment
// deprecated-in: ""
// removed-in: ""
// replacement-api: ""
// deprecated-in: v1.9.0
// removed-in: v1.16.0
// replacement-api: apps/v1
// component: foo
// deprecated: false
// removed: false
// deprecated: true
// removed: true
// - name: some name two
// api:
// version: extensions/v1beta1
Expand All @@ -181,7 +151,6 @@ func ExampleInstance_DisplayOutput_showAll_yaml() {
// component: foo
// deprecated: true
// removed: true
// show-all: true
// target-versions:
// foo: v1.16.0
}
Expand All @@ -192,14 +161,13 @@ func ExampleInstance_DisplayOutput_noOutput() {
"foo": "v1.16.0",
},
Outputs: []*Output{
testOutput1,
testOutputNoOutput,
},
OutputFormat: "normal",
ShowAll: false,
}
_ = instance.DisplayOutput()

// Output: APIVersions were found, but none were deprecated. Try --show-all.
// Output: No output to display
}

func ExampleInstance_DisplayOutput_badFormat() {
Expand All @@ -212,7 +180,6 @@ func ExampleInstance_DisplayOutput_badFormat() {
testOutput2,
},
OutputFormat: "foo",
ShowAll: false,
}
_ = instance.DisplayOutput()

Expand All @@ -226,7 +193,6 @@ func ExampleInstance_DisplayOutput_zeroLength() {
},
Outputs: []*Output{},
OutputFormat: "normal",
ShowAll: false,
}
_ = instance.DisplayOutput()

Expand Down
2 changes: 1 addition & 1 deletion pkg/api/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func yamlToStub(data []byte) ([]*Stub, error) {
break
}
if errors.As(err, &tError) {
klog.Infof("skipping for invalid yaml in manifest: %s", err)
klog.V(2).Infof("skipping for invalid yaml in manifest: %s", err)
continue
}
return stubs, err
Expand Down
Loading

0 comments on commit 8c8b057

Please sign in to comment.