Skip to content

Commit

Permalink
Clean up the way diffs are printed
Browse files Browse the repository at this point in the history
  • Loading branch information
SaaldjorMike committed Dec 13, 2024
1 parent b9f8344 commit 75c8b55
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 159 deletions.
99 changes: 49 additions & 50 deletions controllers/humioaction_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"errors"
"fmt"
"strings"
"time"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -181,9 +180,9 @@ func (r *HumioActionReconciler) reconcileHumioAction(ctx context.Context, client
return reconcile.Result{}, r.logErrorAndReturn(err, "could not parse expected action")
}

if asExpected, diff := actionAlreadyAsExpected(expectedAction, curAction); !asExpected {
if asExpected, diffKeysAndValues := actionAlreadyAsExpected(expectedAction, curAction); !asExpected {
r.Log.Info("information differs, triggering update",
"diff", diff,
helpers.MapToAnySlice(diffKeysAndValues)...,
)
err = r.HumioClient.UpdateAction(ctx, client, req, ha)
if err != nil {
Expand Down Expand Up @@ -317,165 +316,165 @@ func (r *HumioActionReconciler) logErrorAndReturn(err error, msg string) error {

// actionAlreadyAsExpected compares fromKubernetesCustomResource and fromGraphQL. It returns a boolean indicating
// if the details from GraphQL already matches what is in the desired state of the custom resource.
// If they do not match, a string is returned with details on what the diff is.
func actionAlreadyAsExpected(expectedAction humiographql.ActionDetails, currentAction humiographql.ActionDetails) (bool, string) {
var diffs []string
// If they do not match, a map is returned with details on what the diff is.
func actionAlreadyAsExpected(expectedAction humiographql.ActionDetails, currentAction humiographql.ActionDetails) (bool, map[string]string) {
var keyValues map[string]string

switch e := (expectedAction).(type) {
case *humiographql.ActionDetailsEmailAction:
switch c := (currentAction).(type) {
case *humiographql.ActionDetailsEmailAction:
if diff := cmp.Diff(c.GetName(), e.GetName()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.name=%q", e, diff))
keyValues[fmt.Sprintf("%T.name", e)] = diff
}
if diff := cmp.Diff(c.GetRecipients(), e.GetRecipients()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.recipients=%q", e, diff))
keyValues[fmt.Sprintf("%T.recipients", e)] = diff
}
if diff := cmp.Diff(c.GetSubjectTemplate(), e.GetSubjectTemplate()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.subjectTemplate()=%q", e, diff))
keyValues[fmt.Sprintf("%T.subjectTemplate", e)] = diff
}
if diff := cmp.Diff(c.GetEmailBodyTemplate(), e.GetEmailBodyTemplate()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.bodyTemplate=%q", e, diff))
keyValues[fmt.Sprintf("%T.bodyTemplate", e)] = diff
}
if diff := cmp.Diff(c.GetUseProxy(), e.GetUseProxy()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.useProxy=%q", e, diff))
keyValues[fmt.Sprintf("%T.useProxy", e)] = diff
}
default:
diffs = append(diffs, fmt.Sprintf("expected type %T but current is %T", e, c))
keyValues["wrongType"] = fmt.Sprintf("expected type %T but current is %T", e, c)
}
case *humiographql.ActionDetailsHumioRepoAction:
switch c := (currentAction).(type) {
case *humiographql.ActionDetailsHumioRepoAction:
if diff := cmp.Diff(c.GetName(), e.GetName()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.name=%q", e, diff))
keyValues[fmt.Sprintf("%T.name", e)] = diff
}
if diff := cmp.Diff(c.GetIngestToken(), e.GetIngestToken()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.ingestToken=%q", e, "<redacted>"))
keyValues[fmt.Sprintf("%T.ingestToken", e)] = "<redacted>"
}
default:
diffs = append(diffs, fmt.Sprintf("expected type %T but current is %T", e, c))
keyValues["wrongType"] = fmt.Sprintf("expected type %T but current is %T", e, c)
}
case *humiographql.ActionDetailsOpsGenieAction:
switch c := (currentAction).(type) {
case *humiographql.ActionDetailsOpsGenieAction:
if diff := cmp.Diff(c.GetName(), e.GetName()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.name=%q", e, diff))
keyValues[fmt.Sprintf("%T.name", e)] = diff
}
if diff := cmp.Diff(c.GetApiUrl(), e.GetApiUrl()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.apiUrl=%q", e, diff))
keyValues[fmt.Sprintf("%T.apiUrl", e)] = diff
}
if diff := cmp.Diff(c.GetGenieKey(), e.GetGenieKey()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.genieKey=%q", e, "<redacted>"))
keyValues[fmt.Sprintf("%T.genieKey", e)] = "<redacted>"
}
if diff := cmp.Diff(c.GetUseProxy(), e.GetUseProxy()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.useProxy=%q", e, diff))
keyValues[fmt.Sprintf("%T.useProxy", e)] = diff
}
default:
diffs = append(diffs, fmt.Sprintf("expected type %T but current is %T", e, c))
keyValues["wrongType"] = fmt.Sprintf("expected type %T but current is %T", e, c)
}
case *humiographql.ActionDetailsPagerDutyAction:
switch c := (currentAction).(type) {
case *humiographql.ActionDetailsPagerDutyAction:
if diff := cmp.Diff(c.GetName(), e.GetName()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.name=%q", e, diff))
keyValues[fmt.Sprintf("%T.name", e)] = diff
}
if diff := cmp.Diff(c.GetRoutingKey(), e.GetRoutingKey()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.apiUrl=%q", e, "<redacted>"))
keyValues[fmt.Sprintf("%T.apiUrl", e)] = "<redacted>"
}
if diff := cmp.Diff(c.GetSeverity(), e.GetSeverity()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.genieKey=%q", e, diff))
keyValues[fmt.Sprintf("%T.genieKey", e)] = diff
}
if diff := cmp.Diff(c.GetUseProxy(), e.GetUseProxy()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.useProxy=%q", e, diff))
keyValues[fmt.Sprintf("%T.useProxy", e)] = diff
}
default:
diffs = append(diffs, fmt.Sprintf("expected type %T but current is %T", e, c))
keyValues["wrongType"] = fmt.Sprintf("expected type %T but current is %T", e, c)
}
case *humiographql.ActionDetailsSlackAction:
switch c := (currentAction).(type) {
case *humiographql.ActionDetailsSlackAction:
if diff := cmp.Diff(c.GetName(), e.GetName()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.name=%q", e, diff))
keyValues[fmt.Sprintf("%T.name", e)] = diff
}
if diff := cmp.Diff(c.GetFields(), e.GetFields()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.fields=%q", e, diff))
keyValues[fmt.Sprintf("%T.fields", e)] = diff
}
if diff := cmp.Diff(c.GetUrl(), e.GetUrl()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.url=%q", e, "<redacted>"))
keyValues[fmt.Sprintf("%T.url", e)] = "<redacted>"
}
if diff := cmp.Diff(c.GetUseProxy(), e.GetUseProxy()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.useProxy=%q", e, diff))
keyValues[fmt.Sprintf("%T.useProxy", e)] = diff
}
default:
diffs = append(diffs, fmt.Sprintf("expected type %T but current is %T", e, c))
keyValues["wrongType"] = fmt.Sprintf("expected type %T but current is %T", e, c)
}
case *humiographql.ActionDetailsSlackPostMessageAction:
switch c := (currentAction).(type) {
case *humiographql.ActionDetailsSlackPostMessageAction:
if diff := cmp.Diff(c.GetName(), e.GetName()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.name=%q", e, diff))
keyValues[fmt.Sprintf("%T.name", e)] = diff
}
if diff := cmp.Diff(c.GetApiToken(), e.GetApiToken()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.apiToken=%q", e, "<redacted>"))
keyValues[fmt.Sprintf("%T.apiToken", e)] = "<redacted>"
}
if diff := cmp.Diff(c.GetChannels(), e.GetChannels()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.channels=%q", e, diff))
keyValues[fmt.Sprintf("%T.channels", e)] = diff
}
if diff := cmp.Diff(c.GetFields(), e.GetFields()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.fields=%q", e, diff))
keyValues[fmt.Sprintf("%T.fields", e)] = diff
}
if diff := cmp.Diff(c.GetUseProxy(), e.GetUseProxy()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.useProxy=%q", e, diff))
keyValues[fmt.Sprintf("%T.useProxy", e)] = diff
}
default:
diffs = append(diffs, fmt.Sprintf("expected type %T but current is %T", e, c))
keyValues["wrongType"] = fmt.Sprintf("expected type %T but current is %T", e, c)
}
case *humiographql.ActionDetailsVictorOpsAction:
switch c := (currentAction).(type) {
case *humiographql.ActionDetailsVictorOpsAction:
if diff := cmp.Diff(c.GetName(), e.GetName()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.name=%q", e, diff))
keyValues[fmt.Sprintf("%T.name", e)] = diff
}
if diff := cmp.Diff(c.GetMessageType(), e.GetMessageType()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.messageType=%q", e, diff))
keyValues[fmt.Sprintf("%T.messageType", e)] = diff
}
if diff := cmp.Diff(c.GetNotifyUrl(), e.GetNotifyUrl()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.notifyUrl=%q", e, "<redacted>"))
keyValues[fmt.Sprintf("%T.notifyUrl", e)] = "<redacted>"
}
if diff := cmp.Diff(c.GetUseProxy(), e.GetUseProxy()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.useProxy=%q", e, diff))
keyValues[fmt.Sprintf("%T.useProxy", e)] = diff
}
default:
diffs = append(diffs, fmt.Sprintf("expected type %T but current is %T", e, c))
keyValues["wrongType"] = fmt.Sprintf("expected type %T but current is %T", e, c)
}
case *humiographql.ActionDetailsWebhookAction:
switch c := (currentAction).(type) {
case *humiographql.ActionDetailsWebhookAction:
if diff := cmp.Diff(c.GetName(), e.GetName()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.name=%q", e, diff))
keyValues[fmt.Sprintf("%T.name", e)] = diff
}
if diff := cmp.Diff(c.GetWebhookBodyTemplate(), e.GetWebhookBodyTemplate()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.bodyTemplate=%q", e, diff))
keyValues[fmt.Sprintf("%T.bodyTemplate", e)] = diff
}
if diff := cmp.Diff(c.GetHeaders(), e.GetHeaders()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.headers=%q", e, "<redacted>"))
keyValues[fmt.Sprintf("%T.headers", e)] = "<redacted>"
}
if diff := cmp.Diff(c.GetMethod(), e.GetMethod()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.method=%q", e, diff))
keyValues[fmt.Sprintf("%T.method", e)] = diff
}
if diff := cmp.Diff(c.GetUrl(), e.GetUrl()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.url=%q", e, "<redacted>"))
keyValues[fmt.Sprintf("%T.url", e)] = "<redacted>"
}
if diff := cmp.Diff(c.GetIgnoreSSL(), e.GetIgnoreSSL()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.ignoreSSL=%q", e, diff))
keyValues[fmt.Sprintf("%T.ignoreSSL", e)] = diff
}
if diff := cmp.Diff(c.GetUseProxy(), e.GetUseProxy()); diff != "" {
diffs = append(diffs, fmt.Sprintf("%T.useProxy=%q", e, diff))
keyValues[fmt.Sprintf("%T.useProxy", e)] = diff
}
default:
diffs = append(diffs, fmt.Sprintf("expected type %T but current is %T", e, c))
keyValues["wrongType"] = fmt.Sprintf("expected type %T but current is %T", e, c)
}
}

return len(diffs) == 0, strings.Join(diffs, ", ")
return len(keyValues) == 0, keyValues
}
35 changes: 17 additions & 18 deletions controllers/humioaggregatealert_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"errors"
"fmt"
"sort"
"strings"
"time"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -178,9 +177,9 @@ func (r *HumioAggregateAlertReconciler) reconcileHumioAggregateAlert(ctx context
return reconcile.Result{}, r.logErrorAndReturn(err, "could not validate actions for aggregate alert")
}

if asExpected, diff := aggregateAlertAlreadyAsExpected(haa, curAggregateAlert); !asExpected {
if asExpected, diffKeysAndValues := aggregateAlertAlreadyAsExpected(haa, curAggregateAlert); !asExpected {
r.Log.Info("information differs, triggering update",
"diff", diff,
helpers.MapToAnySlice(diffKeysAndValues)...,
)
updateErr := r.HumioClient.UpdateAggregateAlert(ctx, client, req, haa)
if updateErr != nil {
Expand Down Expand Up @@ -219,49 +218,49 @@ func (r *HumioAggregateAlertReconciler) logErrorAndReturn(err error, msg string)

// aggregateAlertAlreadyAsExpected compares fromKubernetesCustomResource and fromGraphQL. It returns a boolean indicating
// if the details from GraphQL already matches what is in the desired state of the custom resource.
// If they do not match, a string is returned with details on what the diff is.
func aggregateAlertAlreadyAsExpected(fromKubernetesCustomResource *humiov1alpha1.HumioAggregateAlert, fromGraphQL *humiographql.AggregateAlertDetails) (bool, string) {
var diffs []string
// If they do not match, a map is returned with details on what the diff is.
func aggregateAlertAlreadyAsExpected(fromKubernetesCustomResource *humiov1alpha1.HumioAggregateAlert, fromGraphQL *humiographql.AggregateAlertDetails) (bool, map[string]string) {
var keyValues map[string]string

if diff := cmp.Diff(fromGraphQL.GetDescription(), &fromKubernetesCustomResource.Spec.Description); diff != "" {
diffs = append(diffs, fmt.Sprintf("description=%q", diff))
keyValues["description"] = diff
}
labelsFromGraphQL := fromGraphQL.GetLabels()
sort.Strings(labelsFromGraphQL)
sort.Strings(fromKubernetesCustomResource.Spec.Labels)
if diff := cmp.Diff(labelsFromGraphQL, fromKubernetesCustomResource.Spec.Labels); diff != "" {
diffs = append(diffs, fmt.Sprintf("labels=%q", diff))
keyValues["labels"] = diff
}
if diff := cmp.Diff(fromGraphQL.GetThrottleField(), fromKubernetesCustomResource.Spec.ThrottleField); diff != "" {
diffs = append(diffs, fmt.Sprintf("throttleField=%q", diff))
keyValues["throttleField"] = diff
}
if diff := cmp.Diff(fromGraphQL.GetThrottleTimeSeconds(), fromKubernetesCustomResource.Spec.ThrottleTimeSeconds); diff != "" {
diffs = append(diffs, fmt.Sprintf("throttleTimeSeconds=%q", diff))
keyValues["throttleTimeSeconds"] = diff
}
actionsFromGraphQL := humioapi.GetActionNames(fromGraphQL.GetActions())
sort.Strings(actionsFromGraphQL)
sort.Strings(fromKubernetesCustomResource.Spec.Actions)
if diff := cmp.Diff(actionsFromGraphQL, fromKubernetesCustomResource.Spec.Actions); diff != "" {
diffs = append(diffs, fmt.Sprintf("actions=%q", diff))
keyValues["actions"] = diff
}
if diff := cmp.Diff(fromGraphQL.GetQueryTimestampType(), humiographql.QueryTimestampType(fromKubernetesCustomResource.Spec.QueryTimestampType)); diff != "" {
diffs = append(diffs, fmt.Sprintf("queryTimestampType=%q", diff))
keyValues["queryTimestampType"] = diff
}
if diff := cmp.Diff(fromGraphQL.GetQueryString(), fromKubernetesCustomResource.Spec.QueryString); diff != "" {
diffs = append(diffs, fmt.Sprintf("queryString=%q", diff))
keyValues["queryString"] = diff
}
if diff := cmp.Diff(fromGraphQL.GetTriggerMode(), humiographql.TriggerMode(fromKubernetesCustomResource.Spec.TriggerMode)); diff != "" {
diffs = append(diffs, fmt.Sprintf("triggerMode=%q", diff))
keyValues["triggerMode"] = diff
}
if diff := cmp.Diff(fromGraphQL.GetSearchIntervalSeconds(), int64(fromKubernetesCustomResource.Spec.SearchIntervalSeconds)); diff != "" {
diffs = append(diffs, fmt.Sprintf("searchIntervalSeconds=%q", diff))
keyValues["searchIntervalSeconds"] = diff
}
if diff := cmp.Diff(fromGraphQL.GetEnabled(), fromKubernetesCustomResource.Spec.Enabled); diff != "" {
diffs = append(diffs, fmt.Sprintf("enabled=%q", diff))
keyValues["enabled"] = diff
}
if !humioapi.QueryOwnershipIsOrganizationOwnership(fromGraphQL.GetQueryOwnership()) {
diffs = append(diffs, fmt.Sprintf("queryOwnership=%+v", fromGraphQL.GetQueryOwnership()))
keyValues["queryOwnership"] = fmt.Sprintf("%+v", fromGraphQL.GetQueryOwnership())
}

return len(diffs) == 0, strings.Join(diffs, ", ")
return len(keyValues) == 0, keyValues
}
31 changes: 15 additions & 16 deletions controllers/humioalert_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"errors"
"fmt"
"sort"
"strings"
"time"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -165,9 +164,9 @@ func (r *HumioAlertReconciler) reconcileHumioAlert(ctx context.Context, client *

r.Log.Info("Checking if alert needs to be updated")

if asExpected, diff := alertAlreadyAsExpected(ha, curAlert); !asExpected {
if asExpected, diffKeysAndValues := alertAlreadyAsExpected(ha, curAlert); !asExpected {
r.Log.Info("information differs, triggering update",
"diff", diff,
helpers.MapToAnySlice(diffKeysAndValues)...,
)
err = r.HumioClient.UpdateAlert(ctx, client, req, ha)
if err != nil {
Expand Down Expand Up @@ -205,43 +204,43 @@ func (r *HumioAlertReconciler) logErrorAndReturn(err error, msg string) error {

// alertAlreadyAsExpected compares fromKubernetesCustomResource and fromGraphQL. It returns a boolean indicating
// if the details from GraphQL already matches what is in the desired state of the custom resource.
// If they do not match, a string is returned with details on what the diff is.
func alertAlreadyAsExpected(fromKubernetesCustomResource *humiov1alpha1.HumioAlert, fromGraphQL *humiographql.AlertDetails) (bool, string) {
var diffs []string
// If they do not match, a map is returned with details on what the diff is.
func alertAlreadyAsExpected(fromKubernetesCustomResource *humiov1alpha1.HumioAlert, fromGraphQL *humiographql.AlertDetails) (bool, map[string]string) {
var keyValues map[string]string

if diff := cmp.Diff(fromGraphQL.GetDescription(), &fromKubernetesCustomResource.Spec.Description); diff != "" {
diffs = append(diffs, fmt.Sprintf("description=%q", diff))
keyValues["description"] = diff
}
labelsFromGraphQL := fromGraphQL.GetLabels()
sort.Strings(labelsFromGraphQL)
sort.Strings(fromKubernetesCustomResource.Spec.Labels)
if diff := cmp.Diff(labelsFromGraphQL, fromKubernetesCustomResource.Spec.Labels); diff != "" {
diffs = append(diffs, fmt.Sprintf("labels=%q", diff))
keyValues["labels"] = diff
}
if diff := cmp.Diff(fromGraphQL.GetThrottleField(), fromKubernetesCustomResource.Spec.ThrottleField); diff != "" {
diffs = append(diffs, fmt.Sprintf("throttleField=%q", diff))
keyValues["throttleField"] = diff
}
if diff := cmp.Diff(fromGraphQL.GetThrottleTimeMillis(), int64(fromKubernetesCustomResource.Spec.ThrottleTimeMillis)); diff != "" {
diffs = append(diffs, fmt.Sprintf("throttleTimeMillis=%q", diff))
keyValues["throttleTimeMillis"] = diff
}
actionsFromGraphQL := humioapi.GetActionNames(fromGraphQL.GetActionsV2())
sort.Strings(actionsFromGraphQL)
sort.Strings(fromKubernetesCustomResource.Spec.Actions)
if diff := cmp.Diff(actionsFromGraphQL, fromKubernetesCustomResource.Spec.Actions); diff != "" {
diffs = append(diffs, fmt.Sprintf("actions=%q", diff))
keyValues["actions"] = diff
}
if diff := cmp.Diff(fromGraphQL.GetQueryString(), fromKubernetesCustomResource.Spec.Query.QueryString); diff != "" {
diffs = append(diffs, fmt.Sprintf("queryString=%q", diff))
keyValues["queryString"] = diff
}
if diff := cmp.Diff(fromGraphQL.GetQueryStart(), fromKubernetesCustomResource.Spec.Query.Start); diff != "" {
diffs = append(diffs, fmt.Sprintf("queryString=%q", diff))
keyValues["queryString"] = diff
}
if diff := cmp.Diff(fromGraphQL.GetEnabled(), !fromKubernetesCustomResource.Spec.Silenced); diff != "" {
diffs = append(diffs, fmt.Sprintf("enabled=%q", diff))
keyValues["enabled"] = diff
}
if !humioapi.QueryOwnershipIsOrganizationOwnership(fromGraphQL.GetQueryOwnership()) {
diffs = append(diffs, fmt.Sprintf("queryOwnership=%+v", fromGraphQL.GetQueryOwnership()))
keyValues["queryOwnership"] = fmt.Sprintf("%+v", fromGraphQL.GetQueryOwnership())
}

return len(diffs) == 0, strings.Join(diffs, ", ")
return len(keyValues) == 0, keyValues
}
Loading

0 comments on commit 75c8b55

Please sign in to comment.