一 获取pod基础信息并且格式化展示
package main
import (
"context"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
"log"
"os"
)
var client = InitClient()
func run(c *cobra.Command, args []string) error {
ns, err := c.Flags().GetString("namespace")
if err != nil {
return err
}
if ns == "" {
ns = "default"
}
list, err := client.CoreV1().Pods(ns).List(context.Background(), v1.ListOptions{})
if err != nil {
return err
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"名称", "命名空间", "IP", "状态"})
for _, pod := range list.Items {
table.Append([]string{pod.Name, pod.Namespace, pod.Status.PodIP,
string(pod.Status.Phase)})
}
table.Render()
return nil
}
var cfgFlags *genericclioptions.ConfigFlags
func InitClient() *kubernetes.Clientset {
cfgFlags = genericclioptions.NewConfigFlags(true)
config, err := cfgFlags.ToRawKubeConfigLoader().ClientConfig()
if err != nil {
log.Fatalln(err)
}
c, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalln(err)
}
return c
}
func RunCmd(f func(c *cobra.Command, args []string) error) {
cmd := &cobra.Command{
Use: "kubectl pods [flags]",
Short: "list pods ",
Example: "kubectl pods [flags]",
SilenceUsage: true,
RunE: f,
}
cfgFlags.AddFlags(cmd.Flags())
err := cmd.Execute()
if err != nil {
log.Fatalln(err)
}
}
func main() {
RunCmd(run)
}
go build main.go
mv main /Users/qushuaibo/temp/kubectl-plugins/kubectl-pods
kubectl pods
二 给pod添加标签选项
package main
import (
"context"
"fmt"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
"log"
"os"
)
var (
ShowLabels bool
client = InitClient() //这是 clientset
)
func InitClient() *kubernetes.Clientset{
cfgFlags =genericclioptions.NewConfigFlags(true)
config,err:= cfgFlags.ToRawKubeConfigLoader().ClientConfig()
if err!=nil{log.Fatalln(err)}
c,err:=kubernetes.NewForConfig(config)
if err!=nil{log.Fatalln(err)}
return c
}
func InitHeader(table *tablewriter.Table) []string {
commonHeaders:=[]string{"名称", "命名空间", "IP","状态"}
if ShowLabels{
commonHeaders=append(commonHeaders,"标签")
}
return commonHeaders
}
func Map2String(m map[string]string) (ret string ) {
for k,v:=range m{
ret+=fmt.Sprintf("%s=%s\n",k,v)
}
return
}
func run(c *cobra.Command, args []string) error {
ns, err := c.Flags().GetString("namespace")
if err != nil {
return err
}
if ns == "" {
ns = "default"
}
list, err := client.CoreV1().Pods(ns).List(context.Background(), v1.ListOptions{})
if err != nil {
return err
}
table := tablewriter.NewWriter(os.Stdout)
//设置头
table.SetHeader(InitHeader(table))
for _, pod := range list.Items {
podRow := []string{pod.Name, pod.Namespace, pod.Status.PodIP,
string(pod.Status.Phase)}
if ShowLabels {
podRow = append(podRow, Map2String(pod.Labels))
}
table.Append(podRow)
}
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding("\t") // pad with tabs
table.SetNoWhiteSpace(true)
table.Render()
return nil
}
func RunCmd(f func(c *cobra.Command, args []string) error ) {
cmd := &cobra.Command{
Use: "kubectl pods [flags]",
Short: "list pods ",
Example: "kubectl pods [flags]",
SilenceUsage: true,
RunE: f,
}
cfgFlags.AddFlags(cmd.Flags())
cmd.Flags().BoolVar(&ShowLabels,"show-labels",false,"kubectl pods --show-lables")
err:=cmd.Execute()
if err!=nil{
log.Fatalln(err)
}
}
func main() {
RunCmd(run)
}
|