/
component.go
313 lines (277 loc) · 10.6 KB
/
component.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
/*
Copyright 2021 The KubeVela Authors.
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 applicationconfiguration
import (
"context"
"encoding/json"
"fmt"
"sort"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2"
"github.com/oam-dev/kubevela/pkg/controller/utils"
"github.com/oam-dev/kubevela/pkg/oam"
)
// ControllerRevisionComponentLabel indicate which component the revision belong to
// This label is to filter revision by client api
const ControllerRevisionComponentLabel = oam.LabelControllerRevisionComponent
// ComponentHandler will watch component change and generate Revision automatically.
type ComponentHandler struct {
Client client.Client
RevisionLimit int
CustomRevisionHookURL string
}
// Create implements EventHandler
func (c *ComponentHandler) Create(evt event.CreateEvent, q workqueue.RateLimitingInterface) {
reqs, succeed := c.createControllerRevision(evt.Object, evt.Object)
if !succeed {
// No revision created, return
return
}
for _, req := range reqs {
q.Add(req)
}
}
// Update implements EventHandler
func (c *ComponentHandler) Update(evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
reqs, succeed := c.createControllerRevision(evt.ObjectNew, evt.ObjectNew)
if !succeed {
// No revision created, return
return
}
// Note(wonderflow): MetaOld => MetaNew, requeue once is enough
for _, req := range reqs {
q.Add(req)
}
}
// Delete implements EventHandler
func (c *ComponentHandler) Delete(evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
// controllerRevision will be deleted by ownerReference mechanism
// so we don't need to delete controllerRevision here.
// but trigger an event to AppConfig controller, let it know.
for _, req := range c.getRelatedAppConfig(evt.Object) {
q.Add(req)
}
}
// Generic implements EventHandler
func (c *ComponentHandler) Generic(_ event.GenericEvent, _ workqueue.RateLimitingInterface) {
// Generic is called in response to an event of an unknown type or a synthetic event triggered as a cron or
// external trigger request - e.g. reconcile Autoscaling, or a Webhook.
// so we need to do nothing here.
}
func isMatch(appConfigs *v1alpha2.ApplicationConfigurationList, compName string) (bool, types.NamespacedName) {
for _, app := range appConfigs.Items {
for _, comp := range app.Spec.Components {
if comp.ComponentName == compName || utils.ExtractComponentName(comp.RevisionName) == compName {
return true, types.NamespacedName{Namespace: app.Namespace, Name: app.Name}
}
}
}
return false, types.NamespacedName{}
}
func (c *ComponentHandler) getRelatedAppConfig(object metav1.Object) []reconcile.Request {
var appConfigs v1alpha2.ApplicationConfigurationList
err := c.Client.List(context.Background(), &appConfigs)
if err != nil {
klog.Info(fmt.Sprintf("error list all applicationConfigurations %v", err))
return nil
}
var reqs []reconcile.Request
if match, namespaceName := isMatch(&appConfigs, object.GetName()); match {
reqs = append(reqs, reconcile.Request{NamespacedName: namespaceName})
}
return reqs
}
// IsRevisionDiff check whether there's any different between two component revision
func (c *ComponentHandler) IsRevisionDiff(mt klog.KMetadata, curComp *v1alpha2.Component) (bool, int64) {
if curComp.Status.LatestRevision == nil {
return true, 0
}
// client in controller-runtime will use informer cache
// use client will be more efficient
needNewRevision, err := utils.CompareWithRevision(context.TODO(), c.Client, mt.GetName(), mt.GetNamespace(),
curComp.Status.LatestRevision.Name, &curComp.Spec)
// TODO: this might be a bug that we treat all errors getting from k8s as a new revision
// but the client go event handler doesn't handle an error. We need to see if we can retry this
if err != nil {
klog.InfoS(fmt.Sprintf("Failed to compare the component with its latest revision with err = %+v", err),
"component", mt.GetName(), "latest revision", curComp.Status.LatestRevision.Name)
return true, curComp.Status.LatestRevision.Revision
}
return needNewRevision, curComp.Status.LatestRevision.Revision
}
func (c *ComponentHandler) createControllerRevision(mt metav1.Object, obj client.Object) ([]reconcile.Request, bool) {
curComp := obj.(*v1alpha2.Component)
comp := curComp.DeepCopy()
// No generation changed, will not create revision
if comp.Generation == comp.Status.ObservedGeneration {
return nil, false
}
diff, curRevision := c.IsRevisionDiff(mt, comp)
if !diff {
// No difference, no need to create new revision.
return nil, false
}
reqs := c.getRelatedAppConfig(mt)
// Hook to custom revision service if exist
if err := c.customComponentRevisionHook(reqs, comp); err != nil {
klog.InfoS(fmt.Sprintf("fail to hook from custom revision service(%s) %v", c.CustomRevisionHookURL, err), "componentName", mt.GetName())
return nil, false
}
nextRevision := curRevision + 1
revisionName := utils.ConstructRevisionName(mt.GetName(), nextRevision)
if comp.Status.ObservedGeneration != comp.Generation {
comp.Status.ObservedGeneration = comp.Generation
}
comp.Status.LatestRevision = &common.Revision{
Name: revisionName,
Revision: nextRevision,
}
compRaw, err := json.Marshal(comp)
if err != nil {
klog.InfoS(fmt.Sprintf("json.Marshal failed: %v", err), "componentName", mt.GetName())
return nil, false
}
// set annotation to component
revision := &appsv1.ControllerRevision{
ObjectMeta: metav1.ObjectMeta{
Name: revisionName,
Namespace: comp.Namespace,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: v1alpha2.SchemeGroupVersion.String(),
Kind: v1alpha2.ComponentKind,
Name: comp.Name,
UID: comp.UID,
Controller: pointer.BoolPtr(true),
},
},
Labels: map[string]string{
ControllerRevisionComponentLabel: comp.Name,
},
},
Revision: nextRevision,
Data: runtime.RawExtension{Raw: compRaw},
}
// TODO: we should update the status first. otherwise, the subsequent create will all fail if the update fails
err = c.Client.Create(context.TODO(), revision)
if err != nil {
klog.InfoS(fmt.Sprintf("error create controllerRevision %v", err), "componentName", mt.GetName())
return nil, false
}
err = c.UpdateStatus(context.Background(), comp)
if err != nil {
klog.InfoS(fmt.Sprintf("update component status latestRevision %s err %v", revisionName, err), "componentName", mt.GetName())
return nil, false
}
klog.InfoS("Create ControllerRevision", "name", revisionName)
// garbage collect
if int64(c.RevisionLimit) < nextRevision {
if err := c.cleanupControllerRevision(comp); err != nil {
klog.Info(fmt.Sprintf("failed to clean up revisions of Component %v.", err))
}
}
return reqs, true
}
// get sorted controllerRevisions, prepare to delete controllerRevisions
func sortedControllerRevision(appConfigs []v1alpha2.ApplicationConfiguration, revisions []appsv1.ControllerRevision,
revisionLimit int) (sortedRevisions []appsv1.ControllerRevision, toKill int, liveHashes map[string]bool) {
liveHashes = make(map[string]bool)
sortedRevisions = revisions
// get all revisions used and skipped
for _, appConfig := range appConfigs {
for _, component := range appConfig.Spec.Components {
if component.RevisionName != "" {
liveHashes[component.RevisionName] = true
}
}
}
toKeep := revisionLimit + len(liveHashes)
toKill = len(sortedRevisions) - toKeep
if toKill <= 0 {
toKill = 0
return
}
// Clean up old revisions from smallest to highest revision (from oldest to newest)
sort.Sort(historiesByRevision(sortedRevisions))
return
}
// clean revisions when over limits
func (c *ComponentHandler) cleanupControllerRevision(curComp *v1alpha2.Component) error {
labels := &metav1.LabelSelector{
MatchLabels: map[string]string{
ControllerRevisionComponentLabel: curComp.Name,
},
}
selector, err := metav1.LabelSelectorAsSelector(labels)
if err != nil {
return err
}
// List and Get Object, controller-runtime will create Informer cache
// and will get objects from cache
revisions := &appsv1.ControllerRevisionList{}
if err := c.Client.List(context.TODO(), revisions, &client.ListOptions{LabelSelector: selector}); err != nil {
return err
}
// Get appConfigs and workloads filter controllerRevision used
appConfigs := &v1alpha2.ApplicationConfigurationList{}
if err := c.Client.List(context.Background(), appConfigs); err != nil {
return err
}
// get sorted revisions
controllerRevisions, toKill, liveHashes := sortedControllerRevision(appConfigs.Items, revisions.Items, c.RevisionLimit)
for _, revision := range controllerRevisions {
if toKill <= 0 {
break
}
if hash := revision.GetName(); liveHashes[hash] {
continue
}
// Clean up
revisionToClean := revision
if err := c.Client.Delete(context.TODO(), &revisionToClean); err != nil {
return err
}
klog.InfoS("Delete controllerRevision", "name", revision.Name)
toKill--
}
return nil
}
// UpdateStatus updates v1alpha2.Component's Status with retry.RetryOnConflict
func (c *ComponentHandler) UpdateStatus(ctx context.Context, comp *v1alpha2.Component, opts ...client.UpdateOption) error {
status := comp.DeepCopy().Status
return retry.RetryOnConflict(retry.DefaultBackoff, func() (err error) {
if err = c.Client.Get(ctx, types.NamespacedName{Namespace: comp.Namespace, Name: comp.Name}, comp); err != nil {
return
}
comp.Status = status
return c.Client.Status().Update(ctx, comp, opts...)
})
}
// historiesByRevision sort controllerRevision by revision
type historiesByRevision []appsv1.ControllerRevision
func (h historiesByRevision) Len() int { return len(h) }
func (h historiesByRevision) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h historiesByRevision) Less(i, j int) bool {
return h[i].Revision < h[j].Revision
}