func (r *KMSKeyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
	log := r.Log.WithValues("kmskey", req.NamespacedName)
	kmsKey := &kmsiapi.KMSKey{}
	if err := r.Client.Get(ctx, req.NamespacedName, kmsKey); err != nil {
		log.Error(err, "failed to retrieve KMSKey resource")
		return ctrl.Result{}, client.IgnoreNotFound(err)
	}
	if NeedToAddFinalizer(kmsKey) {
		log.Info("register finalizer")
		AddFinalizer(kmsKey)
		if err := r.Update(ctx, kmsKey); err != nil {
			return ctrl.Result{}, r.manageFailure(ctx, log, kmsKey, err, "Failed to add the object finalizer")
		}
	}
	if IsBeingDeleted(kmsKey) {
		if !NeedToAddFinalizer(kmsKey) {
			if kmsKey.Spec.DeletionPolicy == "Delete" {
				log.Info("delete KMSKey")
				err := r.KMSCA.DeleteKey(&kmsca.DeleteKeyInput{
					AliasName:           kmsKey.Spec.AliasName,
					PendingWindowInDays: kmsKey.Spec.DeletionPendingWindowInDays,
				})
				if err != nil {
					return ctrl.Result{}, r.manageFailure(ctx, log, kmsKey, err, "Failed to delete the KMS key")
				}
			}
			RemoveFinalizer(kmsKey)
			if err := r.Update(ctx, kmsKey); err != nil {
				return ctrl.Result{}, r.manageFailure(ctx, log, kmsKey, err, "Failed to remove the object finalizer")
			}
		}
		return ctrl.Result{}, nil
	}
	keyID, err := r.KMSCA.CreateKey(&kmsca.CreateKeyInput{
		AliasName:             kmsKey.Spec.AliasName,
		Description:           kmsKey.Spec.Description,
		CustomerMasterKeySpec: kmsKey.Spec.CustomerMasterKeySpec,
		Policy:                kmsKey.Spec.Policy,
		Tags:                  kmsKey.Spec.Tags,
	})
	if err != nil {
		return ctrl.Result{}, r.manageFailure(ctx, log, kmsKey, err, "Failed to create the kms key")
	}
	kmsKey.Status.KeyID = keyID
	if err := r.Client.Status().Update(ctx, kmsKey); err != nil {
		return ctrl.Result{}, r.manageFailure(ctx, log, kmsKey, err, "Failed to update kmsKey.Status.KeyId")
	}
	return ctrl.Result{}, r.manageSuccess(ctx, log, kmsKey)
}
Annotation for generating RBAC role for writing Events
+kubebuilder:rbac:groups="",resources=events,verbs=create;patch
+kubebuilder:rbac:groups=cert-manager.skyscanner.net,resources=kmskeys,verbs=get;list;watch;create;update;patch;delete
+kubebuilder:rbac:groups=cert-manager.skyscanner.net,resources=kmskeys/status,verbs=get;update;patch
package controllers
import (
	"context"
	"github.com/go-logr/logr"
	core "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/client-go/tools/record"
	ctrl "sigs.k8s.io/controller-runtime"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/manager"
	kmsiapi "github.com/Skyscanner/kms-issuer/api/v1alpha1"
	kmsca "github.com/Skyscanner/kms-issuer/pkg/kmsca"
)
func NewKMSKeyReconciler(mgr manager.Manager, ca *kmsca.KMSCA) *KMSKeyReconciler {
	return &KMSKeyReconciler{
		Client:   mgr.GetClient(),
		Log:      ctrl.Log.WithName("controllers").WithName("kmskey_controller"),
		Scheme:   mgr.GetScheme(),
		Recorder: mgr.GetEventRecorderFor("kmskey_controller"),
		KMSCA:    ca,
	}
}
type KMSKeyReconciler struct {
	client.Client
	Log      logr.Logger
	Scheme   *runtime.Scheme
	Recorder record.EventRecorder
	KMSCA    *kmsca.KMSCA
}