From 06063e67d2dbbc28a8f2eb70a550c45b46ea0a30 Mon Sep 17 00:00:00 2001 From: Declan <103009135+Rookie0031@users.noreply.github.com> Date: Sun, 13 Oct 2024 22:04:00 +0900 Subject: [PATCH] feat: add ns check code --- kubectl-ns-inspect/cmd/check.go | 39 ++++++++++++++++++++++++++++++ kubectl-ns-inspect/cmd/root.go | 43 ++++++--------------------------- kubectl-ns-inspect/main.go | 3 +-- 3 files changed, 48 insertions(+), 37 deletions(-) create mode 100644 kubectl-ns-inspect/cmd/check.go diff --git a/kubectl-ns-inspect/cmd/check.go b/kubectl-ns-inspect/cmd/check.go new file mode 100644 index 0000000..409d688 --- /dev/null +++ b/kubectl-ns-inspect/cmd/check.go @@ -0,0 +1,39 @@ +package cmd + +import ( + "fmt" + "os/exec" + + "github.com/spf13/cobra" +) + +var namespace string + +var checkCmd = &cobra.Command{ + Use: "check", + Short: "Check if a namespace has no resources", + Long: `This command checks a given namespace to see if it contains any resources.`, + Run: func(cmd *cobra.Command, args []string) { + // 이곳에서 kubectl 명령어를 실행하여 네임스페이스의 리소스를 검사합니다. + if namespace == "" { + fmt.Println("Please provide a namespace.") + return + } + + // `kubectl get all` 명령어로 네임스페이스를 검사하는 예시입니다. + out, err := exec.Command("kubectl", "get", "all", "-n", namespace).Output() + if err != nil { + fmt.Printf("Error checking namespace: %v\n", err) + return + } + + fmt.Printf("Resources in namespace %s:\n%s\n", namespace, string(out)) + }, +} + +func init() { + rootCmd.AddCommand(checkCmd) + + // 플래그 추가 (예: --namespace) + checkCmd.Flags().StringVarP(&namespace, "namespace", "n", "", "Namespace to inspect") +} diff --git a/kubectl-ns-inspect/cmd/root.go b/kubectl-ns-inspect/cmd/root.go index c06d3af..9b0c7b9 100644 --- a/kubectl-ns-inspect/cmd/root.go +++ b/kubectl-ns-inspect/cmd/root.go @@ -1,51 +1,24 @@ -/* -Copyright © 2024 NAME HERE - -*/ package cmd import ( + "fmt" "os" "github.com/spf13/cobra" ) - - -// rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "kubectl-ns-inspect", - Short: "A brief description of your application", - Long: `A longer description that spans multiple lines and likely contains -examples and usage of using your application. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, - // Uncomment the following line if your bare application - // has an action associated with it: - // Run: func(cmd *cobra.Command, args []string) { }, + Short: "Inspect if a namespace has no resources", + Long: `This plugin inspects a given Kubernetes namespace to check if it contains any resources.`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("kubectl-ns-inspect is a Kubernetes plugin to check namespaces.") + }, } -// Execute adds all child commands to the root command and sets flags appropriately. -// This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { - err := rootCmd.Execute() - if err != nil { + if err := rootCmd.Execute(); err != nil { + fmt.Println(err) os.Exit(1) } } - -func init() { - // Here you will define your flags and configuration settings. - // Cobra supports persistent flags, which, if defined here, - // will be global for your application. - - // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.kubectl-ns-inspect.yaml)") - - // Cobra also supports local flags, which will only run - // when this action is called directly. - rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") -} - - diff --git a/kubectl-ns-inspect/main.go b/kubectl-ns-inspect/main.go index c6b1e5e..2a7775a 100644 --- a/kubectl-ns-inspect/main.go +++ b/kubectl-ns-inspect/main.go @@ -1,10 +1,9 @@ /* Copyright © 2024 NAME HERE - */ package main -import "kubectl-ns-inspect/cmd" +import "kubectl-ns-inspect/kubectl-ns-inspect/cmd" func main() { cmd.Execute()