/
controller.go
420 lines (364 loc) · 13.3 KB
/
controller.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// Package bundle registers a controller for Bundle objects.
package bundle
import (
"context"
"fmt"
"sort"
"time"
"github.com/sirupsen/logrus"
"github.com/rancher/fleet/internal/cmd/controller/options"
"github.com/rancher/fleet/internal/cmd/controller/summary"
"github.com/rancher/fleet/internal/cmd/controller/target"
"github.com/rancher/fleet/internal/helmdeployer"
"github.com/rancher/fleet/internal/manifest"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
fleetcontrollers "github.com/rancher/fleet/pkg/generated/controllers/fleet.cattle.io/v1alpha1"
"github.com/rancher/wrangler/v2/pkg/apply"
"github.com/rancher/wrangler/v2/pkg/generic"
"github.com/rancher/wrangler/v2/pkg/relatedresource"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
const (
maxNew = 50
)
type handler struct {
targets *target.Manager
gitRepo fleetcontrollers.GitRepoCache
images fleetcontrollers.ImageScanController
bundles fleetcontrollers.BundleController
bundleDeployments fleetcontrollers.BundleDeploymentController
mapper meta.RESTMapper
}
func Register(ctx context.Context,
apply apply.Apply,
mapper meta.RESTMapper,
targets *target.Manager,
bundles fleetcontrollers.BundleController,
clusters fleetcontrollers.ClusterController,
images fleetcontrollers.ImageScanController,
gitRepo fleetcontrollers.GitRepoCache,
bundleDeployments fleetcontrollers.BundleDeploymentController,
) {
h := &handler{
mapper: mapper,
targets: targets,
bundles: bundles,
bundleDeployments: bundleDeployments,
images: images,
gitRepo: gitRepo,
}
// A generating handler returns a list of objects to be created and
// updates the given condition in the status of the object.
// This handler is triggered for bundles.OnChange
fleetcontrollers.RegisterBundleGeneratingHandler(ctx,
bundles,
apply.WithCacheTypes(bundleDeployments),
"Processed",
"bundle",
h.OnBundleChange,
&generic.GeneratingHandlerOptions{
AllowClusterScoped: true,
UniqueApplyForResourceVersion: true,
})
relatedresource.Watch(ctx, "app", h.resolveBundle, bundles, bundleDeployments)
clusters.OnChange(ctx, "app", h.OnClusterChange)
bundles.OnChange(ctx, "bundle-orphan", h.OnPurgeOrphaned)
images.OnChange(ctx, "imagescan-orphan", h.OnPurgeOrphanedImageScan)
}
func (h *handler) resolveBundle(_ string, _ string, obj runtime.Object) ([]relatedresource.Key, error) {
if ad, ok := obj.(*fleet.BundleDeployment); ok {
ns, name := h.targets.BundleFromDeployment(ad)
if ns != "" && name != "" {
logrus.Debugf("enqueue bundle %s/%s for bundledeployment %s change", ns, name, ad.Name)
return []relatedresource.Key{
{
Namespace: ns,
Name: name,
},
}, nil
}
}
return nil, nil
}
func (h *handler) OnClusterChange(_ string, cluster *fleet.Cluster) (*fleet.Cluster, error) {
if cluster == nil {
return nil, nil
}
logrus.Debugf("OnClusterChange for cluster '%s', checking which bundles to enqueue or cleanup", cluster.Name)
start := time.Now()
bundlesToRefresh, bundlesToCleanup, err := h.targets.BundlesForCluster(cluster)
if err != nil {
return nil, err
}
for _, bundle := range bundlesToCleanup {
bundleDeployments, err := h.targets.GetBundleDeploymentsForBundleInCluster(bundle, cluster)
if err != nil {
return nil, err
}
for _, bundleDeployment := range bundleDeployments {
logrus.Debugf("cleaning up bundleDeployment %v in namespace %v not matching the cluster: %v", bundleDeployment.Name, bundleDeployment.Namespace, cluster.Name)
err := h.bundleDeployments.Delete(bundleDeployment.Namespace, bundleDeployment.Name, nil)
if err != nil {
logrus.Debugf("deleting bundleDeployment returned an error: %v", err)
}
}
}
for _, bundle := range bundlesToRefresh {
h.bundles.Enqueue(bundle.Namespace, bundle.Name)
}
elapsed := time.Since(start)
logrus.Debugf("OnClusterChange for cluster '%s' took %s", cluster.Name, elapsed)
return cluster, nil
}
func (h *handler) OnPurgeOrphaned(key string, bundle *fleet.Bundle) (*fleet.Bundle, error) {
if bundle == nil {
return bundle, nil
}
logrus.Debugf("OnPurgeOrphaned for bundle '%s' change, checking if gitrepo still exists", bundle.Name)
repo := bundle.Labels[fleet.RepoLabel]
if repo == "" {
return nil, nil
}
_, err := h.gitRepo.Get(bundle.Namespace, repo)
if apierrors.IsNotFound(err) {
return nil, h.bundles.Delete(bundle.Namespace, bundle.Name, nil)
} else if err != nil {
return nil, err
}
return bundle, nil
}
func (h *handler) OnPurgeOrphanedImageScan(key string, image *fleet.ImageScan) (*fleet.ImageScan, error) {
if image == nil || image.DeletionTimestamp != nil {
return image, nil
}
logrus.Debugf("OnPurgeOrphanedImageScan for image '%s' change, checking if gitrepo still exists", image.Name)
repo := image.Spec.GitRepoName
_, err := h.gitRepo.Get(image.Namespace, repo)
if apierrors.IsNotFound(err) {
return nil, h.images.Delete(image.Namespace, image.Name, nil)
} else if err != nil {
return nil, err
}
return image, nil
}
func (h *handler) OnBundleChange(bundle *fleet.Bundle, status fleet.BundleStatus) ([]runtime.Object, fleet.BundleStatus, error) {
logrus.Debugf("OnBundleChange for bundle '%s', checking targets, calculating changes, building objects", bundle.Name)
start := time.Now()
manifest, err := manifest.New(bundle.Spec.Resources)
if err != nil {
return nil, status, err
}
matchedTargets, err := h.targets.Targets(bundle, manifest)
if err != nil {
return nil, status, err
}
// Only create the Contents object if there are matching targets/ BundleDeployments will be created
if len(matchedTargets) > 0 {
// this does not need to happen after merging the
// BundleDeploymentOptions, since 'fleet apply' already put the right
// resources into bundle.Spec.Resources
if _, err := h.targets.StoreManifest(manifest); err != nil {
return nil, status, err
}
}
if err := h.updateStatusAndTargets(&status, matchedTargets); err != nil {
updateDisplay(&status)
return nil, status, err
}
if status.ObservedGeneration != bundle.Generation {
if err := setResourceKey(&status, bundle, manifest, h.isNamespaced); err != nil {
updateDisplay(&status)
return nil, status, err
}
}
summary.SetReadyConditions(&status, "Cluster", status.Summary)
status.ObservedGeneration = bundle.Generation
objs := bundleDeployments(matchedTargets, bundle)
elapsed := time.Since(start)
updateDisplay(&status)
logrus.Debugf("OnBundleChange for bundle '%s' took %s", bundle.Name, elapsed)
return objs, status, nil
}
func (h *handler) isNamespaced(gvk schema.GroupVersionKind) bool {
mapping, err := h.mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return true
}
return mapping.Scope.Name() == meta.RESTScopeNameNamespace
}
// setResourceKey updates status.ResourceKey from the bundle, by running helm template (does not mutate bundle)
func setResourceKey(status *fleet.BundleStatus, bundle *fleet.Bundle, manifest *manifest.Manifest, isNSed func(schema.GroupVersionKind) bool) error {
seen := map[fleet.ResourceKey]struct{}{}
// iterate over the defined targets, from "targets.yaml", not the
// actually matched targets to avoid duplicates
for i := range bundle.Spec.Targets {
opts := options.Merge(bundle.Spec.BundleDeploymentOptions, bundle.Spec.Targets[i].BundleDeploymentOptions)
objs, err := helmdeployer.Template(bundle.Name, manifest, opts)
if err != nil {
logrus.Infof("While calculating status.ResourceKey, error running helm template for bundle %s with target options from %s: %v", bundle.Name, bundle.Spec.Targets[i].Name, err)
continue
}
for _, obj := range objs {
m, err := meta.Accessor(obj)
if err != nil {
return err
}
key := fleet.ResourceKey{
Namespace: m.GetNamespace(),
Name: m.GetName(),
}
gvk := obj.GetObjectKind().GroupVersionKind()
if key.Namespace == "" && isNSed(gvk) {
if opts.DefaultNamespace == "" {
key.Namespace = "default"
} else {
key.Namespace = opts.DefaultNamespace
}
}
key.APIVersion, key.Kind = gvk.ToAPIVersionAndKind()
seen[key] = struct{}{}
}
}
keys := []fleet.ResourceKey{}
for k := range seen {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool {
keyi := keys[i]
keyj := keys[j]
if keyi.APIVersion != keyj.APIVersion {
return keyi.APIVersion < keyj.APIVersion
}
if keyi.Kind != keyj.Kind {
return keyi.Kind < keyj.Kind
}
if keyi.Namespace != keyj.Namespace {
return keyi.Namespace < keyj.Namespace
}
if keyi.Name != keyj.Name {
return keyi.Name < keyj.Name
}
return false
})
status.ResourceKey = keys
return nil
}
// bundleDeployments copies BundleDeployments out of targets and into a new slice of runtime.Object
// discarding Status, replacing DependsOn with the bundle's DependsOn (pure function) and replacing the labels with the
// bundle's labels
func bundleDeployments(targets []*target.Target, bundle *fleet.Bundle) (result []runtime.Object) {
for _, target := range targets {
if target.Deployment == nil {
continue
}
// NOTE we don't use the existing BundleDeployment, we discard annotations, status, etc
// copy labels from Bundle as they might have changed
dp := &fleet.BundleDeployment{
ObjectMeta: v1.ObjectMeta{
Name: target.Deployment.Name,
Namespace: target.Deployment.Namespace,
Labels: target.BundleDeploymentLabels(target.Cluster.Namespace, target.Cluster.Name),
},
Spec: target.Deployment.Spec,
}
dp.Spec.Paused = target.IsPaused()
dp.Spec.DependsOn = bundle.Spec.DependsOn
dp.Spec.CorrectDrift = target.Options.CorrectDrift
result = append(result, dp)
}
return
}
// updateStatusAndTargets recomputes status, including partitions, from data in allTargets
// it creates Deployments in allTargets if they are missing
// it updates Deployments in allTargets if they are out of sync (DeploymentID != StagedDeploymentID)
func (h *handler) updateStatusAndTargets(status *fleet.BundleStatus, allTargets []*target.Target) (err error) {
// reset
status.MaxNew = maxNew
status.Summary = fleet.BundleSummary{}
status.PartitionStatus = nil
status.Unavailable = 0
status.NewlyCreated = 0
status.Summary = target.Summary(allTargets)
status.Unavailable = target.Unavailable(allTargets)
status.MaxUnavailable, err = target.MaxUnavailable(allTargets)
if err != nil {
return err
}
partitions, err := target.Partitions(allTargets)
if err != nil {
return err
}
status.UnavailablePartitions = 0
status.MaxUnavailablePartitions, err = target.MaxUnavailablePartitions(partitions, allTargets)
if err != nil {
return err
}
for _, partition := range partitions {
for _, target := range partition.Targets {
// for a new bundledeployment, only stage the first maxNew (50) targets
if target.Deployment == nil && status.NewlyCreated < status.MaxNew {
status.NewlyCreated++
target.Deployment = &fleet.BundleDeployment{
ObjectMeta: v1.ObjectMeta{
Name: target.Bundle.Name,
Namespace: target.Cluster.Status.Namespace,
Labels: target.BundleDeploymentLabels(target.Cluster.Namespace, target.Cluster.Name),
},
}
}
// stage targets that have a Deployment struct
if target.Deployment != nil {
// NOTE merged options from targets.Targets() are set to be staged
target.Deployment.Spec.StagedOptions = target.Options
target.Deployment.Spec.StagedDeploymentID = target.DeploymentID
}
}
for _, currentTarget := range partition.Targets {
// NOTE this will propagate the staged, merged options to the current deployment
updateTarget(currentTarget, status, &partition.Status)
}
if target.UpdateStatusUnavailable(&partition.Status, partition.Targets) {
status.UnavailablePartitions++
}
if status.UnavailablePartitions > status.MaxUnavailablePartitions {
break
}
}
for _, partition := range partitions {
status.PartitionStatus = append(status.PartitionStatus, partition.Status)
}
return nil
}
// updateTarget will update DeploymentID and Options for the target to the
// staging values, if it's in a deployable state
func updateTarget(t *target.Target, status *fleet.BundleStatus, partitionStatus *fleet.PartitionStatus) {
if t.Deployment != nil &&
// Not Paused
!t.IsPaused() &&
// Has been staged
t.Deployment.Spec.StagedDeploymentID != "" &&
// Is out of sync
t.Deployment.Spec.DeploymentID != t.Deployment.Spec.StagedDeploymentID &&
// Global max unavailable not reached
(status.Unavailable < status.MaxUnavailable || target.IsUnavailable(t.Deployment)) &&
// Partition max unavailable not reached
(partitionStatus.Unavailable < partitionStatus.MaxUnavailable || target.IsUnavailable(t.Deployment)) {
if !target.IsUnavailable(t.Deployment) {
// If this was previously available, now increment unavailable count. "Upgrading" is treated as unavailable.
status.Unavailable++
partitionStatus.Unavailable++
}
t.Deployment.Spec.DeploymentID = t.Deployment.Spec.StagedDeploymentID
t.Deployment.Spec.Options = t.Deployment.Spec.StagedOptions
}
}
func updateDisplay(status *fleet.BundleStatus) {
status.Display.ReadyClusters = fmt.Sprintf("%d/%d",
status.Summary.Ready,
status.Summary.DesiredReady)
status.Display.State = string(summary.GetSummaryState(status.Summary))
}