-
Notifications
You must be signed in to change notification settings - Fork 108
/
informer.go
193 lines (153 loc) · 5.17 KB
/
informer.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
// Copyright 2021 Flant JSC
//
// 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 client
import (
"context"
"time"
appsv1 "k8s.io/api/apps/v1"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
crdinformer "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
infappsv1 "k8s.io/client-go/informers/apps/v1"
"k8s.io/client-go/tools/cache"
)
type DeploymentInformer struct {
KubeClient *KubernetesClient
ctx context.Context
cancel context.CancelFunc
// Filter by namespace
Namespace string
// Filter by object name
Name string
// filter labels
LabelSelector *metav1.LabelSelector
// filter by fields
FieldSelector string
SharedInformer cache.SharedInformer
ListOptions metav1.ListOptions
EventCb func(obj *appsv1.Deployment, event string)
}
func NewDeploymentInformer(parentCtx context.Context, client *KubernetesClient) *DeploymentInformer {
ctx, cancel := context.WithCancel(parentCtx)
informer := &DeploymentInformer{
KubeClient: client,
ctx: ctx,
cancel: cancel,
}
return informer
}
func (p *DeploymentInformer) WithKubeEventCb(eventCb func(obj *appsv1.Deployment, event string)) {
p.EventCb = eventCb
}
func (p *DeploymentInformer) CreateSharedInformer() (err error) {
// define resyncPeriod for informer
resyncPeriod := time.Duration(2) * time.Hour
// define indexers for informer
indexers := cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}
// define tweakListOptions for informer
labelSelector, err := metav1.LabelSelectorAsSelector(p.LabelSelector)
if err != nil {
return err
}
tweakListOptions := func(options *metav1.ListOptions) {
if p.FieldSelector != "" {
options.FieldSelector = p.FieldSelector
}
if labelSelector.String() != "" {
options.LabelSelector = labelSelector.String()
}
}
// p.ListOptions = metav1.ListOptions{}
// tweakListOptions(&p.ListOptions)
// create informer with add, update, delete callbacks
informer := infappsv1.NewFilteredDeploymentInformer(p.KubeClient, p.Namespace, resyncPeriod, indexers, tweakListOptions)
informer.AddEventHandler(p)
p.SharedInformer = informer
return nil
}
func (p *DeploymentInformer) OnAdd(obj interface{}, _ bool) {
p.HandleWatchEvent(obj, "Added")
}
func (p *DeploymentInformer) OnUpdate(oldObj, newObj interface{}) {
p.HandleWatchEvent(newObj, "Modified")
}
func (p *DeploymentInformer) OnDelete(obj interface{}) {
p.HandleWatchEvent(obj, "Deleted")
}
func (p *DeploymentInformer) HandleWatchEvent(object interface{}, eventType string) {
if staleObj, stale := object.(cache.DeletedFinalStateUnknown); stale {
object = staleObj.Obj
}
obj := object.(*appsv1.Deployment)
p.EventCb(obj, eventType)
}
func (p *DeploymentInformer) Run() {
p.SharedInformer.Run(p.ctx.Done())
}
func (p *DeploymentInformer) Stop() {
p.cancel()
}
type CRDInformer struct {
KubeClient *KubernetesClient
ctx context.Context
cancel context.CancelFunc
SharedInformer cache.SharedInformer
ListOptions metav1.ListOptions
EventCb func(obj *apiextensionsv1beta1.CustomResourceDefinition, event string)
}
func NewCRDInformer(parentCtx context.Context, client *KubernetesClient) *CRDInformer {
ctx, cancel := context.WithCancel(parentCtx)
informer := &CRDInformer{
KubeClient: client,
ctx: ctx,
cancel: cancel,
}
return informer
}
func (p *CRDInformer) WithKubeEventCb(eventCb func(obj *apiextensionsv1beta1.CustomResourceDefinition, event string)) {
p.EventCb = eventCb
}
func (p *CRDInformer) CreateSharedInformer() (err error) {
// define resyncPeriod for informer
resyncPeriod := time.Duration(2) * time.Hour
// define indexers for informer
indexers := cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}
// Make kubeClient working with crd subscription, now client is replaced with nil value
informer := crdinformer.NewCustomResourceDefinitionInformer(nil, resyncPeriod, indexers)
informer.AddEventHandler(p)
p.SharedInformer = informer
return nil
}
func (p *CRDInformer) OnAdd(obj interface{}, _ bool) {
p.HandleWatchEvent(obj, "Added")
}
func (p *CRDInformer) OnUpdate(oldObj, newObj interface{}) {
p.HandleWatchEvent(newObj, "Modified")
}
func (p *CRDInformer) OnDelete(obj interface{}) {
p.HandleWatchEvent(obj, "Deleted")
}
func (p *CRDInformer) HandleWatchEvent(object interface{}, eventType string) {
if staleObj, stale := object.(cache.DeletedFinalStateUnknown); stale {
object = staleObj.Obj
}
obj := object.(*apiextensionsv1beta1.CustomResourceDefinition)
p.EventCb(obj, eventType)
}
func (p *CRDInformer) Run() {
p.SharedInformer.Run(p.ctx.Done())
}
func (p *CRDInformer) Stop() {
p.cancel()
}