-
Notifications
You must be signed in to change notification settings - Fork 9
/
enqueuePod.go
96 lines (86 loc) · 2.35 KB
/
enqueuePod.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
package main
import (
"flag"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/runtime"
"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"
"k8s.io/client-go/util/homedir"
"k8s.io/client-go/util/workqueue"
"log"
"path/filepath"
"time"
)
func main() {
var defaultKubeConfigPath string
if home := homedir.HomeDir(); home != "" {
// build kubeconfig path from $HOME dir
defaultKubeConfigPath = filepath.Join(home, ".kube", "config")
}
// set kubeconfig flag
kubeconfig := flag.String("kubeconfig", defaultKubeConfigPath, "kubeconfig config file")
flag.Parse()
config, _ := clientcmd.BuildConfigFromFlags("", *kubeconfig)
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatal(err)
}
// Create InformerFactory
informerFactory := informers.NewSharedInformerFactory(clientset, time.Second*30)
// Create pod informer by informerFactory
podInformer := informerFactory.Core().V1().Pods()
// Create RateLimitQueue
queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
// shutdown when process ends
defer queue.ShutDown()
// Add EventHandler to informer
podInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(old interface{}) {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(old); err != nil {
runtime.HandleError(err)
return
}
queue.Add(key)
log.Println("Added: " + key)
},
UpdateFunc: func(old, new interface{}) {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(new); err != nil {
runtime.HandleError(err)
return
}
queue.Add(key)
log.Println("Updated: " + key)
},
DeleteFunc: func(old interface{}) {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(old); err != nil {
runtime.HandleError(err)
return
}
queue.Add(key)
log.Println("Deleted: " + key)
},
})
// Start Go routines
informerFactory.Start(wait.NeverStop)
// Wait until finish caching with List API
informerFactory.WaitForCacheSync(wait.NeverStop)
// Create Pod Lister
podLister := podInformer.Lister()
// Get List of pods
_, err = podLister.List(labels.Nothing())
if err != nil {
log.Fatal(err)
}
for {
time.Sleep(time.Second * 1)
}
}