|
| 1 | +package configmapsyncer |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + corev1 "k8s.io/api/core/v1" |
| 7 | + "k8s.io/apimachinery/pkg/api/equality" |
| 8 | + "k8s.io/apimachinery/pkg/types" |
| 9 | + ctrl "sigs.k8s.io/controller-runtime" |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/cache" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/source" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + configMapInjectFromSecretName = "core.rukpak.io/inject-from-secret-name" |
| 22 | + configMapInjectFromSecretKey = "core.rukpak.io/inject-from-secret-key" |
| 23 | + configMapInjectToDataKey = "core.rukpak.io/inject-to-data-key" |
| 24 | + configMapInjectToBinaryDataKey = "core.rukpak.io/inject-to-binarydata-key" |
| 25 | +) |
| 26 | + |
| 27 | +// Reconciler syncs secret fields to configmaps. |
| 28 | +type Reconciler struct { |
| 29 | + Client client.Client |
| 30 | + Cache cache.Cache |
| 31 | +} |
| 32 | + |
| 33 | +// Reconcile syncs a secret field to a configmap field based on the presence |
| 34 | +// of annotations "core.rukpak.io/inject-from-secret-name" and |
| 35 | +// "core.rukpak.io/inject-from-secret-key" (configmaps without BOTH of these |
| 36 | +// annotations are ignored). When these annotations are present, this |
| 37 | +// reconciler manages all content in the data and binaryData fields. |
| 38 | +// |
| 39 | +// If the annotation "core.rukpak.io/inject-to-data-key" is present, Reconcile |
| 40 | +// creates a data key containing the secret value. Otherwise, the configmap |
| 41 | +// data will be empty. |
| 42 | +// |
| 43 | +// If the annotation "core.rukpak.io/inject-to-binarydata-key" is present, |
| 44 | +// Reconcile creates a binaryData key containing the secret value. Otherwise, |
| 45 | +// the configmap binaryData will be empty. |
| 46 | +func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 47 | + l := log.FromContext(ctx) |
| 48 | + l.V(1).Info("starting reconciliation") |
| 49 | + defer l.V(1).Info("ending reconciliation") |
| 50 | + |
| 51 | + // Get configmap from cache and lookup its secret name annotation |
| 52 | + cm := &corev1.ConfigMap{} |
| 53 | + if err := r.Client.Get(ctx, req.NamespacedName, cm); err != nil { |
| 54 | + return ctrl.Result{}, client.IgnoreNotFound(err) |
| 55 | + } |
| 56 | + |
| 57 | + secretName, ok := cm.Annotations[configMapInjectFromSecretName] |
| 58 | + if !ok { |
| 59 | + return ctrl.Result{}, nil |
| 60 | + } |
| 61 | + |
| 62 | + // Get referenced secret name (in the same namespace as the configmap) |
| 63 | + secret := &corev1.Secret{} |
| 64 | + l.V(1).Info("inject from secret", "secretName", secretName) |
| 65 | + if err := r.Client.Get(ctx, types.NamespacedName{Namespace: req.Namespace, Name: secretName}, secret); err != nil { |
| 66 | + return ctrl.Result{}, client.IgnoreNotFound(err) |
| 67 | + } |
| 68 | + |
| 69 | + // Get the referenced secret's key and value |
| 70 | + secretKey, ok := cm.Annotations[configMapInjectFromSecretKey] |
| 71 | + if !ok { |
| 72 | + return ctrl.Result{}, nil |
| 73 | + } |
| 74 | + secretValue := secret.Data[secretKey] |
| 75 | + |
| 76 | + // If the configmap asks for injection into a binaryData key |
| 77 | + // generate the expected binary data. |
| 78 | + cmBinaryDataKey := cm.Annotations[configMapInjectToBinaryDataKey] |
| 79 | + expectedBinaryData := map[string][]byte(nil) |
| 80 | + if cmBinaryDataKey != "" { |
| 81 | + expectedBinaryData = map[string][]byte{cmBinaryDataKey: secretValue} |
| 82 | + } |
| 83 | + |
| 84 | + // If the configmap asks for injection into a data key |
| 85 | + // generate the expected data. |
| 86 | + cmDataKey := cm.Annotations[configMapInjectToDataKey] |
| 87 | + expectedData := map[string]string(nil) |
| 88 | + if cmDataKey != "" { |
| 89 | + expectedData = map[string]string{cmDataKey: string(secretValue)} |
| 90 | + } |
| 91 | + |
| 92 | + // If binaryData and data already have the expected contents, |
| 93 | + // there's no need to do anything, so return early. |
| 94 | + if equality.Semantic.DeepEqual(cm.BinaryData, expectedBinaryData) && equality.Semantic.DeepEqual(cm.Data, expectedData) { |
| 95 | + return ctrl.Result{}, nil |
| 96 | + } |
| 97 | + |
| 98 | + // Set the expected binaryData and data fields on the configmap |
| 99 | + // and then update it. |
| 100 | + cm.BinaryData = expectedBinaryData |
| 101 | + cm.Data = expectedData |
| 102 | + return ctrl.Result{}, r.Client.Update(ctx, cm) |
| 103 | +} |
| 104 | + |
| 105 | +// SetupWithManager sets up the controller with the Manager. |
| 106 | +func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 107 | + ctrlr, err := controller.New("configmapsyncer", mgr, controller.Options{ |
| 108 | + Reconciler: r, |
| 109 | + }) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + if err := ctrlr.Watch( |
| 115 | + source.NewKindWithCache(&corev1.ConfigMap{}, r.Cache), |
| 116 | + &handler.EnqueueRequestForObject{}, |
| 117 | + configMapHasInjectionAnnotationsPredicate(), |
| 118 | + ); err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + |
| 122 | + if err := ctrlr.Watch( |
| 123 | + source.NewKindWithCache(&corev1.Secret{}, r.Cache), |
| 124 | + secretToConfigMapMapper(r.Client), |
| 125 | + ); err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + return nil |
| 129 | +} |
| 130 | + |
| 131 | +func configMapHasInjectionAnnotationsPredicate() predicate.Predicate { |
| 132 | + return predicate.NewPredicateFuncs(func(object client.Object) bool { |
| 133 | + cm := object.(*corev1.ConfigMap) |
| 134 | + if _, ok := cm.Annotations[configMapInjectFromSecretName]; !ok { |
| 135 | + return false |
| 136 | + } |
| 137 | + if _, ok := cm.Annotations[configMapInjectFromSecretKey]; !ok { |
| 138 | + return false |
| 139 | + } |
| 140 | + return true |
| 141 | + }) |
| 142 | +} |
| 143 | + |
| 144 | +func secretToConfigMapMapper(cl client.Reader) handler.EventHandler { |
| 145 | + return handler.EnqueueRequestsFromMapFunc(func(object client.Object) []reconcile.Request { |
| 146 | + cmList := &corev1.ConfigMapList{} |
| 147 | + if err := cl.List(context.TODO(), cmList, client.InNamespace(object.GetNamespace())); err != nil { |
| 148 | + return nil |
| 149 | + } |
| 150 | + reqs := []reconcile.Request{} |
| 151 | + for _, cm := range cmList.Items { |
| 152 | + if cm.Annotations[configMapInjectFromSecretName] == object.GetName() { |
| 153 | + reqs = append(reqs, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&cm)}) |
| 154 | + } |
| 155 | + } |
| 156 | + return reqs |
| 157 | + }) |
| 158 | +} |
0 commit comments