/
main.go
72 lines (63 loc) · 1.7 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"time"
_ "github.com/golang/glog"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
)
func runInformer() error {
kubeConfigPath := flag.String("kubeconfig", filepath.Join(os.Getenv("HOME"), ".kube", "config"), "kube config path")
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeConfigPath)
if err != nil {
return err
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return err
}
// POD INFORMER OMIT
resyncInterval := 30 * time.Second
informerFactory := informers.NewSharedInformerFactory(clientset, resyncInterval) // HL1
podInformer := informerFactory.Core().V1().Pods() // HL2
// POD INFORMER OMIT
// POD HOOKS OMIT
podInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(new interface{}) { // HL
fmt.Printf("add pod: %v\n", new.(*v1.Pod).Name)
},
UpdateFunc: func(old, new interface{}) { // HL
fmt.Printf("update pod: %v\n", new.(*v1.Pod).Name)
},
DeleteFunc: func(obj interface{}) { // HL
fmt.Printf("delete pod: %v\n", obj.(*v1.Pod).Name)
},
})
go informerFactory.Start(wait.NeverStop) // HL
// POD HOOKS OMIT
for {
time.Sleep(time.Second * 5)
// POD LISTER OMIT
pod, err := podInformer.Lister().Pods("default").Get("shell")
// POD LISTER OMIT
if err != nil {
fmt.Println(fmt.Errorf("Error getting pod: %v", err))
continue
}
fmt.Printf("Labels of pod `shell`: %v\n", pod.GetLabels())
}
}
func main() {
err := runInformer()
if err != nil {
panic(err)
}
}