-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
215 lines (191 loc) · 4.85 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
package main
import (
"context"
"flag"
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"net/http"
"strings"
"time"
_ "k8s.io/client-go/plugin/pkg/client/auth"
_ "net/http/pprof"
kuberntescli "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
"github.com/elastic/elastic-agent-autodiscover/bus"
"github.com/elastic/elastic-agent-autodiscover/kubernetes"
)
type pod struct {
publishFunc func([]bus.Event)
watcher kubernetes.Watcher
client kuberntescli.Interface
}
func main() {
var kubeconfig string
flag.StringVar(&kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file")
flag.Parse()
// Server for pprof
go func() {
fmt.Println(http.ListenAndServe("localhost:6060", nil))
}()
// creates the connection
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
config, err = rest.InClusterConfig()
if err != nil {
klog.Errorf("could not create client %v", err)
return
}
}
// creates the client
client, err := kuberntescli.NewForConfig(config)
if err != nil {
klog.Errorf("could not create client %v", err)
return
}
watcher, err := kubernetes.NewNamedWatcher("pod", client, &kubernetes.Pod{}, kubernetes.WatchOptions{
SyncTimeout: 10 * time.Minute,
Node: "",
Namespace: "",
HonorReSyncs: true,
}, nil)
if err != nil {
klog.Errorf("could not create kubernetes watcher %v", err)
return
}
p := &pod{
watcher: watcher,
client: client,
}
watcher.AddEventHandler(p)
klog.Infof("start watching for pods")
go p.Start()
// Wait forever
select {}
}
// OnAdd ensures processing of pod objects that are newly added.
func (p *pod) OnAdd(obj interface{}) {
o := obj.(*kubernetes.Pod)
klog.Infof("Watcher Pod add: %+v", o.Name)
// Get metadata of the object
accessor, err := meta.Accessor(o)
if err != nil {
return
}
meta := map[string]string{}
for _, ref := range accessor.GetOwnerReferences() {
if ref.Controller != nil && *ref.Controller {
switch ref.Kind {
// grow this list as we keep adding more `state_*` metricsets
case "Deployment",
"ReplicaSet",
"StatefulSet",
"DaemonSet",
"Job",
"CronJob":
meta[strings.ToLower(ref.Kind)+".name"] = ref.Name
}
}
}
if jobName, ok := meta["job.name"]; ok {
dep := p.getCronjobOfJob(jobName, o.GetNamespace())
if dep != "" {
meta["cronjob.name"] = dep
}
klog.Infof("Watcher Pod of Job of Cronjob: %+v", meta)
}
}
// getCronjobOfJob return the name of the Cronjob object that
// owns the Job with the given name under the given Namespace
func (p *pod) getCronjobOfJob(jobName string, ns string) string {
if p.client == nil {
return ""
}
cronjob, err := p.client.BatchV1().Jobs(ns).Get(context.TODO(), jobName, metav1.GetOptions{})
if err != nil {
return ""
}
for _, ref := range cronjob.GetOwnerReferences() {
if ref.Controller != nil && *ref.Controller {
switch ref.Kind {
case "CronJob":
return ref.Name
}
}
}
return ""
}
// OnUpdate handles events for pods that have been updated.
func (p *pod) OnUpdate(obj interface{}) {
o := obj.(*kubernetes.Pod)
klog.Infof("Watcher Pod update: %+v", o.Name)
// Get metadata of the object
accessor, err := meta.Accessor(o)
if err != nil {
return
}
meta := map[string]string{}
for _, ref := range accessor.GetOwnerReferences() {
if ref.Controller != nil && *ref.Controller {
switch ref.Kind {
// grow this list as we keep adding more `state_*` metricsets
case "Deployment",
"ReplicaSet",
"StatefulSet",
"DaemonSet",
"Job",
"CronJob":
meta[strings.ToLower(ref.Kind)+".name"] = ref.Name
}
}
}
if jobName, ok := meta["job.name"]; ok {
dep := p.getCronjobOfJob(jobName, o.GetNamespace())
if dep != "" {
meta["cronjob.name"] = dep
}
klog.Infof("Watcher Pod of Job of Cronjob: %+v", meta)
}
}
// OnDelete stops pod objects that are deleted.
func (p *pod) OnDelete(obj interface{}) {
o := obj.(*kubernetes.Pod)
klog.Infof("Watcher Pod delete: %+v", o.Name)
// Get metadata of the object
accessor, err := meta.Accessor(o)
if err != nil {
return
}
meta := map[string]string{}
for _, ref := range accessor.GetOwnerReferences() {
if ref.Controller != nil && *ref.Controller {
switch ref.Kind {
// grow this list as we keep adding more `state_*` metricsets
case "Deployment",
"ReplicaSet",
"StatefulSet",
"DaemonSet",
"Job",
"CronJob":
meta[strings.ToLower(ref.Kind)+".name"] = ref.Name
}
}
}
if jobName, ok := meta["job.name"]; ok {
dep := p.getCronjobOfJob(jobName, o.GetNamespace())
if dep != "" {
meta["cronjob.name"] = dep
}
klog.Infof("Watcher Pod of Job of Cronjob: %+v", meta)
}
}
// Start starts the eventer
func (p *pod) Start() error {
return p.watcher.Start()
}
// Stop stops the eventer
func (p *pod) Stop() {
p.watcher.Stop()
}