diff --git a/README.md b/README.md index 70e16bb..107c911 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/cmd/deprecation.go b/cmd/deprecation.go index 9909bf5..f38012c 100644 --- a/cmd/deprecation.go +++ b/cmd/deprecation.go @@ -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 } diff --git a/cmd/deprecation/list.go b/cmd/deprecation/list.go new file mode 100644 index 0000000..0a1e540 --- /dev/null +++ b/cmd/deprecation/list.go @@ -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 +} diff --git a/pkg/deprecation/types.go b/pkg/deprecation/types.go index c58e468..88b1979 100644 --- a/pkg/deprecation/types.go +++ b/pkg/deprecation/types.go @@ -12,7 +12,6 @@ import ( type Deprecation struct { KubeOverrides *clientcmd.ConfigOverrides KubeClient *kubernetes.KubeClient - Namespace string K8sVersion string IgnoredKinds []string ThrottleBurst int