|
| 1 | +/* |
| 2 | +Copyright 2024. |
| 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 controller |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "time" |
| 24 | + |
| 25 | + v1 "k8s.io/api/core/v1" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 29 | + |
| 30 | + "k8s.io/apimachinery/pkg/runtime" |
| 31 | + ctrl "sigs.k8s.io/controller-runtime" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 34 | +) |
| 35 | + |
| 36 | +// ConfigMapReconciler reconciles a single ConfigMap object. |
| 37 | +// |
| 38 | +// The watched ConfigMap is used to configure a PodReconcilerConfig object. |
| 39 | +type ConfigMapReconciler struct { |
| 40 | + client.Client |
| 41 | + Scheme *runtime.Scheme |
| 42 | + |
| 43 | + Config *PodReconcilerConfig |
| 44 | +} |
| 45 | + |
| 46 | +const ( |
| 47 | + configMapObjectNamespace = "podbouncer-system" // TODO: Make configurable |
| 48 | + configMapObjectName = "podbouncer-config" // TODO: Make configurable |
| 49 | +) |
| 50 | + |
| 51 | +// TODO: Check if the permissions below are too broad. Maybe there are permissions this controller does not actually need. |
| 52 | +// +kubebuilder:rbac:groups=core,resources=configmaps,verbs=get;list;watch |
| 53 | +// +kubebuilder:rbac:groups=core,resources=configmaps/status,verbs=get |
| 54 | +// +kubebuilder:rbac:groups=core,resources=configmaps/finalizers,verbs=update |
| 55 | + |
| 56 | +// Reconcile is part of the main kubernetes reconciliation loop which aims to |
| 57 | +// move the current state of the cluster closer to the desired state. |
| 58 | +func (r *ConfigMapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 59 | + logger := log.FromContext(ctx) |
| 60 | + |
| 61 | + // Ignore objects which should not be reconciled |
| 62 | + if req.NamespacedName.String() != fmt.Sprintf("%s/%s", configMapObjectNamespace, configMapObjectName) { |
| 63 | + return ctrl.Result{}, nil |
| 64 | + } |
| 65 | + |
| 66 | + // Get object |
| 67 | + var config v1.ConfigMap |
| 68 | + if err := r.Get(ctx, req.NamespacedName, &config); err != nil { |
| 69 | + return ctrl.Result{}, client.IgnoreNotFound(err) |
| 70 | + } |
| 71 | + |
| 72 | + // Retrieve config value |
| 73 | + data := config.Data |
| 74 | + |
| 75 | + if maxPodAgeStr, found := data["maxPodAge"]; !found { |
| 76 | + // Log error but do not requeue - the error must be fixed manually |
| 77 | + err := errors.New("missing maxPodAge property in ConfigMap") |
| 78 | + logger.Error(err, "Configuration will not be updated") |
| 79 | + return ctrl.Result{}, nil |
| 80 | + } else { |
| 81 | + maxPodAge, err := time.ParseDuration(maxPodAgeStr) |
| 82 | + |
| 83 | + if err != nil { |
| 84 | + // Log error but do not requeue - the error must be fixed manually |
| 85 | + err := fmt.Errorf("invalid maxPodAge property in ConfigMap: %s", maxPodAgeStr) |
| 86 | + logger.Error(err, "Configuration will not be updated") |
| 87 | + return ctrl.Result{}, nil |
| 88 | + } |
| 89 | + |
| 90 | + oldMaxPodAge := r.Config.MaxPodAge() |
| 91 | + |
| 92 | + r.Config.SetMaxPodAge(maxPodAge) |
| 93 | + |
| 94 | + logger.Info("Configuration updated", "newMaxPodAge", maxPodAge, "currentMaxPodAge", oldMaxPodAge) |
| 95 | + |
| 96 | + return ctrl.Result{}, nil |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// SetupWithManager sets up the controller with the Manager. |
| 101 | +func (r *ConfigMapReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 102 | + filter := func(o client.Object) bool { |
| 103 | + return o.GetName() == configMapObjectName && o.GetNamespace() == configMapObjectNamespace |
| 104 | + } |
| 105 | + |
| 106 | + p := predicate.Funcs{ |
| 107 | + CreateFunc: func(e event.TypedCreateEvent[client.Object]) bool { |
| 108 | + return filter(e.Object) |
| 109 | + }, |
| 110 | + DeleteFunc: func(e event.TypedDeleteEvent[client.Object]) bool { |
| 111 | + return filter(e.Object) |
| 112 | + }, |
| 113 | + UpdateFunc: func(e event.TypedUpdateEvent[client.Object]) bool { |
| 114 | + return filter(e.ObjectNew) |
| 115 | + }, |
| 116 | + GenericFunc: func(e event.TypedGenericEvent[client.Object]) bool { |
| 117 | + return filter(e.Object) |
| 118 | + }, |
| 119 | + } |
| 120 | + |
| 121 | + return ctrl.NewControllerManagedBy(mgr). |
| 122 | + Watches(&v1.ConfigMap{}, &handler.EnqueueRequestForObject{}). |
| 123 | + WithEventFilter(p). |
| 124 | + Named("configmap"). |
| 125 | + Complete(r) |
| 126 | +} |
0 commit comments