Skip to content

Commit

Permalink
Improve mock for concurrent usage and remove entity ID annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
SaaldjorMike committed Aug 13, 2024
1 parent d26bb10 commit cb10eab
Show file tree
Hide file tree
Showing 27 changed files with 801 additions and 648 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ endif
eval \$$($(GOBIN)/setup-envtest use -p env ${TEST_K8S_VERSION}); \
export USE_CERTMANAGER=false; \
export TEST_USE_EXISTING_CLUSTER=false; \
$(GINKGO) -vv --no-color --procs 3 -output-dir=${PWD} --output-interceptor-mode=none -keep-separate-reports --junit-report=test-results-junit.xml --randomize-suites --randomize-all -timeout 10m ./... -covermode=count -coverprofile cover.out \
$(GINKGO) -vv --no-color --procs 3 -output-dir=${PWD} -keep-separate-reports --junit-report=test-results-junit.xml --randomize-suites --randomize-all -timeout 10m ./... -covermode=count -coverprofile cover.out \
"

##@ Build
Expand Down
34 changes: 0 additions & 34 deletions controllers/humioaction_annotations.go

This file was deleted.

23 changes: 9 additions & 14 deletions controllers/humioaction_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,22 @@ func (r *HumioActionReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return reconcile.Result{}, r.logErrorAndReturn(err, "could not resolve secret references")
}

