-
Notifications
You must be signed in to change notification settings - Fork 49
/
main.go
88 lines (79 loc) · 2.34 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
package main
import (
"context"
"os"
"path/filepath"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"github.com/spotahome/kooper/log"
"github.com/spotahome/kooper/operator/controller"
"github.com/spotahome/kooper/operator/handler"
"github.com/spotahome/kooper/operator/retrieve"
)
func main() {
// Initialize logger.
log := &log.Std{}
// Get k8s client.
k8scfg, err := rest.InClusterConfig()
if err != nil {
// No in cluster? letr's try locally
kubehome := filepath.Join(homedir.HomeDir(), ".kube", "config")
k8scfg, err = clientcmd.BuildConfigFromFlags("", kubehome)
if err != nil {
log.Errorf("error loading kubernetes configuration: %s", err)
os.Exit(1)
}
}
k8scli, err := kubernetes.NewForConfig(k8scfg)
if err != nil {
log.Errorf("error creating kubernetes client: %s", err)
os.Exit(1)
}
// Create our retriever so the controller knows how to get/listen for pod events.
retr := &retrieve.Resource{
Object: &corev1.Pod{},
ListerWatcher: &cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
return k8scli.CoreV1().Pods("").List(options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return k8scli.CoreV1().Pods("").Watch(options)
},
},
}
// Our domain logic that will print every add/sync/update and delete event we .
hand := &handler.HandlerFunc{
AddFunc: func(_ context.Context, obj runtime.Object) error {
pod := obj.(*corev1.Pod)
log.Infof("Pod added: %s/%s", pod.Namespace, pod.Name)
return nil
},
DeleteFunc: func(_ context.Context, s string) error {
log.Infof("Pod deleted: %s", s)
return nil
},
}
// Create the controller with custom configuration.
cfg := &controller.Config{
ProcessingJobRetries: 5,
ResyncInterval: 45 * time.Second,
ConcurrentWorkers: 1,
}
ctrl := controller.New(cfg, hand, retr, nil, nil, nil, log)
// Start our controller.
stopC := make(chan struct{})
if err := ctrl.Run(stopC); err != nil {
log.Errorf("error running controller: %s", err)
os.Exit(1)
}
os.Exit(0)
}