forked from kyma-project/lifecycle-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.go
401 lines (361 loc) · 12.7 KB
/
runner.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
package sync
import (
"context"
"errors"
"fmt"
"time"
apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
machineryruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"github.com/kyma-project/lifecycle-manager/api/shared"
"github.com/kyma-project/lifecycle-manager/api/v1beta2"
commonerrs "github.com/kyma-project/lifecycle-manager/pkg/common" //nolint:importas // a one-time reference for the package
"github.com/kyma-project/lifecycle-manager/pkg/log"
"github.com/kyma-project/lifecycle-manager/pkg/module/common"
"github.com/kyma-project/lifecycle-manager/pkg/templatelookup"
"github.com/kyma-project/lifecycle-manager/pkg/util"
)
var ErrServerSideApplyFailed = errors.New("ServerSideApply failed")
func New(clnt client.Client) *Runner {
return &Runner{
Client: clnt,
versioner: schema.GroupVersions(clnt.Scheme().PreferredVersionAllGroups()),
converter: clnt.Scheme(),
}
}
type ModuleMetrics interface {
RemoveModuleStateMetrics(kymaName, moduleName string)
}
type GetModuleFunc func(ctx context.Context, module client.Object) error
type Runner struct {
client.Client
versioner machineryruntime.GroupVersioner
converter machineryruntime.ObjectConvertor
}
func (r *Runner) ReconcileManifests(ctx context.Context, kyma *v1beta2.Kyma,
modules common.Modules,
) error {
ssaStart := time.Now()
baseLogger := logf.FromContext(ctx)
results := make(chan error, len(modules))
for _, module := range modules {
go func(module *common.Module) {
// Should not happen, but in case of NPE, we should stop process further.
if module.Template == nil {
results <- nil
return
}
// Due to module template visibility change, some module previously deployed should be removed.
if errors.Is(module.Template.Err, templatelookup.ErrTemplateNotAllowed) {
results <- r.deleteManifest(ctx, module)
return
}
// ModuleInStatus template in other error status should be ignored.
if module.Template.Err != nil {
results <- nil
return
}
if err := r.updateManifest(ctx, kyma, module); err != nil {
results <- fmt.Errorf("could not update module %s: %w", module.GetName(), err)
return
}
module.Logger(baseLogger).V(log.DebugLevel).Info("successfully patched module")
results <- nil
}(module)
}
var errs []error
for range len(modules) {
if err := <-results; err != nil {
errs = append(errs, err)
}
}
ssaFinish := time.Since(ssaStart)
if len(errs) != 0 {
errs = append(errs, fmt.Errorf("%w (after %s)", ErrServerSideApplyFailed, ssaFinish))
return errors.Join(errs...)
}
baseLogger.V(log.DebugLevel).Info("ServerSideApply finished", "time", ssaFinish)
return nil
}
func (r *Runner) getModule(ctx context.Context, module client.Object) error {
err := r.Get(ctx, client.ObjectKey{Namespace: module.GetNamespace(), Name: module.GetName()}, module)
if err != nil {
return fmt.Errorf("failed to get module by name-namespace: %w", err)
}
return nil
}
func (r *Runner) updateManifest(ctx context.Context, kyma *v1beta2.Kyma,
module *common.Module,
) error {
if err := r.setupModule(module, kyma); err != nil {
return err
}
obj, err := r.converter.ConvertToVersion(module.Manifest, r.versioner)
if err != nil {
return fmt.Errorf("failed to convert object to version: %w", err)
}
newManifest, ok := obj.(*v1beta2.Manifest)
if !ok {
return commonerrs.ErrTypeAssert
}
moduleStatus := kyma.GetModuleStatusMap()[module.ModuleName]
manifestInCluster, err := r.getManifest(ctx, newManifest.GetName(), newManifest.GetNamespace())
if err != nil {
return err
}
if err := r.doUpdateWithStrategy(ctx, kyma.Labels[shared.ManagedBy], module,
manifestInCluster, newManifest, moduleStatus); err != nil {
return err
}
module.Manifest = newManifest
module.Manifest.Status = getManifestStatus(newManifest, manifestInCluster)
return nil
}
func getManifestStatus(manifest, manifestInCluster *v1beta2.Manifest) shared.Status {
// In case manifest in cluster exists, collect status from it.
if manifestInCluster != nil {
return manifestInCluster.Status
}
// status could also come from manifest after patch.
return manifest.Status
}
func (r *Runner) doUpdateWithStrategy(ctx context.Context, owner string, module *common.Module,
manifestInCluster, newManifest *v1beta2.Manifest, kymaModuleStatus *v1beta2.ModuleStatus,
) error {
if !NeedToUpdate(manifestInCluster, newManifest, kymaModuleStatus, module) {
return nil
}
if module.Enabled {
return r.patchManifest(ctx, owner, newManifest)
}
// For disabled module, the manifest CR is under deleting, in this case, we only update the spec when it's still not deleted.
if err := r.updateAvailableManifestSpec(ctx, manifestInCluster, newManifest); err != nil && !util.IsNotFound(err) {
return err
}
return nil
}
func (r *Runner) getManifest(ctx context.Context, name, namespace string) (*v1beta2.Manifest, error) {
manifestInCluster := &v1beta2.Manifest{}
err := r.Get(ctx, client.ObjectKey{
Namespace: namespace,
Name: name,
}, manifestInCluster)
if err != nil {
if util.IsNotFound(err) {
return nil, nil //nolint:nilnil //use nil to indicate an empty Manifest
}
return nil, fmt.Errorf("error get manifest %s/%s: %w", namespace, name,
err)
}
return manifestInCluster, nil
}
func (r *Runner) patchManifest(ctx context.Context, owner string, newManifest *v1beta2.Manifest) error {
if err := r.Patch(ctx, newManifest,
client.Apply,
client.FieldOwner(owner),
client.ForceOwnership,
); err != nil {
return fmt.Errorf("error applying manifest %s: %w", client.ObjectKeyFromObject(newManifest), err)
}
return nil
}
func (r *Runner) updateAvailableManifestSpec(ctx context.Context,
manifestInCluster, newManifest *v1beta2.Manifest,
) error {
if manifestInCluster == nil {
return nil
}
manifestInCluster.Spec = newManifest.Spec
if err := r.Update(ctx, manifestInCluster); err != nil {
return fmt.Errorf("error update manifest %s: %w", client.ObjectKeyFromObject(newManifest), err)
}
return nil
}
func NeedToUpdate(manifestInCluster, newManifest *v1beta2.Manifest, moduleInStatus *v1beta2.ModuleStatus,
module *common.Module,
) bool {
if manifestInCluster == nil {
return !(module.IsUnmanaged)
}
if manifestInCluster.IsUnmanaged() {
return false
}
if module.IsUnmanaged {
return true
}
diffInSpec := newManifest.Spec.Version != manifestInCluster.Spec.Version ||
!newManifest.IsSameChannel(manifestInCluster)
if manifestInCluster.IsMandatoryModule() || moduleInStatus == nil {
return diffInSpec
}
diffInTemplate := moduleInStatus.Template != nil && moduleInStatus.Template.GetGeneration() != module.Template.GetGeneration()
return diffInTemplate || diffInSpec
}
func (r *Runner) deleteManifest(ctx context.Context, module *common.Module) error {
err := r.Delete(ctx, module.Manifest)
if util.IsNotFound(err) {
return nil
}
return fmt.Errorf("failed to delete manifest: %w", err)
}
func (r *Runner) setupModule(module *common.Module, kyma *v1beta2.Kyma) error {
module.ApplyDefaultMetaToManifest(kyma)
refs := module.GetOwnerReferences()
if len(refs) == 0 {
if err := controllerutil.SetControllerReference(kyma, module.Manifest, r.Scheme()); err != nil {
return fmt.Errorf("error setting owner reference on component CR of type: %s for resource %s %w",
module.GetName(), kyma.Name, err)
}
}
return nil
}
func (r *Runner) SyncModuleStatus(ctx context.Context, kyma *v1beta2.Kyma, modules common.Modules,
metrics ModuleMetrics,
) {
updateModuleStatusFromExistingModules(modules, kyma)
DeleteNoLongerExistingModuleStatus(ctx, kyma, r.getModule, metrics)
}
func updateModuleStatusFromExistingModules(
modules common.Modules,
kyma *v1beta2.Kyma,
) {
moduleStatusMap := kyma.GetModuleStatusMap()
for idx := range modules {
module := modules[idx]
moduleStatus, exists := moduleStatusMap[module.ModuleName]
latestModuleStatus := generateModuleStatus(module, moduleStatus)
if exists {
*moduleStatus = latestModuleStatus
} else {
kyma.Status.Modules = append(kyma.Status.Modules, latestModuleStatus)
}
}
}
func generateModuleStatus(module *common.Module, existStatus *v1beta2.ModuleStatus) v1beta2.ModuleStatus {
if errors.Is(module.Template.Err, templatelookup.ErrTemplateUpdateNotAllowed) {
newModuleStatus := existStatus.DeepCopy()
newModuleStatus.State = shared.StateWarning
newModuleStatus.Message = module.Template.Err.Error()
return *newModuleStatus
}
if errors.Is(module.Template.Err, templatelookup.ErrNoTemplatesInListResult) {
return v1beta2.ModuleStatus{
Name: module.ModuleName,
Channel: module.Template.DesiredChannel,
FQDN: module.FQDN,
State: shared.StateWarning,
Message: module.Template.Err.Error(),
}
}
if module.Template.Err != nil {
return v1beta2.ModuleStatus{
Name: module.ModuleName,
Channel: module.Template.DesiredChannel,
FQDN: module.FQDN,
State: shared.StateError,
Message: module.Template.Err.Error(),
}
}
manifestObject := module.Manifest
manifestAPIVersion, manifestKind := manifestObject.GetObjectKind().GroupVersionKind().ToAPIVersionAndKind()
templateAPIVersion, templateKind := module.Template.GetObjectKind().GroupVersionKind().ToAPIVersionAndKind()
var moduleResource *v1beta2.TrackingObject
if manifestObject.Spec.Resource != nil {
moduleCRAPIVersion, moduleCRKind := manifestObject.Spec.Resource.
GetObjectKind().GroupVersionKind().ToAPIVersionAndKind()
moduleResource = &v1beta2.TrackingObject{
PartialMeta: v1beta2.PartialMeta{
Name: manifestObject.Spec.Resource.GetName(),
Namespace: manifestObject.Spec.Resource.GetNamespace(),
Generation: manifestObject.Spec.Resource.GetGeneration(),
},
TypeMeta: apimetav1.TypeMeta{Kind: moduleCRKind, APIVersion: moduleCRAPIVersion},
}
if module.Template.Annotations[shared.IsClusterScopedAnnotation] == shared.EnableLabelValue {
moduleResource.PartialMeta.Namespace = ""
}
}
moduleStatus := v1beta2.ModuleStatus{
Name: module.ModuleName,
FQDN: module.FQDN,
State: manifestObject.Status.State,
Channel: module.Template.Spec.Channel,
Version: manifestObject.Spec.Version,
Manifest: &v1beta2.TrackingObject{
PartialMeta: v1beta2.PartialMeta{
Name: manifestObject.GetName(),
Namespace: manifestObject.GetNamespace(),
Generation: manifestObject.GetGeneration(),
},
TypeMeta: apimetav1.TypeMeta{Kind: manifestKind, APIVersion: manifestAPIVersion},
},
Template: &v1beta2.TrackingObject{
PartialMeta: v1beta2.PartialMeta{
Name: module.Template.GetName(),
Namespace: module.Template.GetNamespace(),
Generation: module.Template.GetGeneration(),
},
TypeMeta: apimetav1.TypeMeta{Kind: templateKind, APIVersion: templateAPIVersion},
},
Resource: moduleResource,
}
if module.IsUnmanaged {
moduleStatus.State = shared.StateUnmanaged
moduleStatus.Manifest = nil
moduleStatus.Template = nil
moduleStatus.Resource = nil
}
return moduleStatus
}
func stateFromManifest(obj client.Object) shared.State {
switch manifest := obj.(type) {
case *v1beta2.Manifest:
return manifest.Status.State
case *unstructured.Unstructured:
state, _, _ := unstructured.NestedString(manifest.Object, "status", "state")
return shared.State(state)
default:
return ""
}
}
func DeleteNoLongerExistingModuleStatus(ctx context.Context, kyma *v1beta2.Kyma, moduleFunc GetModuleFunc,
metrics ModuleMetrics,
) {
moduleStatusMap := kyma.GetModuleStatusMap()
moduleStatusesToBeDeletedFromKymaStatus := kyma.GetNoLongerExistingModuleStatus()
for idx := range moduleStatusesToBeDeletedFromKymaStatus {
moduleStatus := moduleStatusesToBeDeletedFromKymaStatus[idx]
if moduleStatus.Manifest == nil {
if metrics != nil {
metrics.RemoveModuleStateMetrics(kyma.Name, moduleStatus.Name)
}
delete(moduleStatusMap, moduleStatus.Name)
continue
}
module := &unstructured.Unstructured{}
module.SetGroupVersionKind(moduleStatus.Manifest.GroupVersionKind())
module.SetName(moduleStatus.Manifest.GetName())
module.SetNamespace(moduleStatus.Manifest.GetNamespace())
err := moduleFunc(ctx, module)
if util.IsNotFound(err) {
if metrics != nil {
metrics.RemoveModuleStateMetrics(kyma.Name, moduleStatus.Name)
}
delete(moduleStatusMap, moduleStatus.Name)
} else {
moduleStatus.State = stateFromManifest(module)
}
}
kyma.Status.Modules = convertToNewModuleStatus(moduleStatusMap)
}
func convertToNewModuleStatus(moduleStatusMap map[string]*v1beta2.ModuleStatus) []v1beta2.ModuleStatus {
newModuleStatus := make([]v1beta2.ModuleStatus, 0)
for _, moduleStatus := range moduleStatusMap {
newModuleStatus = append(newModuleStatus, *moduleStatus)
}
return newModuleStatus
}