Skip to content

Commit

Permalink
Quality improvements (#90)
Browse files Browse the repository at this point in the history
* patch - adding alert reconcile test and fixing bug saving alert's id instead of unique-id
  • Loading branch information
OrNovo authored Oct 13, 2023
1 parent ca013b7 commit 9b2090d
Show file tree
Hide file tree
Showing 19 changed files with 751 additions and 104 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/unit-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ jobs:
- uses: actions/setup-go@v4
with:
go-version-file: go.mod
- name: Create k8s Kind Cluster
uses: helm/kind-action@v1.4.0
- name: Install CRDs
run: make install
- run: make test
- name: Patch Coverage
uses: seriousben/go-patch-cover-action@v1
Expand Down
2 changes: 1 addition & 1 deletion apis/coralogix/v1alpha1/alert_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ var (
}
msInHour = int(time.Hour.Milliseconds())
msInMinute = int(time.Minute.Milliseconds())
WebhooksClient *clientset.WebhooksClient
WebhooksClient clientset.WebhooksClientInterface
)

type ProtoTimeFrameAndRelativeTimeFrame struct {
Expand Down
22 changes: 11 additions & 11 deletions controllers/alphacontrollers/alert_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ var (
alertProtoArithmeticOperatorToSchemaArithmeticOperator = utils.ReverseMap(coralogixv1alpha1.AlertSchemaArithmeticOperatorToProtoArithmeticOperator)
alertProtoNotifyOn = utils.ReverseMap(coralogixv1alpha1.AlertSchemaNotifyOnToProtoNotifyOn)
alertProtoFlowOperatorToProtoFlowOperator = utils.ReverseMap(coralogixv1alpha1.AlertSchemaFlowOperatorToProtoFlowOperator)
alertFinalizerName = "alert.coralogix.com/finalizer"
)

// AlertReconciler reconciles a Alert object
type AlertReconciler struct {
client.Client
CoralogixClientSet *clientset.ClientSet
CoralogixClientSet clientset.ClientSetInterface
Scheme *runtime.Scheme
}

Expand All @@ -73,11 +74,13 @@ type AlertReconciler struct {
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.14.4/pkg/reconcile

func (r *AlertReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := log.FromContext(ctx)
jsm := &jsonpb.Marshaler{
EmitDefaults: true,
}

alertsClient := r.CoralogixClientSet.Alerts()
coralogixv1alpha1.WebhooksClient = r.CoralogixClientSet.Webhooks()

Expand All @@ -94,27 +97,24 @@ func (r *AlertReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
return ctrl.Result{RequeueAfter: defaultErrRequeuePeriod}, err
}

// name of our custom finalizer
myFinalizerName := "batch.tutorial.kubebuilder.io/finalizer"

// examine DeletionTimestamp to determine if object is under deletion
if alertCRD.ObjectMeta.DeletionTimestamp.IsZero() {
// The object is not being deleted, so if it does not have our finalizer,
// then lets add the finalizer and update the object. This is equivalent
// registering our finalizer.
if !controllerutil.ContainsFinalizer(alertCRD, myFinalizerName) {
controllerutil.AddFinalizer(alertCRD, myFinalizerName)
if !controllerutil.ContainsFinalizer(alertCRD, alertFinalizerName) {
controllerutil.AddFinalizer(alertCRD, alertFinalizerName)
if err := r.Update(ctx, alertCRD); err != nil {
log.Error(err, "Error on updating alert", "Name", alertCRD.Name, "Namespace", alertCRD.Namespace)
return ctrl.Result{}, err
}
}
} else {
// The object is being deleted
if controllerutil.ContainsFinalizer(alertCRD, myFinalizerName) {
if controllerutil.ContainsFinalizer(alertCRD, alertFinalizerName) {
// our finalizer is present, so lets handle any external dependency
if alertCRD.Status.ID == nil {
controllerutil.RemoveFinalizer(alertCRD, myFinalizerName)
controllerutil.RemoveFinalizer(alertCRD, alertFinalizerName)
err := r.Update(ctx, alertCRD)
return ctrl.Result{}, err
}
Expand All @@ -126,7 +126,7 @@ func (r *AlertReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
// if fail to delete the external dependency here, return with error
// so that it can be retried
if status.Code(err) == codes.NotFound {
controllerutil.RemoveFinalizer(alertCRD, myFinalizerName)
controllerutil.RemoveFinalizer(alertCRD, alertFinalizerName)
err := r.Update(ctx, alertCRD)
return ctrl.Result{}, err
}
Expand All @@ -137,7 +137,7 @@ func (r *AlertReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl

log.V(1).Info("Alert was deleted", "Alert ID", alertId)
// remove our finalizer from the list and update it.
controllerutil.RemoveFinalizer(alertCRD, myFinalizerName)
controllerutil.RemoveFinalizer(alertCRD, alertFinalizerName)
if err := r.Update(ctx, alertCRD); err != nil {
log.Error(err, "Error on updating alert", "Name", alertCRD.Name, "Namespace", alertCRD.Namespace)
return ctrl.Result{}, err
Expand Down Expand Up @@ -197,7 +197,7 @@ func (r *AlertReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
log.V(1).Info("Alert was created", "alert", jstr)

//To avoid a situation of the operator falling between the creation of the alert in coralogix and being saved in the cluster (something that would cause it to be created again and again), its id will be saved ASAP.
id := createAlertResp.GetAlert().GetId().GetValue()
id := createAlertResp.GetAlert().GetUniqueIdentifier().GetValue()
alertCRD.Status = coralogixv1alpha1.AlertStatus{ID: &id}
if err := r.Status().Update(ctx, alertCRD); err != nil {
log.Error(err, "Error on updating alert status", "Name", alertCRD.Name, "Namespace", alertCRD.Namespace)
Expand Down
Loading

0 comments on commit 9b2090d

Please sign in to comment.