From 469073da79478be05674245beb1b1a3e2cafc3b9 Mon Sep 17 00:00:00 2001 From: Alexander Dammeier Date: Mon, 7 Oct 2024 17:55:49 +0200 Subject: [PATCH] #81 calculate StateDiff by new GlobalConfig --- pkg/application/stateDiffUseCase.go | 9 +- pkg/application/stateDiffUseCase_test.go | 122 +++++++++++++++-------- pkg/bootstrap.go | 4 +- pkg/domain/ecosystem/EcosystemState.go | 3 +- pkg/domain/stateDiffConfig_test.go | 41 ++++---- pkg/domain/stateDiffGlobalConfig.go | 19 ++-- 6 files changed, 125 insertions(+), 73 deletions(-) diff --git a/pkg/application/stateDiffUseCase.go b/pkg/application/stateDiffUseCase.go index 382a6167..4577deaf 100644 --- a/pkg/application/stateDiffUseCase.go +++ b/pkg/application/stateDiffUseCase.go @@ -6,16 +6,15 @@ import ( "fmt" "github.com/cloudogu/k8s-blueprint-operator/pkg/domain" "github.com/cloudogu/k8s-blueprint-operator/pkg/domain/ecosystem" - "sigs.k8s.io/controller-runtime/pkg/log" - "github.com/cloudogu/k8s-blueprint-operator/pkg/domainservice" + "sigs.k8s.io/controller-runtime/pkg/log" ) type StateDiffUseCase struct { blueprintSpecRepo blueprintSpecRepository doguInstallationRepo doguInstallationRepository componentInstallationRepo componentInstallationRepository - globalConfigRepo globalConfigEntryRepository + globalConfigRepo globalConfigRepository doguConfigRepo doguConfigEntryRepository sensitiveDoguConfigRepo sensitiveDoguConfigEntryRepository } @@ -24,7 +23,7 @@ func NewStateDiffUseCase( blueprintSpecRepo domainservice.BlueprintSpecRepository, doguInstallationRepo domainservice.DoguInstallationRepository, componentInstallationRepo domainservice.ComponentInstallationRepository, - globalConfigRepo domainservice.GlobalConfigEntryRepository, + globalConfigRepo domainservice.GlobalConfigRepository, doguConfigRepo domainservice.DoguConfigEntryRepository, sensitiveDoguConfigRepo domainservice.SensitiveDoguConfigEntryRepository, ) *StateDiffUseCase { @@ -91,7 +90,7 @@ func (useCase *StateDiffUseCase) collectEcosystemState(ctx context.Context, effe installedComponents, componentErr := useCase.componentInstallationRepo.GetAll(ctx) // load current config logger.Info("collect needed global config") - globalConfig, globalConfigErr := useCase.globalConfigRepo.GetAllByKey(ctx, effectiveBlueprint.Config.Global.GetGlobalConfigKeys()) + globalConfig, globalConfigErr := useCase.globalConfigRepo.Get(ctx) logger.Info("collect needed dogu config") doguConfig, doguConfigErr := useCase.doguConfigRepo.GetAllByKey(ctx, effectiveBlueprint.Config.GetDoguConfigKeys()) logger.Info("collect needed sensitive dogu config") diff --git a/pkg/application/stateDiffUseCase_test.go b/pkg/application/stateDiffUseCase_test.go index c787f61c..5b4a94b5 100644 --- a/pkg/application/stateDiffUseCase_test.go +++ b/pkg/application/stateDiffUseCase_test.go @@ -6,6 +6,7 @@ import ( "github.com/cloudogu/k8s-blueprint-operator/pkg/domain/common" "github.com/cloudogu/k8s-blueprint-operator/pkg/domain/ecosystem" "github.com/cloudogu/k8s-blueprint-operator/pkg/domainservice" + "github.com/cloudogu/k8s-registry-lib/config" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "testing" @@ -69,8 +70,12 @@ func TestStateDiffUseCase_DetermineStateDiff(t *testing.T) { doguInstallRepoMock.EXPECT().GetAll(testCtx).Return(nil, internalTestError) componentInstallRepoMock := newMockComponentInstallationRepository(t) componentInstallRepoMock.EXPECT().GetAll(testCtx).Return(nil, nil) - globalConfigRepoMock := newMockGlobalConfigEntryRepository(t) - globalConfigRepoMock.EXPECT().GetAllByKey(testCtx, []common.GlobalConfigKey(nil)).Return(map[common.GlobalConfigKey]*ecosystem.GlobalConfigEntry{}, nil) + + globalConfigRepoMock := newMockGlobalConfigRepository(t) + entries, _ := config.MapToEntries(map[string]any{}) + globalConfig := config.CreateGlobalConfig(entries) + globalConfigRepoMock.EXPECT().Get(testCtx).Return(globalConfig, nil) + doguConfigRepoMock := newMockDoguConfigEntryRepository(t) doguConfigRepoMock.EXPECT().GetAllByKey(testCtx, []common.DoguConfigKey(nil)).Return(map[common.DoguConfigKey]*ecosystem.DoguConfigEntry{}, nil) sensitiveDoguConfigRepoMock := newMockSensitiveDoguConfigEntryRepository(t) @@ -98,8 +103,12 @@ func TestStateDiffUseCase_DetermineStateDiff(t *testing.T) { doguInstallRepoMock.EXPECT().GetAll(testCtx).Return(map[common.SimpleDoguName]*ecosystem.DoguInstallation{}, nil) componentInstallRepoMock := newMockComponentInstallationRepository(t) componentInstallRepoMock.EXPECT().GetAll(testCtx).Return(nil, internalTestError) - globalConfigRepoMock := newMockGlobalConfigEntryRepository(t) - globalConfigRepoMock.EXPECT().GetAllByKey(testCtx, []common.GlobalConfigKey(nil)).Return(map[common.GlobalConfigKey]*ecosystem.GlobalConfigEntry{}, nil) + + globalConfigRepoMock := newMockGlobalConfigRepository(t) + entries, _ := config.MapToEntries(map[string]any{}) + globalConfig := config.CreateGlobalConfig(entries) + globalConfigRepoMock.EXPECT().Get(testCtx).Return(globalConfig, nil) + doguConfigRepoMock := newMockDoguConfigEntryRepository(t) doguConfigRepoMock.EXPECT().GetAllByKey(testCtx, []common.DoguConfigKey(nil)).Return(map[common.DoguConfigKey]*ecosystem.DoguConfigEntry{}, nil) sensitiveDoguConfigRepoMock := newMockSensitiveDoguConfigEntryRepository(t) @@ -127,10 +136,12 @@ func TestStateDiffUseCase_DetermineStateDiff(t *testing.T) { doguInstallRepoMock.EXPECT().GetAll(testCtx).Return(map[common.SimpleDoguName]*ecosystem.DoguInstallation{}, nil) componentInstallRepoMock := newMockComponentInstallationRepository(t) componentInstallRepoMock.EXPECT().GetAll(testCtx).Return(nil, nil) - globalConfigRepoMock := newMockGlobalConfigEntryRepository(t) - globalConfigRepoMock.EXPECT(). - GetAllByKey(testCtx, blueprint.EffectiveBlueprint.Config.Global.GetGlobalConfigKeys()). - Return(map[common.GlobalConfigKey]*ecosystem.GlobalConfigEntry{}, domainservice.NewInternalError(assert.AnError, "internal error")) + + globalConfigRepoMock := newMockGlobalConfigRepository(t) + entries, _ := config.MapToEntries(map[string]any{}) + globalConfig := config.CreateGlobalConfig(entries) + globalConfigRepoMock.EXPECT().Get(testCtx).Return(globalConfig, domainservice.NewInternalError(assert.AnError, "internal error")) + doguConfigRepoMock := newMockDoguConfigEntryRepository(t) doguConfigRepoMock.EXPECT().GetAllByKey(testCtx, []common.DoguConfigKey(nil)).Return(map[common.DoguConfigKey]*ecosystem.DoguConfigEntry{}, nil) sensitiveDoguConfigRepoMock := newMockSensitiveDoguConfigEntryRepository(t) @@ -160,8 +171,12 @@ func TestStateDiffUseCase_DetermineStateDiff(t *testing.T) { doguInstallRepoMock.EXPECT().GetAll(testCtx).Return(map[common.SimpleDoguName]*ecosystem.DoguInstallation{}, nil) componentInstallRepoMock := newMockComponentInstallationRepository(t) componentInstallRepoMock.EXPECT().GetAll(testCtx).Return(nil, nil) - globalConfigRepoMock := newMockGlobalConfigEntryRepository(t) - globalConfigRepoMock.EXPECT().GetAllByKey(testCtx, blueprint.EffectiveBlueprint.Config.Global.GetGlobalConfigKeys()).Return(map[common.GlobalConfigKey]*ecosystem.GlobalConfigEntry{}, nil) + + globalConfigRepoMock := newMockGlobalConfigRepository(t) + entries, _ := config.MapToEntries(map[string]any{}) + globalConfig := config.CreateGlobalConfig(entries) + globalConfigRepoMock.EXPECT().Get(testCtx).Return(globalConfig, nil) + doguConfigRepoMock := newMockDoguConfigEntryRepository(t) doguConfigRepoMock.EXPECT(). GetAllByKey(testCtx, []common.DoguConfigKey(nil)). @@ -193,8 +208,12 @@ func TestStateDiffUseCase_DetermineStateDiff(t *testing.T) { doguInstallRepoMock.EXPECT().GetAll(testCtx).Return(map[common.SimpleDoguName]*ecosystem.DoguInstallation{}, nil) componentInstallRepoMock := newMockComponentInstallationRepository(t) componentInstallRepoMock.EXPECT().GetAll(testCtx).Return(nil, nil) - globalConfigRepoMock := newMockGlobalConfigEntryRepository(t) - globalConfigRepoMock.EXPECT().GetAllByKey(testCtx, blueprint.EffectiveBlueprint.Config.Global.GetGlobalConfigKeys()).Return(map[common.GlobalConfigKey]*ecosystem.GlobalConfigEntry{}, nil) + + globalConfigRepoMock := newMockGlobalConfigRepository(t) + entries, _ := config.MapToEntries(map[string]any{}) + globalConfig := config.CreateGlobalConfig(entries) + globalConfigRepoMock.EXPECT().Get(testCtx).Return(globalConfig, nil) + doguConfigRepoMock := newMockDoguConfigEntryRepository(t) doguConfigRepoMock.EXPECT().GetAllByKey(testCtx, []common.DoguConfigKey(nil)).Return(map[common.DoguConfigKey]*ecosystem.DoguConfigEntry{}, nil) sensitiveDoguConfigRepoMock := newMockSensitiveDoguConfigEntryRepository(t) @@ -226,8 +245,12 @@ func TestStateDiffUseCase_DetermineStateDiff(t *testing.T) { doguInstallRepoMock.EXPECT().GetAll(testCtx).Return(nil, nil) componentInstallRepoMock := newMockComponentInstallationRepository(t) componentInstallRepoMock.EXPECT().GetAll(testCtx).Return(nil, nil) - globalConfigRepoMock := newMockGlobalConfigEntryRepository(t) - globalConfigRepoMock.EXPECT().GetAllByKey(testCtx, blueprint.EffectiveBlueprint.Config.Global.GetGlobalConfigKeys()).Return(map[common.GlobalConfigKey]*ecosystem.GlobalConfigEntry{}, nil) + + globalConfigRepoMock := newMockGlobalConfigRepository(t) + entries, _ := config.MapToEntries(map[string]any{}) + globalConfig := config.CreateGlobalConfig(entries) + globalConfigRepoMock.EXPECT().Get(testCtx).Return(globalConfig, nil) + doguConfigRepoMock := newMockDoguConfigEntryRepository(t) doguConfigRepoMock.EXPECT().GetAllByKey(testCtx, []common.DoguConfigKey(nil)).Return(map[common.DoguConfigKey]*ecosystem.DoguConfigEntry{}, nil) sensitiveDoguConfigRepoMock := newMockSensitiveDoguConfigEntryRepository(t) @@ -254,8 +277,12 @@ func TestStateDiffUseCase_DetermineStateDiff(t *testing.T) { doguInstallRepoMock.EXPECT().GetAll(testCtx).Return(map[common.SimpleDoguName]*ecosystem.DoguInstallation{}, nil) componentInstallRepoMock := newMockComponentInstallationRepository(t) componentInstallRepoMock.EXPECT().GetAll(testCtx).Return(nil, nil) - globalConfigRepoMock := newMockGlobalConfigEntryRepository(t) - globalConfigRepoMock.EXPECT().GetAllByKey(testCtx, blueprint.EffectiveBlueprint.Config.Global.GetGlobalConfigKeys()).Return(map[common.GlobalConfigKey]*ecosystem.GlobalConfigEntry{}, nil) + + globalConfigRepoMock := newMockGlobalConfigRepository(t) + entries, _ := config.MapToEntries(map[string]any{}) + globalConfig := config.CreateGlobalConfig(entries) + globalConfigRepoMock.EXPECT().Get(testCtx).Return(globalConfig, nil) + doguConfigRepoMock := newMockDoguConfigEntryRepository(t) doguConfigRepoMock.EXPECT().GetAllByKey(testCtx, []common.DoguConfigKey(nil)).Return(map[common.DoguConfigKey]*ecosystem.DoguConfigEntry{}, nil) sensitiveDoguConfigRepoMock := newMockSensitiveDoguConfigEntryRepository(t) @@ -315,8 +342,12 @@ func TestStateDiffUseCase_DetermineStateDiff(t *testing.T) { doguInstallRepoMock.EXPECT().GetAll(testCtx).Return(installedDogus, nil) componentInstallRepoMock := newMockComponentInstallationRepository(t) componentInstallRepoMock.EXPECT().GetAll(testCtx).Return(nil, nil) - globalConfigRepoMock := newMockGlobalConfigEntryRepository(t) - globalConfigRepoMock.EXPECT().GetAllByKey(testCtx, []common.GlobalConfigKey(nil)).Return(map[common.GlobalConfigKey]*ecosystem.GlobalConfigEntry{}, nil) + + globalConfigRepoMock := newMockGlobalConfigRepository(t) + entries, _ := config.MapToEntries(map[string]any{}) + globalConfig := config.CreateGlobalConfig(entries) + globalConfigRepoMock.EXPECT().Get(testCtx).Return(globalConfig, nil) + doguConfigRepoMock := newMockDoguConfigEntryRepository(t) doguConfigRepoMock.EXPECT().GetAllByKey(testCtx, []common.DoguConfigKey(nil)).Return(map[common.DoguConfigKey]*ecosystem.DoguConfigEntry{}, nil) sensitiveDoguConfigRepoMock := newMockSensitiveDoguConfigEntryRepository(t) @@ -414,10 +445,12 @@ func TestStateDiffUseCase_DetermineStateDiff(t *testing.T) { doguInstallRepoMock.EXPECT().GetAll(testCtx).Return(installedDogus, nil) componentInstallRepoMock := newMockComponentInstallationRepository(t) componentInstallRepoMock.EXPECT().GetAll(testCtx).Return(nil, nil) - globalConfigRepoMock := newMockGlobalConfigEntryRepository(t) - globalConfigRepoMock.EXPECT(). - GetAllByKey(testCtx, blueprint.EffectiveBlueprint.Config.Global.GetGlobalConfigKeys()). - Return(map[common.GlobalConfigKey]*ecosystem.GlobalConfigEntry{}, nil) + + globalConfigRepoMock := newMockGlobalConfigRepository(t) + entries, _ := config.MapToEntries(map[string]any{}) + globalConfig := config.CreateGlobalConfig(entries) + globalConfigRepoMock.EXPECT().Get(testCtx).Return(globalConfig, nil) + doguConfigRepoMock := newMockDoguConfigEntryRepository(t) doguConfigRepoMock.EXPECT().GetAllByKey(testCtx, []common.DoguConfigKey(nil)).Return(map[common.DoguConfigKey]*ecosystem.DoguConfigEntry{}, nil) sensitiveDoguConfigRepoMock := newMockSensitiveDoguConfigEntryRepository(t) @@ -483,8 +516,12 @@ func TestStateDiffUseCase_DetermineStateDiff(t *testing.T) { doguInstallRepoMock.EXPECT().GetAll(testCtx).Return(installedDogus, nil) componentInstallRepoMock := newMockComponentInstallationRepository(t) componentInstallRepoMock.EXPECT().GetAll(testCtx).Return(nil, nil) - globalConfigRepoMock := newMockGlobalConfigEntryRepository(t) - globalConfigRepoMock.EXPECT().GetAllByKey(testCtx, blueprint.EffectiveBlueprint.Config.Global.GetGlobalConfigKeys()).Return(map[common.GlobalConfigKey]*ecosystem.GlobalConfigEntry{}, nil) + + globalConfigRepoMock := newMockGlobalConfigRepository(t) + entries, _ := config.MapToEntries(map[string]any{}) + globalConfig := config.CreateGlobalConfig(entries) + globalConfigRepoMock.EXPECT().Get(testCtx).Return(globalConfig, nil) + doguConfigRepoMock := newMockDoguConfigEntryRepository(t) doguConfigRepoMock.EXPECT(). GetAllByKey(testCtx, []common.DoguConfigKey{ @@ -570,8 +607,12 @@ func TestStateDiffUseCase_DetermineStateDiff(t *testing.T) { doguInstallRepoMock.EXPECT().GetAll(testCtx).Return(installedDogus, nil) componentInstallRepoMock := newMockComponentInstallationRepository(t) componentInstallRepoMock.EXPECT().GetAll(testCtx).Return(nil, nil) - globalConfigRepoMock := newMockGlobalConfigEntryRepository(t) - globalConfigRepoMock.EXPECT().GetAllByKey(testCtx, blueprint.EffectiveBlueprint.Config.Global.GetGlobalConfigKeys()).Return(map[common.GlobalConfigKey]*ecosystem.GlobalConfigEntry{}, nil) + + globalConfigRepoMock := newMockGlobalConfigRepository(t) + entries, _ := config.MapToEntries(map[string]any{}) + globalConfig := config.CreateGlobalConfig(entries) + globalConfigRepoMock.EXPECT().Get(testCtx).Return(globalConfig, nil) + doguConfigRepoMock := newMockDoguConfigEntryRepository(t) doguConfigRepoMock.EXPECT().GetAllByKey(testCtx, []common.DoguConfigKey(nil)).Return(nil, nil) sensitiveDoguConfigRepoMock := newMockSensitiveDoguConfigEntryRepository(t) @@ -679,13 +720,12 @@ func TestStateDiffUseCase_collectEcosystemState(t *testing.T) { doguInstallRepoMock.EXPECT().GetAll(testCtx).Return(nil, nil) componentInstallRepoMock := newMockComponentInstallationRepository(t) componentInstallRepoMock.EXPECT().GetAll(testCtx).Return(nil, nil) - globalConfigRepoMock := newMockGlobalConfigEntryRepository(t) - globalConfigRepoMock.EXPECT(). - GetAllByKey(testCtx, effectiveBlueprint.Config.Global.GetGlobalConfigKeys()). - Return( - map[common.GlobalConfigKey]*ecosystem.GlobalConfigEntry{}, - globalConfigNotFoundError, - ) + + globalConfigRepoMock := newMockGlobalConfigRepository(t) + entries, _ := config.MapToEntries(map[string]any{}) + globalConfig := config.CreateGlobalConfig(entries) + globalConfigRepoMock.EXPECT().Get(testCtx).Return(globalConfig, globalConfigNotFoundError) + doguConfigRepoMock := newMockDoguConfigEntryRepository(t) doguConfigRepoMock.EXPECT(). GetAllByKey(testCtx, effectiveBlueprint.Config.GetDoguConfigKeys()). @@ -710,8 +750,9 @@ func TestStateDiffUseCase_collectEcosystemState(t *testing.T) { // then assert.NoError(t, err) + assert.Equal(t, ecosystem.EcosystemState{ - GlobalConfig: map[common.GlobalConfigKey]*ecosystem.GlobalConfigEntry{}, + GlobalConfig: globalConfig, DoguConfig: map[common.DoguConfigKey]*ecosystem.DoguConfigEntry{}, SensitiveDoguConfig: map[common.SensitiveDoguConfigKey]*ecosystem.SensitiveDoguConfigEntry{ nginxStaticSensitiveConfigKeyNginxKey1: encryptedEntry, @@ -761,13 +802,12 @@ func TestStateDiffUseCase_collectEcosystemState(t *testing.T) { doguInstallRepoMock.EXPECT().GetAll(testCtx).Return(nil, nil) componentInstallRepoMock := newMockComponentInstallationRepository(t) componentInstallRepoMock.EXPECT().GetAll(testCtx).Return(nil, nil) - globalConfigRepoMock := newMockGlobalConfigEntryRepository(t) - globalConfigRepoMock.EXPECT(). - GetAllByKey(testCtx, effectiveBlueprint.Config.Global.GetGlobalConfigKeys()). - Return( - map[common.GlobalConfigKey]*ecosystem.GlobalConfigEntry{}, - globalConfigNotFoundError, - ) + + globalConfigRepoMock := newMockGlobalConfigRepository(t) + entries, _ := config.MapToEntries(map[string]any{}) + globalConfig := config.CreateGlobalConfig(entries) + globalConfigRepoMock.EXPECT().Get(testCtx).Return(globalConfig, globalConfigNotFoundError) + doguConfigRepoMock := newMockDoguConfigEntryRepository(t) doguConfigRepoMock.EXPECT(). GetAllByKey(testCtx, effectiveBlueprint.Config.GetDoguConfigKeys()). diff --git a/pkg/bootstrap.go b/pkg/bootstrap.go index ab808e6f..5bed7ce0 100644 --- a/pkg/bootstrap.go +++ b/pkg/bootstrap.go @@ -73,6 +73,7 @@ func Bootstrap(restConfig *rest.Config, eventRecorder record.EventRecorder, name return nil, err } + //TODO: move maintenance mode adapter to registry lib maintenanceMode := maintenance.New(configRegistry.GlobalConfig()) remoteDoguRegistry, err := createRemoteDoguRegistry() @@ -82,7 +83,6 @@ func Bootstrap(restConfig *rest.Config, eventRecorder record.EventRecorder, name doguConfigAdapter := adapterconfigetcd.NewDoguConfigRepository(configRegistry) sensitiveDoguConfigAdapter := adapterconfigetcd.NewSensitiveDoguConfigRepository(configRegistry) - globalConfigEntryRepoAdapter := adapterconfigetcd.NewGlobalConfigRepository(configRegistry.GlobalConfig()) k8sGlobalConfigRepo := repository.NewGlobalConfigRepository(ecosystemClientSet.CoreV1().ConfigMaps(namespace)) globalConfigRepoAdapter := adapterconfigk8s.NewGlobalConfigRepository(*k8sGlobalConfigRepo) @@ -95,7 +95,7 @@ func Bootstrap(restConfig *rest.Config, eventRecorder record.EventRecorder, name blueprintSpecDomainUseCase := domainservice.NewValidateDependenciesDomainUseCase(remoteDoguRegistry) blueprintValidationUseCase := application.NewBlueprintSpecValidationUseCase(blueprintSpecRepository, blueprintSpecDomainUseCase) effectiveBlueprintUseCase := application.NewEffectiveBlueprintUseCase(blueprintSpecRepository) - stateDiffUseCase := application.NewStateDiffUseCase(blueprintSpecRepository, doguInstallationRepo, componentInstallationRepo, globalConfigEntryRepoAdapter, doguConfigAdapter, sensitiveDoguConfigAdapter) + stateDiffUseCase := application.NewStateDiffUseCase(blueprintSpecRepository, doguInstallationRepo, componentInstallationRepo, globalConfigRepoAdapter, doguConfigAdapter, sensitiveDoguConfigAdapter) doguInstallationUseCase := application.NewDoguInstallationUseCase(blueprintSpecRepository, doguInstallationRepo, healthConfigRepo) componentInstallationUseCase := application.NewComponentInstallationUseCase(blueprintSpecRepository, componentInstallationRepo, healthConfigRepo) ecosystemHealthUseCase := application.NewEcosystemHealthUseCase(doguInstallationUseCase, componentInstallationUseCase, healthConfigRepo) diff --git a/pkg/domain/ecosystem/EcosystemState.go b/pkg/domain/ecosystem/EcosystemState.go index c8741033..0b565a78 100644 --- a/pkg/domain/ecosystem/EcosystemState.go +++ b/pkg/domain/ecosystem/EcosystemState.go @@ -2,13 +2,14 @@ package ecosystem import ( "github.com/cloudogu/k8s-blueprint-operator/pkg/domain/common" + "github.com/cloudogu/k8s-registry-lib/config" "golang.org/x/exp/maps" ) type EcosystemState struct { InstalledDogus map[common.SimpleDoguName]*DoguInstallation InstalledComponents map[common.SimpleComponentName]*ComponentInstallation - GlobalConfig map[common.GlobalConfigKey]*GlobalConfigEntry + GlobalConfig config.GlobalConfig DoguConfig map[common.DoguConfigKey]*DoguConfigEntry SensitiveDoguConfig map[common.SensitiveDoguConfigKey]*SensitiveDoguConfigEntry } diff --git a/pkg/domain/stateDiffConfig_test.go b/pkg/domain/stateDiffConfig_test.go index 713fbe06..83d3b04d 100644 --- a/pkg/domain/stateDiffConfig_test.go +++ b/pkg/domain/stateDiffConfig_test.go @@ -3,6 +3,7 @@ package domain import ( "github.com/cloudogu/k8s-blueprint-operator/pkg/domain/common" "github.com/cloudogu/k8s-blueprint-operator/pkg/domain/ecosystem" + "github.com/cloudogu/k8s-registry-lib/config" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "testing" @@ -23,28 +24,31 @@ var ( func Test_determineConfigDiff(t *testing.T) { t.Run("empty", func(t *testing.T) { - config := Config{} + emptyConfig := Config{} clusterState := ecosystem.EcosystemState{} - dogusConfigDiffs, globalConfigDiff := determineConfigDiffs(config, clusterState) + dogusConfigDiffs, globalConfigDiff := determineConfigDiffs(emptyConfig, clusterState) assert.Equal(t, map[common.SimpleDoguName]CombinedDoguConfigDiffs{}, dogusConfigDiffs) assert.Equal(t, GlobalConfigDiffs(nil), globalConfigDiff) }) t.Run("all actions global config", func(t *testing.T) { //given ecosystem config + + entries, _ := config.MapToEntries(map[string]any{ + "key1": "value1", // for action none + "key2": "value2", // for action set + "key3": "value3", // for action delete + // key4 is absent -> action none + }) + globalConfig := config.CreateGlobalConfig(entries) clusterState := ecosystem.EcosystemState{ - GlobalConfig: map[common.GlobalConfigKey]*ecosystem.GlobalConfigEntry{ - "key1": {Key: "key1", Value: "value1"}, // for action none - "key2": {Key: "key2", Value: "value2"}, // for action set - "key3": {Key: "key3", Value: "value3"}, // for action delete - // key4 is absent -> action none - }, + GlobalConfig: globalConfig, DoguConfig: map[common.DoguConfigKey]*ecosystem.DoguConfigEntry{}, SensitiveDoguConfig: map[common.SensitiveDoguConfigKey]*ecosystem.SensitiveDoguConfigEntry{}, } //given blueprint config - config := Config{ + givenConfig := Config{ Global: GlobalConfig{ Present: map[common.GlobalConfigKey]common.GlobalConfigValue{ "key1": "value1", @@ -57,7 +61,7 @@ func Test_determineConfigDiff(t *testing.T) { } //when - dogusConfigDiffs, globalConfigDiff := determineConfigDiffs(config, clusterState) + dogusConfigDiffs, globalConfigDiff := determineConfigDiffs(givenConfig, clusterState) //then assert.Equal(t, map[common.SimpleDoguName]CombinedDoguConfigDiffs{}, dogusConfigDiffs) @@ -112,9 +116,12 @@ func Test_determineConfigDiff(t *testing.T) { }) }) t.Run("all actions normal dogu config", func(t *testing.T) { + + entries, _ := config.MapToEntries(map[string]any{}) + globalConfig := config.CreateGlobalConfig(entries) //given ecosystem config clusterState := ecosystem.EcosystemState{ - GlobalConfig: map[common.GlobalConfigKey]*ecosystem.GlobalConfigEntry{}, + GlobalConfig: globalConfig, DoguConfig: map[common.DoguConfigKey]*ecosystem.DoguConfigEntry{ dogu1Key1: {Key: dogu1Key1, Value: "value"}, //action none dogu1Key2: {Key: dogu1Key2, Value: "value"}, //action set @@ -126,7 +133,7 @@ func Test_determineConfigDiff(t *testing.T) { } //given blueprint config - config := Config{ + givenConfig := Config{ Dogus: map[common.SimpleDoguName]CombinedDoguConfig{ "dogu1": { DoguName: "dogu1", @@ -144,7 +151,7 @@ func Test_determineConfigDiff(t *testing.T) { } //when - dogusConfigDiffs, globalConfigDiff := determineConfigDiffs(config, clusterState) + dogusConfigDiffs, globalConfigDiff := determineConfigDiffs(givenConfig, clusterState) //then assert.Equal(t, GlobalConfigDiffs(nil), globalConfigDiff) require.NotNil(t, dogusConfigDiffs["dogu1"]) @@ -217,7 +224,7 @@ func Test_determineConfigDiff(t *testing.T) { } //given blueprint config - config := Config{ + givenConfig := Config{ Dogus: map[common.SimpleDoguName]CombinedDoguConfig{ "dogu1": { DoguName: "dogu1", @@ -243,7 +250,7 @@ func Test_determineConfigDiff(t *testing.T) { } //when - dogusConfigDiffs, globalConfigDiff := determineConfigDiffs(config, clusterState) + dogusConfigDiffs, globalConfigDiff := determineConfigDiffs(givenConfig, clusterState) //then assert.Equal(t, GlobalConfigDiffs(nil), globalConfigDiff) require.NotNil(t, dogusConfigDiffs["dogu1"]) @@ -328,7 +335,7 @@ func Test_determineConfigDiff(t *testing.T) { clusterState := ecosystem.EcosystemState{} //given blueprint config - config := Config{ + givenConfig := Config{ Dogus: map[common.SimpleDoguName]CombinedDoguConfig{ "dogu1": { DoguName: "dogu1", @@ -343,7 +350,7 @@ func Test_determineConfigDiff(t *testing.T) { } //when - dogusConfigDiffs, _ := determineConfigDiffs(config, clusterState) + dogusConfigDiffs, _ := determineConfigDiffs(givenConfig, clusterState) //then require.NotNil(t, dogusConfigDiffs["dogu1"]) require.Equal(t, 1, len(dogusConfigDiffs["dogu1"].SensitiveDoguConfigDiff)) diff --git a/pkg/domain/stateDiffGlobalConfig.go b/pkg/domain/stateDiffGlobalConfig.go index 22ab0b9b..53d7b4b5 100644 --- a/pkg/domain/stateDiffGlobalConfig.go +++ b/pkg/domain/stateDiffGlobalConfig.go @@ -4,6 +4,7 @@ import ( "github.com/cloudogu/k8s-blueprint-operator/pkg/domain/common" "github.com/cloudogu/k8s-blueprint-operator/pkg/domain/ecosystem" "github.com/cloudogu/k8s-blueprint-operator/pkg/util" + "github.com/cloudogu/k8s-registry-lib/config" ) type GlobalConfigDiffs []GlobalConfigEntryDiff @@ -52,11 +53,15 @@ func (diffs GlobalConfigDiffs) countByAction() map[ConfigAction]int { func newGlobalConfigEntryDiff( key common.GlobalConfigKey, - actualEntry *ecosystem.GlobalConfigEntry, + actualValue common.GlobalConfigValue, + actualExists bool, expectedValue common.GlobalConfigValue, expectedExists bool, ) GlobalConfigEntryDiff { - actual := NewGlobalConfigValueStateFromEntry(actualEntry) + actual := GlobalConfigValueState{ + Value: string(actualValue), + Exists: actualExists, + } expected := GlobalConfigValueState{ Value: string(expectedValue), Exists: expectedExists, @@ -71,19 +76,19 @@ func newGlobalConfigEntryDiff( func determineGlobalConfigDiffs( config GlobalConfig, - actualDoguConfig map[common.GlobalConfigKey]*ecosystem.GlobalConfigEntry, + actualDoguConfig config.GlobalConfig, ) GlobalConfigDiffs { var configDiffs []GlobalConfigEntryDiff // present entries for key, expectedValue := range config.Present { - actualEntry := actualDoguConfig[key] - configDiffs = append(configDiffs, newGlobalConfigEntryDiff(key, actualEntry, expectedValue, true)) + actualEntry, actualExists := actualDoguConfig.Get(key) + configDiffs = append(configDiffs, newGlobalConfigEntryDiff(key, actualEntry, actualExists, expectedValue, true)) } // absent entries for _, key := range config.Absent { - actualEntry := actualDoguConfig[key] - configDiffs = append(configDiffs, newGlobalConfigEntryDiff(key, actualEntry, "", false)) + actualEntry, actualExists := actualDoguConfig.Get(key) + configDiffs = append(configDiffs, newGlobalConfigEntryDiff(key, actualEntry, actualExists, "", false)) } return configDiffs }