-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
62 lines (47 loc) · 1.57 KB
/
Copy pathmain.go
File metadata and controls
62 lines (47 loc) · 1.57 KB
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
package main
import (
"fmt"
"github.com/spongeprojects/client-go/api/spongeprojects.com/v1alpha1"
"github.com/spongeprojects/client-go/client/clientset/versioned"
"github.com/spongeprojects/client-go/client/informers/externalversions"
"github.com/spongeprojects/magicconch"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
"os"
)
// main just demonstrates usage of this package
func main() {
kubeconfig := os.Getenv("KUBECONFIG")
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
magicconch.Must(err)
clientset, err := versioned.NewForConfig(config)
magicconch.Must(err)
informerFactory := externalversions.NewSharedInformerFactory(clientset, 0)
channelsInformer := informerFactory.Spongeprojects().V1alpha1().Channels().Informer()
channelsLister := informerFactory.Spongeprojects().V1alpha1().Channels().Lister()
channelsInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
c, ok := obj.(*v1alpha1.Channel)
if !ok {
return
}
fmt.Printf("channel added: %s\n", c.Name)
},
})
stopCh := make(chan struct{})
defer close(stopCh)
klog.Info("start syncing")
go informerFactory.Start(stopCh)
klog.Info("waiting for cache to sync...")
cache.WaitForCacheSync(stopCh, channelsInformer.HasSynced)
klog.Info("cache synced")
channels, err := channelsLister.List(labels.Everything())
magicconch.Must(err)
fmt.Printf("listing %d channels:\n", len(channels))
for _, channel := range channels {
fmt.Printf(" %s\n", channel.Name)
}
<-stopCh
}