-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (72 loc) · 2.44 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
package main
import (
"flag"
"fmt"
coreV1 "k8s.io/api/core/v1"
"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/util/workqueue"
)
type LogEvent struct {
ResourceName string
Action string
}
func main() {
// parse command line flags
podFilter := flag.String("f", "", "limit logging to pods that match this filter")
flag.Parse()
options := controllerOptions{
PodFilter: *podFilter,
}
fmt.Println("podFilter", *podFilter)
// create a k8s client
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
// create informer that will be used to watch for pods changes
factory := informers.NewSharedInformerFactory(clientset, 0)
podsInformer := factory.Core().V1().Pods()
informer := podsInformer.Informer()
// the pods we care about will be pushed here
podQueue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
// on changes to pods, perform actions
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
updateHandler(obj, "ADD", cache.MetaNamespaceKeyFunc, podQueue, options)
},
UpdateFunc: func(oldObj, newObj interface{}) {
updateHandler(newObj, "UPDATE", cache.MetaNamespaceKeyFunc, podQueue, options)
},
DeleteFunc: func(obj interface{}) {
updateHandler(obj, "DELETE", cache.DeletionHandlingMetaNamespaceKeyFunc, podQueue, options)
},
})
// start the controller
stopCh := make(chan struct{})
defer close(stopCh)
controller := NewController(podQueue, informer, options)
controller.Run(stopCh)
}
// process the change event for pod. queue the name if it has required label.
type namespaceFuncType = func(interface{}) (string, error)
func updateHandler(obj interface{}, action string, namespaceFunc namespaceFuncType, queue workqueue.RateLimitingInterface, options controllerOptions) {
namespacedName, _ := namespaceFunc(obj)
pod := obj.(*coreV1.Pod)
labelValue := pod.Labels["k8s-pod-logger"]
if (options.PodFilter == "") || (options.PodFilter == labelValue) {
fmt.Printf("[QUEUE] %q. Pod has the required label value. Adding to work queue.\n", namespacedName)
queue.Add(LogEvent{
ResourceName: namespacedName,
Action: action,
})
} else {
fmt.Printf("[SKIP] %q. Pod does not have the required label value. Skipping.\n", namespacedName)
}
}