三个Client

在Kubernetes上,通常需要Client来访问Kubernetes中的对象,目前最常用的是RESTClient, DynamicClient和ClientSet这三种Client。今天就先介绍下这三个Client基本含义及大概的用法。Demo的编写是参考了http://blog.csdn.net/column/details/14420.html ,非常感谢。

RESTClient

RESTClient是Kubernetes最基础的Client,直接负责与Request(RESTClient中的概念)打交道。下面的Demo就描述如何生成一个RESTClient,并用该RESTClient获取某具体Pod的详细信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"flag"
"fmt"
"k8s.io/client-go/pkg/runtime"
"k8s.io/client-go/pkg/runtime/serializer"
"k8s.io/client-go/pkg/api"
v1 "k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/pkg/api/unversioned"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
kubeconfig := flag.String("kubeconfig", "/root/.kube/config", "Path to a kube config. Only required if out-of-cluster.")
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
fmt.Println("BuildConfigFromFlags error")
}
groupversion := &unversioned.GroupVersion{"", "v1"}
config.GroupVersion = groupversion
config.APIPath = "/api"
config.ContentType = runtime.ContentTypeJSON
config.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: api.Codecs}
restClient, err := rest.RESTClientFor(config)
if err != nil {
fmt.Println("RESTClientFor error")
}
pod := v1.Pod{}
err = restClient.Get().Resource("pods").Namespace("default").Name("nginx-1487191267-b4w5j").Do().Into(&pod)
if err != nil {
fmt.Println("error")
}
fmt.Println(pod)
}

运行结果内容过多,略。

DynamicClient

RESTClient需要自己设置请求各属性,用起来很不方便。DynamicClient是对RESTClient的封装,支持动态设置访问类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"flag"
"fmt"
"reflect"
"encoding/json"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/pkg/api/unversioned"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/rest"
)
func main() {
kubeconfig := flag.String("kubeconfig", "/root/.kube/config", "Path to a kube config. Only required if out-of-cluster.")
fmt.Println(kubeconfig)
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
fmt.Println("error1")
}
// 生成dynamicClient
gv := &unversioned.GroupVersion{"", "v1"}
resource := &unversioned.APIResource{Name: "pods", Namespaced: true}
config.ContentConfig = rest.ContentConfig{GroupVersion: gv}
config.APIPath = "/api"
dynamicClient, err := dynamic.NewClient(config)
if err != nil {
fmt.Println("dynamic NewCLient error")
}
// 获取所有namespace的pod列表
obj, err := dynamicClient.Resource(resource, "").List(&v1.ListOptions{})
if err != nil {
fmt.Println("dynamicClient Resource error")
}
js, err := json.Marshal(reflect.ValueOf(obj).Elem().Interface())
if err != nil {
fmt.Println("error")
}
podlist := v1.PodList{}
json.Unmarshal(js, &podlist)
fmt.Println(podlist)
fmt.Println("------------------------")
// 获取具体具体pod
obj, err = dynamicClient.Resource(resource, "default").Get("nginx-1487191267-b4w5j")
if err != nil {
fmt.Println("dynamicClient Resource error")
}
js, err = json.Marshal(obj)
if err != nil {
fmt.Println("error")
}
pod := v1.Pod{}
json.Unmarshal(js, &pod)
fmt.Println(pod)
}

运行结果内容过多,略。

ClientSet

ClientSet也是对RESTClient的一种封装,与DynamicClient不同的是,ClientSet支持衍生出具体资源的Client,如PodClient等。ClientSet是Kubernetes用的最多的Client类型 。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main
import (
"flag"
"fmt"
apiv1 "k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
kubeconfig := flag.String("kubeconfig", "/root/.kube/config", "Path to a kube config. Only required if out-of-cluster.")
fmt.Println(kubeconfig)
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
fmt.Println("error")
}
// 生成clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
fmt.Println("error")
}
// 生成podCLient
podClient := clientset.Core().Pods("")
pods, err := podClient.List(apiv1.ListOptions{})
if err != nil {
fmt.Println("error")
}
for _, pod := range pods.Items {
fmt.Println(pod)
}
}

运行结果内容过多,略。