Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

Commit

Permalink
feat: move deprecation logic to "deprecation list"
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Kotzbauer <christian.kotzbauer@gmail.com>
  • Loading branch information
ckotzbauer committed Sep 19, 2021
1 parent 16d5fca commit 0a8acf2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 41 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ Flags:

### Deprecated API objects

#### List deprecated API objects

To get an overview of api-objects in your cluster which are deprecated you can use this feature. It scans all objects and gives you a list
of objects which are deprecated and will be removed in a future version. You can ignore kinds with `-i`. To only view deprecations until a given
version you can specify `-V`. By default chekr will use the server-version of your cluster. This will hide all items, which are deprecated in
Expand All @@ -155,7 +157,7 @@ This feature was inspired by https://github.com/rikatz/kubepug.
List deprected objects in your cluster.
Usage:
chekr deprecation [flags]
chekr deprecation list [flags]
Flags:
-h, --help help for deprecation
Expand Down
43 changes: 4 additions & 39 deletions cmd/deprecation.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,19 @@
package cmd

import (
"os"

"github.com/ckotzbauer/chekr/pkg/deprecation"
"github.com/ckotzbauer/chekr/pkg/kubernetes"
"github.com/ckotzbauer/chekr/pkg/printer"
"github.com/ckotzbauer/chekr/cmd/deprecation"
"github.com/spf13/cobra"
)

// deprecationCmd represents the deprecation command
var deprecationCmd = &cobra.Command{
Use: "deprecation",
Short: "Lists deprecated objects in cluster.",
Run: func(cmd *cobra.Command, args []string) {
namespace, _ := cmd.Flags().GetString("namespace")
k8sVersion, _ := cmd.Flags().GetString("k8s-version")
ignoredKinds, _ := cmd.Flags().GetStringSlice("ignored-kinds")
throttleBurst, _ := cmd.Flags().GetInt("throttle-burst")

r := deprecation.Deprecation{
KubeOverrides: overrides,
KubeClient: kubernetes.NewClient(cmd, overrides),
Namespace: namespace,
K8sVersion: k8sVersion,
IgnoredKinds: ignoredKinds,
ThrottleBurst: throttleBurst,
}

list := r.Execute()

output, _ := cmd.Flags().GetString("output")
outputFile, _ := cmd.Flags().GetString("output-file")
omitExitCode, _ := cmd.Flags().GetBool("omit-exit-code")

printer := printer.Printer{Type: output, File: outputFile}
printer.Print(list)

items := list.(deprecation.DeprecatedResourceList)
if len(items.Items) > 0 && !omitExitCode {
os.Exit(1)
}
},
Short: "Handle deprecated objects in cluster.",
}

func init() {
rootCmd.AddCommand(deprecationCmd)
deprecationCmd.Flags().StringP("k8s-version", "V", "", "Highest K8s major.minor version to show deprecations for (e.g. 1.21)")
deprecationCmd.Flags().StringSliceP("ignored-kinds", "i", []string{}, "All kinds you want to ignore (e.g. Deployment,DaemonSet)")
deprecationCmd.Flags().IntP("throttle-burst", "t", 100, "Burst used for throttling of Kubernetes discovery-client")
deprecationCmd.Flags().Bool("omit-exit-code", false, "Omits the non-zero exit code if deprecations were found.")

deprecation.InitListCmd(deprecationCmd, overrides)
// Output
}
57 changes: 57 additions & 0 deletions cmd/deprecation/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package deprecation

import (
"os"

"github.com/ckotzbauer/chekr/pkg/deprecation"
"github.com/ckotzbauer/chekr/pkg/kubernetes"
"github.com/ckotzbauer/chekr/pkg/printer"
"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
)

func createListCmd(overrides *clientcmd.ConfigOverrides) *cobra.Command {
// listCmd represents the deprecation list command
listCmd := &cobra.Command{
Use: "list",
Short: "Lists deprecated objects in cluster.",
Run: func(cmd *cobra.Command, args []string) {
k8sVersion, _ := cmd.Flags().GetString("k8s-version")
ignoredKinds, _ := cmd.Flags().GetStringSlice("ignored-kinds")
throttleBurst, _ := cmd.Flags().GetInt("throttle-burst")

r := deprecation.Deprecation{
KubeOverrides: overrides,
KubeClient: kubernetes.NewClient(cmd, overrides),
K8sVersion: k8sVersion,
IgnoredKinds: ignoredKinds,
ThrottleBurst: throttleBurst,
}

list := r.Execute()

output, _ := cmd.Flags().GetString("output")
outputFile, _ := cmd.Flags().GetString("output-file")
omitExitCode, _ := cmd.Flags().GetBool("omit-exit-code")

printer := printer.Printer{Type: output, File: outputFile}
printer.Print(list)

items := list.(deprecation.DeprecatedResourceList)
if len(items.Items) > 0 && !omitExitCode {
os.Exit(1)
}
},
}

listCmd.Flags().StringP("k8s-version", "V", "", "Highest K8s major.minor version to show deprecations for (e.g. 1.21)")
listCmd.Flags().StringSliceP("ignored-kinds", "i", []string{}, "All kinds you want to ignore (e.g. Deployment,DaemonSet)")
listCmd.Flags().Bool("omit-exit-code", false, "Omits the non-zero exit code if deprecations were found.")
listCmd.Flags().IntP("throttle-burst", "t", 100, "Burst used for throttling of Kubernetes discovery-client")
return listCmd
}

func InitListCmd(deprecationCmd *cobra.Command, overrides *clientcmd.ConfigOverrides) {
deprecationCmd.AddCommand(createListCmd(overrides))
// Output
}
1 change: 0 additions & 1 deletion pkg/deprecation/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
type Deprecation struct {
KubeOverrides *clientcmd.ConfigOverrides
KubeClient *kubernetes.KubeClient
Namespace string
K8sVersion string
IgnoredKinds []string
ThrottleBurst int
Expand Down

0 comments on commit 0a8acf2

Please sign in to comment.