forked from concur/kubegowatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.go
267 lines (222 loc) · 7.47 KB
/
package.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
/*
Copyright 2016 Concur Technologies, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"time"
"log"
api "k8s.io/client-go/pkg/api/v1"
//"k8s.io/apimachinery/pkg/apis/meta/v1"
"encoding/json"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
// Client can be used to hold state for business logic as needed.
type Client struct {
//kubeClient to use inside functions
kubeClient *kubernetes.Clientset
}
func (c *Client) newService(obj interface{}) {
var s = api.Service{}
b, err := json.Marshal(obj)
if err = json.Unmarshal(b, &s); err == nil {
log.Printf("New Service: %+v", s)
// potentially useful if you delay or check for existing ExternalIPs
// //check delay since event occurred
// var k8sTimeFmt = "2006-01-02 15:04:05 -0700 MST"
// t, _ := time.Parse(k8sTimeFmt, s.ObjectMeta.CreationTimestamp.String())
// delay := time.Now().Sub(t).Seconds()
// if delay > 60 && s.Spec.ExternalIPs != nil {
// //bail no need to do an update
// log.Printf("no need to check this service: %v delay: %v ExternalIPs: %v", s.ObjectMeta.Name, delay, s.Spec.ExternalIPs)
// return
// } else {
// log.Printf("Must check this service: %v delay: %v ExternalIPs: %v", s.ObjectMeta.Name, delay, s.Spec.ExternalIPs)
// }
//svcClient is used to make updates to the service
svcClient := c.kubeClient.Core().Services(s.Namespace)
if s.ObjectMeta.Annotations == nil {
s.ObjectMeta.Annotations = make(map[string]string)
}
if s.ObjectMeta.Annotations["Example"] == "" {
s.ObjectMeta.Annotations["Example"] = "myInterestingAnnotation"
log.Printf("Setting Annotation 'Example': %v", s.ObjectMeta.Name)
if _, err := svcClient.Update(&s); err != nil {
log.Printf("error updating service fields: %v", err)
}
} else {
log.Printf("Annotation 'Example' already exists: %v Value: %v", s.ObjectMeta.Name, s.ObjectMeta.Annotations["Example"])
}
}
}
func newNode(obj interface{}) {
var s = api.Node{}
b, err := json.Marshal(obj)
if err = json.Unmarshal(b, &s); err == nil {
log.Printf("Node Added: %+v", s)
//skip this node if it's not scheduleable
if s.Spec.Unschedulable {return}
// add code here to handle new nodes
// s.ObjectMeta.Name
}
}
func removeNode(obj interface{}) {
var s = api.Node{}
b, err := json.Marshal(obj)
if err = json.Unmarshal(b, &s); err == nil {
log.Printf("Node Removed: %+v", s)
//add code here to handle removed nodes
//s.ObjectMeta.Name
}
}
func newPod(obj interface{}) {
var s = api.Pod{}
b, err := json.Marshal(obj)
if err = json.Unmarshal(b, &s); err == nil {
log.Printf("Pod Added: %+v", s)
// add code here to handle new nodes
// s.ObjectMeta.Name
}
}
func removePod(obj interface{}) {
var s = api.Pod{}
b, err := json.Marshal(obj)
if err = json.Unmarshal(b, &s); err == nil {
log.Printf("Pod Removed: %+v", s)
//add code here to handle removed nodes
//s.ObjectMeta.Name
}
}
func removeService(obj interface{}) {
var s = api.Service{}
b, err := json.Marshal(obj)
if err = json.Unmarshal(b, &s); err == nil {
log.Printf("Service Removed: %+v", s)
//add business logic here for service removed
}
}
func updateService(obj interface{}) {
var s = api.Service{}
b, err := json.Marshal(obj)
if err = json.Unmarshal(b, &s); err == nil {
log.Printf("Service Updated: %+v", s)
}
//add business logic here for service updated
// var k8sTimeFmt = "2006-01-02 15:04:05 -0700 MST"
// t, _ := time.Parse(k8sTimeFmt, sNew.ObjectMeta.CreationTimestamp.String())
// delay := time.Now().Sub(t).Seconds()
// if delay < 60 {
// p.newService(obj)
// }
}
func newKubeClient() (*kubernetes.Clientset, error) {
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
log.Printf("Using %s for kubernetes master", config.Host)
if config.Host == "https://172.16.123.1:8001" {
config.Host = "http://172.16.123.1:8001"
}
// creates the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
return clientset, err
}
func (c *Client) watchForServices(timeout int64) error {
watchServicesInterface, err := c.kubeClient.Core().Services("").Watch(api.ListOptions{Watch: true, TimeoutSeconds: &timeout})
if err != nil {
log.Printf("Error retrieving watch interface for services: %+v", err)
panic(err.Error())
}
events := watchServicesInterface.ResultChan()
for {
event, ok := <-events
log.Printf("Service Event %v: %+v", event.Type, event.Object)
if event.Type == "ADDED" {
c.newService(event.Object)
} else if event.Type == "MODIFIED" {
updateService(event.Object)
} else if event.Type == "DELETED" {
removeService(event.Object)
} else if event.Type == "" {
log.Printf("Service watch timed out: %v seconds", timeout)
} else {
log.Printf("Service watch unhandled event: %v", event.Type)
}
if !ok { break }
}
return err
}
func (c *Client) watchForNodes(timeout int64) {
watchNodesInterface, err := c.kubeClient.Core().Nodes().Watch(api.ListOptions{Watch: true, TimeoutSeconds: &timeout})
if err != nil {
log.Printf("Error retrieving watch interface for nodes: %+v", err)
panic(err.Error())
}
events := watchNodesInterface.ResultChan()
for {
event, ok := <-events
// log.Printf("Node Event %v: %+v", event.Type, event.Object)
log.Printf("Node Event Type %v", event.Type)
if event.Type == "ADDED" {
newNode(event.Object)
} else if event.Type == "DELETED" {
removeNode(event.Object)
} else if event.Type == "" {
log.Printf("Node watch timed out: %v seconds", timeout)
}
if !ok { break }
}
return
}
func (c *Client) watchForPods(timeout int64) {
watchInterface, err := c.kubeClient.Core().Pods("").Watch(api.ListOptions{Watch: true, TimeoutSeconds: &timeout})
if err != nil {
log.Printf("Error retrieving watch interface for pods: %+v", err)
panic(err.Error())
}
events := watchInterface.ResultChan()
for {
event, ok := <-events
log.Printf("Pod Event %v: %+v", event.Type, event.Object)
if event.Type == "ADDED" {
//newPod(event.Object)
} else if event.Type == "DELETED" {
//removePod(event.Object)
} else if event.Type == "" {
log.Printf("Pod watch timed out: %v seconds", timeout)
}
if !ok { break }
}
return
}
func main() {
watchTimeout := int64(1800) //30 minute resync
k8sClient, err := newKubeClient()
if err != nil {
log.Printf("Failed to create a kubernetes client: %+v", err)
} else {
log.Printf("k8s client created.")
}
//add the client
c := Client{kubeClient: k8sClient}
for {
go c.watchForNodes(watchTimeout)
go c.watchForServices(watchTimeout)
go c.watchForPods(watchTimeout)
time.Sleep(time.Second * time.Duration(watchTimeout) + time.Second * 2)
log.Printf("Restarting watchers from %v second timeout.", watchTimeout + 2)
}
}