这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
package action
import "k8s.io/client-go/kubernetes"
type Action interface {
DoAction(clientset *kubernetes.Clientset) error
}
package action
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
type ListPod struct{}
func (listPod ListPod) DoAction(clientset *kubernetes.Clientset) error {
namespace := "kube-system"
// 查询pod列表
pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
nums := len(pods.Items)
fmt.Printf("There are %d pods in the cluster\n", nums)
// 如果没有pod就返回了
if nums < 1 {
return nil
}
// 遍历列表中的每个pod
for index, pod := range pods.Items {
fmt.Printf("%v. pod name : %v\n", index, pod.Name)
// 用pod name精确搜索单个pod
podObj, err := clientset.CoreV1().Pods(namespace).Get(context.TODO(), pod.Name, metav1.GetOptions{})
if errors.IsNotFound(err) {
fmt.Printf("Pod %s in namespace %s not found\n", pod.Name, namespace)
} else if statusError, isStatus := err.(*errors.StatusError); isStatus {
fmt.Printf("Error getting pod %s in namespace %s: %v\n",
pod.Name, namespace, statusError.ErrStatus.Message)
} else if err != nil {
panic(err.Error())
} else {
fmt.Printf("Found pod %s in namespace %s\n", podObj.Name, namespace)
}
}
return nil
}
package main
import (
"client-go-tutorials/action"
"flag"
"fmt"
"path/filepath"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
func main() {
var kubeconfig *string
var actionFlag *string
// 试图取到当前账号的家目录
if home := homedir.HomeDir(); home != "" {
// 如果能取到,就把家目录下的.kube/config作为默认配置文件
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
// 如果取不到,就没有默认配置文件,必须通过kubeconfig参数来指定
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
actionFlag = flag.String("action", "list-pod", "指定实际操作功能")
flag.Parse()
fmt.Println("解析命令完毕,开始加载配置文件")
// 加载配置文件
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
// 用clientset类来执行后续的查询操作
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
fmt.Printf("加载配置文件完毕,即将执行业务 [%v]\n", *actionFlag)
var actionInterface action.Action
// 注意,如果有新的功能类实现,就在这里添加对应的处理
switch *actionFlag {
case "list-pod":
listPod := action.ListPod{}
actionInterface = &listPod
case "conflict":
conflict := action.Confilct{}
actionInterface = &conflict
}
err = actionInterface.DoAction(clientset)
if err != nil {
fmt.Printf("err: %v\n", err)
} else {
fmt.Println("执行完成")
}
}
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"args": ["-action=list-pod"]
}
]
}
名称 | 链接 | 备注 |
---|---|---|
项目主页 | 该项目在GitHub上的主页 | |
git仓库地址(https) | 该项目源码的仓库地址,https协议 | |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |