diff --git a/Makefile b/Makefile index a0be17147..ac972c493 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/controllers/humioaction_annotations.go b/controllers/humioaction_annotations.go deleted file mode 100644 index 3fd0b1988..000000000 --- a/controllers/humioaction_annotations.go +++ /dev/null @@ -1,34 +0,0 @@ -package controllers - -import ( - "context" - "fmt" - humioapi "github.com/humio/cli/api" - humiov1alpha1 "github.com/humio/humio-operator/api/v1alpha1" - "github.com/humio/humio-operator/pkg/humio" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/reconcile" -) - -func (r *HumioActionReconciler) reconcileHumioActionAnnotations(ctx context.Context, addedAction *humioapi.Action, ha *humiov1alpha1.HumioAction, req ctrl.Request) (reconcile.Result, error) { - r.Log.Info(fmt.Sprintf("Adding ID %s to action %s", addedAction.ID, addedAction.Name)) - actionCR := &humiov1alpha1.HumioAction{} - err := r.Get(ctx, req.NamespacedName, actionCR) - if err != nil { - return reconcile.Result{}, r.logErrorAndReturn(err, "failed to add ID annotation to action") - } - - if len(actionCR.ObjectMeta.Annotations) < 1 { - actionCR.ObjectMeta.Annotations = make(map[string]string) - } - - actionCR.ObjectMeta.Annotations[humio.ActionIdentifierAnnotation] = addedAction.ID - - err = r.Update(ctx, actionCR) - if err != nil { - return reconcile.Result{}, r.logErrorAndReturn(err, "failed to add ID annotation to action") - } - - r.Log.Info("Added ID to Action", "Action", ha.Spec.Name) - return reconcile.Result{}, nil -} diff --git a/controllers/humioaction_controller.go b/controllers/humioaction_controller.go index 49f38d737..eca81448c 100644 --- a/controllers/humioaction_controller.go +++ b/controllers/humioaction_controller.go @@ -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 } @@ -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 { diff --git a/controllers/humioalert_annotations.go b/controllers/humioalert_annotations.go deleted file mode 100644 index fa4504570..000000000 --- a/controllers/humioalert_annotations.go +++ /dev/null @@ -1,42 +0,0 @@ -package controllers - -import ( - "context" - "fmt" - - humioapi "github.com/humio/cli/api" - humiov1alpha1 "github.com/humio/humio-operator/api/v1alpha1" - "github.com/humio/humio-operator/pkg/humio" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/reconcile" -) - -func (r *HumioAlertReconciler) reconcileHumioAlertAnnotations(ctx context.Context, addedAlert *humioapi.Alert, ha *humiov1alpha1.HumioAlert, req ctrl.Request) (reconcile.Result, error) { - r.Log.Info(fmt.Sprintf("Adding ID %q to alert %q", addedAlert.ID, addedAlert.Name)) - currentAlert := &humiov1alpha1.HumioAlert{} - err := r.Get(ctx, req.NamespacedName, currentAlert) - if err != nil { - return reconcile.Result{}, r.logErrorAndReturn(err, "failed to add ID annotation to alert") - } - - // Copy annotations from the alerts transformer to get the current alert ID - hydratedHumioAlert := &humiov1alpha1.HumioAlert{} - if err = humio.AlertHydrate(hydratedHumioAlert, addedAlert, map[string]string{}); err != nil { - return reconcile.Result{}, r.logErrorAndReturn(err, "failed to hydrate alert") - } - - if len(currentAlert.ObjectMeta.Annotations) < 1 { - currentAlert.ObjectMeta.Annotations = make(map[string]string) - } - for k, v := range hydratedHumioAlert.Annotations { - currentAlert.ObjectMeta.Annotations[k] = v - } - - err = r.Update(ctx, currentAlert) - if err != nil { - return reconcile.Result{}, r.logErrorAndReturn(err, "failed to add ID annotation to alert") - } - - r.Log.Info("Added id to Alert", "Alert", ha.Spec.Name) - return reconcile.Result{}, nil -} diff --git a/controllers/humioalert_controller.go b/controllers/humioalert_controller.go index 8fe8eaf61..e772f31ae 100644 --- a/controllers/humioalert_controller.go +++ b/controllers/humioalert_controller.go @@ -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) @@ -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 { @@ -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", diff --git a/controllers/humiofilteralert_annotations.go b/controllers/humiofilteralert_annotations.go deleted file mode 100644 index dcc03668c..000000000 --- a/controllers/humiofilteralert_annotations.go +++ /dev/null @@ -1,42 +0,0 @@ -package controllers - -import ( - "context" - "fmt" - - humioapi "github.com/humio/cli/api" - humiov1alpha1 "github.com/humio/humio-operator/api/v1alpha1" - "github.com/humio/humio-operator/pkg/humio" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/reconcile" -) - -func (r *HumioFilterAlertReconciler) reconcileHumioFilterAlertAnnotations(ctx context.Context, addedFilterAlert *humioapi.FilterAlert, hfa *humiov1alpha1.HumioFilterAlert, req ctrl.Request) (reconcile.Result, error) { - r.Log.Info(fmt.Sprintf("Adding annotations to filter alert %q", addedFilterAlert.Name)) - currentFilterAlert := &humiov1alpha1.HumioFilterAlert{} - err := r.Get(ctx, req.NamespacedName, currentFilterAlert) - if err != nil { - return reconcile.Result{}, r.logErrorAndReturn(err, "failed to add annotations to filter alert") - } - - // Copy annotations from the filter alerts transformer to get the current filter alert annotations - hydratedHumioFilterAlert := &humiov1alpha1.HumioFilterAlert{} - if err = humio.FilterAlertHydrate(hydratedHumioFilterAlert, addedFilterAlert); err != nil { - return reconcile.Result{}, r.logErrorAndReturn(err, "failed to hydrate alert") - } - - if len(currentFilterAlert.ObjectMeta.Annotations) < 1 { - currentFilterAlert.ObjectMeta.Annotations = make(map[string]string) - } - for k, v := range hydratedHumioFilterAlert.Annotations { - currentFilterAlert.ObjectMeta.Annotations[k] = v - } - - err = r.Update(ctx, currentFilterAlert) - if err != nil { - return reconcile.Result{}, r.logErrorAndReturn(err, "failed to add annotations to filter alert") - } - - r.Log.Info("Added annotations to FilterAlert", "FilterAlert", hfa.Spec.Name) - return reconcile.Result{}, nil -} diff --git a/controllers/humiofilteralert_controller.go b/controllers/humiofilteralert_controller.go index 09dcfa5fb..d620b4385 100644 --- a/controllers/humiofilteralert_controller.go +++ b/controllers/humiofilteralert_controller.go @@ -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) @@ -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 { @@ -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", @@ -218,5 +209,6 @@ func (r *HumioFilterAlertReconciler) logErrorAndReturn(err error, msg string) er } func sanitizeFilterAlert(filterAlert *humioapi.FilterAlert) { + filterAlert.ID = "" filterAlert.RunAsUserID = "" } diff --git a/controllers/humioingesttoken_controller.go b/controllers/humioingesttoken_controller.go index 7e399e762..f27b9f7d6 100644 --- a/controllers/humioingesttoken_controller.go +++ b/controllers/humioingesttoken_controller.go @@ -18,6 +18,7 @@ package controllers import ( "context" + "errors" "fmt" "github.com/go-logr/logr" humioapi "github.com/humio/cli/api" @@ -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) @@ -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 { @@ -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") } } } diff --git a/controllers/humioparser_controller.go b/controllers/humioparser_controller.go index af07b0638..14d157996 100644 --- a/controllers/humioparser_controller.go +++ b/controllers/humioparser_controller.go @@ -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 } diff --git a/controllers/humiorepository_controller.go b/controllers/humiorepository_controller.go index bbb776601..ff4238e80 100644 --- a/controllers/humiorepository_controller.go +++ b/controllers/humiorepository_controller.go @@ -18,8 +18,8 @@ package controllers import ( "context" + "errors" "fmt" - "reflect" "time" humioapi "github.com/humio/cli/api" @@ -121,14 +121,13 @@ func (r *HumioRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Requ } defer func(ctx context.Context, humioClient humio.Client, hr *humiov1alpha1.HumioRepository) { - curRepository, err := humioClient.GetRepository(cluster.Config(), req, hr) - if err != nil { - _ = r.setState(ctx, humiov1alpha1.HumioRepositoryStateUnknown, hr) + _, err := humioClient.GetRepository(cluster.Config(), req, hr) + if errors.As(err, &humioapi.EntityNotFound{}) { + _ = r.setState(ctx, humiov1alpha1.HumioRepositoryStateNotFound, hr) return } - emptyRepository := humioapi.Parser{} - if reflect.DeepEqual(emptyRepository, *curRepository) { - _ = r.setState(ctx, humiov1alpha1.HumioRepositoryStateNotFound, hr) + if err != nil { + _ = r.setState(ctx, humiov1alpha1.HumioRepositoryStateUnknown, hr) return } _ = r.setState(ctx, humiov1alpha1.HumioRepositoryStateExists, hr) @@ -137,12 +136,7 @@ func (r *HumioRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Requ // Get current repository r.Log.Info("get current repository") curRepository, err := r.HumioClient.GetRepository(cluster.Config(), req, hr) - if err != nil { - return reconcile.Result{}, r.logErrorAndReturn(err, "could not check if repository exists") - } - - emptyRepository := humioapi.Repository{} - if reflect.DeepEqual(emptyRepository, *curRepository) { + if errors.As(err, &humioapi.EntityNotFound{}) { r.Log.Info("repository doesn't exist. Now adding repository") // create repository _, err := r.HumioClient.AddRepository(cluster.Config(), req, hr) @@ -152,6 +146,9 @@ func (r *HumioRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Requ r.Log.Info("created repository", "RepositoryName", hr.Spec.Name) return reconcile.Result{Requeue: true}, nil } + if err != nil { + return reconcile.Result{}, r.logErrorAndReturn(err, "could not check if repository exists") + } if (curRepository.Description != hr.Spec.Description) || (curRepository.RetentionDays != float64(hr.Spec.Retention.TimeInDays)) || diff --git a/controllers/humioscheduledsearch_annotations.go b/controllers/humioscheduledsearch_annotations.go deleted file mode 100644 index f02a6392b..000000000 --- a/controllers/humioscheduledsearch_annotations.go +++ /dev/null @@ -1,42 +0,0 @@ -package controllers - -import ( - "context" - "fmt" - - humioapi "github.com/humio/cli/api" - humiov1alpha1 "github.com/humio/humio-operator/api/v1alpha1" - "github.com/humio/humio-operator/pkg/humio" - ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/reconcile" -) - -func (r *HumioScheduledSearchReconciler) reconcileHumioScheduledSearchAnnotations(ctx context.Context, addedScheduledSearch *humioapi.ScheduledSearch, hss *humiov1alpha1.HumioScheduledSearch, req ctrl.Request) (reconcile.Result, error) { - r.Log.Info(fmt.Sprintf("Adding annotations to scheduled search %q", addedScheduledSearch.Name)) - currentScheduledSearch := &humiov1alpha1.HumioScheduledSearch{} - err := r.Get(ctx, req.NamespacedName, currentScheduledSearch) - if err != nil { - return reconcile.Result{}, r.logErrorAndReturn(err, "failed to add annotations to scheduled search") - } - - // Copy annotations from the scheduled search transformer to get the current scheduled search annotations - hydratedHumioScheduledSearch := &humiov1alpha1.HumioScheduledSearch{} - if err = humio.ScheduledSearchHydrate(hydratedHumioScheduledSearch, addedScheduledSearch); err != nil { - return reconcile.Result{}, r.logErrorAndReturn(err, "failed to hydrate scheduled search") - } - - if len(currentScheduledSearch.ObjectMeta.Annotations) < 1 { - currentScheduledSearch.ObjectMeta.Annotations = make(map[string]string) - } - for k, v := range hydratedHumioScheduledSearch.Annotations { - currentScheduledSearch.ObjectMeta.Annotations[k] = v - } - - err = r.Update(ctx, currentScheduledSearch) - if err != nil { - return reconcile.Result{}, r.logErrorAndReturn(err, "failed to add annotations to scheduled search") - } - - r.Log.Info("Added annotations to ScheduledSearch", "ScheduledSearch", hss.Spec.Name) - return reconcile.Result{}, nil -} diff --git a/controllers/humioscheduledsearch_controller.go b/controllers/humioscheduledsearch_controller.go index 533dc4624..31d6b38e9 100644 --- a/controllers/humioscheduledsearch_controller.go +++ b/controllers/humioscheduledsearch_controller.go @@ -87,13 +87,13 @@ func (r *HumioScheduledSearchReconciler) Reconcile(ctx context.Context, req ctrl } defer func(ctx context.Context, humioClient humio.Client, hss *humiov1alpha1.HumioScheduledSearch) { - curScheduledSearch, err := r.HumioClient.GetScheduledSearch(cluster.Config(), req, hss) + _, err := r.HumioClient.GetScheduledSearch(cluster.Config(), req, hss) if errors.As(err, &humioapi.EntityNotFound{}) { _ = r.setState(ctx, humiov1alpha1.HumioScheduledSearchStateNotFound, hss) return } - if err != nil || curScheduledSearch == nil { - _ = r.setState(ctx, humiov1alpha1.HumioScheduledSearchStateConfigError, hss) + if err != nil { + _ = r.setState(ctx, humiov1alpha1.HumioScheduledSearchStateUnknown, hss) return } _ = r.setState(ctx, humiov1alpha1.HumioScheduledSearchStateExists, hss) @@ -148,12 +148,7 @@ func (r *HumioScheduledSearchReconciler) reconcileHumioScheduledSearch(ctx conte if err != nil { return reconcile.Result{}, r.logErrorAndReturn(err, "could not create scheduled search") } - r.Log.Info("Created scheduled search", "ScheduledSearch", hss.Spec.Name) - - result, err := r.reconcileHumioScheduledSearchAnnotations(ctx, addedScheduledSearch, hss, req) - if err != nil { - return result, err - } + r.Log.Info("Created scheduled search", "ScheduledSearch", hss.Spec.Name, "ID", addedScheduledSearch.ID) return reconcile.Result{Requeue: true}, nil } if err != nil { @@ -164,10 +159,7 @@ func (r *HumioScheduledSearchReconciler) reconcileHumioScheduledSearch(ctx conte if err := r.HumioClient.ValidateActionsForScheduledSearch(config, req, hss); err != nil { return reconcile.Result{}, r.logErrorAndReturn(err, "could not get action id mapping") } - expectedScheduledSearch, err := humio.ScheduledSearchTransform(hss) - if err != nil { - return reconcile.Result{}, r.logErrorAndReturn(err, "could not parse expected ScheduledSearch") - } + expectedScheduledSearch := humio.ScheduledSearchTransform(hss) sanitizeScheduledSearch(curScheduledSearch) if !reflect.DeepEqual(*curScheduledSearch, *expectedScheduledSearch) { @@ -209,5 +201,6 @@ func (r *HumioScheduledSearchReconciler) logErrorAndReturn(err error, msg string } func sanitizeScheduledSearch(scheduledSearch *humioapi.ScheduledSearch) { + scheduledSearch.ID = "" scheduledSearch.RunAsUserID = "" } diff --git a/controllers/humioview_controller.go b/controllers/humioview_controller.go index 146673b94..e56e04e33 100644 --- a/controllers/humioview_controller.go +++ b/controllers/humioview_controller.go @@ -18,8 +18,8 @@ package controllers import ( "context" + "errors" "fmt" - "reflect" "sort" "time" @@ -84,32 +84,6 @@ func (r *HumioViewReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( return reconcile.Result{RequeueAfter: 5 * time.Second}, r.logErrorAndReturn(err, "unable to obtain humio client config") } - defer func(ctx context.Context, humioClient humio.Client, hv *humiov1alpha1.HumioView) { - curView, err := r.HumioClient.GetView(cluster.Config(), req, hv) - if err != nil { - _ = r.setState(ctx, humiov1alpha1.HumioViewStateUnknown, hv) - return - } - emptyView := humioapi.View{} - if reflect.DeepEqual(emptyView, *curView) { - _ = r.setState(ctx, humiov1alpha1.HumioViewStateNotFound, hv) - return - } - _ = r.setState(ctx, humiov1alpha1.HumioViewStateExists, hv) - }(ctx, r.HumioClient, hv) - - r.Log.Info("get current view") - curView, err := r.HumioClient.GetView(cluster.Config(), req, hv) - if err != nil { - return reconcile.Result{}, r.logErrorAndReturn(err, "could not check if view exists") - } - - return r.reconcileHumioView(ctx, cluster.Config(), curView, hv, req) -} - -func (r *HumioViewReconciler) reconcileHumioView(ctx context.Context, config *humioapi.Config, curView *humioapi.View, hv *humiov1alpha1.HumioView, req reconcile.Request) (reconcile.Result, error) { - emptyView := humioapi.View{} - // Delete r.Log.Info("Checking if view is marked to be deleted") isMarkedForDeletion := hv.GetDeletionTimestamp() != nil @@ -120,7 +94,7 @@ func (r *HumioViewReconciler) reconcileHumioView(ctx context.Context, config *hu // finalization logic fails, don't remove the finalizer so // that we can retry during the next reconciliation. r.Log.Info("Deleting View") - if err := r.HumioClient.DeleteView(config, req, hv); err != nil { + if err := r.HumioClient.DeleteView(cluster.Config(), req, hv); err != nil { return reconcile.Result{}, r.logErrorAndReturn(err, "Delete view returned error") } @@ -147,16 +121,33 @@ func (r *HumioViewReconciler) reconcileHumioView(ctx context.Context, config *hu return reconcile.Result{Requeue: true}, nil } - // Add View - if reflect.DeepEqual(emptyView, *curView) { + defer func(ctx context.Context, humioClient humio.Client, hv *humiov1alpha1.HumioView) { + _, err := r.HumioClient.GetView(cluster.Config(), req, hv) + if errors.As(err, &humioapi.EntityNotFound{}) { + _ = r.setState(ctx, humiov1alpha1.HumioViewStateNotFound, hv) + return + } + if err != nil { + _ = r.setState(ctx, humiov1alpha1.HumioViewStateUnknown, hv) + return + } + _ = r.setState(ctx, humiov1alpha1.HumioViewStateExists, hv) + }(ctx, r.HumioClient, hv) + + r.Log.Info("get current view") + curView, err := r.HumioClient.GetView(cluster.Config(), req, hv) + if errors.As(err, &humioapi.EntityNotFound{}) { r.Log.Info("View doesn't exist. Now adding view") - _, err := r.HumioClient.AddView(config, req, hv) + _, err := r.HumioClient.AddView(cluster.Config(), req, hv) if err != nil { return reconcile.Result{}, r.logErrorAndReturn(err, "could not create view") } r.Log.Info("created view", "ViewName", hv.Spec.Name) return reconcile.Result{Requeue: true}, nil } + if err != nil { + return reconcile.Result{}, r.logErrorAndReturn(err, "could not check if view exists") + } // Update if viewConnectionsDiffer(curView.Connections, hv.GetViewConnections()) || @@ -169,7 +160,7 @@ func (r *HumioViewReconciler) reconcileHumioView(ctx context.Context, config *hu curView.Connections, curView.Description, curView.AutomaticSearch)) - _, err := r.HumioClient.UpdateView(config, req, hv) + _, err := r.HumioClient.UpdateView(cluster.Config(), req, hv) if err != nil { return reconcile.Result{}, r.logErrorAndReturn(err, "could not update view") } diff --git a/controllers/suite/clusters/humiocluster_controller_test.go b/controllers/suite/clusters/humiocluster_controller_test.go index 7a4384c1c..35a7bc176 100644 --- a/controllers/suite/clusters/humiocluster_controller_test.go +++ b/controllers/suite/clusters/humiocluster_controller_test.go @@ -56,12 +56,12 @@ var _ = Describe("HumioCluster Controller", func() { BeforeEach(func() { // failed test runs that don't clean up leave resources behind. - humioClientForTestSuite.ClearHumioClientConnections() + testHumioClient.ClearHumioClientConnections() }) AfterEach(func() { // Add any teardown steps that needs to be executed after each test - humioClientForTestSuite.ClearHumioClientConnections() + testHumioClient.ClearHumioClientConnections() }) // Add Tests for OpenAPI validation (or additional CRD features) specified in @@ -79,7 +79,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) }) }) @@ -94,7 +94,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - createAndBootstrapMultiNodePoolCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning) + createAndBootstrapMultiNodePoolCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning) Eventually(func() error { _, err := kubernetes.GetService(ctx, k8sClient, controllers.NewHumioNodeManagerFromHumioNodePool(toCreate, &toCreate.Spec.NodePools[0]).GetServiceName(), key.Namespace) @@ -143,7 +143,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - createAndBootstrapMultiNodePoolCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning) + createAndBootstrapMultiNodePoolCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning) _, err := kubernetes.GetService(ctx, k8sClient, controllers.NewHumioNodeManagerFromHumioCluster(toCreate).GetServiceName(), key.Namespace) Expect(k8serrors.IsNotFound(err)).Should(BeTrue()) @@ -163,7 +163,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) }) }) @@ -186,7 +186,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) }) }) @@ -201,7 +201,7 @@ var _ = Describe("HumioCluster Controller", func() { toCreate.Spec.Image = oldUnsupportedHumioVersion ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateConfigError, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateConfigError, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) var updatedHumioCluster humiov1alpha1.HumioCluster suite.UsingClusterBy(key.Name, "should indicate cluster configuration error") @@ -236,7 +236,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) revisionKey, _ := controllers.NewHumioNodeManagerFromHumioCluster(toCreate).GetHumioClusterNodePoolRevisionAnnotation() @@ -309,7 +309,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) originalAffinity := toCreate.Spec.Affinity @@ -421,7 +421,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) revisionKey, _ := controllers.NewHumioNodeManagerFromHumioCluster(toCreate).GetHumioClusterNodePoolRevisionAnnotation() @@ -497,7 +497,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) revisionKey, _ := controllers.NewHumioNodeManagerFromHumioCluster(toCreate).GetHumioClusterNodePoolRevisionAnnotation() @@ -592,7 +592,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) revisionKey, _ := controllers.NewHumioNodeManagerFromHumioCluster(toCreate).GetHumioClusterNodePoolRevisionAnnotation() @@ -668,7 +668,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) revisionKey, _ := controllers.NewHumioNodeManagerFromHumioCluster(toCreate).GetHumioClusterNodePoolRevisionAnnotation() @@ -742,7 +742,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) revisionKey, _ := controllers.NewHumioNodeManagerFromHumioCluster(toCreate).GetHumioClusterNodePoolRevisionAnnotation() @@ -825,7 +825,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - createAndBootstrapMultiNodePoolCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning) + createAndBootstrapMultiNodePoolCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning) defer suite.CleanupCluster(ctx, k8sClient, toCreate) mainNodePoolManager := controllers.NewHumioNodeManagerFromHumioCluster(toCreate) @@ -999,7 +999,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) clusterPods, _ := kubernetes.ListPods(ctx, k8sClient, key.Namespace, controllers.NewHumioNodeManagerFromHumioCluster(toCreate).GetPodLabels()) @@ -1105,7 +1105,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) var updatedHumioCluster humiov1alpha1.HumioCluster @@ -1222,7 +1222,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating a cluster with default helper image") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Validating pod uses default helper image as init container") @@ -1339,7 +1339,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) var updatedHumioCluster humiov1alpha1.HumioCluster @@ -1503,7 +1503,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - createAndBootstrapMultiNodePoolCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning) + createAndBootstrapMultiNodePoolCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning) defer suite.CleanupCluster(ctx, k8sClient, toCreate) mainNodePoolManager := controllers.NewHumioNodeManagerFromHumioCluster(toCreate) @@ -1773,7 +1773,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Waiting for ingresses to be created") @@ -1932,7 +1932,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) Eventually(func() bool { @@ -1959,7 +1959,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) Eventually(func() bool { @@ -1986,7 +1986,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) svc, _ := kubernetes.GetService(ctx, k8sClient, key.Name, key.Namespace) @@ -2238,7 +2238,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully without ephemeral disks") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) hnp := controllers.NewHumioNodeManagerFromHumioCluster(toCreate) @@ -2284,7 +2284,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) hnp := controllers.NewHumioNodeManagerFromHumioCluster(toCreate) clusterPods, _ := kubernetes.ListPods(ctx, k8sClient, key.Namespace, hnp.GetPodLabels()) @@ -2328,7 +2328,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) humioServiceAccountName := fmt.Sprintf("%s-%s", key.Name, controllers.HumioServiceAccountNameSuffix) @@ -2386,7 +2386,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) clusterPods, _ := kubernetes.ListPods(ctx, k8sClient, key.Namespace, controllers.NewHumioNodeManagerFromHumioCluster(toCreate).GetPodLabels()) @@ -2460,7 +2460,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) clusterPods, _ := kubernetes.ListPods(ctx, k8sClient, key.Namespace, controllers.NewHumioNodeManagerFromHumioCluster(toCreate).GetPodLabels()) @@ -2558,7 +2558,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) clusterPods, _ := kubernetes.ListPods(ctx, k8sClient, key.Namespace, controllers.NewHumioNodeManagerFromHumioCluster(toCreate).GetPodLabels()) @@ -2806,7 +2806,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully with extra kafka configs") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) clusterPods, _ := kubernetes.ListPods(ctx, k8sClient, key.Namespace, controllers.NewHumioNodeManagerFromHumioCluster(toCreate).GetPodLabels()) @@ -2948,7 +2948,7 @@ var _ = Describe("HumioCluster Controller", func() { ` suite.UsingClusterBy(key.Name, "Creating the cluster successfully with view group permissions") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Confirming config map was created") @@ -3118,7 +3118,7 @@ var _ = Describe("HumioCluster Controller", func() { ` suite.UsingClusterBy(key.Name, "Creating the cluster successfully with role permissions") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Confirming config map was created") @@ -3243,7 +3243,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Bootstrapping the cluster successfully without persistent volumes") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) Expect(kubernetes.ListPersistentVolumeClaims(ctx, k8sClient, key.Namespace, controllers.NewHumioNodeManagerFromHumioCluster(toCreate).GetNodePoolLabels())).To(HaveLen(0)) @@ -3308,7 +3308,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) initialExpectedVolumesCount := 6 @@ -3397,7 +3397,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Confirming PUBLIC_URL is set to default value and PROXY_PREFIX_URL is not set") @@ -3470,7 +3470,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Confirming PUBLIC_URL is set to default value and PROXY_PREFIX_URL is not set") @@ -3774,7 +3774,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Confirming ingress objects do not have TLS configured") @@ -3806,7 +3806,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully without any Hostnames defined") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateConfigError, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateConfigError, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Confirming we did not create any ingresses") @@ -4108,7 +4108,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Confirming init container is using the correct service account") @@ -4166,7 +4166,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Confirming init container is using the correct service account") @@ -4234,7 +4234,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Confirming service was created using the correct annotations") @@ -4271,7 +4271,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Confirming the humio pods use the requested tolerations") @@ -4299,7 +4299,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Confirming the humio pods use the requested topology spread constraint") @@ -4330,7 +4330,7 @@ var _ = Describe("HumioCluster Controller", func() { Expect(k8sClient.Create(ctx, priorityClass)).To(Succeed()) suite.UsingClusterBy(key.Name, "Creating the cluster successfully") - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Confirming the humio pods use the requested priority class name") @@ -4357,7 +4357,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Confirming service was created using the correct annotations") @@ -4387,7 +4387,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Confirming the humio pods are not using shared process namespace nor additional sidecars") @@ -4480,7 +4480,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating Humio cluster without a termination grace period set") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Validating pod is created with the default grace period") @@ -4553,7 +4553,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully with a license secret") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) secretName := fmt.Sprintf("%s-license", key.Name) @@ -4652,7 +4652,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Ensuring the state is Running") @@ -4695,7 +4695,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Confirming the humio pods are not using env var source") @@ -4799,7 +4799,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Confirming the humio pods are not using env var source") @@ -4900,7 +4900,7 @@ var _ = Describe("HumioCluster Controller", func() { suite.UsingClusterBy(key.Name, "Creating the cluster successfully") ctx := context.Background() - suite.CreateAndBootstrapCluster(ctx, k8sClient, humioClientForTestSuite, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) + suite.CreateAndBootstrapCluster(ctx, k8sClient, testHumioClient, toCreate, true, humiov1alpha1.HumioClusterStateRunning, testTimeout) defer suite.CleanupCluster(ctx, k8sClient, toCreate) suite.UsingClusterBy(key.Name, "Removing the node pool label from the pod") diff --git a/controllers/suite/clusters/suite_test.go b/controllers/suite/clusters/suite_test.go index c8411c2c0..c4687ac3e 100644 --- a/controllers/suite/clusters/suite_test.go +++ b/controllers/suite/clusters/suite_test.go @@ -67,15 +67,7 @@ import ( var k8sClient client.Client var testEnv *envtest.Environment var k8sManager ctrl.Manager -var humioClientForHumioAction humio.Client -var humioClientForHumioAlert humio.Client -var humioClientForHumioCluster humio.Client -var humioClientForHumioExternalCluster humio.Client -var humioClientForHumioIngestToken humio.Client -var humioClientForHumioParser humio.Client -var humioClientForHumioRepository humio.Client -var humioClientForHumioView humio.Client -var humioClientForTestSuite humio.Client +var testHumioClient humio.Client var testTimeout time.Duration var testProcessNamespace string var err error @@ -103,15 +95,7 @@ var _ = BeforeSuite(func() { testEnv = &envtest.Environment{ UseExistingCluster: &useExistingCluster, } - humioClientForTestSuite = humio.NewClient(log, &humioapi.Config{}, "") - humioClientForHumioAction = humio.NewClient(log, &humioapi.Config{}, "") - humioClientForHumioAlert = humio.NewClient(log, &humioapi.Config{}, "") - humioClientForHumioCluster = humio.NewClient(log, &humioapi.Config{}, "") - humioClientForHumioExternalCluster = humio.NewClient(log, &humioapi.Config{}, "") - humioClientForHumioIngestToken = humio.NewClient(log, &humioapi.Config{}, "") - humioClientForHumioParser = humio.NewClient(log, &humioapi.Config{}, "") - humioClientForHumioRepository = humio.NewClient(log, &humioapi.Config{}, "") - humioClientForHumioView = humio.NewClient(log, &humioapi.Config{}, "") + testHumioClient = humio.NewClient(log, &humioapi.Config{}, "") } else { testTimeout = time.Second * 30 testEnv = &envtest.Environment{ @@ -119,15 +103,7 @@ var _ = BeforeSuite(func() { CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")}, ErrorIfCRDPathMissing: true, } - humioClientForTestSuite = humio.NewMockClient(humioapi.Cluster{}, nil) - humioClientForHumioAction = humio.NewMockClient(humioapi.Cluster{}, nil) - humioClientForHumioAlert = humio.NewMockClient(humioapi.Cluster{}, nil) - humioClientForHumioCluster = humio.NewMockClient(humioapi.Cluster{}, nil) - humioClientForHumioExternalCluster = humio.NewMockClient(humioapi.Cluster{}, nil) - humioClientForHumioIngestToken = humio.NewMockClient(humioapi.Cluster{}, nil) - humioClientForHumioParser = humio.NewMockClient(humioapi.Cluster{}, nil) - humioClientForHumioRepository = humio.NewMockClient(humioapi.Cluster{}, nil) - humioClientForHumioView = humio.NewMockClient(humioapi.Cluster{}, nil) + testHumioClient = humio.NewMockClient() } var cfg *rest.Config @@ -164,7 +140,7 @@ var _ = BeforeSuite(func() { err = (&controllers.HumioActionReconciler{ Client: k8sManager.GetClient(), - HumioClient: humioClientForHumioAction, + HumioClient: testHumioClient, BaseLogger: log, Namespace: testProcessNamespace, }).SetupWithManager(k8sManager) @@ -172,7 +148,7 @@ var _ = BeforeSuite(func() { err = (&controllers.HumioAlertReconciler{ Client: k8sManager.GetClient(), - HumioClient: humioClientForHumioAlert, + HumioClient: testHumioClient, BaseLogger: log, Namespace: testProcessNamespace, }).SetupWithManager(k8sManager) @@ -180,7 +156,7 @@ var _ = BeforeSuite(func() { err = (&controllers.HumioClusterReconciler{ Client: k8sManager.GetClient(), - HumioClient: humioClientForHumioCluster, + HumioClient: testHumioClient, BaseLogger: log, Namespace: testProcessNamespace, }).SetupWithManager(k8sManager) @@ -188,7 +164,7 @@ var _ = BeforeSuite(func() { err = (&controllers.HumioExternalClusterReconciler{ Client: k8sManager.GetClient(), - HumioClient: humioClientForHumioExternalCluster, + HumioClient: testHumioClient, BaseLogger: log, Namespace: testProcessNamespace, }).SetupWithManager(k8sManager) @@ -196,7 +172,7 @@ var _ = BeforeSuite(func() { err = (&controllers.HumioIngestTokenReconciler{ Client: k8sManager.GetClient(), - HumioClient: humioClientForHumioIngestToken, + HumioClient: testHumioClient, BaseLogger: log, Namespace: testProcessNamespace, }).SetupWithManager(k8sManager) @@ -204,7 +180,7 @@ var _ = BeforeSuite(func() { err = (&controllers.HumioParserReconciler{ Client: k8sManager.GetClient(), - HumioClient: humioClientForHumioParser, + HumioClient: testHumioClient, BaseLogger: log, Namespace: testProcessNamespace, }).SetupWithManager(k8sManager) @@ -212,7 +188,7 @@ var _ = BeforeSuite(func() { err = (&controllers.HumioRepositoryReconciler{ Client: k8sManager.GetClient(), - HumioClient: humioClientForHumioRepository, + HumioClient: testHumioClient, BaseLogger: log, Namespace: testProcessNamespace, }).SetupWithManager(k8sManager) @@ -220,7 +196,7 @@ var _ = BeforeSuite(func() { err = (&controllers.HumioViewReconciler{ Client: k8sManager.GetClient(), - HumioClient: humioClientForHumioView, + HumioClient: testHumioClient, BaseLogger: log, Namespace: testProcessNamespace, }).SetupWithManager(k8sManager) diff --git a/controllers/suite/resources/humioresources_controller_test.go b/controllers/suite/resources/humioresources_controller_test.go index 1ac42c745..a06ad15c0 100644 --- a/controllers/suite/resources/humioresources_controller_test.go +++ b/controllers/suite/resources/humioresources_controller_test.go @@ -99,7 +99,7 @@ var _ = Describe("Humio Resources Controllers", func() { }, testTimeout, suite.TestInterval).Should(Succeed()) if os.Getenv("TEST_USE_EXISTING_CLUSTER") != "true" { - Expect(string(ingestTokenSecret.Data["token"])).To(Equal("mocktoken")) + Expect(string(ingestTokenSecret.Data["token"])).ToNot(BeEmpty()) } Expect(ingestTokenSecret.OwnerReferences).Should(HaveLen(1)) @@ -153,7 +153,7 @@ var _ = Describe("Humio Resources Controllers", func() { }, testTimeout, suite.TestInterval).Should(Succeed()) if os.Getenv("TEST_USE_EXISTING_CLUSTER") != "true" { - Expect(string(ingestTokenSecret.Data["token"])).To(Equal("mocktoken")) + Expect(string(ingestTokenSecret.Data["token"])).ToNot(BeEmpty()) } suite.UsingClusterBy(clusterKey.Name, "HumioIngestToken: Successfully deleting it") @@ -224,7 +224,7 @@ var _ = Describe("Humio Resources Controllers", func() { Expect(ingestTokenSecret.Labels).Should(HaveKeyWithValue("custom-label", "custom-value")) if os.Getenv("TEST_USE_EXISTING_CLUSTER") != "true" { - Expect(string(ingestTokenSecret.Data["token"])).To(Equal("mocktoken")) + Expect(string(ingestTokenSecret.Data["token"])).ToNot(BeEmpty()) } suite.UsingClusterBy(clusterKey.Name, "HumioIngestToken: Successfully deleting it") @@ -609,20 +609,8 @@ var _ = Describe("Humio Resources Controllers", func() { }, testTimeout, suite.TestInterval).Should(Succeed()) Expect(initialParser).ToNot(BeNil()) - expectedInitialParser := humioapi.Parser{ - Name: spec.Name, - Script: spec.ParserScript, - FieldsToTag: spec.TagFields, - FieldsToBeRemovedBeforeParsing: []string{}, - } - expectedInitialParser.TestCases = make([]humioapi.ParserTestCase, len(spec.TestData)) - for i := range spec.TestData { - expectedInitialParser.TestCases[i] = humioapi.ParserTestCase{ - Event: humioapi.ParserTestEvent{RawString: spec.TestData[i]}, - Assertions: []humioapi.ParserTestCaseAssertions{}, - } - } - Expect(*initialParser).To(Equal(expectedInitialParser)) + expectedInitialParser := humio.ParserTransform(toCreateParser) + Expect(*initialParser).To(Equal(*expectedInitialParser)) suite.UsingClusterBy(clusterKey.Name, "HumioParser: Updating the parser successfully") updatedScript := "kvParse() | updated" @@ -643,19 +631,7 @@ var _ = Describe("Humio Resources Controllers", func() { }, testTimeout, suite.TestInterval).Should(Succeed()) Expect(updatedParser).ToNot(BeNil()) - expectedUpdatedParser := humioapi.Parser{ - Name: spec.Name, - Script: updatedScript, - FieldsToTag: spec.TagFields, - FieldsToBeRemovedBeforeParsing: []string{}, - } - expectedUpdatedParser.TestCases = make([]humioapi.ParserTestCase, len(spec.TestData)) - for i := range spec.TestData { - expectedUpdatedParser.TestCases[i] = humioapi.ParserTestCase{ - Event: humioapi.ParserTestEvent{RawString: spec.TestData[i]}, - Assertions: []humioapi.ParserTestCaseAssertions{}, - } - } + expectedUpdatedParser := *humio.ParserTransform(fetchedParser) Eventually(func() humioapi.Parser { updatedParser, err := humioClient.GetParser(sharedCluster.Config(), reconcile.Request{NamespacedName: clusterKey}, fetchedParser) if err != nil { @@ -2780,8 +2756,7 @@ var _ = Describe("Humio Resources Controllers", func() { return err }, testTimeout, suite.TestInterval).Should(Succeed()) - originalAlert, err := humio.AlertTransform(toCreateAlert, actionIdMap) - Expect(err).To(BeNil()) + originalAlert := humio.AlertTransform(toCreateAlert, actionIdMap) Expect(alert.Name).To(Equal(originalAlert.Name)) Expect(alert.Description).To(Equal(originalAlert.Description)) Expect(alert.Actions).To(Equal(originalAlert.Actions)) @@ -2821,8 +2796,7 @@ var _ = Describe("Humio Resources Controllers", func() { Expect(expectedUpdatedAlert).ToNot(BeNil()) suite.UsingClusterBy(clusterKey.Name, "HumioAlert: Verifying the alert matches the expected") - verifiedAlert, err := humio.AlertTransform(updatedAlert, actionIdMap) - Expect(err).To(BeNil()) + verifiedAlert := humio.AlertTransform(updatedAlert, actionIdMap) Eventually(func() humioapi.Alert { updatedAlert, err := humioClient.GetAlert(sharedCluster.Config(), reconcile.Request{NamespacedName: clusterKey}, fetchedAlert) if err != nil { @@ -2954,8 +2928,7 @@ var _ = Describe("Humio Resources Controllers", func() { return humioClient.ValidateActionsForFilterAlert(sharedCluster.Config(), reconcile.Request{NamespacedName: clusterKey}, toCreateFilterAlert) }, testTimeout, suite.TestInterval).Should(Succeed()) - originalFilterAlert, err := humio.FilterAlertTransform(toCreateFilterAlert) - Expect(err).To(BeNil()) + originalFilterAlert := humio.FilterAlertTransform(toCreateFilterAlert) Expect(filterAlert.Name).To(Equal(originalFilterAlert.Name)) Expect(filterAlert.Description).To(Equal(originalFilterAlert.Description)) Expect(filterAlert.ThrottleTimeSeconds).To(Equal(originalFilterAlert.ThrottleTimeSeconds)) @@ -2966,8 +2939,7 @@ var _ = Describe("Humio Resources Controllers", func() { Expect(filterAlert.QueryString).To(Equal(originalFilterAlert.QueryString)) createdFilterAlert := toCreateFilterAlert - err = humio.FilterAlertHydrate(createdFilterAlert, filterAlert) - Expect(err).To(BeNil()) + humio.FilterAlertHydrate(createdFilterAlert, filterAlert) Expect(createdFilterAlert.Spec).To(Equal(toCreateFilterAlert.Spec)) suite.UsingClusterBy(clusterKey.Name, "HumioFilterAlert: Updating the filter alert successfully") @@ -2999,11 +2971,10 @@ var _ = Describe("Humio Resources Controllers", func() { Expect(expectedUpdatedFilterAlert).ToNot(BeNil()) suite.UsingClusterBy(clusterKey.Name, "HumioFilterAlert: Verifying the alert matches the expected") - verifiedFilterAlert, err := humio.FilterAlertTransform(updatedFilterAlert) + verifiedFilterAlert := humio.FilterAlertTransform(updatedFilterAlert) verifiedFilterAlert.ID = "" verifiedFilterAlert.RunAsUserID = "" - Expect(err).To(BeNil()) Eventually(func() humioapi.FilterAlert { updatedFilterAlert, err := humioClient.GetFilterAlert(sharedCluster.Config(), reconcile.Request{NamespacedName: clusterKey}, fetchedFilterAlert) if err != nil { @@ -3139,8 +3110,7 @@ var _ = Describe("Humio Resources Controllers", func() { return humioClient.ValidateActionsForScheduledSearch(sharedCluster.Config(), reconcile.Request{NamespacedName: clusterKey}, toCreateScheduledSearch) }, testTimeout, suite.TestInterval).Should(Succeed()) - originalScheduledSearch, err := humio.ScheduledSearchTransform(toCreateScheduledSearch) - Expect(err).To(BeNil()) + originalScheduledSearch := humio.ScheduledSearchTransform(toCreateScheduledSearch) Expect(scheduledSearch.Name).To(Equal(originalScheduledSearch.Name)) Expect(scheduledSearch.Description).To(Equal(originalScheduledSearch.Description)) Expect(scheduledSearch.ActionNames).To(Equal(originalScheduledSearch.ActionNames)) @@ -3154,8 +3124,7 @@ var _ = Describe("Humio Resources Controllers", func() { Expect(scheduledSearch.BackfillLimit).To(Equal(originalScheduledSearch.BackfillLimit)) createdScheduledSearch := toCreateScheduledSearch - err = humio.ScheduledSearchHydrate(createdScheduledSearch, scheduledSearch) - Expect(err).To(BeNil()) + humio.ScheduledSearchHydrate(createdScheduledSearch, scheduledSearch) Expect(createdScheduledSearch.Spec).To(Equal(toCreateScheduledSearch.Spec)) suite.UsingClusterBy(clusterKey.Name, "HumioScheduledSearch: Updating the scheduled search successfully") @@ -3193,11 +3162,10 @@ var _ = Describe("Humio Resources Controllers", func() { Expect(expectedUpdatedScheduledSearch).ToNot(BeNil()) suite.UsingClusterBy(clusterKey.Name, "HumioScheduledSearch: Verifying the scheduled search matches the expected") - verifiedScheduledSearch, err := humio.ScheduledSearchTransform(updatedScheduledSearch) + verifiedScheduledSearch := humio.ScheduledSearchTransform(updatedScheduledSearch) verifiedScheduledSearch.ID = "" verifiedScheduledSearch.RunAsUserID = "" - Expect(err).To(BeNil()) Eventually(func() humioapi.ScheduledSearch { updatedScheduledSearch, err := humioClient.GetScheduledSearch(sharedCluster.Config(), reconcile.Request{NamespacedName: clusterKey}, fetchedScheduledSearch) if err != nil { diff --git a/controllers/suite/resources/suite_test.go b/controllers/suite/resources/suite_test.go index b204a08b0..12a89666f 100644 --- a/controllers/suite/resources/suite_test.go +++ b/controllers/suite/resources/suite_test.go @@ -111,7 +111,7 @@ var _ = BeforeSuite(func() { CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")}, ErrorIfCRDPathMissing: true, } - humioClient = humio.NewMockClient(humioapi.Cluster{}, nil) + humioClient = humio.NewMockClient() } var cfg *rest.Config diff --git a/go.mod b/go.mod index 98a934cc2..557ed67a5 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/go-logr/logr v1.4.2 github.com/go-logr/zapr v1.3.0 github.com/google/go-cmp v0.6.0 - github.com/humio/cli v0.35.2-0.20240712113350-4d23462e72b6 + github.com/humio/cli v0.36.1-0.20240813114317-eafdd46e889a github.com/onsi/ginkgo/v2 v2.20.0 github.com/onsi/gomega v1.34.1 github.com/prometheus/client_golang v1.19.0 diff --git a/go.sum b/go.sum index 030103fcc..f97a105cc 100644 --- a/go.sum +++ b/go.sum @@ -52,8 +52,8 @@ github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 h1:FKHo8hFI3A+7w0aUQu github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/humio/cli v0.35.2-0.20240712113350-4d23462e72b6 h1:6lMszpwioB+ANZyEpwpr8iud7S86q/VfIRAoEM8KUkY= -github.com/humio/cli v0.35.2-0.20240712113350-4d23462e72b6/go.mod h1:Du1GCeQ65rVrUQX/ge45RFflX+I3ZLU3sdCM8kHpuq8= +github.com/humio/cli v0.36.1-0.20240813114317-eafdd46e889a h1:1SegzWT5U+6xslFrJgRnMbMVnSkQj19a6tl5u+nRB+A= +github.com/humio/cli v0.36.1-0.20240813114317-eafdd46e889a/go.mod h1:Du1GCeQ65rVrUQX/ge45RFflX+I3ZLU3sdCM8kHpuq8= github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4= github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= diff --git a/pkg/humio/action_transform.go b/pkg/humio/action_transform.go index f15fa2b0e..e8e43e13c 100644 --- a/pkg/humio/action_transform.go +++ b/pkg/humio/action_transform.go @@ -29,8 +29,6 @@ import ( ) const ( - ActionIdentifierAnnotation = "humio.com/action-id" - ActionTypeWebhook = "Webhook" ActionTypeSlack = "Slack" ActionTypeSlackPostMessage = "SlackPostMessage" @@ -389,9 +387,6 @@ func baseAction(ha *humiov1alpha1.HumioAction) (*humioapi.Action, error) { action := &humioapi.Action{ Name: ha.Spec.Name, } - if _, ok := ha.ObjectMeta.Annotations[ActionIdentifierAnnotation]; ok { - action.ID = ha.ObjectMeta.Annotations[ActionIdentifierAnnotation] - } return action, nil } diff --git a/pkg/humio/alert_transform.go b/pkg/humio/alert_transform.go index 512542ba3..93bea4468 100644 --- a/pkg/humio/alert_transform.go +++ b/pkg/humio/alert_transform.go @@ -3,14 +3,9 @@ package humio import ( humioapi "github.com/humio/cli/api" humiov1alpha1 "github.com/humio/humio-operator/api/v1alpha1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -const ( - AlertIdentifierAnnotation = "humio.com/alert-id" -) - -func AlertTransform(ha *humiov1alpha1.HumioAlert, actionIdMap map[string]string) (*humioapi.Alert, error) { +func AlertTransform(ha *humiov1alpha1.HumioAlert, actionIdMap map[string]string) *humioapi.Alert { alert := &humioapi.Alert{ Name: ha.Spec.Name, QueryString: ha.Spec.Query.QueryString, @@ -27,14 +22,10 @@ func AlertTransform(ha *humiov1alpha1.HumioAlert, actionIdMap map[string]string) alert.QueryStart = "1d" } - if _, ok := ha.ObjectMeta.Annotations[AlertIdentifierAnnotation]; ok { - alert.ID = ha.ObjectMeta.Annotations[AlertIdentifierAnnotation] - } - - return alert, nil + return alert } -func AlertHydrate(ha *humiov1alpha1.HumioAlert, alert *humioapi.Alert, actionIdMap map[string]string) error { +func AlertHydrate(ha *humiov1alpha1.HumioAlert, alert *humioapi.Alert, actionIdMap map[string]string) { ha.Spec = humiov1alpha1.HumioAlertSpec{ Name: alert.Name, Query: humiov1alpha1.HumioQuery{ @@ -49,13 +40,7 @@ func AlertHydrate(ha *humiov1alpha1.HumioAlert, alert *humioapi.Alert, actionIdM Labels: alert.Labels, } - ha.ObjectMeta = metav1.ObjectMeta{ - Annotations: map[string]string{ - AlertIdentifierAnnotation: alert.ID, - }, - } - - return nil + return } func actionIdsFromActionMap(actionList []string, actionIdMap map[string]string) []string { diff --git a/pkg/humio/client.go b/pkg/humio/client.go index efc868834..87ac4575f 100644 --- a/pkg/humio/client.go +++ b/pkg/humio/client.go @@ -252,16 +252,7 @@ func (h *ClientConfig) AddIngestToken(config *humioapi.Config, req reconcile.Req } func (h *ClientConfig) GetIngestToken(config *humioapi.Config, req reconcile.Request, hit *humiov1alpha1.HumioIngestToken) (*humioapi.IngestToken, error) { - tokens, err := h.GetHumioClient(config, req).IngestTokens().List(hit.Spec.RepositoryName) - if err != nil { - return &humioapi.IngestToken{}, err - } - for _, token := range tokens { - if token.Name == hit.Spec.Name { - return &token, nil - } - } - return &humioapi.IngestToken{}, nil + return h.GetHumioClient(config, req).IngestTokens().Get(hit.Spec.RepositoryName, hit.Spec.Name) } func (h *ClientConfig) UpdateIngestToken(config *humioapi.Config, req reconcile.Request, hit *humiov1alpha1.HumioIngestToken) (*humioapi.IngestToken, error) { @@ -414,6 +405,10 @@ func (h *ClientConfig) UpdateRepository(config *humioapi.Config, req reconcile.R } func (h *ClientConfig) DeleteRepository(config *humioapi.Config, req reconcile.Request, hr *humiov1alpha1.HumioRepository) error { + _, err := h.GetRepository(config, req, hr) + if errors.As(err, &humioapi.EntityNotFound{}) { + return nil + } // TODO: perhaps we should allow calls to DeleteRepository() to include the reason instead of hardcoding it return h.GetHumioClient(config, req).Repositories().Delete( hr.Spec.Name, @@ -494,6 +489,10 @@ func (h *ClientConfig) UpdateView(config *humioapi.Config, req reconcile.Request } func (h *ClientConfig) DeleteView(config *humioapi.Config, req reconcile.Request, hv *humiov1alpha1.HumioView) error { + _, err := h.GetView(config, req, hv) + if errors.As(err, &humioapi.EntityNotFound{}) { + return nil + } return h.GetHumioClient(config, req).Views().Delete(hv.Spec.Name, "Deleted by humio-operator") } @@ -525,7 +524,7 @@ func (h *ClientConfig) GetAction(config *humioapi.Config, req reconcile.Request, action, err := h.GetHumioClient(config, req).Actions().Get(ha.Spec.ViewName, ha.Spec.Name) if err != nil { - return action, fmt.Errorf("error when trying to get action %+v, name=%s, view=%s: %w", action, ha.Spec.Name, ha.Spec.ViewName, err) + return nil, fmt.Errorf("error when trying to get action %+v, name=%s, view=%s: %w", action, ha.Spec.Name, ha.Spec.ViewName, err) } if action == nil || action.Name == "" { @@ -564,6 +563,12 @@ func (h *ClientConfig) UpdateAction(config *humioapi.Config, req reconcile.Reque return action, err } + currentAction, err := h.GetAction(config, req, ha) + if err != nil { + return nil, fmt.Errorf("could not find action with name: %q", ha.Spec.Name) + } + action.ID = currentAction.ID + return h.GetHumioClient(config, req).Actions().Update(ha.Spec.ViewName, action) } @@ -622,11 +627,8 @@ func (h *ClientConfig) AddAlert(config *humioapi.Config, req reconcile.Request, if err != nil { return &humioapi.Alert{}, fmt.Errorf("could not get action id mapping: %w", err) } - alert, err := AlertTransform(ha, actionIdMap) - if err != nil { - return alert, err - } + alert := AlertTransform(ha, actionIdMap) createdAlert, err := h.GetHumioClient(config, req).Alerts().Add(ha.Spec.ViewName, alert) if err != nil { return createdAlert, fmt.Errorf("got error when attempting to add alert: %w, alert: %#v", err, *alert) @@ -644,11 +646,8 @@ func (h *ClientConfig) UpdateAlert(config *humioapi.Config, req reconcile.Reques if err != nil { return &humioapi.Alert{}, fmt.Errorf("could not get action id mapping: %w", err) } - alert, err := AlertTransform(ha, actionIdMap) - if err != nil { - return alert, err - } + alert := AlertTransform(ha, actionIdMap) currentAlert, err := h.GetAlert(config, req, ha) if err != nil { return &humioapi.Alert{}, fmt.Errorf("could not find alert with name: %q", alert.Name) @@ -705,11 +704,8 @@ func (h *ClientConfig) AddFilterAlert(config *humioapi.Config, req reconcile.Req if err = h.ValidateActionsForFilterAlert(config, req, hfa); err != nil { return &humioapi.FilterAlert{}, fmt.Errorf("could not get action id mapping: %w", err) } - filterAlert, err := FilterAlertTransform(hfa) - if err != nil { - return filterAlert, err - } + filterAlert := FilterAlertTransform(hfa) createdAlert, err := h.GetHumioClient(config, req).FilterAlerts().Create(hfa.Spec.ViewName, filterAlert) if err != nil { return createdAlert, fmt.Errorf("got error when attempting to add filter alert: %w, filteralert: %#v", err, *filterAlert) @@ -725,11 +721,8 @@ func (h *ClientConfig) UpdateFilterAlert(config *humioapi.Config, req reconcile. if err = h.ValidateActionsForFilterAlert(config, req, hfa); err != nil { return &humioapi.FilterAlert{}, fmt.Errorf("could not get action id mapping: %w", err) } - filterAlert, err := FilterAlertTransform(hfa) - if err != nil { - return filterAlert, err - } + filterAlert := FilterAlertTransform(hfa) currentAlert, err := h.GetFilterAlert(config, req, hfa) if err != nil { return &humioapi.FilterAlert{}, fmt.Errorf("could not find filter alert with name: %q", filterAlert.Name) @@ -758,10 +751,7 @@ func (h *ClientConfig) AddScheduledSearch(config *humioapi.Config, req reconcile if err = h.ValidateActionsForScheduledSearch(config, req, hss); err != nil { return &humioapi.ScheduledSearch{}, fmt.Errorf("could not get action id mapping: %w", err) } - scheduledSearch, err := ScheduledSearchTransform(hss) - if err != nil { - return scheduledSearch, err - } + scheduledSearch := ScheduledSearchTransform(hss) createdScheduledSearch, err := h.GetHumioClient(config, req).ScheduledSearches().Create(hss.Spec.ViewName, scheduledSearch) if err != nil { @@ -809,10 +799,7 @@ func (h *ClientConfig) UpdateScheduledSearch(config *humioapi.Config, req reconc if err = h.ValidateActionsForScheduledSearch(config, req, hss); err != nil { return &humioapi.ScheduledSearch{}, fmt.Errorf("could not get action id mapping: %w", err) } - scheduledSearch, err := ScheduledSearchTransform(hss) - if err != nil { - return scheduledSearch, err - } + scheduledSearch := ScheduledSearchTransform(hss) currentScheduledSearch, err := h.GetScheduledSearch(config, req, hss) if err != nil { diff --git a/pkg/humio/client_mock.go b/pkg/humio/client_mock.go index 1e0164a9d..39e3e861c 100644 --- a/pkg/humio/client_mock.go +++ b/pkg/humio/client_mock.go @@ -20,49 +20,64 @@ import ( "crypto/sha512" "encoding/hex" "fmt" + "github.com/humio/humio-operator/pkg/helpers" "net/url" - "reflect" + "sync" humioapi "github.com/humio/cli/api" humiov1alpha1 "github.com/humio/humio-operator/api/v1alpha1" - "github.com/humio/humio-operator/pkg/helpers" "github.com/humio/humio-operator/pkg/kubernetes" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/reconcile" ) +var ( + humioClientMu sync.Mutex +) + +type resourceKey struct { + // clusterName holds the value of the cluster + clusterName string + + // searchDomainName is the name of the repository or view + searchDomainName string + + // resourceName is the name of resource, like IngestToken, Parser, etc. + resourceName string +} + type ClientMock struct { - Cluster humioapi.Cluster - ClusterError error - IngestToken humioapi.IngestToken - Parser humioapi.Parser - Repository humioapi.Repository - View humioapi.View - OnPremLicense humioapi.OnPremLicense - Action humioapi.Action - Alert humioapi.Alert - FilterAlert humioapi.FilterAlert - ScheduledSearch humioapi.ScheduledSearch + OnPremLicense map[resourceKey]humioapi.OnPremLicense + + Repository map[resourceKey]humioapi.Repository + View map[resourceKey]humioapi.View + + IngestToken map[resourceKey]humioapi.IngestToken + Parser map[resourceKey]humioapi.Parser + Action map[resourceKey]humioapi.Action + Alert map[resourceKey]humioapi.Alert + FilterAlert map[resourceKey]humioapi.FilterAlert + ScheduledSearch map[resourceKey]humioapi.ScheduledSearch } type MockClientConfig struct { apiClient *ClientMock } -func NewMockClient(cluster humioapi.Cluster, clusterError error) *MockClientConfig { +func NewMockClient() *MockClientConfig { mockClientConfig := &MockClientConfig{ apiClient: &ClientMock{ - Cluster: cluster, - ClusterError: clusterError, - IngestToken: humioapi.IngestToken{}, - Parser: humioapi.Parser{}, - Repository: humioapi.Repository{}, - View: humioapi.View{}, - OnPremLicense: humioapi.OnPremLicense{}, - Action: humioapi.Action{}, - Alert: humioapi.Alert{}, - FilterAlert: humioapi.FilterAlert{}, - ScheduledSearch: humioapi.ScheduledSearch{}, + OnPremLicense: map[resourceKey]humioapi.OnPremLicense{}, + + Repository: make(map[resourceKey]humioapi.Repository), + View: make(map[resourceKey]humioapi.View), + + IngestToken: make(map[resourceKey]humioapi.IngestToken), + Parser: make(map[resourceKey]humioapi.Parser), + Action: make(map[resourceKey]humioapi.Action), + Alert: make(map[resourceKey]humioapi.Alert), + FilterAlert: make(map[resourceKey]humioapi.FilterAlert), + ScheduledSearch: make(map[resourceKey]humioapi.ScheduledSearch), }, } @@ -77,10 +92,7 @@ func (h *MockClientConfig) Status(config *humioapi.Config, req reconcile.Request } func (h *MockClientConfig) GetClusters(config *humioapi.Config, req reconcile.Request) (humioapi.Cluster, error) { - if h.apiClient.ClusterError != nil { - return humioapi.Cluster{}, h.apiClient.ClusterError - } - return h.apiClient.Cluster, nil + return humioapi.Cluster{}, fmt.Errorf("not implemented") } func (h *MockClientConfig) GetBaseURL(config *humioapi.Config, req reconcile.Request, hc *humiov1alpha1.HumioCluster) *url.URL { @@ -93,96 +105,262 @@ func (h *MockClientConfig) TestAPIToken(config *humioapi.Config, req reconcile.R } func (h *MockClientConfig) AddIngestToken(config *humioapi.Config, req reconcile.Request, hit *humiov1alpha1.HumioIngestToken) (*humioapi.IngestToken, error) { - h.apiClient.IngestToken = humioapi.IngestToken{ - Name: hit.Spec.Name, - AssignedParser: hit.Spec.ParserName, - Token: "mocktoken", + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hit.Spec.ManagedClusterName, hit.Spec.ExternalClusterName), + searchDomainName: hit.Spec.RepositoryName, + resourceName: hit.Spec.Name, + } + + if _, found := h.apiClient.IngestToken[key]; found { + return nil, fmt.Errorf("ingest token already exists with name %s", hit.Spec.Name) } - return &h.apiClient.IngestToken, nil + + value := IngestTokenTransform(hit) + if value.Token == "" { + value.Token = kubernetes.RandomString() + } + h.apiClient.IngestToken[key] = *value + return value, nil } func (h *MockClientConfig) GetIngestToken(config *humioapi.Config, req reconcile.Request, hit *humiov1alpha1.HumioIngestToken) (*humioapi.IngestToken, error) { - return &h.apiClient.IngestToken, nil + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hit.Spec.ManagedClusterName, hit.Spec.ExternalClusterName), + searchDomainName: hit.Spec.RepositoryName, + resourceName: hit.Spec.Name, + } + if value, found := h.apiClient.IngestToken[key]; found { + return &value, nil + + } + return nil, fmt.Errorf("could not find ingest token in repository %s with name %s, err=%w", hit.Spec.RepositoryName, hit.Spec.Name, humioapi.EntityNotFound{}) } func (h *MockClientConfig) UpdateIngestToken(config *humioapi.Config, req reconcile.Request, hit *humiov1alpha1.HumioIngestToken) (*humioapi.IngestToken, error) { - return h.AddIngestToken(config, req, hit) + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hit.Spec.ManagedClusterName, hit.Spec.ExternalClusterName), + searchDomainName: hit.Spec.RepositoryName, + resourceName: hit.Spec.Name, + } + + if _, found := h.apiClient.IngestToken[key]; !found { + return nil, fmt.Errorf("ingest token not found with name %s, err=%w", hit.Spec.Name, humioapi.EntityNotFound{}) + } + + value := IngestTokenTransform(hit) + if value.Token == "" { + value.Token = h.apiClient.IngestToken[key].Token + } + h.apiClient.IngestToken[key] = *value + return value, nil } func (h *MockClientConfig) DeleteIngestToken(config *humioapi.Config, req reconcile.Request, hit *humiov1alpha1.HumioIngestToken) error { - h.apiClient.IngestToken = humioapi.IngestToken{} + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hit.Spec.ManagedClusterName, hit.Spec.ExternalClusterName), + searchDomainName: hit.Spec.RepositoryName, + resourceName: hit.Spec.Name, + } + + delete(h.apiClient.IngestToken, key) return nil } func (h *MockClientConfig) AddParser(config *humioapi.Config, req reconcile.Request, hp *humiov1alpha1.HumioParser) (*humioapi.Parser, error) { - h.apiClient.Parser = humioapi.Parser{ - Name: hp.Spec.Name, - Script: hp.Spec.ParserScript, - FieldsToTag: hp.Spec.TagFields, - FieldsToBeRemovedBeforeParsing: []string{}, + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hp.Spec.ManagedClusterName, hp.Spec.ExternalClusterName), + searchDomainName: hp.Spec.RepositoryName, + resourceName: hp.Spec.Name, } - testCasesGQL := make([]humioapi.ParserTestCase, len(hp.Spec.TestData)) - for i := range hp.Spec.TestData { - testCasesGQL[i] = humioapi.ParserTestCase{ - Event: humioapi.ParserTestEvent{RawString: hp.Spec.TestData[i]}, - Assertions: []humioapi.ParserTestCaseAssertions{}, - } + if _, found := h.apiClient.Parser[key]; found { + return nil, fmt.Errorf("parser already exists with name %s", hp.Spec.Name) } - h.apiClient.Parser.TestCases = testCasesGQL - return &h.apiClient.Parser, nil + value := ParserTransform(hp) + if value.ID == "" { + value.ID = kubernetes.RandomString() + } + h.apiClient.Parser[key] = *value + return value, nil } func (h *MockClientConfig) GetParser(config *humioapi.Config, req reconcile.Request, hp *humiov1alpha1.HumioParser) (*humioapi.Parser, error) { - if h.apiClient.Parser.Name == "" { - return nil, fmt.Errorf("could not find parser in view %q with name %q, err=%w", hp.Spec.RepositoryName, hp.Spec.Name, humioapi.EntityNotFound{}) + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hp.Spec.ManagedClusterName, hp.Spec.ExternalClusterName), + searchDomainName: hp.Spec.RepositoryName, + resourceName: hp.Spec.Name, } + if value, found := h.apiClient.Parser[key]; found { + return &value, nil - return &h.apiClient.Parser, nil + } + return nil, fmt.Errorf("could not find parser in repository %s with name %s, err=%w", hp.Spec.RepositoryName, hp.Spec.Name, humioapi.EntityNotFound{}) } func (h *MockClientConfig) UpdateParser(config *humioapi.Config, req reconcile.Request, hp *humiov1alpha1.HumioParser) (*humioapi.Parser, error) { - return h.AddParser(config, req, hp) + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hp.Spec.ManagedClusterName, hp.Spec.ExternalClusterName), + searchDomainName: hp.Spec.RepositoryName, + resourceName: hp.Spec.Name, + } + + if _, found := h.apiClient.Parser[key]; !found { + return nil, fmt.Errorf("parser not found with name %s, err=%w", hp.Spec.Name, humioapi.EntityNotFound{}) + } + + value := ParserTransform(hp) + + h.apiClient.Parser[key] = *value + return value, nil } func (h *MockClientConfig) DeleteParser(config *humioapi.Config, req reconcile.Request, hp *humiov1alpha1.HumioParser) error { - h.apiClient.Parser = humioapi.Parser{} + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hp.Spec.ManagedClusterName, hp.Spec.ExternalClusterName), + searchDomainName: hp.Spec.RepositoryName, + resourceName: hp.Spec.Name, + } + + delete(h.apiClient.Parser, key) return nil } func (h *MockClientConfig) AddRepository(config *humioapi.Config, req reconcile.Request, hr *humiov1alpha1.HumioRepository) (*humioapi.Repository, error) { - h.apiClient.Repository = humioapi.Repository{ + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hr.Spec.ManagedClusterName, hr.Spec.ExternalClusterName), + resourceName: hr.Spec.Name, + } + + if _, found := h.apiClient.Repository[key]; found { + return nil, fmt.Errorf("repository already exists with name %s", hr.Spec.Name) + } + + value := &humioapi.Repository{ ID: kubernetes.RandomString(), Name: hr.Spec.Name, Description: hr.Spec.Description, RetentionDays: float64(hr.Spec.Retention.TimeInDays), IngestRetentionSizeGB: float64(hr.Spec.Retention.IngestSizeInGB), StorageRetentionSizeGB: float64(hr.Spec.Retention.StorageSizeInGB), + AutomaticSearch: helpers.BoolTrue(hr.Spec.AutomaticSearch), } - return &h.apiClient.Repository, nil + + h.apiClient.Repository[key] = *value + return value, nil } func (h *MockClientConfig) GetRepository(config *humioapi.Config, req reconcile.Request, hr *humiov1alpha1.HumioRepository) (*humioapi.Repository, error) { - h.apiClient.Repository.AutomaticSearch = helpers.BoolTrue(hr.Spec.AutomaticSearch) - return &h.apiClient.Repository, nil + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hr.Spec.ManagedClusterName, hr.Spec.ExternalClusterName), + resourceName: hr.Spec.Name, + } + if value, found := h.apiClient.Repository[key]; found { + return &value, nil + + } + return nil, fmt.Errorf("could not find repository with name %s, err=%w", hr.Spec.Name, humioapi.EntityNotFound{}) + } func (h *MockClientConfig) UpdateRepository(config *humioapi.Config, req reconcile.Request, hr *humiov1alpha1.HumioRepository) (*humioapi.Repository, error) { - return h.AddRepository(config, req, hr) + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hr.Spec.ManagedClusterName, hr.Spec.ExternalClusterName), + resourceName: hr.Spec.Name, + } + + if _, found := h.apiClient.Repository[key]; !found { + return nil, fmt.Errorf("repository not found with name %s, err=%w", hr.Spec.Name, humioapi.EntityNotFound{}) + } + + value := &humioapi.Repository{ + ID: kubernetes.RandomString(), + Name: hr.Spec.Name, + Description: hr.Spec.Description, + RetentionDays: float64(hr.Spec.Retention.TimeInDays), + IngestRetentionSizeGB: float64(hr.Spec.Retention.IngestSizeInGB), + StorageRetentionSizeGB: float64(hr.Spec.Retention.StorageSizeInGB), + AutomaticSearch: helpers.BoolTrue(hr.Spec.AutomaticSearch), + } + + h.apiClient.Repository[key] = *value + return value, nil } func (h *MockClientConfig) DeleteRepository(config *humioapi.Config, req reconcile.Request, hr *humiov1alpha1.HumioRepository) error { - h.apiClient.Repository = humioapi.Repository{} + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hr.Spec.ManagedClusterName, hr.Spec.ExternalClusterName), + resourceName: hr.Spec.Name, + } + + delete(h.apiClient.Repository, key) return nil } func (h *MockClientConfig) GetView(config *humioapi.Config, req reconcile.Request, hv *humiov1alpha1.HumioView) (*humioapi.View, error) { - h.apiClient.View.AutomaticSearch = helpers.BoolTrue(hv.Spec.AutomaticSearch) - return &h.apiClient.View, nil + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hv.Spec.ManagedClusterName, hv.Spec.ExternalClusterName), + resourceName: hv.Spec.Name, + } + if value, found := h.apiClient.View[key]; found { + return &value, nil + + } + return nil, fmt.Errorf("could not find view with name %s, err=%w", hv.Spec.Name, humioapi.EntityNotFound{}) } func (h *MockClientConfig) AddView(config *humioapi.Config, req reconcile.Request, hv *humiov1alpha1.HumioView) (*humioapi.View, error) { + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hv.Spec.ManagedClusterName, hv.Spec.ExternalClusterName), + resourceName: hv.Spec.Name, + } + + if _, found := h.apiClient.Repository[key]; found { + return nil, fmt.Errorf("view already exists with name %s", hv.Spec.Name) + } + connections := make([]humioapi.ViewConnection, 0) for _, connection := range hv.Spec.Connections { connections = append(connections, humioapi.ViewConnection{ @@ -191,99 +369,252 @@ func (h *MockClientConfig) AddView(config *humioapi.Config, req reconcile.Reques }) } - h.apiClient.View = humioapi.View{ - Name: hv.Spec.Name, - Description: hv.Spec.Description, - Connections: connections, + value := &humioapi.View{ + Name: hv.Spec.Name, + Description: hv.Spec.Description, + Connections: connections, + AutomaticSearch: helpers.BoolTrue(hv.Spec.AutomaticSearch), } - return &h.apiClient.View, nil + h.apiClient.View[key] = *value + return value, nil } func (h *MockClientConfig) UpdateView(config *humioapi.Config, req reconcile.Request, hv *humiov1alpha1.HumioView) (*humioapi.View, error) { - return h.AddView(config, req, hv) + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hv.Spec.ManagedClusterName, hv.Spec.ExternalClusterName), + resourceName: hv.Spec.Name, + } + + if _, found := h.apiClient.View[key]; !found { + return nil, fmt.Errorf("view not found with name %s, err=%w", hv.Spec.Name, humioapi.EntityNotFound{}) + } + + connections := make([]humioapi.ViewConnection, 0) + for _, connection := range hv.Spec.Connections { + connections = append(connections, humioapi.ViewConnection{ + RepoName: connection.RepositoryName, + Filter: connection.Filter, + }) + } + + value := &humioapi.View{ + Name: hv.Spec.Name, + Description: hv.Spec.Description, + Connections: connections, + AutomaticSearch: helpers.BoolTrue(hv.Spec.AutomaticSearch), + } + h.apiClient.View[key] = *value + return value, nil } func (h *MockClientConfig) DeleteView(config *humioapi.Config, req reconcile.Request, hv *humiov1alpha1.HumioView) error { - h.apiClient.View = humioapi.View{} + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hv.Spec.ManagedClusterName, hv.Spec.ExternalClusterName), + resourceName: hv.Spec.Name, + } + + delete(h.apiClient.View, key) return nil } func (h *MockClientConfig) GetLicense(config *humioapi.Config, req reconcile.Request) (humioapi.License, error) { - emptyOnPremLicense := humioapi.OnPremLicense{} + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + resourceName: fmt.Sprintf("%s%s", req.Namespace, req.Name), + } + + if value, found := h.apiClient.OnPremLicense[key]; found { + return &value, nil - if !reflect.DeepEqual(h.apiClient.OnPremLicense, emptyOnPremLicense) { - return h.apiClient.OnPremLicense, nil } - // by default, humio starts without a license - return emptyOnPremLicense, nil + return humioapi.OnPremLicense{}, nil } func (h *MockClientConfig) InstallLicense(config *humioapi.Config, req reconcile.Request, licenseString string) error { + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + resourceName: fmt.Sprintf("%s%s", req.Namespace, req.Name), + } + onPremLicense, err := ParseLicenseType(licenseString) if err != nil { return fmt.Errorf("failed to parse license type: %w", err) } - if onPremLicense != nil { - h.apiClient.OnPremLicense = *onPremLicense - } - + h.apiClient.OnPremLicense[key] = *onPremLicense return nil } func (h *MockClientConfig) GetAction(config *humioapi.Config, req reconcile.Request, ha *humiov1alpha1.HumioAction) (*humioapi.Action, error) { - if h.apiClient.Action.Name == "" { - return nil, fmt.Errorf("could not find action in view %q with name %q, err=%w", ha.Spec.ViewName, ha.Spec.Name, humioapi.EntityNotFound{}) + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", ha.Spec.ManagedClusterName, ha.Spec.ExternalClusterName), + searchDomainName: ha.Spec.ViewName, + resourceName: ha.Spec.Name, } + if value, found := h.apiClient.Action[key]; found { + return &value, nil - return &h.apiClient.Action, nil + } + return nil, fmt.Errorf("could not find action in view %q with name %q, err=%w", ha.Spec.ViewName, ha.Spec.Name, humioapi.EntityNotFound{}) } func (h *MockClientConfig) AddAction(config *humioapi.Config, req reconcile.Request, ha *humiov1alpha1.HumioAction) (*humioapi.Action, error) { + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", ha.Spec.ManagedClusterName, ha.Spec.ExternalClusterName), + searchDomainName: ha.Spec.ViewName, + resourceName: ha.Spec.Name, + } + + if _, found := h.apiClient.Action[key]; found { + return nil, fmt.Errorf("action already exists with name %s", ha.Spec.Name) + } + action, err := ActionFromActionCR(ha) if err != nil { - return action, err + return nil, err } - h.apiClient.Action = *action - return &h.apiClient.Action, nil + action.ID = kubernetes.RandomString() + + h.apiClient.Action[key] = *action + return action, nil } func (h *MockClientConfig) UpdateAction(config *humioapi.Config, req reconcile.Request, ha *humiov1alpha1.HumioAction) (*humioapi.Action, error) { - return h.AddAction(config, req, ha) + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", ha.Spec.ManagedClusterName, ha.Spec.ExternalClusterName), + searchDomainName: ha.Spec.ViewName, + resourceName: ha.Spec.Name, + } + + currentAction, found := h.apiClient.Action[key] + + if !found { + return nil, fmt.Errorf("could not find action in view %q with name %q, err=%w", ha.Spec.ViewName, ha.Spec.Name, humioapi.EntityNotFound{}) + } + + action, err := ActionFromActionCR(ha) + if err != nil { + return nil, err + } + action.ID = currentAction.ID + + h.apiClient.Action[key] = *action + return action, nil } func (h *MockClientConfig) DeleteAction(config *humioapi.Config, req reconcile.Request, ha *humiov1alpha1.HumioAction) error { - h.apiClient.Action = humioapi.Action{} + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", ha.Spec.ManagedClusterName, ha.Spec.ExternalClusterName), + searchDomainName: ha.Spec.ViewName, + resourceName: ha.Spec.Name, + } + + delete(h.apiClient.Action, key) return nil } func (h *MockClientConfig) GetAlert(config *humioapi.Config, req reconcile.Request, ha *humiov1alpha1.HumioAlert) (*humioapi.Alert, error) { - if h.apiClient.Alert.Name == "" { - return nil, fmt.Errorf("could not find alert in view %q with name %q, err=%w", ha.Spec.ViewName, ha.Spec.Name, humioapi.EntityNotFound{}) + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", ha.Spec.ManagedClusterName, ha.Spec.ExternalClusterName), + searchDomainName: ha.Spec.ViewName, + resourceName: ha.Spec.Name, } - return &h.apiClient.Alert, nil + if value, found := h.apiClient.Alert[key]; found { + return &value, nil + + } + return nil, fmt.Errorf("could not find alert in view %q with name %q, err=%w", ha.Spec.ViewName, ha.Spec.Name, humioapi.EntityNotFound{}) } func (h *MockClientConfig) AddAlert(config *humioapi.Config, req reconcile.Request, ha *humiov1alpha1.HumioAlert) (*humioapi.Alert, error) { - actionIdMap, err := h.GetActionIDsMapForAlerts(config, req, ha) - if err != nil { - return &humioapi.Alert{}, fmt.Errorf("could not get action id mapping: %w", err) + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", ha.Spec.ManagedClusterName, ha.Spec.ExternalClusterName), + searchDomainName: ha.Spec.ViewName, + resourceName: ha.Spec.Name, + } + + if _, found := h.apiClient.Alert[key]; found { + return nil, fmt.Errorf("alert already exists with name %s", ha.Spec.Name) } - alert, err := AlertTransform(ha, actionIdMap) + actionIdMap, err := h.GetActionIDsMapForAlerts(config, req, ha) if err != nil { - return alert, err + return nil, fmt.Errorf("could not get action id mapping: %w", err) } - h.apiClient.Alert = *alert - return &h.apiClient.Alert, nil + + value := AlertTransform(ha, actionIdMap) + value.ID = kubernetes.RandomString() + + h.apiClient.Alert[key] = *value + return value, nil } func (h *MockClientConfig) UpdateAlert(config *humioapi.Config, req reconcile.Request, ha *humiov1alpha1.HumioAlert) (*humioapi.Alert, error) { - return h.AddAlert(config, req, ha) + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", ha.Spec.ManagedClusterName, ha.Spec.ExternalClusterName), + searchDomainName: ha.Spec.ViewName, + resourceName: ha.Spec.Name, + } + + currentAlert, found := h.apiClient.Alert[key] + + if !found { + return nil, fmt.Errorf("alert not found with name %s, err=%w", ha.Spec.Name, humioapi.EntityNotFound{}) + } + actionIdMap, err := h.GetActionIDsMapForAlerts(config, req, ha) + if err != nil { + return nil, fmt.Errorf("could not get action id mapping: %w", err) + } + + value := AlertTransform(ha, actionIdMap) + value.ID = currentAlert.ID + + h.apiClient.Alert[key] = *value + return value, nil } func (h *MockClientConfig) DeleteAlert(config *humioapi.Config, req reconcile.Request, ha *humiov1alpha1.HumioAlert) error { - h.apiClient.Alert = humioapi.Alert{} + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", ha.Spec.ManagedClusterName, ha.Spec.ExternalClusterName), + searchDomainName: ha.Spec.ViewName, + resourceName: ha.Spec.Name, + } + + delete(h.apiClient.Alert, key) return nil } @@ -297,30 +628,82 @@ func (h *MockClientConfig) GetActionIDsMapForAlerts(config *humioapi.Config, req } func (h *MockClientConfig) GetFilterAlert(config *humioapi.Config, req reconcile.Request, hfa *humiov1alpha1.HumioFilterAlert) (*humioapi.FilterAlert, error) { - if h.apiClient.FilterAlert.Name == "" { - return nil, fmt.Errorf("could not find alert in view %q with name %q, err=%w", hfa.Spec.ViewName, hfa.Spec.Name, humioapi.EntityNotFound{}) + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hfa.Spec.ManagedClusterName, hfa.Spec.ExternalClusterName), + searchDomainName: hfa.Spec.ViewName, + resourceName: hfa.Spec.Name, } - return &h.apiClient.FilterAlert, nil + if value, found := h.apiClient.FilterAlert[key]; found { + return &value, nil + + } + return nil, fmt.Errorf("could not find alert in view %q with name %q, err=%w", hfa.Spec.ViewName, hfa.Spec.Name, humioapi.EntityNotFound{}) } func (h *MockClientConfig) AddFilterAlert(config *humioapi.Config, req reconcile.Request, hfa *humiov1alpha1.HumioFilterAlert) (*humioapi.FilterAlert, error) { - if err := h.ValidateActionsForFilterAlert(config, req, hfa); err != nil { - return &humioapi.FilterAlert{}, fmt.Errorf("could not get action id mapping: %w", err) + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hfa.Spec.ManagedClusterName, hfa.Spec.ExternalClusterName), + searchDomainName: hfa.Spec.ViewName, + resourceName: hfa.Spec.Name, } - filterAlert, err := FilterAlertTransform(hfa) - if err != nil { - return filterAlert, err + + if _, found := h.apiClient.FilterAlert[key]; found { + return nil, fmt.Errorf("filter alert already exists with name %s", hfa.Spec.Name) + } + if err := h.ValidateActionsForFilterAlert(config, req, hfa); err != nil { + return nil, fmt.Errorf("could not get action id mapping: %w", err) } - h.apiClient.FilterAlert = *filterAlert - return &h.apiClient.FilterAlert, nil + + value := FilterAlertTransform(hfa) + value.ID = kubernetes.RandomString() + + h.apiClient.FilterAlert[key] = *value + return value, nil } func (h *MockClientConfig) UpdateFilterAlert(config *humioapi.Config, req reconcile.Request, hfa *humiov1alpha1.HumioFilterAlert) (*humioapi.FilterAlert, error) { - return h.AddFilterAlert(config, req, hfa) + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hfa.Spec.ManagedClusterName, hfa.Spec.ExternalClusterName), + searchDomainName: hfa.Spec.ViewName, + resourceName: hfa.Spec.Name, + } + + currentFilterAlert, found := h.apiClient.FilterAlert[key] + + if !found { + return nil, fmt.Errorf("could not find filter alert in view %q with name %q, err=%w", hfa.Spec.ViewName, hfa.Spec.Name, humioapi.EntityNotFound{}) + } + if err := h.ValidateActionsForFilterAlert(config, req, hfa); err != nil { + return nil, fmt.Errorf("could not get action id mapping: %w", err) + } + + value := FilterAlertTransform(hfa) + value.ID = currentFilterAlert.ID + + h.apiClient.FilterAlert[key] = *value + return value, nil } func (h *MockClientConfig) DeleteFilterAlert(config *humioapi.Config, req reconcile.Request, hfa *humiov1alpha1.HumioFilterAlert) error { - h.apiClient.FilterAlert = humioapi.FilterAlert{} + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hfa.Spec.ManagedClusterName, hfa.Spec.ExternalClusterName), + searchDomainName: hfa.Spec.ViewName, + resourceName: hfa.Spec.Name, + } + + delete(h.apiClient.FilterAlert, key) return nil } @@ -329,30 +712,82 @@ func (h *MockClientConfig) ValidateActionsForFilterAlert(config *humioapi.Config } func (h *MockClientConfig) AddScheduledSearch(config *humioapi.Config, req reconcile.Request, hss *humiov1alpha1.HumioScheduledSearch) (*humioapi.ScheduledSearch, error) { - if err := h.ValidateActionsForScheduledSearch(config, req, hss); err != nil { - return &humioapi.ScheduledSearch{}, fmt.Errorf("could not get action id mapping: %w", err) + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hss.Spec.ManagedClusterName, hss.Spec.ExternalClusterName), + searchDomainName: hss.Spec.ViewName, + resourceName: hss.Spec.Name, } - scheduledSearch, err := ScheduledSearchTransform(hss) - if err != nil { - return scheduledSearch, err + + if _, found := h.apiClient.ScheduledSearch[key]; found { + return nil, fmt.Errorf("scheduled search already exists with name %s", hss.Spec.Name) } - h.apiClient.ScheduledSearch = *scheduledSearch - return &h.apiClient.ScheduledSearch, nil + if err := h.ValidateActionsForScheduledSearch(config, req, hss); err != nil { + return nil, fmt.Errorf("could not get action id mapping: %w", err) + } + + value := ScheduledSearchTransform(hss) + value.ID = kubernetes.RandomString() + + h.apiClient.ScheduledSearch[key] = *value + return value, nil } func (h *MockClientConfig) GetScheduledSearch(config *humioapi.Config, req reconcile.Request, hss *humiov1alpha1.HumioScheduledSearch) (*humioapi.ScheduledSearch, error) { - if h.apiClient.ScheduledSearch.Name == "" { - return nil, fmt.Errorf("could not find scheduled search in view %q with name %q, err=%w", hss.Spec.ViewName, hss.Spec.Name, humioapi.EntityNotFound{}) + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hss.Spec.ManagedClusterName, hss.Spec.ExternalClusterName), + searchDomainName: hss.Spec.ViewName, + resourceName: hss.Spec.Name, } - return &h.apiClient.ScheduledSearch, nil + if value, found := h.apiClient.ScheduledSearch[key]; found { + return &value, nil + + } + return nil, fmt.Errorf("could not find scheduled search in view %q with name %q, err=%w", hss.Spec.ViewName, hss.Spec.Name, humioapi.EntityNotFound{}) } func (h *MockClientConfig) UpdateScheduledSearch(config *humioapi.Config, req reconcile.Request, hss *humiov1alpha1.HumioScheduledSearch) (*humioapi.ScheduledSearch, error) { - return h.AddScheduledSearch(config, req, hss) + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hss.Spec.ManagedClusterName, hss.Spec.ExternalClusterName), + searchDomainName: hss.Spec.ViewName, + resourceName: hss.Spec.Name, + } + + currentScheduledSearch, found := h.apiClient.ScheduledSearch[key] + + if !found { + return nil, fmt.Errorf("could not find scheduled search in view %q with name %q, err=%w", hss.Spec.ViewName, hss.Spec.Name, humioapi.EntityNotFound{}) + } + if err := h.ValidateActionsForScheduledSearch(config, req, hss); err != nil { + return nil, fmt.Errorf("could not get action id mapping: %w", err) + } + + value := ScheduledSearchTransform(hss) + value.ID = currentScheduledSearch.ID + + h.apiClient.ScheduledSearch[key] = *value + return value, nil } func (h *MockClientConfig) DeleteScheduledSearch(config *humioapi.Config, req reconcile.Request, hss *humiov1alpha1.HumioScheduledSearch) error { - h.apiClient.ScheduledSearch = humioapi.ScheduledSearch{} + humioClientMu.Lock() + defer humioClientMu.Unlock() + + key := resourceKey{ + clusterName: fmt.Sprintf("%s%s", hss.Spec.ManagedClusterName, hss.Spec.ExternalClusterName), + searchDomainName: hss.Spec.ViewName, + resourceName: hss.Spec.Name, + } + + delete(h.apiClient.ScheduledSearch, key) return nil } @@ -366,13 +801,16 @@ func (h *MockClientConfig) GetHumioClient(config *humioapi.Config, req ctrl.Requ } func (h *MockClientConfig) ClearHumioClientConnections() { - h.apiClient.IngestToken = humioapi.IngestToken{} - h.apiClient.Parser = humioapi.Parser{} - h.apiClient.Repository = humioapi.Repository{} - h.apiClient.View = humioapi.View{} - h.apiClient.OnPremLicense = humioapi.OnPremLicense{} - h.apiClient.Action = humioapi.Action{} - h.apiClient.Alert = humioapi.Alert{} - h.apiClient.FilterAlert = humioapi.FilterAlert{} - h.apiClient.ScheduledSearch = humioapi.ScheduledSearch{} + // TODO: Find out if we can rip this function out entirely. Maybe tests should be updated to include object deletions? + h.apiClient.OnPremLicense = make(map[resourceKey]humioapi.OnPremLicense) + + h.apiClient.Repository = make(map[resourceKey]humioapi.Repository) + h.apiClient.View = make(map[resourceKey]humioapi.View) + + h.apiClient.IngestToken = make(map[resourceKey]humioapi.IngestToken) + h.apiClient.Parser = make(map[resourceKey]humioapi.Parser) + h.apiClient.Action = make(map[resourceKey]humioapi.Action) + h.apiClient.Alert = make(map[resourceKey]humioapi.Alert) + h.apiClient.FilterAlert = make(map[resourceKey]humioapi.FilterAlert) + h.apiClient.ScheduledSearch = make(map[resourceKey]humioapi.ScheduledSearch) } diff --git a/pkg/humio/filteralert_transform.go b/pkg/humio/filteralert_transform.go index 4378824f5..00c90dfa7 100644 --- a/pkg/humio/filteralert_transform.go +++ b/pkg/humio/filteralert_transform.go @@ -3,14 +3,9 @@ package humio import ( humioapi "github.com/humio/cli/api" humiov1alpha1 "github.com/humio/humio-operator/api/v1alpha1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -const ( - FilterAlertIdentifierAnnotation = "humio.com/filter-alert-id" -) - -func FilterAlertTransform(hfa *humiov1alpha1.HumioFilterAlert) (*humioapi.FilterAlert, error) { +func FilterAlertTransform(hfa *humiov1alpha1.HumioFilterAlert) *humioapi.FilterAlert { filterAlert := &humioapi.FilterAlert{ Name: hfa.Spec.Name, QueryString: hfa.Spec.QueryString, @@ -23,18 +18,14 @@ func FilterAlertTransform(hfa *humiov1alpha1.HumioFilterAlert) (*humioapi.Filter QueryOwnershipType: humioapi.QueryOwnershipTypeOrganization, } - if _, ok := hfa.ObjectMeta.Annotations[FilterAlertIdentifierAnnotation]; ok { - filterAlert.ID = hfa.ObjectMeta.Annotations[FilterAlertIdentifierAnnotation] - } - if filterAlert.Labels == nil { filterAlert.Labels = []string{} } - return filterAlert, nil + return filterAlert } -func FilterAlertHydrate(hfa *humiov1alpha1.HumioFilterAlert, alert *humioapi.FilterAlert) error { +func FilterAlertHydrate(hfa *humiov1alpha1.HumioFilterAlert, alert *humioapi.FilterAlert) { hfa.Spec = humiov1alpha1.HumioFilterAlertSpec{ Name: alert.Name, QueryString: alert.QueryString, @@ -45,12 +36,4 @@ func FilterAlertHydrate(hfa *humiov1alpha1.HumioFilterAlert, alert *humioapi.Fil Actions: alert.ActionNames, Labels: alert.Labels, } - - hfa.ObjectMeta = metav1.ObjectMeta{ - Annotations: map[string]string{ - FilterAlertIdentifierAnnotation: alert.ID, - }, - } - - return nil } diff --git a/pkg/humio/ingesttoken_transform.go b/pkg/humio/ingesttoken_transform.go new file mode 100644 index 000000000..35ac21265 --- /dev/null +++ b/pkg/humio/ingesttoken_transform.go @@ -0,0 +1,15 @@ +package humio + +import ( + humioapi "github.com/humio/cli/api" + humiov1alpha1 "github.com/humio/humio-operator/api/v1alpha1" +) + +func IngestTokenTransform(hit *humiov1alpha1.HumioIngestToken) *humioapi.IngestToken { + ingestToken := &humioapi.IngestToken{ + Name: hit.Spec.Name, + AssignedParser: hit.Spec.ParserName, + } + + return ingestToken +} diff --git a/pkg/humio/parser_transform.go b/pkg/humio/parser_transform.go new file mode 100644 index 000000000..e6a603fa2 --- /dev/null +++ b/pkg/humio/parser_transform.go @@ -0,0 +1,26 @@ +package humio + +import ( + humioapi "github.com/humio/cli/api" + humiov1alpha1 "github.com/humio/humio-operator/api/v1alpha1" +) + +func ParserTransform(hp *humiov1alpha1.HumioParser) *humioapi.Parser { + parser := &humioapi.Parser{ + Name: hp.Spec.Name, + Script: hp.Spec.ParserScript, + FieldsToTag: hp.Spec.TagFields, + FieldsToBeRemovedBeforeParsing: []string{}, + } + + testCasesGQL := make([]humioapi.ParserTestCase, len(hp.Spec.TestData)) + for i := range hp.Spec.TestData { + testCasesGQL[i] = humioapi.ParserTestCase{ + Event: humioapi.ParserTestEvent{RawString: hp.Spec.TestData[i]}, + Assertions: []humioapi.ParserTestCaseAssertions{}, + } + } + parser.TestCases = testCasesGQL + + return parser +} diff --git a/pkg/humio/scheduledsearch_transform.go b/pkg/humio/scheduledsearch_transform.go index b56100e54..d8d3d177e 100644 --- a/pkg/humio/scheduledsearch_transform.go +++ b/pkg/humio/scheduledsearch_transform.go @@ -3,14 +3,9 @@ package humio import ( humioapi "github.com/humio/cli/api" humiov1alpha1 "github.com/humio/humio-operator/api/v1alpha1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -const ( - ScheduledSearchIdentifierAnnotation = "humio.com/scheduled-search-id" -) - -func ScheduledSearchTransform(hss *humiov1alpha1.HumioScheduledSearch) (*humioapi.ScheduledSearch, error) { +func ScheduledSearchTransform(hss *humiov1alpha1.HumioScheduledSearch) *humioapi.ScheduledSearch { scheduledSearch := &humioapi.ScheduledSearch{ Name: hss.Spec.Name, QueryString: hss.Spec.QueryString, @@ -26,18 +21,14 @@ func ScheduledSearchTransform(hss *humiov1alpha1.HumioScheduledSearch) (*humioap QueryOwnershipType: humioapi.QueryOwnershipTypeOrganization, } - if _, ok := hss.ObjectMeta.Annotations[ScheduledSearchIdentifierAnnotation]; ok { - scheduledSearch.ID = hss.ObjectMeta.Annotations[ScheduledSearchIdentifierAnnotation] - } - if scheduledSearch.Labels == nil { scheduledSearch.Labels = []string{} } - return scheduledSearch, nil + return scheduledSearch } -func ScheduledSearchHydrate(hss *humiov1alpha1.HumioScheduledSearch, scheduledSearch *humioapi.ScheduledSearch) error { +func ScheduledSearchHydrate(hss *humiov1alpha1.HumioScheduledSearch, scheduledSearch *humioapi.ScheduledSearch) { hss.Spec = humiov1alpha1.HumioScheduledSearchSpec{ Name: scheduledSearch.Name, QueryString: scheduledSearch.QueryString, @@ -52,11 +43,5 @@ func ScheduledSearchHydrate(hss *humiov1alpha1.HumioScheduledSearch, scheduledSe Labels: scheduledSearch.Labels, } - hss.ObjectMeta = metav1.ObjectMeta{ - Annotations: map[string]string{ - ScheduledSearchIdentifierAnnotation: scheduledSearch.ID, - }, - } - - return nil + return }