forked from k8s-club/k8s-club
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinformer_dynamic_test.go
128 lines (110 loc) · 3.52 KB
/
informer_dynamic_test.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package informer
import (
"K8s_demo/demo/examples/client"
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/dynamic/dynamicinformer"
"k8s.io/client-go/tools/cache"
"log"
"testing"
"time"
)
var (
namespace = "default"
label = "informer-dynamic-simple-" + rand.String(5)
configMapResource = schema.GroupVersionResource{
Group: "",
Version: "v1",
Resource: "configmaps",
}
)
// createConfigMap 使用unstructured.Unstructured创建configMap
func createConfigMap(client dynamic.Interface) *unstructured.Unstructured {
// Unstructured对象
cm := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"namespace": namespace,
"generateName": "informer-dynamic-simple-",
"labels": map[string]interface{}{
"examples": label,
},
},
"data": map[string]interface{}{
"test": "test",
},
},
}
cm, err := client.Resource(configMapResource).Namespace(namespace).Create(context.Background(), cm, metav1.CreateOptions{})
if err != nil {
log.Fatal(err)
}
fmt.Printf("Created ConfigMap %s/%s\n", cm.GetNamespace(), cm.GetName())
return cm
}
func deleteConfigMap(client dynamic.Interface, cm *unstructured.Unstructured) {
err := client.Resource(configMapResource).Namespace(cm.GetNamespace()).Delete(context.Background(), cm.GetName(), metav1.DeleteOptions{})
if err != nil {
log.Fatal(err)
}
fmt.Printf("Deleted ConfigMap %s/%s\n", cm.GetNamespace(), cm.GetName())
}
func TestDynamicInformer(t *testing.T) {
// dynamic客户端
client := client.ClientSet.DynamicClient
// 先创建一个config
first := createConfigMap(client)
factory := dynamicinformer.NewDynamicSharedInformerFactory(client, 5*time.Second)
dynamicInformer := factory.ForResource(configMapResource)
// eventHandler 回调
dynamicInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
cm := obj.(*unstructured.Unstructured)
fmt.Printf("Informer event: ConfigMap add %s/%s\n", cm.GetNamespace(), cm.GetName())
},
UpdateFunc: func(old, new interface{}) {
cm := old.(*unstructured.Unstructured)
fmt.Printf("Informer event: ConfigMap update %s/%s\n", cm.GetNamespace(), cm.GetName())
},
DeleteFunc: func(obj interface{}) {
cm := obj.(*unstructured.Unstructured)
fmt.Printf("Informer event: ConfigMap delete %s/%s\n", cm.GetNamespace(), cm.GetName())
},
})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
fmt.Println("------开始使用informer监听------------")
factory.Start(ctx.Done())
for gvr, ok := range factory.WaitForCacheSync(ctx.Done()) {
if !ok {
log.Fatal(fmt.Sprintf("Failed to sync cache for resource %v", gvr))
}
}
// label查找的方法
selector, err := labels.Parse("examples==" + label)
if err != nil {
log.Fatal(err)
}
list, err := dynamicInformer.Lister().List(selector) // 使用informer list,不从api server
if err != nil {
log.Fatal(err)
}
if len(list) != 1 {
log.Println("expected ConfigMap not found")
}
// 创建cm
second := createConfigMap(client)
// Delete config maps created by this test.
deleteConfigMap(client, first)
deleteConfigMap(client, second)
// 阻塞,可以发现 定时会有update事件,这是同步更新的状态
select {}
}