-
Notifications
You must be signed in to change notification settings - Fork 68
/
bundle.go
485 lines (402 loc) · 16.6 KB
/
bundle.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
Copyright 2021 The cert-manager 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 bundle
import (
"context"
"errors"
"fmt"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/tools/record"
"k8s.io/utils/clock"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
trustapi "github.com/cert-manager/trust-manager/pkg/apis/trust/v1alpha1"
"github.com/cert-manager/trust-manager/pkg/bundle/internal/ssa_client"
"github.com/cert-manager/trust-manager/pkg/fspkg"
)
// Options hold options for the Bundle controller.
type Options struct {
// Log is the Bundle controller logger.
Log logr.Logger
// Namespace is the trust Namespace that source data can be referenced.
Namespace string
// DefaultPackageLocation is the location on the filesystem from which the 'default'
// certificate package should be loaded. If set, a valid package must be successfully
// loaded in order for the controller to start. If unset, referring to the default
// certificate package in a `Bundle` resource will cause that Bundle to error.
DefaultPackageLocation string
// SecretTargetsEnabled controls if secret targets are enabled in the Bundle API.
SecretTargetsEnabled bool
// FilterExpiredCerts controls if expired certificates are filtered from the bundle.
FilterExpiredCerts bool
}
// bundle is a controller-runtime controller. Implements the actual controller
// logic by reconciling over Bundles.
type bundle struct {
// a cache-backed Kubernetes client
client client.Client
// a direct Kubernetes client (only used for CSA to CSA migration)
directClient client.Client
// targetCache is a cache.Cache that holds cached ConfigMap and Secret
// resources that are used as targets for Bundles.
targetCache client.Reader
// defaultPackage holds the loaded 'default' certificate package, if one was specified
// at startup.
defaultPackage *fspkg.Package
// recorder is used for create Kubernetes Events for reconciled Bundles.
recorder record.EventRecorder
// clock returns time which can be overwritten for testing.
clock clock.Clock
// Options holds options for the Bundle controller.
Options
// patchResourceOverwrite allows use to override the patchResource function
// it is used for testing purposes
patchResourceOverwrite func(ctx context.Context, obj interface{}) error
}
// Reconcile is the top level function for reconciling over synced Bundles.
// Reconcile will be called whenever a Bundle event happens, or whenever any
// related resource event to that bundle occurs.
func (b *bundle) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
result, statusPatch, resultErr := b.reconcileBundle(ctx, req)
if statusPatch != nil {
con, patch, err := ssa_client.GenerateBundleStatusPatch(req.Name, req.Namespace, statusPatch)
if err != nil {
err = fmt.Errorf("failed to generate bundle status patch: %w", err)
return ctrl.Result{}, utilerrors.NewAggregate([]error{resultErr, err})
}
if err := b.client.Status().Patch(ctx, con, patch, &client.SubResourcePatchOptions{
PatchOptions: client.PatchOptions{
FieldManager: fieldManager,
Force: ptr.To(true),
},
}); err != nil {
err = fmt.Errorf("failed to apply bundle status patch: %w", err)
return ctrl.Result{}, utilerrors.NewAggregate([]error{resultErr, err})
}
}
return result, resultErr
}
func (b *bundle) reconcileBundle(ctx context.Context, req ctrl.Request) (result ctrl.Result, statusPatch *trustapi.BundleStatus, returnedErr error) {
log := b.Log.WithValues("bundle", req.NamespacedName.Name)
log.V(2).Info("syncing bundle")
var bundle trustapi.Bundle
err := b.client.Get(ctx, req.NamespacedName, &bundle)
if apierrors.IsNotFound(err) {
log.V(2).Info("bundle no longer exists, ignoring")
return ctrl.Result{}, nil, nil
}
if err != nil {
log.Error(err, "failed to get bundle")
return ctrl.Result{}, nil, fmt.Errorf("failed to get %q: %s", req.NamespacedName, err)
}
// MIGRATION: If we are upgrading from a version of trust-manager that did use Update to set
// the Bundle status, we need to ensure that we do remove the old status fields in case we apply.
if didMigrate, err := b.migrateBundleStatusToApply(ctx, &bundle); err != nil {
log.Error(err, "failed to migrate bundle status")
return ctrl.Result{}, nil, fmt.Errorf("failed to migrate bundle status: %w", err)
} else if didMigrate {
log.V(2).Info("migrated bundle status from CSA to SSA")
}
// Initialize patch with current status field values, except conditions.
// This is done to ensure information is not lost in patch if exiting early.
statusPatch = &trustapi.BundleStatus{
DefaultCAPackageVersion: bundle.Status.DefaultCAPackageVersion,
}
resolvedBundle, err := b.buildSourceBundle(ctx, &bundle)
// If any source is not found, update the Bundle status to an unready state.
if errors.As(err, ¬FoundError{}) {
log.Error(err, "bundle source was not found")
b.setBundleCondition(
bundle.Status.Conditions,
&statusPatch.Conditions,
trustapi.BundleCondition{
Type: trustapi.BundleConditionSynced,
Status: metav1.ConditionFalse,
Reason: "SourceNotFound",
Message: "Bundle source was not found: " + err.Error(),
ObservedGeneration: bundle.Generation,
},
)
b.recorder.Eventf(&bundle, corev1.EventTypeWarning, "SourceNotFound", "Bundle source was not found: %s", err)
return ctrl.Result{}, statusPatch, nil
}
if err != nil {
log.Error(err, "failed to build source bundle")
b.recorder.Eventf(&bundle, corev1.EventTypeWarning, "SourceBuildError", "Failed to build bundle sources: %s", err)
return ctrl.Result{}, nil, fmt.Errorf("failed to build bundle source: %w", err)
}
// Detect if we have a bundle with Secret targets but the feature is disabled.
if !b.Options.SecretTargetsEnabled && bundle.Spec.Target.Secret != nil {
log.Error(err, "bundle has Secret targets but the feature is disabled")
b.recorder.Eventf(&bundle, corev1.EventTypeWarning, "SecretTargetsDisabled", "Bundle has Secret targets but the feature is disabled")
b.setBundleCondition(
bundle.Status.Conditions,
&statusPatch.Conditions,
trustapi.BundleCondition{
Type: trustapi.BundleConditionSynced,
Status: metav1.ConditionFalse,
Reason: "SecretTargetsDisabled",
Message: "Bundle has Secret targets but the feature is disabled",
ObservedGeneration: bundle.Generation,
},
)
return ctrl.Result{}, statusPatch, nil
}
type targetKind string
const (
configMapTarget targetKind = "ConfigMap"
secretTarget targetKind = "Secret"
)
type targetResource struct {
Kind targetKind
types.NamespacedName
}
targetResources := map[targetResource]bool{}
namespaceSelector, err := b.bundleTargetNamespaceSelector(&bundle)
if err != nil {
b.recorder.Eventf(&bundle, corev1.EventTypeWarning, "NamespaceSelectorError", "Failed to build namespace match labels selector: %s", err)
return ctrl.Result{}, nil, fmt.Errorf("failed to build NamespaceSelector: %w", err)
}
// Find all desired targetResources.
{
var namespaceList corev1.NamespaceList
if err := b.client.List(ctx, &namespaceList, &client.ListOptions{
LabelSelector: namespaceSelector,
}); err != nil {
log.Error(err, "failed to list namespaces")
b.recorder.Eventf(&bundle, corev1.EventTypeWarning, "NamespaceListError", "Failed to list namespaces: %s", err)
return ctrl.Result{}, nil, fmt.Errorf("failed to list Namespaces: %w", err)
}
for _, namespace := range namespaceList.Items {
namespaceLog := log.WithValues("namespace", namespace.Name)
// Don't reconcile target for Namespaces that are being terminated.
if namespace.Status.Phase == corev1.NamespaceTerminating {
namespaceLog.V(2).WithValues("phase", corev1.NamespaceTerminating).Info("skipping sync for namespace as it is terminating")
continue
}
namespacedName := types.NamespacedName{
Name: bundle.Name,
Namespace: namespace.Name,
}
if bundle.Spec.Target.Secret != nil {
targetResources[targetResource{Kind: secretTarget, NamespacedName: namespacedName}] = true
}
if bundle.Spec.Target.ConfigMap != nil {
targetResources[targetResource{Kind: configMapTarget, NamespacedName: namespacedName}] = true
}
}
}
// Find all old existing ConfigMap targetResources.
{
configMapList := &metav1.PartialObjectMetadataList{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
}
err := b.targetCache.List(ctx, configMapList, &client.ListOptions{
LabelSelector: labels.SelectorFromSet(map[string]string{
trustapi.BundleLabelKey: bundle.Name,
}),
})
if err != nil {
log.Error(err, "failed to list configmaps")
b.recorder.Eventf(&bundle, corev1.EventTypeWarning, "ConfigMapListError", "Failed to list configmaps: %s", err)
return ctrl.Result{}, nil, fmt.Errorf("failed to list ConfigMaps: %w", err)
}
for _, configMap := range configMapList.Items {
key := targetResource{
Kind: configMapTarget,
NamespacedName: types.NamespacedName{
Name: configMap.Name,
Namespace: configMap.Namespace,
},
}
configmapLog := log.WithValues("configmap", key)
if _, ok := targetResources[key]; ok {
// This ConfigMap is still a target, so we don't need to remove it.
continue
}
// Don't reconcile target for ConfigMaps that are being deleted.
if configMap.GetDeletionTimestamp() != nil {
configmapLog.V(2).WithValues("deletionTimestamp", configMap.GetDeletionTimestamp()).Info("skipping sync for configmap as it is being deleted")
continue
}
if !metav1.IsControlledBy(&configMap, &bundle) {
configmapLog.V(2).Info("skipping sync for configmap as it is not controlled by bundle")
continue
}
targetResources[key] = false
}
}
// Find all old existing Secret targetResources.
if b.Options.SecretTargetsEnabled {
secretLists := &metav1.PartialObjectMetadataList{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Secret",
},
}
err := b.targetCache.List(ctx, secretLists, &client.ListOptions{
LabelSelector: labels.SelectorFromSet(map[string]string{
trustapi.BundleLabelKey: bundle.Name,
}),
})
if err != nil {
log.Error(err, "failed to list secrets")
b.recorder.Eventf(&bundle, corev1.EventTypeWarning, "SecretListError", "Failed to list secrets: %s", err)
return ctrl.Result{}, nil, fmt.Errorf("failed to list Secrets: %w", err)
}
for _, secret := range secretLists.Items {
key := targetResource{
Kind: secretTarget,
NamespacedName: types.NamespacedName{
Name: secret.Name,
Namespace: secret.Namespace,
},
}
secretLog := log.WithValues("secret", key)
if _, ok := targetResources[key]; ok {
// This Secret is still a target, so we don't need to remove it.
continue
}
// Don't reconcile target for Secrets that are being deleted.
if secret.GetDeletionTimestamp() != nil {
secretLog.V(2).WithValues("deletionTimestamp", secret.GetDeletionTimestamp()).Info("skipping sync for secret as it is being deleted")
continue
}
if !metav1.IsControlledBy(&secret, &bundle) {
secretLog.V(2).Info("skipping sync for configmap as it is not controlled by bundle")
continue
}
targetResources[key] = false
}
}
var needsUpdate bool
for target, shouldExist := range targetResources {
targetLog := log.WithValues("target", target)
var cmSynced, secretSynced bool
var err error
if target.Kind == configMapTarget {
cmSynced, err = b.syncConfigMapTarget(ctx, targetLog, &bundle, target.Name, target.Namespace, resolvedBundle, shouldExist)
if err != nil {
targetLog.Error(err, "failed sync bundle to ConfigMap target namespace")
b.recorder.Eventf(&bundle, corev1.EventTypeWarning, "SyncConfigMapTargetFailed", "Failed to sync target in Namespace %q: %s", target.Namespace, err)
b.setBundleCondition(
bundle.Status.Conditions,
&statusPatch.Conditions,
trustapi.BundleCondition{
Type: trustapi.BundleConditionSynced,
Status: metav1.ConditionFalse,
Reason: "SyncConfigMapTargetFailed",
Message: fmt.Sprintf("Failed to sync bundle to namespace %q: %s", target.Namespace, err),
ObservedGeneration: bundle.Generation,
},
)
return ctrl.Result{Requeue: true}, statusPatch, nil
}
}
if target.Kind == secretTarget {
secretSynced, err = b.syncSecretTarget(ctx, targetLog, &bundle, target.Name, target.Namespace, resolvedBundle, shouldExist)
if err != nil {
targetLog.Error(err, "failed sync bundle to Secret target namespace")
b.recorder.Eventf(&bundle, corev1.EventTypeWarning, "SyncSecretTargetFailed", "Failed to sync target in Namespace %q: %s", target.Namespace, err)
b.setBundleCondition(
bundle.Status.Conditions,
&statusPatch.Conditions,
trustapi.BundleCondition{
Type: trustapi.BundleConditionSynced,
Status: metav1.ConditionFalse,
Reason: "SyncSecretTargetFailed",
Message: fmt.Sprintf("Failed to sync bundle to namespace %q: %s", target.Namespace, err),
ObservedGeneration: bundle.Generation,
},
)
return ctrl.Result{Requeue: true}, statusPatch, nil
}
}
if cmSynced || secretSynced {
// We need to update if any target is synced.
needsUpdate = true
}
}
if b.setBundleStatusDefaultCAVersion(statusPatch, resolvedBundle.defaultCAPackageStringID) {
needsUpdate = true
}
message := "Successfully synced Bundle to all namespaces"
if !namespaceSelector.Empty() {
message = fmt.Sprintf("Successfully synced Bundle to namespaces that match this label selector: %s", namespaceSelector)
}
syncedCondition := trustapi.BundleCondition{
Type: trustapi.BundleConditionSynced,
Status: metav1.ConditionTrue,
Reason: "Synced",
Message: message,
ObservedGeneration: bundle.Generation,
}
if !needsUpdate && bundleHasCondition(bundle.Status.Conditions, syncedCondition) {
return ctrl.Result{}, nil, nil
}
log.V(2).Info("successfully synced bundle")
b.setBundleCondition(
bundle.Status.Conditions,
&statusPatch.Conditions,
syncedCondition,
)
b.recorder.Eventf(&bundle, corev1.EventTypeNormal, "Synced", message)
return ctrl.Result{}, statusPatch, nil
}
func (b *bundle) bundleTargetNamespaceSelector(bundleObj *trustapi.Bundle) (labels.Selector, error) {
nsSelector := bundleObj.Spec.Target.NamespaceSelector
if nsSelector == nil || nsSelector.MatchLabels == nil {
return labels.Everything(), nil
}
return metav1.LabelSelectorAsSelector(&metav1.LabelSelector{MatchLabels: nsSelector.MatchLabels})
}
// MIGRATION: This is a migration function that migrates the ownership of
// fields from the Update operation to the Apply operation. This is required
// to ensure that the apply operations will also remove fields that were
// created by the Update operation.
func (b *bundle) migrateBundleStatusToApply(ctx context.Context, obj client.Object) (bool, error) {
// isOldBundleStatusManagedFieldsEntry returns true if the given ManagedFieldsEntry is
// an entry that was created by the old fieldManager and is an update to the status
// subresource. We need to check for this as we need to migrate the entry to the new
// fieldManager.
isOldBundleStatusManagedFieldsEntry := func(mf *metav1.ManagedFieldsEntry) bool {
return (mf.Manager == fieldManager || mf.Manager == crRegressionFieldManager) &&
mf.Operation == metav1.ManagedFieldsOperationUpdate &&
mf.Subresource == "status"
}
needsUpdate := false
managedFields := obj.GetManagedFields()
for i, mf := range managedFields {
if !isOldBundleStatusManagedFieldsEntry(&mf) {
continue
}
needsUpdate = true
managedFields[i].Operation = metav1.ManagedFieldsOperationApply
managedFields[i].Manager = fieldManager
}
if !needsUpdate {
return false, nil
}
obj.SetManagedFields(managedFields)
return true, b.directClient.Update(ctx, obj)
}