Skip to content

Commit

Permalink
fix: correctly assign apps to nested envs (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zebradil authored Feb 2, 2025
1 parent 60e8d68 commit 39f185d
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 1 deletion.
17 changes: 16 additions & 1 deletion internal/myks/globe.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func (g *Globe) ValidateRootDir() error {

func (g *Globe) Init(asyncLevel int, envSearchPathToAppMap EnvAppMap) error {
envAppMap := g.collectEnvironments(g.AddBaseDirToEnvAppMap(envSearchPathToAppMap))
log.Debug().Interface("envAppMap", envAppMap).Msg(g.Msg("Environments collected from search paths"))

return process(asyncLevel, maps.Keys(envAppMap), func(item interface{}) error {
envPath, ok := item.(string)
Expand Down Expand Up @@ -391,9 +392,23 @@ func (g *Globe) collectEnvironments(envSearchPathToAppMap EnvAppMap) EnvAppMap {

for searchPath, appNames := range envSearchPathToAppMap {
for _, envPath := range g.collectEnvironmentsInPath(searchPath) {
envAppMap[envPath] = appNames
// If appNames is nil or empty, it means all applications in the environment should be processed
if len(appNames) == 0 {
envAppMap[envPath] = nil
continue
}
// If the environment is already in the map, append the appNames to the existing list,
// but only if its apps are not nil
if apps, ok := envAppMap[envPath]; !ok {
envAppMap[envPath] = appNames
} else if apps != nil {
envAppMap[envPath] = append(apps, appNames...)
}
}
}
for env, apps := range envAppMap {
envAppMap[env] = unique(apps)
}

log.Debug().Interface("envToAppMap", envAppMap).Msg(g.Msg("Collected environments"))
return envAppMap
Expand Down
115 changes: 115 additions & 0 deletions internal/myks/globe_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package myks

import (
"sort"
"testing"
)

Expand All @@ -26,3 +27,117 @@ func Test_AddBaseDirToEnvPath(t *testing.T) {
}
}
}

func Test_collectEnvironments(t *testing.T) {
defer chdir(t, "../../testData/collect-environments")()

g := NewWithDefaults()
g.environments = make(map[string]*Environment)
tests := []struct {
name string
in EnvAppMap
out EnvAppMap
}{
{
"empty map",
EnvAppMap{},
EnvAppMap{
"envs/bar/prod": []string{},
"envs/bar/stage": []string{},
"envs/foo/prod": []string{},
"envs/foo/stage": []string{},
},
},
{
"one env",
EnvAppMap{
"envs/bar/prod": []string{"app1", "app2"},
},
EnvAppMap{
"envs/bar/prod": []string{"app1", "app2"},
},
},
{
"multiple envs",
EnvAppMap{
"envs/bar/prod": []string{"app1", "app2"},
"envs/bar/stage": []string{"app1", "app3"},
"envs/foo/prod": []string{"app1"},
},
EnvAppMap{
"envs/bar/prod": []string{"app1", "app2"},
"envs/bar/stage": []string{"app1", "app3"},
"envs/foo/prod": []string{"app1"},
},
},
{
"nested envs",
EnvAppMap{
"envs": []string{"app1", "envsApp"},
"envs/bar": []string{"app2", "barApp"},
"envs/bar/stage": []string{"app3", "stageApp"},
},
EnvAppMap{
"envs/bar/prod": []string{"app1", "envsApp", "app2", "barApp"},
"envs/bar/stage": []string{"app1", "envsApp", "app2", "barApp", "app3", "stageApp"},
"envs/foo/prod": []string{"app1", "envsApp"},
"envs/foo/stage": []string{"app1", "envsApp"},
},
},
{
"deduplication",
EnvAppMap{
"envs/bar": []string{"app1", "app1", "app2"},
},
EnvAppMap{
"envs/bar/prod": []string{"app1", "app2"},
"envs/bar/stage": []string{"app1", "app2"},
},
},
{
"empty list prioritised",
EnvAppMap{
"envs/bar": nil,
"envs/bar/prod": []string{"app1", "app2"},
"envs/foo": []string{},
"envs/foo/prod": []string{"app3"},
},
EnvAppMap{
"envs/bar/prod": []string{},
"envs/bar/stage": []string{},
"envs/foo/prod": []string{},
"envs/foo/stage": []string{},
},
},
}

for _, tt := range tests {
out := g.collectEnvironments(tt.in)
if !compareEnvAppMap(out, tt.out) {
t.Errorf("%s:\n got %v\n want %v", tt.name, out, tt.out)
}
}
}

func compareEnvAppMap(left, right EnvAppMap) bool {
if len(left) != len(right) {
return false
}
for leftEnv, leftApps := range left {
if rightApps, ok := right[leftEnv]; !ok {
return false
} else {
if len(leftApps) != len(rightApps) {
return false
}
sort.Strings(leftApps)
sort.Strings(rightApps)
for i, leftApp := range leftApps {
if leftApp != rightApps[i] {
return false
}
}
}
}
return true
}
2 changes: 2 additions & 0 deletions testData/collect-environments/envs/bar/prod/env-data.ytt.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
environment:
id: bar-prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
environment:
id: bar-stage
2 changes: 2 additions & 0 deletions testData/collect-environments/envs/foo/prod/env-data.ytt.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
environment:
id: foo-prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
environment:
id: foo-stage

0 comments on commit 39f185d

Please sign in to comment.