Skip to content

Commit

Permalink
Change the type of resons in status, fix the flow
Browse files Browse the repository at this point in the history
  • Loading branch information
mvshao committed Aug 29, 2024
1 parent a0a0b83 commit 0902ae7
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 75 deletions.
1 change: 1 addition & 0 deletions api/v1/runtime_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ const (

ConditionReasonAdministratorsConfigured = RuntimeConditionReason("AdministratorsConfigured")
ConditionReasonAuditLogConfigured = RuntimeConditionReason("AuditLogConfigured")
ConditionReasonAuditLogError = RuntimeConditionReason("AuditLogErr")
)

//+kubebuilder:object:root=true
Expand Down
17 changes: 4 additions & 13 deletions internal/auditlogging/auditlogging.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"

gardener "github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/go-logr/logr"
"github.com/pkg/errors"
v12 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -31,7 +30,6 @@ type AuditLogConfigurator interface {
CanEnableAuditLogsForShoot(seedName string) bool
GetPolicyConfigMapName() string
GetSeedObj(ctx context.Context, seedKey types.NamespacedName) (gardener.Seed, error)
GetLogInstance() logr.Logger
UpdateShoot(ctx context.Context, shoot *gardener.Shoot) error
GetConfigFromFile() (data map[string]map[string]AuditLogData, err error)
}
Expand All @@ -44,7 +42,6 @@ type auditLogConfig struct {
tenantConfigPath string
policyConfigMapName string
client client.Client
log logr.Logger
}

