-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
205 lines (169 loc) · 4.59 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"context"
"github.com/deinstapel/domainik/domainik"
"github.com/deinstapel/domainik/domainmanager"
corev1 "github.com/ericchiang/k8s/apis/core/v1"
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
"os/signal"
"sync"
"syscall"
"github.com/ericchiang/k8s"
"github.com/ghodss/yaml"
)
func init() {
k8s.Register("dns.deinstapel.de", "v1", "domains", false, &domainik.Domain{})
k8s.Register("dns.deinstapel.de", "v1", "domainmanagers", false, &domainik.DomainManager{})
}
func watchChanges(
ctx context.Context,
kubernetesClient *k8s.Client,
resourceFactory func() k8s.Resource,
processEntry func(eventType string, resource k8s.Resource),
) error {
// Watch configmaps in the "kube-system" namespace
for {
node := resourceFactory()
watcher, err := kubernetesClient.Watch(ctx, k8s.AllNamespaces, node)
if err != nil {
return err
}
for {
entity := resourceFactory()
eventType, err := watcher.Next(entity)
if err != nil && err == context.Canceled {
watcher.Close()
return nil
} else if err != nil {
watcher.Close()
break
}
processEntry(eventType, entity)
}
}
}
func watchNodeChanges(ctx context.Context, kubernetesClient *k8s.Client, watchCombinator *domainik.EventHandler) error {
return watchChanges(
ctx,
kubernetesClient,
func() k8s.Resource {
var node corev1.Node
return &node
},
func(eventType string, resource k8s.Resource) {
node := resource.(*corev1.Node)
watchCombinator.ProcessNode(eventType, node)
},
)
}
func watchDomainChanges(ctx context.Context, kubernetesClient *k8s.Client, watchCombinator *domainik.EventHandler) error {
return watchChanges(
ctx,
kubernetesClient,
func() k8s.Resource {
var domain domainik.Domain
return &domain
},
func(eventType string, resource k8s.Resource) {
domain := resource.(*domainik.Domain)
watchCombinator.ProcessDomain(eventType, domain)
},
)
}
func watchDomainManagerChanges(ctx context.Context, kubernetesClient *k8s.Client, eventHandler *domainik.EventHandler) error {
return watchChanges(
ctx,
kubernetesClient,
func() k8s.Resource {
var domainManager domainik.DomainManager
return &domainManager
},
func(eventType string, resource k8s.Resource) {
domainManager := resource.(*domainik.DomainManager)
eventHandler.ProcessDomainManager(eventType, domainManager)
},
)
}
func main() {
logrus.SetFormatter(&logrus.TextFormatter{})
logger := logrus.WithField("app", "domainik")
client, err := makeClient()
if err != nil {
logger.WithError(err).Error("Failed to create kubernetes client")
os.Exit(1)
}
domainManagers := make([]domainmanager.DomainManager, 0)
route53DomainManager := domainmanager.CreateRoute53RouteManager("", "")
domainManagers = append(domainManagers, route53DomainManager)
watchCombinator := domainik.CreateEventHandler(logger, client)
watchContext := context.Background()
watchContext, cancelWatchContext := context.WithCancel(watchContext)
reconcileContext := context.Background()
reconcileContext, cancelReconcileContext := context.WithCancel(watchContext)
stopApplication := func() {
cancelWatchContext()
cancelReconcileContext()
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
err := watchDomainChanges(watchContext, client, watchCombinator)
if err != nil {
logger.WithError(err).Error("Failed to watch Domain changes")
stopApplication()
}
wg.Done()
}()
wg.Add(1)
go func() {
err := watchNodeChanges(watchContext, client, watchCombinator)
if err != nil {
logger.WithError(err).Error("Failed to watch Node changes")
stopApplication()
}
wg.Done()
}()
wg.Add(1)
go func() {
err := watchDomainManagerChanges(watchContext, client, watchCombinator)
if err != nil {
logger.WithError(err).Error("Failed to watch DomainManager changes")
stopApplication()
}
wg.Done()
}()
wg.Add(1)
go func() {
watchCombinator.StartReconcile(reconcileContext)
wg.Done()
}()
sigCh := make(chan os.Signal)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigCh
logger.WithField("signal", sig).Info("Received Signal")
stopApplication()
wg.Wait()
}
func makeKubeconfigClient(path string) (*k8s.Client, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
config := new(k8s.Config)
if err := yaml.Unmarshal(data, config); err != nil {
return nil, err
}
client, err := k8s.NewClient(config)
if err != nil {
return nil, err
}
return client, nil
}
func makeClient() (*k8s.Client, error) {
if kubeconfig := os.Getenv("KUBECONFIG"); kubeconfig != "" {
return makeKubeconfigClient(kubeconfig)
}
return k8s.NewInClusterClient()
}