-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathapplier.go
More file actions
362 lines (304 loc) · 11.2 KB
/
Copy pathapplier.go
File metadata and controls
362 lines (304 loc) · 11.2 KB
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
package applier
import (
"context"
"time"
"github.com/samber/lo"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/dynamic"
"k8s.io/klog/v2"
"github.com/pluralsh/console/go/client"
"github.com/pluralsh/console/go/deployment-operator/internal/helpers"
discoverycache "github.com/pluralsh/console/go/deployment-operator/pkg/cache/discovery"
"github.com/pluralsh/console/go/deployment-operator/pkg/log"
smcommon "github.com/pluralsh/console/go/deployment-operator/pkg/streamline/common"
"github.com/pluralsh/console/go/deployment-operator/pkg/streamline/store"
"github.com/pluralsh/console/go/polly/containers"
)
type Applier struct {
filters *FilterEngine
client dynamic.Interface
discoveryCache discoverycache.Cache
store store.Store
waveDelay time.Duration
// onApply callback is called after each resource is applied
onApply func(unstructured.Unstructured)
}
func (in *Applier) skipResource(resource unstructured.Unstructured, dryRun bool) bool {
return !in.filters.MatchOmit(resource, lo.Ternary(dryRun, []Filter{FilterCache}, []Filter{})...)
}
func (in *Applier) Apply(ctx context.Context,
service client.ServiceDeploymentForAgent,
resources []unstructured.Unstructured,
opts ...WaveProcessorOption,
) ([]client.ComponentAttributes, []client.ServiceErrorAttributes, error) {
resources = in.ensureServiceAnnotation(resources, service.ID)
gates := []Gate{newCRDGate(in.store, in.discoveryCache, withCRDGateResources(resources, lo.FromPtr(service.DryRun)))}
opts = append(opts, WithWaveGates(lo.Filter(gates, func(g Gate, _ int) bool { return g.Enabled() })...))
if err := in.store.SyncServiceComponents(service.ID, resources); err != nil {
return nil, nil, err
}
deleteFilterFunc, err := in.getDeleteFilterFunc(service.ID)
if err != nil {
return nil, nil, err
}
phases := NewPhases(
resources,
func(resource unstructured.Unstructured) bool {
return in.skipResource(resource, lo.FromPtr(service.DryRun))
},
func(resources []unstructured.Unstructured) (toApply, toDelete []unstructured.Unstructured) {
return deleteFilterFunc(resources)
},
)
var failed bool
var syncPhase *smcommon.SyncPhase
var phase *Phase
var hasOnFailPhase bool
serviceErrorList := make([]client.ServiceErrorAttributes, 0)
for {
if phase, hasOnFailPhase = phases.Next(syncPhase, failed); phase == nil {
break
}
now := time.Now()
syncPhase = new(phase.Name())
if !phase.HasWaves() {
klog.V(log.LogLevelDefault).InfoS(
"apply result",
"service", service.Name,
"id", service.ID,
"phase", phase.Name(),
"resources", phase.ResourceCount(),
"skipped", len(phase.Skipped()),
"applied", "0/0",
"deleted", "0/0",
"dryRun", lo.FromPtr(service.DryRun),
"duration", time.Since(now),
)
continue
}
waves := phase.Waves()
waveStatistics := WaveStatistics{}
for i, wave := range waves {
processor := NewWaveProcessor(in.client, in.discoveryCache, phase.Name(), wave, opts...)
_, serviceErrors := processor.Run(ctx)
serviceErrorList = append(serviceErrorList, serviceErrors...)
waveStatistics.Add(processor.Statistics())
if i < len(waves)-1 {
time.Sleep(in.waveDelay)
}
}
klog.V(log.LogLevelDefault).InfoS(
"apply result",
"service", service.Name,
"id", service.ID,
"phase", phase.Name(),
"resources", phase.ResourceCount(),
"skipped", phase.SkippedCount(),
"applied", waveStatistics.Applied(),
"deleted", waveStatistics.Deleted(),
"dryRun", lo.FromPtr(service.DryRun),
"duration", time.Since(now),
)
if !phases.HasResourcesInFollowingPhases(syncPhase) {
klog.V(log.LogLevelTrace).InfoS("no more resources to be applied", "service", service.Name)
break
}
hasPendingResources, hasFailedResources, err := phase.ResourceHealth()
if err != nil {
klog.V(log.LogLevelDefault).ErrorS(err, "failed to get phase health", "phase", phase.Name())
break
}
if hasPendingResources {
serviceErrorList = append(serviceErrorList, client.ServiceErrorAttributes{
Source: string(phase.Name()),
Message: "waiting for resources to be ready",
Warning: new(true),
})
klog.V(log.LogLevelTrace).InfoS("waiting for resources to be ready", "phase", phase.Name())
break
}
failed = hasFailedResources ||
lo.ContainsBy(serviceErrorList, func(e client.ServiceErrorAttributes) bool { return !lo.FromPtr(e.Warning) })
if failed {
serviceErrorList = append(serviceErrorList, client.ServiceErrorAttributes{
Source: string(phase.Name()),
Message: "could not complete phase, check errors and failing resources",
})
if !hasOnFailPhase {
klog.V(log.LogLevelTrace).InfoS("failed to apply phase", "phase", phase.Name())
break
}
}
}
attrs, err := in.store.GetComponentAttributes(service.ID, false)
return attrs, serviceErrorList, err
}
func (in *Applier) Destroy(ctx context.Context, serviceID string) ([]client.ComponentAttributes, error) {
deleted := 0
deleteFilterFunc, err := in.getDeleteFilterFunc(serviceID)
if err != nil {
return nil, err
}
toDelete, _ := deleteFilterFunc([]unstructured.Unstructured{})
for _, resource := range toDelete {
live, err := in.client.Resource(helpers.GVRFromGVK(resource.GroupVersionKind())).Namespace(resource.GetNamespace()).Get(ctx, resource.GetName(), metav1.GetOptions{})
if errors.IsNotFound(err) {
if err := in.store.DeleteComponent(smcommon.NewStoreKeyFromUnstructured(resource)); err != nil {
klog.V(log.LogLevelDefault).ErrorS(err, "failed to delete component from store", "resource", resource.GetUID())
}
continue
}
if err != nil {
return nil, err
}
if live.GetAnnotations() != nil && live.GetAnnotations()[smcommon.LifecycleDeleteAnnotation] == smcommon.PreventDeletion {
if err := in.store.DeleteComponent(smcommon.NewStoreKeyFromUnstructured(lo.FromPtr(live))); err != nil {
klog.V(log.LogLevelDefault).ErrorS(err, "failed to delete component from store", "resource", live.GetUID())
}
// Delete service ID annotation so it will not be synced to store.
annotations := live.GetAnnotations()
delete(annotations, smcommon.OwningInventoryKey)
live.SetAnnotations(annotations)
if _, err := in.client.Resource(helpers.GVRFromGVK(live.GroupVersionKind())).
Update(ctx, live, metav1.UpdateOptions{}); err != nil {
return nil, err
}
continue
}
err = in.client.
Resource(helpers.GVRFromGVK(live.GroupVersionKind())).
Namespace(live.GetNamespace()).Delete(ctx, live.GetName(), metav1.DeleteOptions{
GracePeriodSeconds: new(int64(0)),
PropagationPolicy: new(metav1.DeletePropagationBackground),
})
if errors.IsNotFound(err) {
if err := in.store.DeleteComponent(smcommon.NewStoreKeyFromUnstructured(lo.FromPtr(live))); err != nil {
klog.V(log.LogLevelDefault).ErrorS(err, "failed to delete component from store", "resource", live.GetUID())
}
continue
}
if err != nil {
return nil, err
}
deleted++
}
defer klog.V(log.LogLevelDefault).InfoS("deleted service components", "deleted", deleted, "service", serviceID)
return in.store.GetComponentAttributes(serviceID, true)
}
func (in *Applier) ensureServiceAnnotation(resources []unstructured.Unstructured, serviceID string) []unstructured.Unstructured {
for _, obj := range resources {
annotations := obj.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string)
}
annotations[smcommon.OwningInventoryKey] = serviceID
annotations[smcommon.TrackingIdentifierKey] = smcommon.NewKeyFromUnstructured(obj).String()
obj.SetAnnotations(annotations)
}
return resources
}
var (
mirrorGroups = map[string]string{
"authorization.openshift.io": "rbac.authorization.k8s.io",
"rbac.authorization.k8s.io": "authorization.openshift.io",
}
)
func (in *Applier) getDeleteFilterFunc(serviceID string) (func(resources []unstructured.Unstructured) (toDelete []unstructured.Unstructured, toApply []unstructured.Unstructured), error) {
components, err := in.store.GetServiceComponents(serviceID, true)
if err != nil {
return nil, err
}
componentsSet := containers.NewSet[smcommon.Key]()
for _, component := range components {
componentsSet.Add(component.StoreKey().VersionlessKey())
}
hooks, err := in.store.GetHookComponents(serviceID)
if err != nil {
return nil, err
}
// Create a map of hooks for an easy lookup.
keyToHookComponent := make(map[smcommon.Key]smcommon.HookComponent)
for _, hook := range hooks {
keyToHookComponent[hook.StoreKey().VersionlessKey()] = hook
}
return func(resources []unstructured.Unstructured) (toDelete []unstructured.Unstructured, toApply []unstructured.Unstructured) {
skipApply := containers.NewSet[smcommon.Key]()
// Create a map of resources for an easy lookup.
keyToResource := make(map[smcommon.Key]unstructured.Unstructured)
for _, obj := range resources {
key := smcommon.NewStoreKeyFromUnstructured(obj).VersionlessKey()
keyToResource[key] = obj
}
for _, component := range components {
entryKey := component.StoreKey()
toCheck := []smcommon.Key{entryKey.VersionlessKey()}
if mirrorGroup, ok := mirrorGroups[component.Group]; ok {
toCheck = append(toCheck, entryKey.ReplaceGroup(mirrorGroup).VersionlessKey())
}
if shouldKeep := lo.SomeBy(toCheck, func(key smcommon.Key) bool {
_, ok := keyToResource[key]
return ok
}); !shouldKeep {
toDelete = append(toDelete, component.DeletableUnstructured()) // Ensures annotations that are checked later in NewPhase func.
skipApply.Add(entryKey.VersionlessKey())
}
}
for key, resource := range keyToResource {
// If the resource:
// - is in our hook component store,
// - has reached its desired state according to the delete policies,
// - and has not changed manifest recently;
// Then we can skip applying it and ensure that these resources are deleted.
deletePolicies := smcommon.ParseHookDeletePolicy(resource)
hook, ok := keyToHookComponent[key]
if ok && hook.HasDesiredState(deletePolicies) && !hook.HasManifestChanged(resource) {
skipApply.Add(key)
if componentsSet.Has(key) {
toDelete = append(toDelete, resource)
}
continue
}
// Skip applying hook resource if their manifest did not change, and it has reached its desired state.
if ok && !hook.HasManifestChanged(resource) && hook.HadDesiredState() {
skipApply.Add(key)
continue
}
// Apply resources if they were not marked to be skipped.
if !skipApply.Has(key) {
toApply = append(toApply, resource)
}
}
return
}, nil
}
type Option func(*Applier)
func WithWaveDelay(d time.Duration) Option {
return func(a *Applier) {
a.waveDelay = d
}
}
func WithFilter(name Filter, f FilterFunc) Option {
return func(a *Applier) {
a.filters.Add(name, f)
}
}
func WithOnApply(f func(unstructured.Unstructured)) Option {
return func(a *Applier) {
a.onApply = f
}
}
func NewApplier(client dynamic.Interface, discoveryCache discoverycache.Cache, store store.Store, opts ...Option) *Applier {
result := &Applier{
discoveryCache: discoveryCache,
client: client,
store: store,
filters: NewFilterEngine(),
waveDelay: 1 * time.Second,
}
for _, opt := range opts {
opt(result)
}
return result
}