Skip to content

Commit

Permalink
#81 inject new globalConfigRepo in configUseCase
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-dammeier committed Oct 7, 2024
1 parent 8dfbdce commit 79a5f48
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
16 changes: 12 additions & 4 deletions pkg/application/ecosystemConfigUseCase.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,24 @@ type EcosystemConfigUseCase struct {
blueprintRepository blueprintSpecRepository
doguConfigRepository doguConfigEntryRepository
doguSensitiveConfigRepository sensitiveDoguConfigEntryRepository
globalConfigRepository globalConfigEntryRepository
globalConfigEntryRepository globalConfigEntryRepository
globalConfigRepository globalConfigRepository
}

var errSensitiveDoguConfigEntry = fmt.Errorf("sensitive dogu config error")

func NewEcosystemConfigUseCase(blueprintRepository blueprintSpecRepository, doguConfigRepository doguConfigEntryRepository, doguSensitiveConfigRepository sensitiveDoguConfigEntryRepository, globalConfigRepository globalConfigEntryRepository) *EcosystemConfigUseCase {
func NewEcosystemConfigUseCase(
blueprintRepository blueprintSpecRepository,
doguConfigRepository doguConfigEntryRepository,
doguSensitiveConfigRepository sensitiveDoguConfigEntryRepository,
globalConfigEntryRepository globalConfigEntryRepository,
globalConfigRepository globalConfigRepository,
) *EcosystemConfigUseCase {
return &EcosystemConfigUseCase{
blueprintRepository: blueprintRepository,
doguConfigRepository: doguConfigRepository,
doguSensitiveConfigRepository: doguSensitiveConfigRepository,
globalConfigEntryRepository: globalConfigEntryRepository,
globalConfigRepository: globalConfigRepository,
}
}
Expand Down Expand Up @@ -91,8 +99,8 @@ func (useCase *EcosystemConfigUseCase) applyGlobalConfigDiffs(ctx context.Contex
keysToDelete = append(keysToDelete, diff.Key)
}

errs = append(errs, callIfNotEmpty(ctx, entriesToSet, useCase.globalConfigRepository.SaveAll))
errs = append(errs, callIfNotEmpty(ctx, keysToDelete, useCase.globalConfigRepository.DeleteAllByKeys))
errs = append(errs, callIfNotEmpty(ctx, entriesToSet, useCase.globalConfigEntryRepository.SaveAll))
errs = append(errs, callIfNotEmpty(ctx, keysToDelete, useCase.globalConfigEntryRepository.DeleteAllByKeys))

return errors.Join(errs...)
}
Expand Down
26 changes: 13 additions & 13 deletions pkg/application/ecosystemConfigUseCase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestEcosystemConfigUseCase_ApplyConfig(t *testing.T) {
blueprintRepoMock.EXPECT().GetById(testCtx, testBlueprintID).Return(spec, nil)
blueprintRepoMock.EXPECT().Update(testCtx, mock.Anything).Return(nil).Times(2)

sut := EcosystemConfigUseCase{blueprintRepository: blueprintRepoMock, doguConfigRepository: doguConfigMock, doguSensitiveConfigRepository: sensitiveDoguConfigMock, globalConfigRepository: globalConfigMock}
sut := EcosystemConfigUseCase{blueprintRepository: blueprintRepoMock, doguConfigRepository: doguConfigMock, doguSensitiveConfigRepository: sensitiveDoguConfigMock, globalConfigEntryRepository: globalConfigMock}

// when
err := sut.ApplyConfig(testCtx, testBlueprintID)
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestEcosystemConfigUseCase_ApplyConfig(t *testing.T) {
blueprintRepoMock.EXPECT().GetById(testCtx, testBlueprintID).Return(spec, nil)
blueprintRepoMock.EXPECT().Update(testCtx, mock.Anything).Return(nil).Times(2)

sut := EcosystemConfigUseCase{blueprintRepository: blueprintRepoMock, doguConfigRepository: doguConfigMock, doguSensitiveConfigRepository: sensitiveDoguConfigMock, globalConfigRepository: globalConfigMock}
sut := EcosystemConfigUseCase{blueprintRepository: blueprintRepoMock, doguConfigRepository: doguConfigMock, doguSensitiveConfigRepository: sensitiveDoguConfigMock, globalConfigEntryRepository: globalConfigMock}

// when
err := sut.ApplyConfig(testCtx, testBlueprintID)
Expand All @@ -206,7 +206,7 @@ func TestEcosystemConfigUseCase_applyDoguConfigDiffs(t *testing.T) {
t.Run("should save diffs with action set", func(t *testing.T) {
// given
doguConfigMock := newMockDoguConfigEntryRepository(t)
sut := NewEcosystemConfigUseCase(nil, doguConfigMock, nil, nil)
sut := NewEcosystemConfigUseCase(nil, doguConfigMock, nil, nil, nil)
diff1 := getSetDoguConfigEntryDiff("/key", "value", testSimpleDoguNameRedmine)
diff2 := getSetDoguConfigEntryDiff("/key1", "value1", testSimpleDoguNameRedmine)
byAction := map[domain.ConfigAction]domain.DoguConfigDiffs{domain.ConfigActionSet: {diff1, diff2}}
Expand All @@ -232,7 +232,7 @@ func TestEcosystemConfigUseCase_applyDoguConfigDiffs(t *testing.T) {
t.Run("should delete diffs with action remove", func(t *testing.T) {
// given
doguConfigMock := newMockDoguConfigEntryRepository(t)
sut := NewEcosystemConfigUseCase(nil, doguConfigMock, nil, nil)
sut := NewEcosystemConfigUseCase(nil, doguConfigMock, nil, nil, nil)
diff1 := getRemoveDoguConfigEntryDiff("/key", testSimpleDoguNameRedmine)
diff2 := getRemoveDoguConfigEntryDiff("/key1", testSimpleDoguNameRedmine)
byAction := map[domain.ConfigAction]domain.DoguConfigDiffs{domain.ConfigActionRemove: {diff1, diff2}}
Expand All @@ -252,7 +252,7 @@ func TestEcosystemConfigUseCase_applyDoguConfigDiffs(t *testing.T) {
t.Run("should return nil on action none", func(t *testing.T) {
// given
doguConfigMock := newMockDoguConfigEntryRepository(t)
sut := NewEcosystemConfigUseCase(nil, doguConfigMock, nil, nil)
sut := NewEcosystemConfigUseCase(nil, doguConfigMock, nil, nil, nil)
diff1 := domain.DoguConfigEntryDiff{
NeededAction: domain.ConfigActionNone,
}
Expand All @@ -270,7 +270,7 @@ func TestEcosystemConfigUseCase_applyGlobalConfigDiffs(t *testing.T) {
t.Run("should save diffs with action set", func(t *testing.T) {
// given
globalConfigMock := newMockGlobalConfigEntryRepository(t)
sut := NewEcosystemConfigUseCase(nil, nil, nil, globalConfigMock)
sut := NewEcosystemConfigUseCase(nil, nil, nil, globalConfigMock, nil)
diff1 := getSetGlobalConfigEntryDiff("/key", "value")
diff2 := getSetGlobalConfigEntryDiff("/key1", "value1")
byAction := map[domain.ConfigAction][]domain.GlobalConfigEntryDiff{domain.ConfigActionSet: {diff1, diff2}}
Expand All @@ -296,7 +296,7 @@ func TestEcosystemConfigUseCase_applyGlobalConfigDiffs(t *testing.T) {
t.Run("should delete diffs with action remove", func(t *testing.T) {
// given
globalConfigMock := newMockGlobalConfigEntryRepository(t)
sut := NewEcosystemConfigUseCase(nil, nil, nil, globalConfigMock)
sut := NewEcosystemConfigUseCase(nil, nil, nil, globalConfigMock, nil)
diff1 := getRemoveGlobalConfigEntryDiff("/key")
diff2 := getRemoveGlobalConfigEntryDiff("/key1")
byAction := map[domain.ConfigAction][]domain.GlobalConfigEntryDiff{domain.ConfigActionRemove: {diff1, diff2}}
Expand All @@ -312,7 +312,7 @@ func TestEcosystemConfigUseCase_applyGlobalConfigDiffs(t *testing.T) {

t.Run("should return nil on action none", func(t *testing.T) {
// given
sut := NewEcosystemConfigUseCase(nil, nil, nil, newMockGlobalConfigEntryRepository(t))
sut := NewEcosystemConfigUseCase(nil, nil, nil, newMockGlobalConfigEntryRepository(t), nil)
diff1 := domain.GlobalConfigEntryDiff{
NeededAction: domain.ConfigActionNone,
}
Expand All @@ -330,7 +330,7 @@ func TestEcosystemConfigUseCase_applySensitiveDoguConfigDiffs(t *testing.T) {
t.Run("should save diffs with action set", func(t *testing.T) {
// given
sensitiveDoguConfigMock := newMockSensitiveDoguConfigEntryRepository(t)
sut := NewEcosystemConfigUseCase(nil, nil, sensitiveDoguConfigMock, nil)
sut := NewEcosystemConfigUseCase(nil, nil, sensitiveDoguConfigMock, nil, nil)
diff1 := getSensitiveDoguConfigEntryDiffForAction("key1", "value1", testSimpleDoguNameRedmine, domain.ConfigActionSet)
diff2 := getSensitiveDoguConfigEntryDiffForAction("key2", "value2", testSimpleDoguNameRedmine, domain.ConfigActionSet)
byAction := map[domain.ConfigAction]domain.SensitiveDoguConfigDiffs{domain.ConfigActionSet: {diff1, diff2}}
Expand All @@ -356,7 +356,7 @@ func TestEcosystemConfigUseCase_applySensitiveDoguConfigDiffs(t *testing.T) {
t.Run("should delete diffs with action remove", func(t *testing.T) {
// given
sensitiveDoguConfigMock := newMockSensitiveDoguConfigEntryRepository(t)
sut := NewEcosystemConfigUseCase(nil, nil, sensitiveDoguConfigMock, nil)
sut := NewEcosystemConfigUseCase(nil, nil, sensitiveDoguConfigMock, nil, nil)
diff1 := getRemoveSensitiveDoguConfigEntryDiff("key", testSimpleDoguNameRedmine)
diff2 := getRemoveSensitiveDoguConfigEntryDiff("key", testSimpleDoguNameRedmine)
byAction := map[domain.ConfigAction]domain.SensitiveDoguConfigDiffs{domain.ConfigActionRemove: {diff1, diff2}}
Expand All @@ -375,7 +375,7 @@ func TestEcosystemConfigUseCase_applySensitiveDoguConfigDiffs(t *testing.T) {

t.Run("should return nil on action none", func(t *testing.T) {
// given
sut := NewEcosystemConfigUseCase(nil, nil, newMockSensitiveDoguConfigEntryRepository(t), nil)
sut := NewEcosystemConfigUseCase(nil, nil, newMockSensitiveDoguConfigEntryRepository(t), nil, nil)
diff1 := domain.SensitiveDoguConfigEntryDiff{
NeededAction: domain.ConfigActionNone,
}
Expand Down Expand Up @@ -520,13 +520,13 @@ func TestNewEcosystemConfigUseCase(t *testing.T) {
globalConfigMock := newMockGlobalConfigEntryRepository(t)

// when
useCase := NewEcosystemConfigUseCase(blueprintRepoMock, doguConfigMock, sensitiveDoguConfigMock, globalConfigMock)
useCase := NewEcosystemConfigUseCase(blueprintRepoMock, doguConfigMock, sensitiveDoguConfigMock, globalConfigMock, nil)

// then
assert.Equal(t, blueprintRepoMock, useCase.blueprintRepository)
assert.Equal(t, doguConfigMock, useCase.doguConfigRepository)
assert.Equal(t, sensitiveDoguConfigMock, useCase.doguSensitiveConfigRepository)
assert.Equal(t, globalConfigMock, useCase.globalConfigRepository)
assert.Equal(t, globalConfigMock, useCase.globalConfigEntryRepository)
})
}

Expand Down
12 changes: 8 additions & 4 deletions pkg/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package pkg
import (
"fmt"
adapterconfigetcd "github.com/cloudogu/k8s-blueprint-operator/pkg/adapter/config/etcd"
adapterconfigk8s "github.com/cloudogu/k8s-blueprint-operator/pkg/adapter/config/kubernetes"
"github.com/cloudogu/k8s-blueprint-operator/pkg/adapter/kubernetes/restartcr"
"github.com/cloudogu/k8s-registry-lib/repository"

"github.com/cloudogu/k8s-blueprint-operator/pkg/domain/common"

Expand Down Expand Up @@ -80,7 +82,9 @@ func Bootstrap(restConfig *rest.Config, eventRecorder record.EventRecorder, name

doguConfigAdapter := adapterconfigetcd.NewDoguConfigRepository(configRegistry)
sensitiveDoguConfigAdapter := adapterconfigetcd.NewSensitiveDoguConfigRepository(configRegistry)
globalConfigAdapter := adapterconfigetcd.NewGlobalConfigRepository(configRegistry.GlobalConfig())
globalConfigEntryRepoAdapter := adapterconfigetcd.NewGlobalConfigRepository(configRegistry.GlobalConfig())
k8sGlobalConfigRepo := repository.NewGlobalConfigRepository(ecosystemClientSet.CoreV1().ConfigMaps(namespace))
globalConfigRepoAdapter := adapterconfigk8s.NewGlobalConfigRepository(*k8sGlobalConfigRepo)

doguInstallationRepo := dogucr.NewDoguInstallationRepo(dogusInterface.Dogus(namespace), ecosystemClientSet.CoreV1().PersistentVolumeClaims(namespace))
componentInstallationRepo := componentcr.NewComponentInstallationRepo(componentsInterface.Components(namespace))
Expand All @@ -91,20 +95,20 @@ 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, globalConfigAdapter, doguConfigAdapter, sensitiveDoguConfigAdapter)
stateDiffUseCase := application.NewStateDiffUseCase(blueprintSpecRepository, doguInstallationRepo, componentInstallationRepo, globalConfigEntryRepoAdapter, doguConfigAdapter, sensitiveDoguConfigAdapter)
doguInstallationUseCase := application.NewDoguInstallationUseCase(blueprintSpecRepository, doguInstallationRepo, healthConfigRepo)
componentInstallationUseCase := application.NewComponentInstallationUseCase(blueprintSpecRepository, componentInstallationRepo, healthConfigRepo)
ecosystemHealthUseCase := application.NewEcosystemHealthUseCase(doguInstallationUseCase, componentInstallationUseCase, healthConfigRepo)
applyBlueprintSpecUseCase := application.NewApplyBlueprintSpecUseCase(blueprintSpecRepository, doguInstallationUseCase, ecosystemHealthUseCase, componentInstallationUseCase, maintenanceMode)
registryConfigUseCase := application.NewEcosystemConfigUseCase(blueprintSpecRepository, doguConfigAdapter, sensitiveDoguConfigAdapter, globalConfigAdapter)
ConfigUseCase := application.NewEcosystemConfigUseCase(blueprintSpecRepository, doguConfigAdapter, sensitiveDoguConfigAdapter, globalConfigEntryRepoAdapter, globalConfigRepoAdapter)
doguRestartUseCase := application.NewDoguRestartUseCase(doguInstallationRepo, blueprintSpecRepository, restartRepository)

selfUpgradeUseCase := application.NewSelfUpgradeUseCase(blueprintSpecRepository, componentInstallationRepo, componentInstallationUseCase, blueprintOperatorName.SimpleName, healthConfigRepo)

blueprintChangeUseCase := application.NewBlueprintSpecChangeUseCase(
blueprintSpecRepository, blueprintValidationUseCase,
effectiveBlueprintUseCase, stateDiffUseCase,
applyBlueprintSpecUseCase, registryConfigUseCase,
applyBlueprintSpecUseCase, ConfigUseCase,
doguRestartUseCase,
selfUpgradeUseCase,
)
Expand Down

0 comments on commit 79a5f48

Please sign in to comment.