-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
87 lines (73 loc) · 2.04 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
package main
import (
"context"
"fmt"
"path/filepath"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
func main() {
var config *rest.Config
var err error
config, err = rest.InClusterConfig()
if err != nil {
var kubeconfig string
if home := homedir.HomeDir(); home != "" {
kubeconfig = filepath.Join(home, ".kube", "config")
}
config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
fmt.Printf("Error building kubeconfig: %s\n", err)
return
}
}
dynClient, err := dynamic.NewForConfig(config)
if err != nil {
panic(err.Error())
}
thefoothebar := schema.GroupVersionResource{
Group: "myk8s.io",
Version: "v1",
Resource: "thefoosthebars",
}
informer := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
return dynClient.Resource(thefoothebar).Namespace("").List(context.TODO(), options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return dynClient.Resource(thefoothebar).Namespace("").Watch(context.TODO(), options)
},
},
&unstructured.Unstructured{},
0,
cache.Indexers{},
)
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
fmt.Println("Add event detected: obj", obj)
},
UpdateFunc: func(oldObj, newObj interface{}) {
fmt.Println("Update event detected:", newObj)
},
DeleteFunc: func(obj interface{}) {
fmt.Println("Update event detected:", obj)
},
})
stop := make(chan struct{})
defer close(stop)
go informer.Run(stop)
if !cache.WaitForCacheSync(stop, informer.HasSynced) {
panic("Timeout waiting for cache sync")
}
fmt.Println("Custom Resource Controller started successfully")
<-stop
}