-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
432 lines (358 loc) · 12.8 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"time"
nadclient "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/clientset/versioned"
nadinformers "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/informers/externalversions"
nadlister "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/client/listers/k8s.cni.cncf.io/v1"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"k8s.io/apimachinery/pkg/util/wait"
k8sinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
wbclient "github.com/k8snetworkplumbingwg/whereabouts/pkg/client/clientset/versioned"
wbinformers "github.com/k8snetworkplumbingwg/whereabouts/pkg/client/informers/externalversions"
wblister "github.com/k8snetworkplumbingwg/whereabouts/pkg/client/listers/whereabouts.cni.cncf.io/v1alpha1"
corev1 "k8s.io/api/core/v1"
v1corelisters "k8s.io/client-go/listers/core/v1"
)
const (
MULTUS_NET_ANNOTATION = "k8s.v1.cni.cncf.io/networks"
MAX_RETRIES = 2
)
type WhereaboutsController struct {
k8sclient kubernetes.Interface
arePodsSynced cache.InformerSynced
areIPPoolsSynced cache.InformerSynced
areNetAttachDefsSynced cache.InformerSynced
podsInformer cache.SharedIndexInformer
ipPoolInformer cache.SharedIndexInformer
netAttachDefInformer cache.SharedIndexInformer
podLister v1corelisters.PodLister
ipPoolLister wblister.IPPoolLister
netAttachDefLister nadlister.NetworkAttachmentDefinitionLister
broadcaster record.EventBroadcaster
recorder record.EventRecorder
addQueue workqueue.RateLimitingInterface
delQueue workqueue.RateLimitingInterface
}
type PodInfo struct {
operand string
podRef string
networks []string
}
func NewWhereaboutsController(
clientset kubernetes.Interface,
k8sCoreInformerFactory k8sinformers.SharedInformerFactory,
wbSharedInformerFactory wbinformers.SharedInformerFactory,
netAttachDefInformerFactory nadinformers.SharedInformerFactory) *WhereaboutsController {
k8sPodFilteredInformer := k8sCoreInformerFactory.Core().V1().Pods()
ipPoolInformer := wbSharedInformerFactory.Whereabouts().V1alpha1().IPPools()
netAttachDefInformer := netAttachDefInformerFactory.K8sCniCncfIo().V1().NetworkAttachmentDefinitions()
poolInformer := ipPoolInformer.Informer()
networksInformer := netAttachDefInformer.Informer()
podsInformer := k8sPodFilteredInformer.Informer()
addQueue := workqueue.NewNamedRateLimitingQueue(
workqueue.DefaultControllerRateLimiter(),
"whereabouts-add-queue")
delQueue := workqueue.NewNamedRateLimitingQueue(
workqueue.DefaultControllerRateLimiter(),
"whereabouts-del-queue")
wc := &WhereaboutsController{
k8sclient: clientset,
arePodsSynced: podsInformer.HasSynced,
areIPPoolsSynced: poolInformer.HasSynced,
areNetAttachDefsSynced: networksInformer.HasSynced,
podsInformer: podsInformer,
ipPoolInformer: poolInformer,
netAttachDefInformer: networksInformer,
addQueue: addQueue,
delQueue: delQueue,
}
podsInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: wc.eventAddPod,
DeleteFunc: wc.eventDeletePod,
})
/*
ipPoolInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.NewPool,
})
networksInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.NewNetwork,
})
*/
return nil
}
func (wc *WhereaboutsController) Run(stopCh <-chan struct{}) error {
defer wc.addQueue.ShutDown()
defer wc.delQueue.ShutDown()
zap.S().Infof("Waiting for informer caches to sync")
if ok := cache.WaitForCacheSync(stopCh, wc.arePodsSynced, wc.areNetAttachDefsSynced, wc.areIPPoolsSynced); !ok {
return fmt.Errorf("failed to wait for caches to sync")
}
zap.S().Infof("Starting workers")
go wait.Until(wc.runAddPodWorker, time.Second, stopCh)
go wait.Until(wc.runDelPodWorker, time.Second, stopCh)
zap.S().Infof("Started workers")
<-stopCh
zap.S().Infof("Shutting down workers")
return nil
}
func (wc *WhereaboutsController) runAddPodWorker() {
for wc.processNextAddPod() {
}
}
func (wc *WhereaboutsController) processNextAddPod() bool {
queueItem, shouldQuit := wc.addQueue.Get()
if shouldQuit {
return false
}
defer wc.addQueue.Done(queueItem)
podInfo, ok := queueItem.(*PodInfo)
if !ok {
wc.addQueue.Forget(queueItem)
zap.S().Errorf("Expected PodInfo in queue but got %+v", *podInfo)
return true
}
err := wc.AllocateIpForPod(podInfo)
wc.handleResultAndRequeue(podInfo, wc.addQueue, err)
return true
}
func (wc *WhereaboutsController) runDelPodWorker() {
for wc.processNextDelPod() {
}
}
func (wc *WhereaboutsController) processNextDelPod() bool {
queueItem, shouldQuit := wc.delQueue.Get()
if shouldQuit {
return false
}
defer wc.delQueue.Done(queueItem)
podInfo, ok := queueItem.(*PodInfo)
if !ok {
wc.addQueue.Forget(queueItem)
zap.S().Errorf("Expected PodInfo in queue but got %+v", *podInfo)
return true
}
err := wc.DeleteIpForPod(podInfo)
wc.handleResultAndRequeue(podInfo, wc.delQueue, err)
return true
}
func (wc *WhereaboutsController) handleResultAndRequeue(podInfo *PodInfo, queue workqueue.RateLimitingInterface, err error) {
if err == nil {
queue.Forget(podInfo)
return
}
currentRetries := queue.NumRequeues(podInfo)
if currentRetries <= MAX_RETRIES {
zap.S().Warnf(
"re-queuing request for pod %+v; retry #: %d",
*podInfo,
currentRetries)
queue.AddRateLimited(podInfo)
return
}
queue.Forget(podInfo)
}
func (wc *WhereaboutsController) AllocateIpForPod(podInfo *PodInfo) error {
fmt.Printf("Allocated IP for pod %s", podInfo.podRef)
return nil
}
func (wc *WhereaboutsController) DeleteIpForPod(podInfo *PodInfo) error {
fmt.Printf("Allocated IP for pod %s", podInfo.podRef)
return nil
}
func (wc *WhereaboutsController) eventAddPod(obj interface{}) {
pod, isPod := obj.(*corev1.Pod)
if !isPod {
zap.S().Errorf("Received non-Pod object: %+v", obj)
}
zap.S().Infof("Got new pod %s\n", pod.ObjectMeta.Name)
networks, isMultus := GetPodMultusNetworks(pod)
if !isMultus {
zap.S().Infof("Pod %s has no multus network annotations, skipping", pod.GetName())
return
}
zap.S().Infof("Has annotations %v", networks)
podInfo := new(PodInfo)
podInfo.networks = networks
podInfo.podRef = composePodRef(pod)
podInfo.operand = "ADD"
wc.addQueue.Add(podInfo)
}
func (wc *WhereaboutsController) eventDeletePod(obj interface{}) {
pod, isPod := obj.(*corev1.Pod)
if !isPod {
zap.S().Errorf("Received non-Pod object: %+v", obj)
}
zap.S().Infof("Got new pod %s\n", pod.ObjectMeta.Name)
networks, isMultus := GetPodMultusNetworks(pod)
if !isMultus {
zap.S().Infof("Pod %s has no multus network annotations, skipping", pod.GetName())
return
}
zap.S().Infof("Has annotations %v", networks)
podInfo := new(PodInfo)
podInfo.networks = networks
podInfo.podRef = composePodRef(pod)
podInfo.operand = "DEL"
wc.delQueue.Add(podInfo)
}
func GetPodMultusNetworks(pod *corev1.Pod) ([]string, bool) {
annotations := pod.ObjectMeta.Annotations
if networks, ok := annotations[MULTUS_NET_ANNOTATION]; ok {
netList := strings.Split(networks, ",")
return netList, true
}
return nil, false
}
func composePodRef(pod *corev1.Pod) string {
return fmt.Sprintf("%s/%s", pod.GetNamespace(), pod.GetName())
}
func main() {
kubeconfig := flag.String("kubeconfig", filepath.Join(os.Getenv("HOME"), ".kube", "config"), "absolute path to the kubeconfig file")
verbose := flag.Bool("verbose", false, "print verbose logs to the console")
flag.Parse()
// Initializing zap log with console and file logging support
if err := configureGlobalLog(*verbose, filepath.Join("/var/log/", "whereaboutsController.log")); err != nil {
fmt.Printf("log initialization failed: %s", err.Error())
os.Exit(1)
}
stopChan := make(chan struct{})
defer close(stopChan)
k8sClientset, err := getClient(*kubeconfig)
if err != nil {
zap.S().Fatalf("Failed to get k8s clientset: %s", err)
}
nadClientset, err := getNADClient(*kubeconfig)
if err != nil {
zap.S().Fatalf("Failed to get k8s clientset: %s", err)
}
wbClientset, err := getWhereaboutsClient(*kubeconfig)
if err != nil {
zap.S().Fatalf("Failed to get k8s clientset: %s", err)
}
podInformerFactory := k8sinformers.NewSharedInformerFactory(k8sClientset, 0)
ipPoolInformerFactory := wbinformers.NewSharedInformerFactory(wbClientset, 0)
netAttachDefInformerFactory := nadinformers.NewSharedInformerFactory(nadClientset, 0)
controller := NewWhereaboutsController(k8sClientset, podInformerFactory, ipPoolInformerFactory, netAttachDefInformerFactory)
zap.S().Infof("Starting informer factories ...")
podInformerFactory.Start(stopChan)
netAttachDefInformerFactory.Start(stopChan)
ipPoolInformerFactory.Start(stopChan)
zap.S().Infof("Informer factories started")
zap.S().Infof("Starting the controller")
if err = controller.Run(stopChan); err != nil {
zap.S().Fatalf("error running controller: %s", err)
}
}
func getClient(kubeconfig string) (*kubernetes.Clientset, error) {
var kubeClient *kubernetes.Clientset
if inClusterConfig, err := rest.InClusterConfig(); err != nil {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, fmt.Errorf("unable to build config from kubeconfig file: %s", err)
}
kubeClient, err = kubernetes.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("unable to build clientset from config: %s", err)
}
} else {
kubeClient, err = kubernetes.NewForConfig(inClusterConfig)
if err != nil {
return nil, fmt.Errorf("unable to build clientset from in cluster config: %s", err)
}
}
return kubeClient, nil
}
func getNADClient(kubeconfig string) (*nadclient.Clientset, error) {
var kubeClient *nadclient.Clientset
if inClusterConfig, err := rest.InClusterConfig(); err != nil {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, fmt.Errorf("unable to build config from kubeconfig file: %s", err)
}
kubeClient, err = nadclient.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("unable to build clientset from config: %s", err)
}
} else {
kubeClient, err = nadclient.NewForConfig(inClusterConfig)
if err != nil {
return nil, fmt.Errorf("unable to build clientset from in cluster config: %s", err)
}
}
return kubeClient, nil
}
func getWhereaboutsClient(kubeconfig string) (*wbclient.Clientset, error) {
var kubeClient *wbclient.Clientset
if inClusterConfig, err := rest.InClusterConfig(); err != nil {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return nil, fmt.Errorf("unable to build config from kubeconfig file: %s", err)
}
kubeClient, err = wbclient.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("unable to build clientset from config: %s", err)
}
} else {
kubeClient, err = wbclient.NewForConfig(inClusterConfig)
if err != nil {
return nil, fmt.Errorf("unable to build clientset from in cluster config: %s", err)
}
}
return kubeClient, nil
}
// ConfigureGlobalLog will log debug to console, else would put logs
// in the home directory
func configureGlobalLog(debugConsole bool, logFile string) error {
// use lumberjack for log rotation
f := zapcore.AddSync(&lumberjack.Logger{
Filename: logFile,
MaxSize: 100, // mb
MaxBackups: 5,
MaxAge: 30,
Compress: true,
})
// This seems pretty complicated, but all we are doing is making sure
// error level is reported on console and console looks more 'production' like
// whereas our log file is more like development log with stack traces etc.
devEncoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
prodEncoder := zapcore.NewConsoleEncoder(getProdEncoderConfig())
// consoleLog will go to stderr
consoleLogs := zapcore.Lock(os.Stderr)
// all the logs to file
fileLogs := zapcore.Lock(f)
// by default on console we will only print panic error, unless debugConsole is specified
consoleLvl := zap.PanicLevel
if debugConsole {
consoleLvl = zap.DebugLevel
}
core := zapcore.NewTee(
zapcore.NewCore(prodEncoder, consoleLogs, consoleLvl),
zapcore.NewCore(devEncoder, fileLogs, zap.DebugLevel),
)
logger := zap.New(core)
defer logger.Sync()
// use the logger we created globally
zap.ReplaceGlobals(logger)
// Now start the logging business
zap.S().Debug("Logger started")
return nil
}
func getProdEncoderConfig() zapcore.EncoderConfig {
prodcfg := zap.NewProductionEncoderConfig()
// by default production encoder has epoch time, using something more readable
prodcfg.EncodeTime = zapcore.ISO8601TimeEncoder
return prodcfg
}