-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.go
426 lines (394 loc) · 13.5 KB
/
controller.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
package main
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
coreinformerv1 "k8s.io/client-go/informers/core/v1"
networkingv1informerv1 "k8s.io/client-go/informers/networking/v1"
"k8s.io/client-go/kubernetes"
corelisterv1 "k8s.io/client-go/listers/core/v1"
networkinglisterv1 "k8s.io/client-go/listers/networking/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog"
"strings"
"time"
)
const (
driverServiceSuffix = "-driver-svc"
ingressSuffix = "-ingress"
sparkUIIngressPath = "/" // spark ui ingress backend path
sparkUIIngressClass = "nginx" // spark ui ingress class
sparkUIIngressPathType networkingv1.PathType = networkingv1.PathTypePrefix
)
type Controller struct {
// kubeclientset is a standard kubernetes clientset
kubeclientset kubernetes.Interface
servicesSynced cache.InformerSynced
servicesLister corelisterv1.ServiceLister
ingressSynced cache.InformerSynced
ingressLister networkinglisterv1.IngressLister
workqueue workqueue.RateLimitingInterface
hostsuffix string
requestTimeout string
}
// NewController returns a new sample controller
func NewController(
hostsuffix string,
requestTimeout string,
kubeclientset kubernetes.Interface,
servicesInformer coreinformerv1.ServiceInformer,
ingressInformer networkingv1informerv1.IngressInformer) *Controller {
queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
servicesInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
klog.Infof("Add service: %s", key)
if err == nil {
queue.Add(key)
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(newObj)
klog.Infof("Update service: %s", key)
if err == nil {
queue.Add(key)
}
},
DeleteFunc: func(obj interface{}) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
klog.Infof("Delete service: %s", key)
if err == nil {
queue.Add(key)
}
},
})
ingressInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(obj)
klog.Infof("Add ingress: %s", key)
if err == nil {
queue.Add(key)
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(newObj)
klog.Infof("Update ingress: %s", key)
if err == nil {
queue.Add(key)
}
},
DeleteFunc: func(obj interface{}) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
klog.Infof("Delete ingress: %s", key)
if err == nil {
queue.Add(key)
}
},
})
controller := &Controller{
kubeclientset: kubeclientset,
servicesSynced: servicesInformer.Informer().HasSynced,
servicesLister: servicesInformer.Lister(),
ingressSynced: ingressInformer.Informer().HasSynced,
ingressLister: ingressInformer.Lister(),
workqueue: queue,
hostsuffix: hostsuffix,
requestTimeout: requestTimeout,
}
return controller
}
// Run is the main path of execution for the controller loop
func (c *Controller) Run(threadiness int, stopCh <-chan struct{}) error {
defer runtime.HandleCrash()
defer c.workqueue.ShutDown()
// do the initial synchronization (one time) to populate resources
if ok := cache.WaitForCacheSync(stopCh, c.HasSynced); !ok {
return fmt.Errorf("Error syncing cache")
}
klog.Info("Starting workers")
for i := 0; i < threadiness; i++ {
go wait.Until(c.runWorker, time.Second, stopCh)
}
klog.Info("Started workers")
<-stopCh
klog.Info("Shutting down workers")
return nil
}
func (c *Controller) HasSynced() bool {
return c.servicesSynced() && c.ingressSynced()
}
// runWorker is a long-running function that will continually call the
// processNextWorkItem function in order to read and process a message on the workqueue.
func (c *Controller) runWorker() {
for c.processNextWorkItem() {
}
}
func (c *Controller) processNextWorkItem() bool {
obj, shutdown := c.workqueue.Get()
if shutdown {
return false
}
// We wrap this block in a func so we can defer c.workqueue.Done.
err := func(obj interface{}) error {
// We call Done here so the workqueue knows we have finished
// processing this item. We also must remember to call Forget if we
// do not want this work item being re-queued. For example, we do
// not call Forget if a transient error occurs, instead the item is
// put back on the workqueue and attempted again after a back-off
// period.
defer c.workqueue.Done(obj)
var key string
var ok bool
// We expect strings to come off the workqueue. These are of the
// form namespace/name. We do this as the delayed nature of the
// workqueue means the items in the informer cache may actually be
// more up to date that when the item was initially put onto the
// workqueue.
if key, ok = obj.(string); !ok {
// As the item in the workqueue is actually invalid, we call
// Forget here else we'd go into a loop of attempting to
// process a work item that is invalid.
c.workqueue.Forget(obj)
runtime.HandleError(fmt.Errorf("expected string in workqueue but got %#v", obj))
return nil
}
// Run the syncHandler, passing it the namespace/name string of the Service
// resource to be synced.
if err := c.syncHandler(key); err != nil {
// Put the item back on the workqueue to handle any transient errors.
c.workqueue.AddRateLimited(key)
return fmt.Errorf("error syncing '%s': %s, requeuing", key, err.Error())
}
// Finally, if no error occurs we Forget this item so it does not
// get queued again until another change happens.
c.workqueue.Forget(obj)
klog.Infof("Successfully synced '%s'", key)
return nil
}(obj)
if err != nil {
runtime.HandleError(err)
return true
}
return true
}
// syncHandler compares the actual state with the desired, and attempts to
// converge the two. It then updates the Status block of the ? resource
// with the current status of the resource.
func (c *Controller) syncHandler(key string) error {
namespace, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
runtime.HandleError(fmt.Errorf("invalid resource key: %s", key))
return nil
}
// spark driver svc should end with driverServiceSuffix
if !strings.HasSuffix(name, driverServiceSuffix) {
klog.Infof("Get service: %s, not end with %s, ignoring it", key, driverServiceSuffix)
return nil
}
// Get the service resource with this namespace/name
service, err := c.servicesLister.Services(namespace).Get(name)
if err != nil {
if errors.IsNotFound(err) {
runtime.HandleError(fmt.Errorf("service '%s' in work queue no longer exists", key))
// this should a spark driver service deleted event.
// service not found depend on owner reference to garbage collect ui service and ingress
return nil
}
return err
}
// spark driver svc should has a selector spark-role: driver
if service.Spec.Selector["spark-role"] != "driver" {
klog.Infof("Get service: %s, has not a selector spark-role: driver, ignoring it", service)
return nil
}
// check and create spark UI ingress
err = c.createSparkUIIngressFromService(service)
if err != nil {
return err
}
return nil
}
func isSparkUIServices(serviceName string) bool {
//TODO: 根据label或者annotation二次验证service是否属于spark driver
if strings.HasSuffix(serviceName, driverServiceSuffix) {
klog.Infof("is spark driver service")
return true
} else {
klog.Infof("is not spark driver service")
return false
}
}
func getPodNameFromService(service *corev1.Service) string {
// spark on k8s driver的service的唯一owner为其podName
podName := service.OwnerReferences[0].Name
return podName
}
func getIngressNameFromService(service *corev1.Service) string {
ingressName := service.Name + ingressSuffix
return ingressName
}
func getSparkDriverUIPort(service *corev1.Service) int32 {
//- name: spark-ui
// port: 4040
// protocol: TCP
// targetPort: 4040
for _, port := range service.Spec.Ports {
if port.Name == "spark-ui" {
sparkDriverUIPort := port.Port
return sparkDriverUIPort
}
}
return 4040
}
func (c *Controller) getHostOfIngressFromService(service *corev1.Service) string {
//// 注意:host域名解析需要配置成泛解析,每级域名不超过63字符,域名总长不超过253字符
//// host子域名根据spark driver pod name命名,即driverPodName + hostSuffix
//podName := getPodNameFromService(service)
//// host子域名截取driverPodName后23位,即"16位ID" + "-driver",解决域名字符串长度限制问题
//podNameSuffix := string([]byte(podName)[len(podName)-23:])
return service.Name + c.hostsuffix
}
func (c *Controller) NewSparkUIIngress(sparkUIService *corev1.Service) *networkingv1.Ingress {
// 定义Ingress对象
sparkUIIC := sparkUIIngressClass
sparkUIIngressPT := sparkUIIngressPathType
ingress := &networkingv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: getIngressNameFromService(sparkUIService),
Namespace: sparkUIService.Namespace,
OwnerReferences: sparkUIService.OwnerReferences,
},
Spec: networkingv1.IngressSpec{
IngressClassName: &sparkUIIC,
Rules: []networkingv1.IngressRule{
{
Host: c.getHostOfIngressFromService(sparkUIService),
IngressRuleValue: networkingv1.IngressRuleValue{
HTTP: &networkingv1.HTTPIngressRuleValue{
Paths: []networkingv1.HTTPIngressPath{
{
PathType: &sparkUIIngressPT,
Path: sparkUIIngressPath,
Backend: networkingv1.IngressBackend{
Service: &networkingv1.IngressServiceBackend{
Name: sparkUIService.Name,
Port: networkingv1.ServiceBackendPort{
Number: getSparkDriverUIPort(sparkUIService),
},
},
},
},
},
},
},
},
},
},
}
return ingress
}
func (c *Controller) CreateSparkUIIngress(sparkUIService *corev1.Service) (*networkingv1.Ingress, error) {
// 定义Ingress对象
ingress := c.NewSparkUIIngress(sparkUIService)
// 创建或更新Ingress对象
ctx := context.TODO()
_, err := c.kubeclientset.NetworkingV1().Ingresses(sparkUIService.Namespace).Create(ctx, ingress, metav1.CreateOptions{})
if err != nil {
_, err = c.kubeclientset.NetworkingV1().Ingresses(sparkUIService.Namespace).Update(ctx, ingress, metav1.UpdateOptions{})
if err != nil {
//panic(err.Error())
return nil, err
}
klog.Infof(">>>>>>Ingress %s updated successfully", ingress.Name)
}
klog.Infof(">>>>>>Ingress %s created successfully", ingress.Name)
return ingress, nil
}
// TODO: remove
func (c *Controller) DeleteSparkUIIngress(sparkUIIngressName, sparkUIIngressNS string) error {
// 删除Ingress对象
ctx := context.TODO()
err := c.kubeclientset.NetworkingV1().Ingresses(sparkUIIngressNS).Delete(ctx, sparkUIIngressName, metav1.DeleteOptions{})
if err != nil {
panic(err.Error())
}
klog.Infof("Ingress %s deleted successfully", sparkUIIngressName)
return nil
}
// TODO: remove
func (c *Controller) WatchServiceToOperateIngress(namespace string) error {
ctx := context.TODO()
// watch all namespace, when namespace=""
watcher, err := c.kubeclientset.CoreV1().Services(namespace).Watch(ctx, metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
for event := range watcher.ResultChan() {
service, ok := event.Object.(*corev1.Service)
if !ok {
continue
}
switch event.Type {
case watch.Added:
klog.Infof("Service added: %s\n", service.Name)
// 处理Service创建事件
// 判断是否为spark driver service
if isSparkUIServices(service.Name) {
ingress, err := c.CreateSparkUIIngress(service)
if err != nil {
return err
}
klog.Infof("spark driver pod service %s 关联的ingress %s 创建成功", service.Name, ingress.Name)
}
case watch.Modified:
klog.Infof("Service modified: %s\n", service.Name)
// 处理Service更新事件
case watch.Deleted:
klog.Infof("Service deleted: %s\n", service.Name)
// 处理Service删除事件
if isSparkUIServices(service.Name) {
ingressName := getIngressNameFromService(service)
err := c.DeleteSparkUIIngress(ingressName, service.Namespace)
if err != nil {
return err
}
klog.Infof("spark driver pod service %s 关联的ingress %s 删除成功", service.Name, ingressName)
}
}
}
return nil
}
// create spark ui ingress from driver svc, if ingress is not exist
func (c *Controller) createSparkUIIngressFromService(service *corev1.Service) error {
ingressName := getIngressNameFromService(service)
ingressNamespace := service.Namespace
klog.Infof("Operate ingressName:%s of ingressNamespace:%s ", ingressName, ingressNamespace)
ingress, err := c.ingressLister.Ingresses(ingressNamespace).Get(ingressName)
klog.Infof("Get ingress %s info:%s", ingressName, ingress)
if ingress == nil {
klog.Infof("Spark ui ingress: %s is not found, now create one ...", ingressName)
_, err = c.CreateSparkUIIngress(service)
if err != nil {
return err
}
}
if err != nil {
if errors.IsNotFound(err) {
klog.Infof("Spark ui ingress: %s is not found, now create one ...", ingressName)
_, err = c.CreateSparkUIIngress(service)
if err != nil {
return err
}
}
} else {
klog.Infof("Spark ui ingress: %s already exists", ingressName)
}
return nil
}