|
| 1 | +/* |
| 2 | +Copyright 2021 The cert-manager Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package inject |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "crypto/sha256" |
| 22 | + "fmt" |
| 23 | + |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + v1 "k8s.io/client-go/applyconfigurations/core/v1" |
| 26 | + ctrl "sigs.k8s.io/controller-runtime" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/builder" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 31 | + |
| 32 | + "github.com/cert-manager/trust-manager/pkg/apis/trust/v1alpha1" |
| 33 | + "github.com/cert-manager/trust-manager/pkg/bundle/internal/ssa_client" |
| 34 | +) |
| 35 | + |
| 36 | +const ( |
| 37 | + BundleInjectLabelKey = "trust-manager.io/inject-bundle" |
| 38 | + |
| 39 | + fieldManager = "trust-manager-injector" |
| 40 | +) |
| 41 | + |
| 42 | +var configMap = &metav1.PartialObjectMetadata{TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "ConfigMap"}} |
| 43 | + |
| 44 | +type Injector struct { |
| 45 | + client.Client |
| 46 | +} |
| 47 | + |
| 48 | +func (i *Injector) SetupWithManager(mgr ctrl.Manager) error { |
| 49 | + return ctrl.NewControllerManagedBy(mgr). |
| 50 | + Named("configmap-injector"). |
| 51 | + For(configMap, |
| 52 | + builder.WithPredicates( |
| 53 | + hasLabel(BundleInjectLabelKey), |
| 54 | + )). |
| 55 | + Complete(i) |
| 56 | +} |
| 57 | + |
| 58 | +func (i *Injector) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { |
| 59 | + data := map[string]string{"ca.crt": "bundle data"} |
| 60 | + dataHash := fmt.Sprintf("%x", sha256.Sum256([]byte("bundle data hash"))) |
| 61 | + |
| 62 | + applyConfig := v1.ConfigMap(request.Name, request.Namespace). |
| 63 | + WithAnnotations(map[string]string{v1alpha1.BundleHashAnnotationKey: dataHash}). |
| 64 | + WithData(data) |
| 65 | + |
| 66 | + return reconcile.Result{}, patchConfigMap(ctx, i.Client, applyConfig) |
| 67 | +} |
| 68 | + |
| 69 | +type Cleaner struct { |
| 70 | + client.Client |
| 71 | +} |
| 72 | + |
| 73 | +func (c *Cleaner) SetupWithManager(mgr ctrl.Manager) error { |
| 74 | + return ctrl.NewControllerManagedBy(mgr). |
| 75 | + Named("configmap-injector-cleaner"). |
| 76 | + For(configMap, |
| 77 | + builder.WithPredicates( |
| 78 | + hasAnnotation(v1alpha1.BundleHashAnnotationKey), |
| 79 | + predicate.Not(hasLabel(BundleInjectLabelKey)), |
| 80 | + )). |
| 81 | + Complete(c) |
| 82 | +} |
| 83 | + |
| 84 | +func (c *Cleaner) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { |
| 85 | + applyConfig := v1.ConfigMap(request.Name, request.Namespace) |
| 86 | + |
| 87 | + return reconcile.Result{}, patchConfigMap(ctx, c.Client, applyConfig) |
| 88 | +} |
| 89 | + |
| 90 | +func patchConfigMap(ctx context.Context, c client.Client, applyConfig *v1.ConfigMapApplyConfiguration) error { |
| 91 | + configMap, patch, err := ssa_client.GenerateConfigMapPatch(applyConfig) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + |
| 96 | + return c.Patch(ctx, configMap, patch, client.FieldOwner(fieldManager), client.ForceOwnership) |
| 97 | +} |
| 98 | + |
| 99 | +func hasLabel(key string) predicate.Predicate { |
| 100 | + return predicate.NewPredicateFuncs(func(obj client.Object) bool { |
| 101 | + _, ok := obj.GetLabels()[key] |
| 102 | + return ok |
| 103 | + }) |
| 104 | +} |
| 105 | + |
| 106 | +func hasAnnotation(key string) predicate.Predicate { |
| 107 | + return predicate.NewPredicateFuncs(func(obj client.Object) bool { |
| 108 | + _, ok := obj.GetAnnotations()[key] |
| 109 | + return ok |
| 110 | + }) |
| 111 | +} |
0 commit comments