|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + gatewayv1 "github.com/labring/laf/core/controllers/gateway/api/v1" |
| 6 | + "github.com/labring/laf/core/pkg/common" |
| 7 | + corev1 "k8s.io/api/core/v1" |
| 8 | + "k8s.io/apimachinery/pkg/runtime" |
| 9 | + ctrl "sigs.k8s.io/controller-runtime" |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 14 | + "strconv" |
| 15 | + "time" |
| 16 | +) |
| 17 | + |
| 18 | +var secretUpdateTimeAnnotation = "secret.laf.dev/secret-update-time" |
| 19 | + |
| 20 | +type SecretReconciler struct { |
| 21 | + client.Client |
| 22 | + Scheme *runtime.Scheme |
| 23 | +} |
| 24 | + |
| 25 | +func (r *SecretReconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { |
| 26 | + // if namespace is not laf system, ignore |
| 27 | + if request.Namespace != common.GetSystemNamespace() { |
| 28 | + return ctrl.Result{}, nil |
| 29 | + } |
| 30 | + _log := log.FromContext(ctx) |
| 31 | + err := r.Get(ctx, request.NamespacedName, &corev1.Secret{}) |
| 32 | + if err != nil { |
| 33 | + return reconcile.Result{}, err |
| 34 | + } |
| 35 | + |
| 36 | + var domainList gatewayv1.DomainList |
| 37 | + if err := r.List(ctx, &domainList); err != nil { |
| 38 | + return reconcile.Result{}, err |
| 39 | + } |
| 40 | + for _, item := range domainList.Items { |
| 41 | + if item.Spec.CertConfigRef == request.Name { |
| 42 | + _log.Info("will reconcile domain", "name", item.Name) |
| 43 | + if _, err := controllerutil.CreateOrUpdate(ctx, r.Client, &item, func() error { |
| 44 | + if item.Annotations == nil { |
| 45 | + item.Annotations = make(map[string]string) |
| 46 | + } |
| 47 | + item.Annotations[secretUpdateTimeAnnotation] = strconv.FormatInt(time.Now().Unix(), 10) |
| 48 | + return nil |
| 49 | + }); err != nil { |
| 50 | + return ctrl.Result{}, err |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return ctrl.Result{}, nil |
| 56 | +} |
| 57 | + |
| 58 | +func (r *SecretReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 59 | + return ctrl.NewControllerManagedBy(mgr). |
| 60 | + For(&corev1.Secret{}). |
| 61 | + Complete(r) |
| 62 | +} |
0 commit comments