This repository has been archived by the owner on Feb 28, 2022. It is now read-only.
/
main.go
117 lines (103 loc) · 3.36 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
114
115
116
117
package main
import (
"context"
"flag"
"log"
"os"
"os/signal"
"sync"
"syscall"
"time"
coreinf "k8s.io/client-go/informers"
corecli "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
"github.com/ricardomaraschini/tagger/controllers"
itagcli "github.com/ricardomaraschini/tagger/imagetags/generated/clientset/versioned"
itaginf "github.com/ricardomaraschini/tagger/imagetags/generated/informers/externalversions"
"github.com/ricardomaraschini/tagger/services"
)
// Controller interface is implemented by all our controllers. Designs a
// process or thread that can be started with a context.
type Controller interface {
Start(ctx context.Context) error
Name() string
}
func main() {
klog.InitFlags(nil)
flag.Parse()
ctx, cancel := context.WithCancel(context.Background())
sigs := make(chan os.Signal)
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-sigs
cancel()
}()
klog.Info(` _|_ __, __, __, _ ,_ `)
klog.Info(` | / | / | / | |/ / | `)
klog.Info(` |_/\_/|_/\_/|/\_/|/|__/ |_/ `)
klog.Info(` /| /| `)
klog.Info(` \| \| `)
klog.Info(`starting image tag controller...`)
kubeconfig := os.Getenv("KUBECONFIG")
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
klog.Fatalf("unable to read kubeconfig: %v", err)
}
// creates tag client and informer.
tagcli, err := itagcli.NewForConfig(config)
if err != nil {
log.Fatalf("unable to create image tag client: %v", err)
}
taginf := itaginf.NewSharedInformerFactory(tagcli, time.Minute)
// creates core client and informer.
corcli, err := corecli.NewForConfig(config)
if err != nil {
log.Fatalf("unable to create core client: %v", err)
}
corinf := coreinf.NewSharedInformerFactory(corcli, time.Minute)
// create our service layer
depsvc := services.NewDeployment(corcli, corinf, taginf)
tagsvc := services.NewTag(corcli, corinf, tagcli, taginf)
mtrsvc := services.NewMetrics()
// create controller layer
itctrl := controllers.NewTag(tagsvc, mtrsvc)
mtctrl := controllers.NewMutatingWebHook(tagsvc)
qyctrl := controllers.NewQuayWebHook(tagsvc)
dkctrl := controllers.NewDockerWebHook(tagsvc)
dpctrl := controllers.NewDeployment(depsvc)
moctrl := controllers.NewMetric()
// starts up all informers and waits for their cache to sync up,
// only then we start the controllers i.e. start to process events
// from the queue.
klog.Info("waiting for caches to sync ...")
corinf.Start(ctx.Done())
taginf.Start(ctx.Done())
if !cache.WaitForCacheSync(
ctx.Done(),
corinf.Core().V1().ConfigMaps().Informer().HasSynced,
corinf.Core().V1().Secrets().Informer().HasSynced,
corinf.Apps().V1().ReplicaSets().Informer().HasSynced,
corinf.Apps().V1().Deployments().Informer().HasSynced,
taginf.Images().V1().Tags().Informer().HasSynced,
) {
klog.Fatal("caches not syncing")
}
klog.Info("caches in sync, moving on.")
var wg sync.WaitGroup
ctrls := []Controller{mtctrl, qyctrl, dkctrl, dpctrl, itctrl, moctrl}
for _, ctrl := range ctrls {
wg.Add(1)
go func(c Controller) {
defer wg.Done()
klog.Infof("starting controller for %q", c.Name())
if err := c.Start(ctx); err != nil {
klog.Errorf("%q failed: %s", c.Name(), err)
return
}
klog.Infof("%q controller ended.", c.Name())
}(ctrl)
}
wg.Wait()
}