From bdaef0e04e02d6eb6c7295b1c136d2804f59fafc Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Fri, 19 Jul 2024 23:55:02 -0500 Subject: [PATCH] feat(ingress): Record an event when annotation parsing fails --- cmd/main.go | 5 +++-- config/rbac/role.yaml | 7 +++++++ internal/controller/ingress_controller.go | 6 +++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 2aaa696..3bd6dd7 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -149,8 +149,9 @@ func main() { setupLog.Error(err, "unable to create controller", "controller", "Account") } if err = (&controller.IngressReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + Recorder: mgr.GetEventRecorderFor("uptime-robot-controller"), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "Ingress") os.Exit(1) diff --git a/config/rbac/role.yaml b/config/rbac/role.yaml index 086874d..afd5008 100644 --- a/config/rbac/role.yaml +++ b/config/rbac/role.yaml @@ -12,6 +12,13 @@ rules: - get - list - watch +- apiGroups: + - "" + resources: + - events + verbs: + - create + - patch - apiGroups: - networking.k8s.io resources: diff --git a/internal/controller/ingress_controller.go b/internal/controller/ingress_controller.go index d82e9a0..39b7f6e 100644 --- a/internal/controller/ingress_controller.go +++ b/internal/controller/ingress_controller.go @@ -31,6 +31,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" @@ -44,12 +45,14 @@ var IngressAnnotationPrefix = "uptime-robot.clevyr.com/" // IngressReconciler reconciles a Ingress object type IngressReconciler struct { client.Client - Scheme *runtime.Scheme + Scheme *runtime.Scheme + Recorder record.EventRecorder } //+kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses,verbs=get;list;watch;create;update;patch;delete //+kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses/status,verbs=get;update;patch //+kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses/finalizers,verbs=update +//+kubebuilder:rbac:groups=core,resources=events,verbs=create;patch // Reconcile is part of the main kubernetes reconciliation loop which aims to // move the current state of the cluster closer to the desired state. @@ -132,6 +135,7 @@ func (r *IngressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct for _, monitor := range list.Items { if err := r.updateValues(ingress, &monitor, annotations); err != nil { + r.Recorder.Event(ingress, "Warning", "Sync", err.Error()) return ctrl.Result{}, err }