Experiment introduction:
Use client-go to connect to the k8s cluster and get the resource list of Deployment under the default namespace
Basic environment preparation:
This experiment is to install and deploy a single-node k8s cluster on the ubuntu operating system, connect the cluster through client-go and operate its resources
1. To use client-go to operate resources outside the k8s cluster, you first need to obtain the kubeconfig configuration file to establish a connection cluster
// 声明三个变量
var err error
var config *rest.Config
var kubeconfig *string
// 定义一个函数用来在操作系统中获取家目录路径
func homeDir() string{
if h:=os.Getenv("HOME");h!=""{
return h
}
return os.Getenv("USERPROFILE") //windows
}
// 在k8s的环境中认证文件一般存放在用户家目录的.kube文件中
if home:=homeDir();home!=""{
kubeconfig=flag.String("kubeconfig",filepath.Join(home,".kube","config"),"(可选)kubeconfig 文件的绝对路径")
}else{
kubeconfig=flag.String("kubeconfig","","kubeconfig 文件的绝对路径") // 如果用户家目录下面没有认证文件则手动指定文件的路径
}
flag.Parse()
2. Create a cluster configuration
// 如果使用 inCluster 模式(需要区配置对应的RBAC 权限,默认的sa是default-->是没有获取deployment的List权限)
if config,err =rest.InClusterConfig();err!=nil{
// 使用Kubeonfig文件配置集群配置Config对象
if config,err=clientcmd.BuildConfigFromFlags("",*kubeconfig);err!=nil{
panic(err.Error())
}
}
3. After obtaining the Config object configured using the Kubeonfig file, create a Clientset object and operate on it
lientset,err:=kubernetes.NewForConfig(config)
if err !=nil{
panic(err.Error())
}
// 可以使用Clientset对象获取资源对象,进行增删改查
// 获取default命令空间下面的Deployment的列表,并打印到终端
deployment,err:=clientset.AppsV1().Deployments("default").List(metav1.ListOptions{})
if err!=nil{
panic(err.Error())
}
for idx,deploy:=range deployment.Items{
fmt.Printf("%d-%s\n",idx,deploy.Name)
}
4. Run code verification function
go run main.go
The complete code example is as follows:
package main
import (
"os"
"fmt"
"flag"
"path/filepath"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func main(){
var err error
var config *rest.Config
// inCluster(Pod).kuberconfig(kubectl)
var kubeconfig *string
if home:=homeDir();home!=""{
kubeconfig=flag.String("kubeconfig",filepath.Join(home,".kube","config"),"(可选)kubeconfig 文件的绝对路径")
}else{
kubeconfig=flag.String("kubeconfig","","kubeconfig 文件的绝对路径")
}
flag.Parse()
// 首先使用 inCluster 模式(需要区配置对应的RBAC 权限,默认的sa是default-->是没有获取deployment的List权限)
if config,err =rest.InClusterConfig();err!=nil{
// 使用Kubeonfig文件配置集群配置Config对象
if config,err=clientcmd.BuildConfigFromFlags("",*kubeconfig);err!=nil{
panic(err.Error())
}
}
// 已经获得了rest.Config对象
// 创建Clientset对象
clientset,err:=kubernetes.NewForConfig(config)
if err !=nil{
panic(err.Error())
}
// 就可以使用Clientset对象获取资源对象,进行增删改查
// 想要获取default命令空间下面的Deployment的列表
deployment,err:=clientset.AppsV1().Deployments("default").List(metav1.ListOptions{})
if err!=nil{
panic(err.Error())
}
for idx,deploy:=range deployment.Items{
fmt.Printf("%d-%s\n",idx,deploy.Name)
}
}
func homeDir() string{
if h:=os.Getenv("HOME");h!=""{
return h
}
return os.Getenv("USERPROFILE") //windows
}