Skip to content

Commit

Permalink
#81 WIP remove CombinedDoguConfigDiffs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-dammeier committed Oct 30, 2024
1 parent 65f4898 commit 4f4ec23
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 139 deletions.
38 changes: 14 additions & 24 deletions pkg/application/ecosystemConfigUseCase.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,14 @@ func (useCase *EcosystemConfigUseCase) ApplyConfig(ctx context.Context, blueprin
return useCase.handleFailedApplyEcosystemConfig(ctx, blueprintSpec, err)
}

// do not apply further configs if error happens, we don't want to corrupt the system more than needed
// apply normal and sensitive config with this
err = useCase.applyDoguConfigDiffs(ctx, blueprintSpec.StateDiff.DoguConfigDiffs)
// do not apply further configs if error happens, we don't want to corrupt the system more than needed.
err = applyDoguConfigDiffs(ctx, useCase.doguConfigRepository, blueprintSpec.StateDiff.DoguConfigDiffs)
if err != nil {
return useCase.handleFailedApplyEcosystemConfig(ctx, blueprintSpec, err)
return useCase.handleFailedApplyEcosystemConfig(ctx, blueprintSpec, fmt.Errorf("could not apply normal dogu config: %w", err))
}
err = applyDoguConfigDiffs(ctx, useCase.sensitiveDoguConfigRepository, blueprintSpec.StateDiff.SensitiveDoguConfigDiffs)
if err != nil {
return useCase.handleFailedApplyEcosystemConfig(ctx, blueprintSpec, fmt.Errorf("could not apply normal dogu config: %w", err))
}
err = useCase.applyGlobalConfigDiffs(ctx, globalConfigDiffs.GetGlobalConfigDiffsByAction())
if err != nil {
Expand Down Expand Up @@ -107,34 +110,21 @@ func (useCase *EcosystemConfigUseCase) applyGlobalConfigDiffs(ctx context.Contex
return errors.Join(errs...)
}

func (useCase *EcosystemConfigUseCase) applyDoguConfigDiffs(
func applyDoguConfigDiffs(
ctx context.Context,
diffsByDogu map[common.SimpleDoguName]domain.CombinedDoguConfigDiffs,
repo doguConfigRepository,
diffsByDogu map[common.SimpleDoguName]domain.DoguConfigDiffs,
) error {
var doguConfigDiffs = map[common.SimpleDoguName]domain.DoguConfigDiffs{}
var sensitiveDoguConfigDiffs = map[common.SimpleDoguName]domain.SensitiveDoguConfigDiffs{}

for dogu, combinedDiff := range diffsByDogu {
for dogu, entryDiffs := range diffsByDogu {
// only collect doguConfigs with changes, so we don't need to load all.
if combinedDiff.DoguConfigDiff.HasChangesForDogu(dogu) {
doguConfigDiffs[dogu] = combinedDiff.DoguConfigDiff
}
if combinedDiff.SensitiveDoguConfigDiff.HasChangesForDogu(dogu) {
sensitiveDoguConfigDiffs[dogu] = combinedDiff.SensitiveDoguConfigDiff
if entryDiffs.HasChanges() {
doguConfigDiffs[dogu] = entryDiffs
}
}

err := saveDoguConfigs(ctx, useCase.doguConfigRepository, doguConfigDiffs)
if err != nil {
return fmt.Errorf("could not apply normal dogu config: %w", err)
}

err = saveDoguConfigs(ctx, useCase.sensitiveDoguConfigRepository, sensitiveDoguConfigDiffs)
if err != nil {
return fmt.Errorf("could not apply sensitive dogu config: %w", err)
}

return nil
return saveDoguConfigs(ctx, repo, doguConfigDiffs)
}

func saveDoguConfigs(
Expand Down
21 changes: 11 additions & 10 deletions pkg/domain/blueprintSpec.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,24 +273,25 @@ func (spec *BlueprintSpec) DetermineStateDiff(
// we need to analyze first, what kind of error this is. Why do we need one?
return err
}
doguConfigDiffs, globalConfigDiffs := determineConfigDiffs(
doguConfigDiffs, sensitiveDoguConfigDiffs, globalConfigDiffs := determineConfigDiffs(
spec.EffectiveBlueprint.Config,
ecosystemState.GlobalConfig,
ecosystemState.ConfigByDogu,
ecosystemState.SensitiveConfigByDogu,
)

spec.StateDiff = StateDiff{
DoguDiffs: doguDiffs,
ComponentDiffs: compDiffs,
DoguConfigDiffs: doguConfigDiffs,
GlobalConfigDiffs: globalConfigDiffs,
DoguDiffs: doguDiffs,
ComponentDiffs: compDiffs,
DoguConfigDiffs: doguConfigDiffs,
SensitiveDoguConfigDiffs: sensitiveDoguConfigDiffs,
GlobalConfigDiffs: globalConfigDiffs,
}

spec.Events = append(spec.Events, newStateDiffDoguEvent(spec.StateDiff.DoguDiffs))
spec.Events = append(spec.Events, newStateDiffComponentEvent(spec.StateDiff.ComponentDiffs))
spec.Events = append(spec.Events, GlobalConfigDiffDeterminedEvent{GlobalConfigDiffs: spec.StateDiff.GlobalConfigDiffs})
spec.Events = append(spec.Events, DoguConfigDiffDeterminedEvent{spec.StateDiff.DoguConfigDiffs})
spec.Events = append(spec.Events, NewDoguConfigDiffDeterminedEvent(spec.StateDiff.DoguConfigDiffs, spec.StateDiff.SensitiveDoguConfigDiffs))

invalidBlueprintError := spec.validateStateDiff()
if invalidBlueprintError != nil {
Expand Down Expand Up @@ -384,9 +385,7 @@ func (spec *BlueprintSpec) MarkBlueprintApplied() {
func (spec *BlueprintSpec) CensorSensitiveData() {
spec.Blueprint.Config = spec.Blueprint.Config.censorValues()
spec.EffectiveBlueprint.Config = spec.EffectiveBlueprint.Config.censorValues()
for k, v := range spec.StateDiff.DoguConfigDiffs {
spec.StateDiff.DoguConfigDiffs[k] = v.censorValues()
}
spec.StateDiff.DoguConfigDiffs = censorValues(spec.StateDiff.DoguConfigDiffs)

spec.Events = append(spec.Events, SensitiveConfigDataCensoredEvent{})
}
Expand Down Expand Up @@ -466,7 +465,9 @@ func (spec *BlueprintSpec) GetDogusThatNeedARestart() []common.SimpleDoguName {
var dogusThatNeedRestart []common.SimpleDoguName
dogusInEffectiveBlueprint := spec.EffectiveBlueprint.Dogus
for _, dogu := range dogusInEffectiveBlueprint {
if spec.StateDiff.DoguConfigDiffs[dogu.Name.SimpleName].HasChanges() {
//TODO: test this
if spec.StateDiff.DoguConfigDiffs[dogu.Name.SimpleName].HasChanges() ||
spec.StateDiff.SensitiveDoguConfigDiffs[dogu.Name.SimpleName].HasChanges() {
dogusThatNeedRestart = append(dogusThatNeedRestart, dogu.Name.SimpleName)
}
}
Expand Down
54 changes: 29 additions & 25 deletions pkg/domain/blueprintSpec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,22 @@ func TestBlueprintSpec_DetermineStateDiff(t *testing.T) {
err := spec.DetermineStateDiff(clusterState)

// then
stateDiff := StateDiff{DoguDiffs: DoguDiffs{}, ComponentDiffs: ComponentDiffs{}, DoguConfigDiffs: map[common.SimpleDoguName]CombinedDoguConfigDiffs{}}
stateDiff := StateDiff{
DoguDiffs: DoguDiffs{},
ComponentDiffs: ComponentDiffs{},
DoguConfigDiffs: map[common.SimpleDoguName]DoguConfigDiffs{},
SensitiveDoguConfigDiffs: map[common.SimpleDoguName]SensitiveDoguConfigDiffs{},
}
require.NoError(t, err)
assert.Equal(t, StatusPhaseStateDiffDetermined, spec.Status)
require.Equal(t, 4, len(spec.Events))
assert.Equal(t, newStateDiffDoguEvent(stateDiff.DoguDiffs), spec.Events[0])
assert.Equal(t, newStateDiffComponentEvent(stateDiff.ComponentDiffs), spec.Events[1])
assert.Equal(t, GlobalConfigDiffDeterminedEvent{GlobalConfigDiffs: GlobalConfigDiffs(nil)}, spec.Events[2])
assert.Equal(t, DoguConfigDiffDeterminedEvent{CombinedDogusConfigDiffs: map[common.SimpleDoguName]CombinedDoguConfigDiffs{}}, spec.Events[3])
assert.Equal(t, DoguConfigDiffDeterminedEvent{
DoguConfigDiffs: map[common.SimpleDoguName]DoguConfigDiffs{},
SensitiveDoguConfigDiffs: map[common.SimpleDoguName]SensitiveDoguConfigDiffs{},
}, spec.Events[3])
assert.Equal(t, stateDiff, spec.StateDiff)
})

Expand Down Expand Up @@ -743,11 +751,11 @@ func TestBlueprintSpec_CensorSensitiveData(t *testing.T) {
},
},
StateDiff: StateDiff{
DoguConfigDiffs: map[common.SimpleDoguName]CombinedDoguConfigDiffs{
"ldapDiff": {SensitiveDoguConfigDiff: []SensitiveDoguConfigEntryDiff{{
SensitiveDoguConfigDiffs: map[common.SimpleDoguName]SensitiveDoguConfigDiffs{
"ldapDiff": []SensitiveDoguConfigEntryDiff{{
Actual: DoguConfigValueState{Value: "Test1"},
Expected: DoguConfigValueState{Value: "Test2"},
}}},
}},
},
},
}
Expand All @@ -765,9 +773,9 @@ func TestBlueprintSpec_CensorSensitiveData(t *testing.T) {

require.Len(t, spec.StateDiff.DoguConfigDiffs, 1)
assert.Contains(t, maps.Keys(spec.StateDiff.DoguConfigDiffs), common.SimpleDoguName("ldapDiff"))
require.Len(t, spec.StateDiff.DoguConfigDiffs["ldapDiff"].SensitiveDoguConfigDiff, 1)
assert.Equal(t, censorValue, spec.StateDiff.DoguConfigDiffs["ldapDiff"].SensitiveDoguConfigDiff[0].Actual.Value)
assert.Equal(t, censorValue, spec.StateDiff.DoguConfigDiffs["ldapDiff"].SensitiveDoguConfigDiff[0].Expected.Value)
require.Len(t, spec.StateDiff.SensitiveDoguConfigDiffs["ldapDiff"], 1)
assert.Equal(t, censorValue, spec.StateDiff.SensitiveDoguConfigDiffs["ldapDiff"][0].Actual.Value)
assert.Equal(t, censorValue, spec.StateDiff.SensitiveDoguConfigDiffs["ldapDiff"][0].Expected.Value)
}

func TestBlueprintSpec_CompletePostProcessing(t *testing.T) {
Expand Down Expand Up @@ -955,26 +963,22 @@ func TestBlueprintSpec_MarkSelfUpgradeCompleted(t *testing.T) {
}

func TestBlueprintSpec_GetDogusThatNeedARestart(t *testing.T) {
testdogu1 := Dogu{Name: common.QualifiedDoguName{Namespace: "testnamespace", SimpleName: "testdogu1"}}
testBlueprint1 := Blueprint{Dogus: []Dogu{testdogu1}}
testDoguConfigDiffsChanged := CombinedDoguConfigDiffs{
DoguConfigDiff: []DoguConfigEntryDiff{{
Actual: DoguConfigValueState{},
Expected: DoguConfigValueState{Value: "testvalue", Exists: true},
NeededAction: ConfigActionSet,
}},
}
testDoguConfigDiffsActionNone := CombinedDoguConfigDiffs{
DoguConfigDiff: []DoguConfigEntryDiff{{
NeededAction: ConfigActionNone,
}},
}
testDogu1 := Dogu{Name: common.QualifiedDoguName{Namespace: "testNamespace", SimpleName: "testDogu1"}}
testBlueprint1 := Blueprint{Dogus: []Dogu{testDogu1}}
testDoguConfigDiffsChanged := []DoguConfigEntryDiff{{
Actual: DoguConfigValueState{},
Expected: DoguConfigValueState{Value: "testValue", Exists: true},
NeededAction: ConfigActionSet,
}}
testDoguConfigDiffsActionNone := []DoguConfigEntryDiff{{
NeededAction: ConfigActionNone,
}}

testDoguConfigChangeDiffChanged := StateDiff{
DoguConfigDiffs: map[common.SimpleDoguName]CombinedDoguConfigDiffs{testdogu1.Name.SimpleName: testDoguConfigDiffsChanged},
DoguConfigDiffs: map[common.SimpleDoguName]DoguConfigDiffs{testDogu1.Name.SimpleName: testDoguConfigDiffsChanged},
}
testDoguConfigChangeDiffActionNone := StateDiff{
DoguConfigDiffs: map[common.SimpleDoguName]CombinedDoguConfigDiffs{testdogu1.Name.SimpleName: testDoguConfigDiffsActionNone},
DoguConfigDiffs: map[common.SimpleDoguName]DoguConfigDiffs{testDogu1.Name.SimpleName: testDoguConfigDiffsActionNone},
}

type fields struct {
Expand Down Expand Up @@ -1004,7 +1008,7 @@ func TestBlueprintSpec_GetDogusThatNeedARestart(t *testing.T) {
EffectiveBlueprint: EffectiveBlueprint(testBlueprint1),
StateDiff: testDoguConfigChangeDiffChanged,
},
want: []common.SimpleDoguName{testdogu1.Name.SimpleName},
want: []common.SimpleDoguName{testDogu1.Name.SimpleName},
},
{
name: "return nothing on dogu config unchanged",
Expand Down
23 changes: 20 additions & 3 deletions pkg/domain/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,39 @@ func (e GlobalConfigDiffDeterminedEvent) Message() string {
}

type DoguConfigDiffDeterminedEvent struct {
CombinedDogusConfigDiffs map[common.SimpleDoguName]CombinedDoguConfigDiffs
DoguConfigDiffs map[common.SimpleDoguName]DoguConfigDiffs
SensitiveDoguConfigDiffs map[common.SimpleDoguName]SensitiveDoguConfigDiffs
}

func NewDoguConfigDiffDeterminedEvent(
doguConfigDiffs map[common.SimpleDoguName]DoguConfigDiffs,
sensitiveDoguConfigDiffs map[common.SimpleDoguName]SensitiveDoguConfigDiffs) *DoguConfigDiffDeterminedEvent {
return &DoguConfigDiffDeterminedEvent{DoguConfigDiffs: doguConfigDiffs, SensitiveDoguConfigDiffs: sensitiveDoguConfigDiffs}
}

func (e DoguConfigDiffDeterminedEvent) Name() string {
return "DoguConfigDiffDetermined"
}

func (e DoguConfigDiffDeterminedEvent) Message() string {
// TODO for review: decide if we keep a multiline string as event or add a second event (type) for sensitive config
return fmt.Sprintf(
"dogu config diff determined: %s \n"+
"sensitive dogu config diff determined: %s",
generateDoguConfigDiffCounterString(e.DoguConfigDiffs),
generateDoguConfigDiffCounterString(e.SensitiveDoguConfigDiffs),
)
}

func generateDoguConfigDiffCounterString(doguConfigDiffs map[common.SimpleDoguName]DoguConfigDiffs) string {
var stringPerAction []string
var actionsCounter int
for action, amount := range countByAction(e.CombinedDogusConfigDiffs) {
for action, amount := range countByAction(doguConfigDiffs) {
stringPerAction = append(stringPerAction, fmt.Sprintf("%q: %d", action, amount))
actionsCounter += amount
}
slices.Sort(stringPerAction)
return fmt.Sprintf("dogu config diff determined: %d actions (%s)", actionsCounter, strings.Join(stringPerAction, ", "))
return fmt.Sprintf("%d actions (%s)", actionsCounter, strings.Join(stringPerAction, ", "))
}

// StateDiffComponentDeterminedEvent provides event information over detected changes regarding components.
Expand Down
33 changes: 5 additions & 28 deletions pkg/domain/stateDiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,11 @@ import (
// StateDiff represents the diff between the defined state in the effective blueprint and the actual state in the ecosystem.
// If there is a state in the ecosystem, which is not represented in the effective blueprint, then the expected state is the actual state.
type StateDiff struct {
DoguDiffs DoguDiffs
ComponentDiffs ComponentDiffs
DoguConfigDiffs map[common.SimpleDoguName]CombinedDoguConfigDiffs
GlobalConfigDiffs GlobalConfigDiffs
}

func (diff StateDiff) GetDoguConfigDiffsByAction() map[ConfigAction]DoguConfigDiffs {
configDiffsByAction := map[ConfigAction]DoguConfigDiffs{}

for _, combinedConfig := range diff.DoguConfigDiffs {
for key, value := range combinedConfig.DoguConfigDiff.GetDoguConfigDiffByAction() {
configDiffsByAction[key] = append(configDiffsByAction[key], value...)
}
}

return configDiffsByAction
}

func (diff StateDiff) GetSensitiveDoguConfigDiffsByAction() map[ConfigAction]SensitiveDoguConfigDiffs {
configDiffsByAction := map[ConfigAction]SensitiveDoguConfigDiffs{}

for _, combinedConfig := range diff.DoguConfigDiffs {
for key, value := range combinedConfig.SensitiveDoguConfigDiff.GetSensitiveDoguConfigDiffByAction() {
configDiffsByAction[key] = append(configDiffsByAction[key], value...)
}
}

return configDiffsByAction
DoguDiffs DoguDiffs
ComponentDiffs ComponentDiffs
DoguConfigDiffs map[common.SimpleDoguName]DoguConfigDiffs
SensitiveDoguConfigDiffs map[common.SimpleDoguName]SensitiveDoguConfigDiffs
GlobalConfigDiffs GlobalConfigDiffs
}

// Action represents a needed Action for a dogu to reach the expected state.
Expand Down
Loading

0 comments on commit 4f4ec23

Please sign in to comment.