-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (60 loc) · 1.85 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
package main
import (
"context"
"fmt"
"path/filepath"
"github.com/musale-ms/hello-crd/pkg/hello"
v1 "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 kubecfg string
// check the home directory exists and fill the path to the .kube/config
if home := homedir.HomeDir(); home != "" {
kubecfg = filepath.Join(home, ".kube", "config")
}
config, err := clientcmd.BuildConfigFromFlags("", kubecfg)
if err != nil {
fmt.Println("Using the config in the cluster")
config, err = rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
}
client, err := dynamic.NewForConfig(config)
if err != nil {
panic(err.Error())
}
helloResource := schema.GroupVersionResource{Group: "localusr.io", Version: "v1", Resource: "hello"}
informer := cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options v1.ListOptions) (runtime.Object, error) {
return client.Resource(helloResource).Namespace("").List(context.TODO(), options)
},
WatchFunc: func(options v1.ListOptions) (watch.Interface, error) {
return client.Resource(helloResource).Namespace("").Watch(context.TODO(), options)
},
},
&unstructured.Unstructured{},
0,
cache.Indexers{},
)
helloEventHandler := hello.HelloEventHandler{}
informer.AddEventHandler(helloEventHandler.NewEvent())
stop := make(chan struct{})
defer close(stop)
go informer.Run(stop)
if !cache.WaitForCacheSync(stop, informer.HasSynced) {
panic("Timeout waiting for the cache to sync")
}
fmt.Println("Custom Resource Controller started successfully")
<-stop
}