github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/k8score/event_controller.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package k8score
    21  
    22  import (
    23  	"context"
    24  
    25  	corev1 "k8s.io/api/core/v1"
    26  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    27  	"k8s.io/apimachinery/pkg/runtime"
    28  	"k8s.io/client-go/tools/record"
    29  	ctrl "sigs.k8s.io/controller-runtime"
    30  	"sigs.k8s.io/controller-runtime/pkg/client"
    31  	"sigs.k8s.io/controller-runtime/pkg/log"
    32  
    33  	intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil"
    34  )
    35  
    36  // EventReconciler reconciles an Event object
    37  type EventReconciler struct {
    38  	client.Client
    39  	Scheme   *runtime.Scheme
    40  	Recorder record.EventRecorder
    41  }
    42  
    43  // events API only allows ready-only, create, patch
    44  // +kubebuilder:rbac:groups=core,resources=events,verbs=get;list;watch;create;patch
    45  
    46  // Reconcile is part of the main kubernetes reconciliation loop which aims to
    47  // move the current state of the cluster closer to the desired state.
    48  //
    49  // For more details, check Reconcile and its Result here:
    50  // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.14.4/pkg/reconcile
    51  func (r *EventReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
    52  	reqCtx := intctrlutil.RequestCtx{
    53  		Ctx: ctx,
    54  		Req: req,
    55  		Log: log.FromContext(ctx).WithValues("event", req.NamespacedName),
    56  	}
    57  
    58  	reqCtx.Log.V(1).Info("event watcher")
    59  
    60  	event := &corev1.Event{}
    61  	if err := r.Client.Get(ctx, req.NamespacedName, event); err != nil {
    62  		return intctrlutil.CheckedRequeueWithError(err, reqCtx.Log, "getEventError")
    63  	}
    64  
    65  	for _, handler := range EventHandlerMap {
    66  		// ignores the not found error.
    67  		if err := handler.Handle(r.Client, reqCtx, r.Recorder, event); err != nil && !apierrors.IsNotFound(err) {
    68  			return intctrlutil.RequeueWithError(err, reqCtx.Log, "handleEventError")
    69  		}
    70  	}
    71  
    72  	return intctrlutil.Reconciled()
    73  }
    74  
    75  // SetupWithManager sets up the controller with the Manager.
    76  func (r *EventReconciler) SetupWithManager(mgr ctrl.Manager) error {
    77  	return ctrl.NewControllerManagedBy(mgr).
    78  		For(&corev1.Event{}).
    79  		Complete(r)
    80  }