一、客户端类型
类型 | 简介 | 是否支持CRD资源 | 所需信息 |
---|---|---|---|
RestClient | 提供RestClient 客户端,也是最基础的客户端。对Kubernetes API 执行RESTFUL 操作 | 支持 | (gvr)Group Version Resource |
ClientSet | 对restClient 进行了对象分类方式的封装,可以实例化特定资源的客户端。以 Resource 和Version 的方式暴露。 例如实例化一个只操作 appv1 版本的Deploy 客户端。 ClientSet 可以认为是一系列资源的集合客户端 | 不支持 | (gvr)Group Version Resource |
DynamicClient | 这是一种动态的客户端,对k8s 任意资源进行操作,包括CRD 。请求返回的结果是 map[string]interface{} | 支持 | (gvr)Group Version Resource |
DiscoveryClient | 发现客户端,在前面的客户端中需要知道资源的Resource 和Version 才能找到你想要的,这些信息太多很难全部记住,这个客户端用于获取资源组、版本等信息 |
二、客户端使用
2.1 RestClient
获取node
信息
// 最基础的客户端,对HTTP Request进行了封装,实现了RESTFul风格的API。
package main
import (
"fmt"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"context"
)
func main() {
config, err := clientcmd.BuildConfigFromFlags("", "/Users/xianyuluo/.kube/config-dev")
if err != nil {
panic(err.Error())
}
config.APIPath = "api"
config.GroupVersion = &corev1.SchemeGroupVersion
config.NegotiatedSerializer = scheme.Codecs
restClient, err := rest.RESTClientFor(config)
if err != nil {
panic(err.Error())
}
result := &corev1.NodeList{}
err = restClient.Get().Namespace("").Resource("nodes").VersionedParams(
&metav1.ListOptions{Limit: 100}, scheme.ParameterCodec).Do(context.TODO()).Into(result)
if err != nil {
panic(err.Error())
}
for _, d := range result.Items {
fmt.Printf("Node name is %s \n", d.Name)
}
}
2.2 ClientSet
获取pod
列表和service
列表
package main
import (
"context"
"fmt"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
config, err := clientcmd.BuildConfigFromFlags("", "/Users/xianyuluo/.kube/config-dev")
if err != nil {
panic(err)
}
clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err)
}
podClient := clientSet.CoreV1().Pods(apiv1.NamespaceAll)
list, err := podClient.List(context.TODO(), metav1.ListOptions{Limit: 500})
if err != nil {
panic(err)
}
for _, pod := range list.Items {
if pod.Name == ""{
fmt.Println("%s pod name is null!", pod)
}
fmt.Print("NAME: %s", pod.Name)
}
svcClient := clientSet.CoreV1().Services("website-operator-system")
svcList, err := svcClient.List(context.TODO(), metav1.ListOptions{Limit: 100})
if err != nil {
panic(err)
}
for _, svc := range svcList.Items {
fmt.Println("SvcName: \t")
fmt.Println(svc.Name)
}
}
2.3 DynamicClient
获取pod
列表和指定的CRD
资源
package main
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
config, err := clientcmd.BuildConfigFromFlags("", "/Users/xianyuluo/.kube/config-dev")
if err != nil {
panic(err.Error())
}
dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
panic(err.Error())
}
gvr := schema.GroupVersionResource{Version: "v1", Resource: "pods"}
unstructObj, err := dynamicClient.Resource(gvr).Namespace(apiv1.NamespaceAll).List(context.TODO(), metav1.ListOptions{Limit: 100})
if err != nil {
panic(err.Error())
}
podList := &corev1.PodList{}
err = runtime.DefaultUnstructuredConverter.FromUnstructured(unstructObj.UnstructuredContent(), podList)
if err != nil {
panic(err.Error())
}
for _, pod := range podList.Items {
fmt.Println(pod.Namespace, pod.Name)
}
// 获取CRD资源
websiteGvr := schema.GroupVersionResource{Version: "v1", Resource: "Website", Group: "website.xianyuluo.com"}
websiteUnstructObj, _ := dynamicClient.Resource(websiteGvr).Namespace("website-operator-system").List(context.TODO(), metav1.ListOptions{Limit: 100})
fmt.Println(websiteUnstructObj)
}
2.4 DiscoverClient
获取所有的GVR
package main
import (
"fmt"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
config, err := clientcmd.BuildConfigFromFlags("","/Users/xianyuluo/.kube/config-dev")
if err != nil {
panic(err.Error())
}
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
panic(err.Error())
}
_, APIResourceList, err := discoveryClient.ServerGroupsAndResources()
if err != nil {
panic(err.Error())
}
for _, list := range APIResourceList {
gv, err := schema.ParseGroupVersion(list.GroupVersion)
if err != nil {
panic(err.Error())
}
for _, resource := range list.APIResources {
fmt.Printf("name: %v, group: %v, version %v\n", resource.Name, gv.Group, gv.Version)
}
}
}