Skip to content

Commit

Permalink
Merge pull request #376 from akgalwas/audit-log-refactor
Browse files Browse the repository at this point in the history
Refactor in audit log state
  • Loading branch information
kyma-bot authored Sep 19, 2024
2 parents 26f8316 + b05851e commit fe5a19a
Showing 1 changed file with 59 additions and 45 deletions.
104 changes: 59 additions & 45 deletions internal/controller/runtime/fsm/runtime_fsm_configure_auditlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
func sFnConfigureAuditLog(ctx context.Context, m *fsm, s *systemState) (stateFn, *ctrl.Result, error) {
m.log.Info("Configure Audit Log state")

wasAuditLogEnabled, err := m.AuditLogging.Enable(ctx, s.shoot)
shootNeedsToBeReconciled, err := m.AuditLogging.Enable(ctx, s.shoot)

if wasAuditLogEnabled && err == nil {
if shootNeedsToBeReconciled && err == nil {
m.log.Info("Audit Log configured for shoot: " + s.shoot.Name)
s.instance.UpdateStatePending(
imv1.ConditionTypeAuditLogConfigured,
Expand All @@ -28,59 +28,73 @@ func sFnConfigureAuditLog(ctx context.Context, m *fsm, s *systemState) (stateFn,
return updateStatusAndRequeueAfter(gardenerRequeueDuration)
}

auditLogMandatoryString := strconv.FormatBool(m.RCCfg.AuditLogMandatory)
if err == nil {
s.instance.UpdateStateReady(
imv1.ConditionTypeAuditLogConfigured,
imv1.ConditionReasonAuditLogConfigured,
"Audit Log state completed successfully",
)

return updateStatusAndStop()
}

if err != nil { //nolint:nestif
if k8serrors.IsConflict(err) {
m.log.Error(err, "Conflict while updating Shoot object after applying Audit Log configuration, retrying")
return handleError(err, m, s)
}

func handleError(err error, m *fsm, s *systemState) (stateFn, *ctrl.Result, error) {
setStateForAuditLogError := func(reason imv1.RuntimeConditionReason, pendingMsg string, readyMsg string) {
if m.RCCfg.AuditLogMandatory {
s.instance.UpdateStatePending(
imv1.ConditionTypeAuditLogConfigured,
imv1.ConditionReasonAuditLogError,
"True",
err.Error(),
reason,
"False",
pendingMsg,
)
return updateStatusAndRequeue()
} else {
s.instance.UpdateStateReady(
imv1.ConditionTypeAuditLogConfigured,
reason,
readyMsg)
}
errorMessage := err.Error()
if errors.Is(err, auditlogging.ErrMissingMapping) {
if m.RCCfg.AuditLogMandatory {
m.log.Error(err, "AuditLogMandatory", auditLogMandatoryString, "providerType", s.shoot.Spec.Provider.Type, "region", s.shoot.Spec.Region)
s.instance.UpdateStatePending(
imv1.ConditionTypeAuditLogConfigured,
imv1.ConditionReasonAuditLogMissingRegionMapping,
"False",
errorMessage,
)
} else {
m.log.Info(errorMessage, "AuditLogMandatory", auditLogMandatoryString, "providerType", s.shoot.Spec.Provider.Type, "region", s.shoot.Spec.Region)
s.instance.UpdateStateReady(
imv1.ConditionTypeAuditLogConfigured,
imv1.ConditionReasonAuditLogMissingRegionMapping,
"Missing region mapping for this shoot. Audit Log is not mandatory. Skipping configuration")
}
}

logError := func(err error, keysAndValues ...any) {
if m.RCCfg.AuditLogMandatory {
m.log.Error(nil, err.Error(), keysAndValues...)
} else {
if m.RCCfg.AuditLogMandatory {
m.log.Error(err, "AuditLogMandatory", auditLogMandatoryString)
s.instance.UpdateStatePending(
imv1.ConditionTypeAuditLogConfigured,
imv1.ConditionReasonAuditLogError,
"False",
errorMessage)
} else {
m.log.Info(errorMessage, "AuditLogMandatory", auditLogMandatoryString)
s.instance.UpdateStateReady(
imv1.ConditionTypeAuditLogConfigured,
imv1.ConditionReasonAuditLogError,
"Configuration of Audit Log is not mandatory, error for context: "+errorMessage)
}
m.log.Info(err.Error(), keysAndValues...)
}
} else {
s.instance.UpdateStateReady(
}

if k8serrors.IsConflict(err) {
m.log.Error(err, "Conflict while updating Shoot object after applying Audit Log configuration, retrying")
s.instance.UpdateStatePending(
imv1.ConditionTypeAuditLogConfigured,
imv1.ConditionReasonAuditLogConfigured,
"Audit Log state completed successfully",
imv1.ConditionReasonAuditLogError,
"True",
err.Error(),
)

return updateStatusAndRequeue()
}

auditLogMandatoryString := strconv.FormatBool(m.RCCfg.AuditLogMandatory)

if errors.Is(err, auditlogging.ErrMissingMapping) {
pendingStatusMsg := err.Error()
readyStatusMsg := "Missing region mapping for this shoot. Audit Log is not mandatory. Skipping configuration"
setStateForAuditLogError(imv1.ConditionReasonAuditLogMissingRegionMapping, pendingStatusMsg, readyStatusMsg)

logError(err, "AuditLogMandatory", auditLogMandatoryString, "providerType", s.shoot.Spec.Provider.Type, "region", s.shoot.Spec.Region)

return updateStatusAndStop()
}

pendingStatusMsg := err.Error()
readyStatusMsg := "Configuration of Audit Log is not mandatory, error for context: " + err.Error()
setStateForAuditLogError(imv1.ConditionReasonAuditLogError, pendingStatusMsg, readyStatusMsg)

logError(err, "AuditLogMandatory", auditLogMandatoryString)

return updateStatusAndStop()
}

0 comments on commit fe5a19a

Please sign in to comment.