type AuditLogData struct {
Expand All @@ -66,18 +63,17 @@ type AuditlogExtensionConfig struct {
SecretReferenceName string `json:"secretReferenceName"`
}

func NewAuditLogging(auditLogTenantConfigPath, auditLogPolicyConfigMapName string, k8s client.Client, log logr.Logger) AuditLogging {
func NewAuditLogging(auditLogTenantConfigPath, auditLogPolicyConfigMapName string, k8s client.Client) AuditLogging {
return &AuditLog{
AuditLogConfigurator: newAuditLogConfigurator(auditLogTenantConfigPath, auditLogPolicyConfigMapName, k8s, log),
AuditLogConfigurator: newAuditLogConfigurator(auditLogTenantConfigPath, auditLogPolicyConfigMapName, k8s),
}
}

func newAuditLogConfigurator(auditLogTenantConfigPath, auditLogPolicyConfigMapName string, k8s client.Client, log logr.Logger) AuditLogConfigurator {
func newAuditLogConfigurator(auditLogTenantConfigPath, auditLogPolicyConfigMapName string, k8s client.Client) AuditLogConfigurator {
return &auditLogConfig{
tenantConfigPath: auditLogTenantConfigPath,
policyConfigMapName: auditLogPolicyConfigMapName,
client: k8s,
log: log,
}
}

Expand All @@ -98,11 +94,10 @@ func (a *auditLogConfig) GetSeedObj(ctx context.Context, seedKey types.Namespace
}

func (al *AuditLog) Enable(ctx context.Context, shoot *gardener.Shoot) (bool, error) {
log := al.GetLogInstance()
seedName := getSeedName(*shoot)

if !al.CanEnableAuditLogsForShoot(seedName) {
log.Info("Seed name or Tenant config path is empty while configuring Audit Logs on shoot: " + shoot.Name)
//log.Info("Seed name or Tenant config path is empty while configuring Audit Logs on shoot: " + shoot.Name)
return false, nil
}

Expand Down Expand Up @@ -294,7 +289,3 @@ func newAuditPolicyConfig(policyConfigMapName string) *gardener.AuditConfig {
func (a *auditLogConfig) UpdateShoot(ctx context.Context, shoot *gardener.Shoot) error {
return a.client.Update(ctx, shoot)
}

func (a *auditLogConfig) GetLogInstance() logr.Logger {
return a.log
}
20 changes: 0 additions & 20 deletions internal/auditlogging/mocks/AuditLogConfigurator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions internal/auditlogging/tests/auditlogging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
gardener "github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/go-logr/logr"
"github.com/kyma-project/infrastructure-manager/internal/auditlogging"
"github.com/kyma-project/infrastructure-manager/internal/auditlogging/mocks"
"github.com/stretchr/testify/require"
Expand All @@ -24,7 +23,6 @@ func TestEnable(t *testing.T) {
configFromFile := fileConfigData()
seedKey := types.NamespacedName{Name: "seed-name", Namespace: ""}

configurator.On("GetLogInstance").Return(logr.Logger{}).Once()
configurator.On("CanEnableAuditLogsForShoot", "seed-name").Return(true).Once()
configurator.On("GetConfigFromFile").Return(configFromFile, nil).Once()
configurator.On("GetPolicyConfigMapName").Return("policyConfigMapName").Once()
Expand Down Expand Up @@ -53,7 +51,6 @@ func TestEnable(t *testing.T) {
// delete shoot region to simulate error
shoot.Spec.Region = ""

configurator.On("GetLogInstance").Return(logr.Logger{}).Once()
configurator.On("CanEnableAuditLogsForShoot", "seed-name").Return(true).Once()
configurator.On("GetConfigFromFile").Return(configFromFile, nil).Once()
configurator.On("GetPolicyConfigMapName").Return("policyConfigMapName").Once()
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/runtime/fsm/runtime_fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,6 @@ func NewFsm(log logr.Logger, cfg RCCfg, k8s K8s) Fsm {
RCCfg: cfg,
log: log,
K8s: k8s,
AuditLogging: auditlogging.NewAuditLogging(cfg.AuditLog.TenantConfigPath, cfg.AuditLog.PolicyConfigMapName, k8s.ShootClient, log),
AuditLogging: auditlogging.NewAuditLogging(cfg.AuditLog.TenantConfigPath, cfg.AuditLog.PolicyConfigMapName, k8s.ShootClient),
}
}
23 changes: 11 additions & 12 deletions internal/controller/runtime/fsm/runtime_fsm_configure_auditlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,25 @@ func sFnConfigureAuditLog(ctx context.Context, m *fsm, s *systemState) (stateFn,
m.log.Info("Configure Audit Log state")

wasAuditLogEnabled, err := m.AuditLogging.Enable(ctx, s.shoot)
if err != nil {
m.log.Error(err, "Failed to configure Audit Log")
s.instance.UpdateStatePending(
imv1.ConditionTypeAuditLogConfigured,
imv1.ConditionReasonConfigurationCompleted,
"False",
err.Error(),
)
return updateStatusAndRequeueAfter(gardenerRequeueDuration)
}

if wasAuditLogEnabled {
m.log.Info("Audit Log configured for shoot: " + s.shoot.Name)
s.instance.UpdateStateReady(
imv1.ConditionTypeAuditLogConfigured,
imv1.ConditionReasonConfigurationCompleted,
imv1.ConditionReasonAuditLogConfigured,
"Audit Log configured",
)

return updateStatusAndStop()
}
return requeue()

m.log.Error(err, "Failed to configure Audit Log")
s.instance.UpdateStatePending(
imv1.ConditionTypeAuditLogConfigured,
imv1.ConditionReasonAuditLogError,
"False",
err.Error(),
)
return updateStatusAndRequeueAfter(gardenerRequeueDuration)

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestAuditLogState(t *testing.T) {
{
Type: string(v1.ConditionTypeAuditLogConfigured),
Status: "True",
Reason: string(v1.ConditionReasonConfigurationCompleted),
Reason: string(v1.ConditionReasonAuditLogConfigured),
Message: "Audit Log configured",
},
}
Expand Down Expand Up @@ -63,7 +63,7 @@ func TestAuditLogState(t *testing.T) {
{
Type: string(v1.ConditionTypeAuditLogConfigured),
Status: "False",
Reason: string(v1.ConditionReasonConfigurationCompleted),
Reason: string(v1.ConditionReasonAuditLogError),
Message: "some error during configuration",
},
}
Expand All @@ -83,29 +83,6 @@ func TestAuditLogState(t *testing.T) {
assert.Equal(t, v1.RuntimeStateFailed, string(systemState.instance.Status.State))
assert.Equal(t, expectedRuntimeConditions, systemState.instance.Status.Conditions)
})

t.Run("Should requeue if initial criteria of enabling Audit Log is not met", func(t *testing.T) {
// given
ctx := context.Background()
auditLog := &mocks.AuditLogging{}
shoot := shootForTest()
instance := runtimeForTest()
systemState := &systemState{
instance: instance,
shoot: shoot,
}

auditLog.On("Enable", ctx, shoot).Return(false, nil).Once()

// when
fsm := &fsm{AuditLogging: auditLog}
stateFn, result, _ := sFnConfigureAuditLog(ctx, fsm, systemState)

// then
auditLog.AssertExpectations(t)
require.Nil(t, stateFn)
require.Equal(t, true, result.Requeue)
})
}

func shootForTest() *gardener.Shoot {
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/runtime/runtime_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ var _ = Describe("Runtime Controller", func() {
return false
}

if !runtime.IsConditionSet(imv1.ConditionTypeAuditLogConfigured, imv1.ConditionReasonConfigurationCompleted) {
if !runtime.IsConditionSet(imv1.ConditionTypeAuditLogConfigured, imv1.ConditionReasonAuditLogConfigured) {
return false
}

Expand Down

0 comments on commit 0902ae7

Please sign in to comment.