You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
package test
import (
"context"
"k8s-manager/handler/common"
"log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
func ClientSetDeployments ( ) {
// 1. 初始化配置
// 加载配置
config , err := clientcmd . BuildConfigFromFlags ( "" , "../" + common . KubeConfigPath )
if err != nil {
log . Fatalln ( err )
}
// 2. 获取clientSet客户端
clientSet , err := kubernetes . NewForConfig ( config )
if err != nil {
log . Fatalln ( err )
}
// 3. 调用封装的方法, 获取资源。获取deployments
namespace := "kube-system"
deploymentList , err := clientSet . AppsV1 ( ) . Deployments ( namespace ) . List ( context . Background ( ) , metav1 . ListOptions { } )
if err != nil {
log . Fatalln ( err )
}
// 4. 处理响应
for _ , item := range deploymentList . Items {
log . Printf ( "NAME: %s READY: %d/%d AVAILABLE: %d" , item . Name , item . Status . ReadyReplicas , item . Status . Replicas , item . Status . AvailableReplicas )
}
}