Skip to content

Commit

Permalink
feat: add ns check code
Browse files Browse the repository at this point in the history
  • Loading branch information
Rookie0031 committed Oct 13, 2024
1 parent b3862cb commit 06063e6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 37 deletions.
39 changes: 39 additions & 0 deletions kubectl-ns-inspect/cmd/check.go
Original file line number Diff line number Diff line change
@@ -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")
}
43 changes: 8 additions & 35 deletions kubectl-ns-inspect/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,24 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
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")
}


3 changes: 1 addition & 2 deletions kubectl-ns-inspect/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package main

import "kubectl-ns-inspect/cmd"
import "kubectl-ns-inspect/kubectl-ns-inspect/cmd"

func main() {
cmd.Execute()
Expand Down

0 comments on commit 06063e6

Please sign in to comment.