-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (93 loc) · 2.94 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
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
package main
import (
"context"
"flag"
"fmt"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
)
func ListNamespaces(ctx context.Context, clientSet *kubernetes.Clientset) {
namespaces, err := clientSet.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
if err != nil {
panic(err)
}
for _, ns := range namespaces.Items {
fmt.Println(ns.Name)
}
}
// Informer is a function that demonstrates how to use informer to watch for changes in the cluster
//
// Why wouldn't we use Watch API? Because using Watch API will put a lot of pressure on the API server
// for a large number of resources.
func Informer(clienSet *kubernetes.Clientset) {
// After resyncTimeout is reached, the informer will sync
// its cache with the newest state of the resource.
resyncTimeout := 30 * time.Second
// SharedInformerFactory will create informers for resources in all namespaces
informerFactory := informers.NewSharedInformerFactory(clienSet, resyncTimeout)
// But sometimes we only want to watch specific resources in a specific namespace
// informerFactory := informers.NewSharedInformerFactoryWithOptions(
// clienSet,
// resyncTimeout,
// informers.WithTweakListOptions(func(options *metav1.ListOptions) {
// options.LabelSelector = "default"
// }),
// )
podInformer := informerFactory.Core().V1().Pods()
// After new state of the resource is detected, the informer will send
// the events to the handler based on the type.
podInformer.Informer().AddEventHandler(
cache.ResourceEventHandlerFuncs{
AddFunc: func(new interface{}) {
fmt.Println("Pod added")
},
UpdateFunc: func(old, new interface{}) {
fmt.Println("Pod updated")
},
DeleteFunc: func(obj interface{}) {
fmt.Println("Pod deleted")
},
},
)
// Initialize the informer
informerFactory.Start(wait.NeverStop)
// Once the informer is initialized, wait for first api call to complete
informerFactory.WaitForCacheSync(wait.NeverStop)
// We can use the informer to get the pod
_, err := podInformer.Lister().Pods("default").Get("default")
if err != nil {
fmt.Println(err)
}
}
func main() {
ctx := context.Background()
isInCluster := false
kubeconfig := flag.String("kubeconfig", "~/.kube/config", "Path to a kubeconfig. Only required if out-of-cluster.")
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
fmt.Printf("Error building kubeconfig: %s\n", err.Error())
isInCluster = true
}
if isInCluster {
config, err = rest.InClusterConfig()
if err != nil {
panic(err)
}
}
config.Timeout = 120 * time.Second
clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err)
}
// First example, list all namespaces
ListNamespaces(ctx, clientSet)
// Second example, informer
Informer(clientSet)
}