這裡分類和彙總了欣宸的全部原創(含配套原始碼):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"]
}
]
}
名稱 | 連結 | 備註 |
---|---|---|
專案主頁 | https://github.com/zq2599/blog_demos | 該專案在GitHub上的主頁 |
git倉庫地址(https) | https://github.com/zq2599/blog_demos.git | 該專案原始碼的倉庫地址,https協定 |
git倉庫地址(ssh) | [email protected]:zq2599/blog_demos.git | 該專案原始碼的倉庫地址,ssh協定 |