|
| 1 | +/* |
| 2 | +Copyright © 2022 poorops poorops@163.com |
| 3 | +
|
| 4 | +*/ |
| 5 | +package cmd |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "k8s.io/cli-runtime/pkg/genericclioptions" |
| 11 | + "k8s.io/utils/strings/slices" |
| 12 | + "kubectl-check/pkg/base" |
| 13 | + "kubectl-check/pkg/check" |
| 14 | + "kubectl-check/pkg/client" |
| 15 | + "kubectl-check/pkg/table" |
| 16 | + "os" |
| 17 | + "strings" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + KubernetesConfigFlags *genericclioptions.ConfigFlags |
| 22 | + field string |
| 23 | + selector string |
| 24 | +) |
| 25 | + |
| 26 | +// rootCmd represents the base command when called without any subcommands |
| 27 | +var rootCmd = &cobra.Command{ |
| 28 | + Use: "kubectl-check", |
| 29 | + Short: "check field from k8s resource definition", |
| 30 | + Long: `kubectl-check is a plugin for kubectl command. |
| 31 | +the plugin is a tool to check definition for the specified filed from k8s resource. |
| 32 | +For Example: |
| 33 | + kubectl check -f nodeSelector`, |
| 34 | + // Uncomment the following line if your bare application |
| 35 | + // has an action associated with it: |
| 36 | + Run: func(cmd *cobra.Command, args []string) { |
| 37 | + namespace, _ := cmd.Flags().GetString("namespace") |
| 38 | + if namespace == "" { |
| 39 | + namespace = "default" |
| 40 | + } |
| 41 | + |
| 42 | + f, _ := cmd.Flags().GetString("field") |
| 43 | + l, _ := cmd.Flags().GetString("selector") |
| 44 | + |
| 45 | + if !slices.Contains(base.ValidFields, f) { |
| 46 | + fmt.Printf("invalid filed, for now supported filed is: %s, current is %s\n", strings.Join(base.ValidFields, ", "), f) |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + client, err := client.NewClientSet(KubernetesConfigFlags) |
| 51 | + if err != nil { |
| 52 | + fmt.Printf("error to set client: %v", err) |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + plugin := check.NewPlugin(f, namespace, l, client) |
| 57 | + result, e := plugin.Value() |
| 58 | + if e != nil { |
| 59 | + fmt.Printf("error to get result: %v", e) |
| 60 | + return |
| 61 | + } |
| 62 | + table.GenTable(result) |
| 63 | + }, |
| 64 | +} |
| 65 | + |
| 66 | +// Execute adds all child commands to the root command and sets flags appropriately. |
| 67 | +// This is called by main.main(). It only needs to happen once to the rootCmd. |
| 68 | +func Execute() { |
| 69 | + err := rootCmd.Execute() |
| 70 | + if err != nil { |
| 71 | + os.Exit(1) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func init() { |
| 76 | + // Here you will define your flags and configuration settings. |
| 77 | + // Cobra supports persistent flags, which, if defined here, |
| 78 | + // will be global for your application. |
| 79 | + |
| 80 | + // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.kubectl-check.yaml)") |
| 81 | + |
| 82 | + // Cobra also supports local flags, which will only run |
| 83 | + // when this action is called directly. |
| 84 | + KubernetesConfigFlags = genericclioptions.NewConfigFlags(true) |
| 85 | + rootCmd.Flags().StringVarP(&field, "field", "f", "image", "The field to check") |
| 86 | + rootCmd.Flags().StringVarP(&selector, "selector", "l", "", "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)") |
| 87 | + KubernetesConfigFlags.AddFlags(rootCmd.Flags()) |
| 88 | +} |
0 commit comments