-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
executable file
·75 lines (59 loc) · 1.98 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
73
74
75
package main
import (
"flag"
"fmt"
"os"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
defaultKubeconfig := os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
if len(defaultKubeconfig) == 0 {
defaultKubeconfig = clientcmd.RecommendedHomeFile
}
kubeconfig := flag.String(clientcmd.RecommendedConfigPathFlag,
defaultKubeconfig, "absolute path to the kubeconfig file")
flag.Parse()
rc, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
// create a client set from config
clientSet, err := kubernetes.NewForConfig(rc)
if err != nil {
panic(err.Error())
}
// create a new instance of sharedInformerFactory for all namespaces
informerFactory := informers.NewSharedInformerFactory(clientSet, time.Minute*1)
// using this factory create an informer for `secret` resources
secretsInformer := informerFactory.Core().V1().Secrets()
// adds an event handler to the shared informer
secretsInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
item := obj.(*corev1.Secret)
fmt.Printf("secret added (ns=%s): %s\n", item.GetNamespace(), item.GetName())
},
UpdateFunc: func(old, new interface{}) {
item := old.(*corev1.Secret)
fmt.Printf("secret updated (ns=%s): %s\n", item.GetNamespace(), item.GetName())
},
DeleteFunc: func(obj interface{}) {
item := obj.(*corev1.Secret)
fmt.Printf("secret deleted (ns=%s): %s\n", item.GetNamespace(), item.GetName())
},
})
stopCh := make(chan struct{})
defer close(stopCh)
// starts the shared informers that have been created by the factory
informerFactory.Start(stopCh)
// wait for the initial synchronization of the local cache
if !cache.WaitForCacheSync(stopCh, secretsInformer.Informer().HasSynced) {
panic("failed to sync")
}
// causes the goroutine to block (hit CTRL+C to exit)
select {}
}