if _, err := humio.ActionFromActionCR(ha); err != nil {
r.Log.Error(err, "unable to validate action")
err = r.setState(ctx, humiov1alpha1.HumioActionStateConfigError, ha)
if err != nil {
return reconcile.Result{}, r.logErrorAndReturn(err, "unable to set action state")
if _, validateErr := humio.ActionFromActionCR(ha); validateErr != nil {
r.Log.Error(validateErr, "unable to validate action")
setStateErr := r.setState(ctx, humiov1alpha1.HumioActionStateConfigError, ha)
if setStateErr != nil {
return reconcile.Result{}, r.logErrorAndReturn(setStateErr, "unable to set action state")
}
return reconcile.Result{}, err
return reconcile.Result{}, validateErr
}

defer func(ctx context.Context, humioClient humio.Client, ha *humiov1alpha1.HumioAction) {
curAction, err := r.HumioClient.GetAction(cluster.Config(), req, ha)
_, err := r.HumioClient.GetAction(cluster.Config(), req, ha)
if errors.As(err, &humioapi.EntityNotFound{}) {
_ = r.setState(ctx, humiov1alpha1.HumioActionStateNotFound, ha)
return
}
if err != nil || curAction == nil {
if err != nil {
_ = r.setState(ctx, humiov1alpha1.HumioActionStateUnknown, ha)
return
}
Expand Down Expand Up @@ -160,12 +160,7 @@ func (r *HumioActionReconciler) reconcileHumioAction(ctx context.Context, config
if err != nil {
return reconcile.Result{}, r.logErrorAndReturn(err, "could not create action")
}
r.Log.Info("Created action", "Action", ha.Spec.Name)

result, err := r.reconcileHumioActionAnnotations(ctx, addedAction, ha, req)
if err != nil {
return result, err
}
r.Log.Info("Created action", "Action", ha.Spec.Name, "ID", addedAction.ID)
return reconcile.Result{Requeue: true}, nil
}
if err != nil {
Expand Down
42 changes: 0 additions & 42 deletions controllers/humioalert_annotations.go

This file was deleted.

19 changes: 5 additions & 14 deletions controllers/humioalert_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ func (r *HumioAlertReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

defer func(ctx context.Context, humioClient humio.Client, ha *humiov1alpha1.HumioAlert) {
curAlert, err := r.HumioClient.GetAlert(cluster.Config(), req, ha)
_, err := r.HumioClient.GetAlert(cluster.Config(), req, ha)
if errors.As(err, &humioapi.EntityNotFound{}) {
_ = r.setState(ctx, humiov1alpha1.HumioAlertStateNotFound, ha)
return
}
if err != nil || curAlert == nil {
_ = r.setState(ctx, humiov1alpha1.HumioAlertStateConfigError, ha)
if err != nil {
_ = r.setState(ctx, humiov1alpha1.HumioAlertStateUnknown, ha)
return
}
_ = r.setState(ctx, humiov1alpha1.HumioAlertStateExists, ha)
Expand Down Expand Up @@ -149,12 +149,7 @@ func (r *HumioAlertReconciler) reconcileHumioAlert(ctx context.Context, config *
if err != nil {
return reconcile.Result{}, r.logErrorAndReturn(err, "could not create alert")
}
r.Log.Info("Created alert", "Alert", ha.Spec.Name)

result, err := r.reconcileHumioAlertAnnotations(ctx, addedAlert, ha, req)
if err != nil {
return result, err
}
r.Log.Info("Created alert", "Alert", ha.Spec.Name, "ID", addedAlert.ID)
return reconcile.Result{Requeue: true}, nil
}
if err != nil {
Expand All @@ -167,11 +162,7 @@ func (r *HumioAlertReconciler) reconcileHumioAlert(ctx context.Context, config *
if err != nil {
return reconcile.Result{}, r.logErrorAndReturn(err, "could not get action id mapping")
}
expectedAlert, err := humio.AlertTransform(ha, actionIdMap)
if err != nil {
return reconcile.Result{}, r.logErrorAndReturn(err, "could not parse expected Alert")
}

expectedAlert := humio.AlertTransform(ha, actionIdMap)
sanitizeAlert(curAlert)
if !reflect.DeepEqual(*curAlert, *expectedAlert) {
r.Log.Info(fmt.Sprintf("Alert differs, triggering update, expected %#v, got: %#v",
Expand Down
42 changes: 0 additions & 42 deletions controllers/humiofilteralert_annotations.go

This file was deleted.

20 changes: 6 additions & 14 deletions controllers/humiofilteralert_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ func (r *HumioFilterAlertReconciler) Reconcile(ctx context.Context, req ctrl.Req
}

defer func(ctx context.Context, humioClient humio.Client, hfa *humiov1alpha1.HumioFilterAlert) {
curFilterAlert, err := r.HumioClient.GetFilterAlert(cluster.Config(), req, hfa)
_, err := r.HumioClient.GetFilterAlert(cluster.Config(), req, hfa)
if errors.As(err, &humioapi.EntityNotFound{}) {
_ = r.setState(ctx, humiov1alpha1.HumioFilterAlertStateNotFound, hfa)
return
}
if err != nil || curFilterAlert == nil {
_ = r.setState(ctx, humiov1alpha1.HumioFilterAlertStateConfigError, hfa)
if err != nil {
_ = r.setState(ctx, humiov1alpha1.HumioFilterAlertStateUnknown, hfa)
return
}
_ = r.setState(ctx, humiov1alpha1.HumioFilterAlertStateExists, hfa)
Expand Down Expand Up @@ -157,12 +157,7 @@ func (r *HumioFilterAlertReconciler) reconcileHumioFilterAlert(ctx context.Conte
if err != nil {
return reconcile.Result{}, r.logErrorAndReturn(err, "could not create filter alert")
}
r.Log.Info("Created filter alert", "FilterAlert", hfa.Spec.Name)

result, err := r.reconcileHumioFilterAlertAnnotations(ctx, addedFilterAlert, hfa, req)
if err != nil {
return result, err
}
r.Log.Info("Created filter alert", "FilterAlert", hfa.Spec.Name, "ID", addedFilterAlert.ID)
return reconcile.Result{Requeue: true}, nil
}
if err != nil {
Expand All @@ -173,11 +168,7 @@ func (r *HumioFilterAlertReconciler) reconcileHumioFilterAlert(ctx context.Conte
if err := r.HumioClient.ValidateActionsForFilterAlert(config, req, hfa); err != nil {
return reconcile.Result{}, r.logErrorAndReturn(err, "could not get action id mapping")
}
expectedFilterAlert, err := humio.FilterAlertTransform(hfa)
if err != nil {
return reconcile.Result{}, r.logErrorAndReturn(err, "could not parse expected FilterAlert")
}

expectedFilterAlert := humio.FilterAlertTransform(hfa)
sanitizeFilterAlert(curFilterAlert)
if !reflect.DeepEqual(*curFilterAlert, *expectedFilterAlert) {
r.Log.Info(fmt.Sprintf("FilterAlert differs, triggering update, expected %#v, got: %#v",
Expand Down Expand Up @@ -218,5 +209,6 @@ func (r *HumioFilterAlertReconciler) logErrorAndReturn(err error, msg string) er
}

func sanitizeFilterAlert(filterAlert *humioapi.FilterAlert) {
filterAlert.ID = ""
filterAlert.RunAsUserID = ""
}
28 changes: 12 additions & 16 deletions controllers/humioingesttoken_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controllers

import (
"context"
"errors"
"fmt"
"github.com/go-logr/logr"
humioapi "github.com/humio/cli/api"
Expand Down Expand Up @@ -122,30 +123,22 @@ func (r *HumioIngestTokenReconciler) Reconcile(ctx context.Context, req ctrl.Req
}

defer func(ctx context.Context, humioClient humio.Client, hit *humiov1alpha1.HumioIngestToken) {
curToken, err := humioClient.GetIngestToken(cluster.Config(), req, hit)
if err != nil {
_ = r.setState(ctx, humiov1alpha1.HumioIngestTokenStateUnknown, hit)
_, err := humioClient.GetIngestToken(cluster.Config(), req, hit)
if errors.As(err, &humioapi.EntityNotFound{}) {
_ = r.setState(ctx, humiov1alpha1.HumioIngestTokenStateNotFound, hit)
return
}
emptyToken := humioapi.IngestToken{}
if emptyToken != *curToken {
_ = r.setState(ctx, humiov1alpha1.HumioIngestTokenStateExists, hit)
if err != nil {
_ = r.setState(ctx, humiov1alpha1.HumioIngestTokenStateUnknown, hit)
return
}
_ = r.setState(ctx, humiov1alpha1.HumioIngestTokenStateNotFound, hit)
_ = r.setState(ctx, humiov1alpha1.HumioIngestTokenStateExists, hit)
}(ctx, r.HumioClient, hit)

// Get current ingest token
r.Log.Info("get current ingest token")
curToken, err := r.HumioClient.GetIngestToken(cluster.Config(), req, hit)
if err != nil {
return reconcile.Result{}, r.logErrorAndReturn(err, "could not check if ingest token exists")
}
// If token doesn't exist, the Get returns: nil, err.
// How do we distinguish between "doesn't exist" and "error while executing get"?
// TODO: change the way we do errors from the API so we can get rid of this hack
emptyToken := humioapi.IngestToken{}
if emptyToken == *curToken {
if errors.As(err, &humioapi.EntityNotFound{}) {
r.Log.Info("ingest token doesn't exist. Now adding ingest token")
// create token
_, err := r.HumioClient.AddIngestToken(cluster.Config(), req, hit)
Expand All @@ -155,6 +148,9 @@ func (r *HumioIngestTokenReconciler) Reconcile(ctx context.Context, req ctrl.Req
r.Log.Info("created ingest token")
return reconcile.Result{Requeue: true}, nil
}
if err != nil {
return reconcile.Result{}, r.logErrorAndReturn(err, "could not check if ingest token exists")
}

// Trigger update if parser name changed
if curToken.AssignedParser != hit.Spec.ParserName {
Expand Down Expand Up @@ -243,7 +239,7 @@ func (r *HumioIngestTokenReconciler) ensureTokenSecretExists(ctx context.Context
if string(existingSecret.Data["token"]) != string(desiredSecret.Data["token"]) {
r.Log.Info("secret does not match the token in Humio. Updating token", "TokenSecretName", hit.Spec.TokenSecretName)
if err = r.Update(ctx, desiredSecret); err != nil {
return r.logErrorAndReturn(err, "unable to update alert")
return r.logErrorAndReturn(err, "unable to update ingest token")
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions controllers/humioparser_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ func (r *HumioParserReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

defer func(ctx context.Context, humioClient humio.Client, hp *humiov1alpha1.HumioParser) {
curParser, err := humioClient.GetParser(cluster.Config(), req, hp)
_, err := humioClient.GetParser(cluster.Config(), req, hp)
if errors.As(err, &humioapi.EntityNotFound{}) {
_ = r.setState(ctx, humiov1alpha1.HumioParserStateNotFound, hp)
return
}
if err != nil || curParser == nil {
if err != nil {
_ = r.setState(ctx, humiov1alpha1.HumioParserStateUnknown, hp)
return
}
Expand Down
Loading

0 comments on commit cb10eab

Please sign in to comment.