/
deployment_app.go
96 lines (88 loc) · 2.86 KB
/
deployment_app.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
package operator
import (
"errors"
"reflect"
"github.com/appscode/go/log"
acrt "github.com/appscode/go/runtime"
"github.com/appscode/kubed/pkg/util"
kutil "github.com/appscode/kutil/apps/v1beta1"
apps "k8s.io/api/apps/v1beta1"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/cache"
)
// Blocks caller. Intended to be called as a Go routine.
func (op *Operator) WatchDeploymentApps() {
if !util.IsPreferredAPIResource(op.KubeClient, apps.SchemeGroupVersion.String(), "Deployment") {
log.Warningf("Skipping watching non-preferred GroupVersion:%s Kind:%s", apps.SchemeGroupVersion.String(), "Deployment")
return
}
defer acrt.HandleCrash()
lw := &cache.ListWatch{
ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) {
return op.KubeClient.AppsV1beta1().Deployments(apiv1.NamespaceAll).List(metav1.ListOptions{})
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return op.KubeClient.AppsV1beta1().Deployments(apiv1.NamespaceAll).Watch(metav1.ListOptions{})
},
}
_, ctrl := cache.NewInformer(lw,
&apps.Deployment{},
op.Opt.ResyncPeriod,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
if res, ok := obj.(*apps.Deployment); ok {
log.Infof("Deployment %s@%s added", res.Name, res.Namespace)
kutil.AssignTypeKind(res)
if op.Config.APIServer.EnableSearchIndex {
if err := op.SearchIndex.HandleAdd(obj); err != nil {
log.Errorln(err)
}
}
}
},
DeleteFunc: func(obj interface{}) {
if res, ok := obj.(*apps.Deployment); ok {
log.Infof("Deployment %s@%s deleted", res.Name, res.Namespace)
kutil.AssignTypeKind(res)
if op.Config.APIServer.EnableSearchIndex {
if err := op.SearchIndex.HandleDelete(obj); err != nil {
log.Errorln(err)
}
}
if op.TrashCan != nil {
op.TrashCan.Delete(res.TypeMeta, res.ObjectMeta, obj)
}
}
},
UpdateFunc: func(old, new interface{}) {
oldRes, ok := old.(*apps.Deployment)
if !ok {
log.Errorln(errors.New("Invalid Deployment object"))
return
}
newRes, ok := new.(*apps.Deployment)
if !ok {
log.Errorln(errors.New("Invalid Deployment object"))
return
}
kutil.AssignTypeKind(oldRes)
kutil.AssignTypeKind(newRes)
if op.Config.APIServer.EnableSearchIndex {
op.SearchIndex.HandleUpdate(old, new)
}
if op.TrashCan != nil && op.Config.RecycleBin.HandleUpdates {
if !reflect.DeepEqual(oldRes.Labels, newRes.Labels) ||
!reflect.DeepEqual(oldRes.Annotations, newRes.Annotations) ||
!reflect.DeepEqual(oldRes.Spec, newRes.Spec) {
op.TrashCan.Update(newRes.TypeMeta, newRes.ObjectMeta, old, new)
}
}
},
},
)
ctrl.Run(wait.NeverStop)
}