Skip to content

Commit

Permalink
Merge pull request #407 from Praqma/enhancement/group-map-reuse-targets
Browse files Browse the repository at this point in the history
Reuse -target flag flow for -group passed
  • Loading branch information
mkubaczyk authored Feb 3, 2020
2 parents d875b4c + 450c15f commit cdc70c6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 57 deletions.
74 changes: 27 additions & 47 deletions internal/app/decision_maker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,81 +225,61 @@ func Test_decide(t *testing.T) {

func Test_decide_group(t *testing.T) {
type args struct {
r *release
s *state
currentState *map[string]helmRelease
s *state
}
tests := []struct {
name string
groupFlag []string
targetFlag []string
args args
want decisionType
name string
groupFlag []string
args args
want map[string]bool
}{
{
name: "decide() - groupMap does not contain this service - skip",
groupFlag: []string{"some-group"},
args: args{
r: &release{
Name: "release1",
Namespace: "namespace",
Enabled: true,
},
s: &state{},
currentState: &map[string]helmRelease{
"release1-namespace": {
Namespace: "namespace",
Chart: "chart-1.0.0",
s: &state{
Apps: map[string]*release{
"release1": {
Name: "release1",
Namespace: "namespace",
Group: "run-me",
Enabled: true,
},
},
},
},
want: ignored,
want: map[string]bool{},
},
{
name: "decide() - groupMap contains this service - proceed",
groupFlag: []string{"run-me"},
args: args{
r: &release{
Name: "release1",
Namespace: "namespace",
Enabled: true,
Group: "run-me",
},
s: &state{
Context: "default",
},
currentState: &map[string]helmRelease{
"release2-namespace": {
Name: "release2",
Namespace: "namespace",
Chart: "chart-1.0.0",
HelmsmanContext: "some-other-context",
Apps: map[string]*release{
"release1": {
Name: "release1",
Namespace: "namespace",
Group: "run-me",
Enabled: true,
},
},
},
},
want: create,
want: map[string]bool{
"release1": true,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.args.s.GroupMap = make(map[string]bool)
cs := currentState{releases: *tt.args.currentState}

for _, target := range tt.targetFlag {
tt.args.s.GroupMap[target] = true
}
for _, group := range tt.groupFlag {
tt.args.s.GroupMap[group] = true
}
outcome := plan{}
cs.decide(tt.args.r, tt.args.s, &outcome)
got := outcome.Decisions[0].Type
t.Log(outcome.Decisions[0].Description)

// Assert
if got != tt.want {
t.Errorf("decide() = %s, want %s", got, tt.want)
tt.args.s.TargetMap = tt.args.s.getAppsInGroupsAsTargetMap()
if len(tt.args.s.TargetMap) != len(tt.want) {
t.Errorf("decide() = %d, want %d", len(tt.args.s.TargetMap), len(tt.want))
}
})
}
Expand Down
7 changes: 3 additions & 4 deletions internal/app/helm_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import (
)

type helmRepo struct {
Name string `json:"name"`
Url string `json:"url"`
Name string `json:"name"`
Url string `json:"url"`
}


// helmCmd prepares a helm command to be executed
func helmCmd(args []string, desc string) command {
return command{
Expand Down Expand Up @@ -93,7 +92,7 @@ func addHelmRepos(repos map[string]string) error {
existingRepos[repo.Name] = repo.Url
}
} else {
if !strings.Contains(reposResult.errors,"no repositories to show") {
if !strings.Contains(reposResult.errors, "no repositories to show") {
return fmt.Errorf("while listing helm repositories: %s", reposResult.errors)
}
}
Expand Down
11 changes: 11 additions & 0 deletions internal/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,20 @@ func Main() {
defer s.cleanup()

flags.readState(&s)
if len(s.GroupMap) > 0 {
s.TargetMap = s.getAppsInGroupsAsTargetMap()
if len(s.TargetMap) == 0 {
log.Info("No apps defined with -group flag were found, exiting...")
os.Exit(0)
}
}
if len(s.TargetMap) > 0 {
s.TargetApps = s.getAppsInTargetsOnly()
s.TargetNamespaces = s.getNamespacesInTargetsOnly()
if len(s.TargetApps) == 0 {
log.Info("No apps defined with -target flag were found, exiting...")
os.Exit(0)
}
}
settings = s.Settings
curContext = s.Context
Expand Down
6 changes: 0 additions & 6 deletions internal/app/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,6 @@ func (r *release) isConsideredToRun(s *state) bool {
}
return false
}
if len(s.GroupMap) > 0 {
if _, ok := s.GroupMap[r.Group]; ok {
return true
}
return false
}
return true
}

Expand Down
10 changes: 10 additions & 0 deletions internal/app/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@ func (s *state) overrideAppsNamespace(newNs string) {
}
}

func (s *state) getAppsInGroupsAsTargetMap() map[string]bool {
targetApps := make(map[string]bool)
for appName, data := range s.Apps {
if use, ok := s.GroupMap[data.Group]; ok && use {
targetApps[appName] = true
}
}
return targetApps
}

// get only those Apps that exist in TargetMap
func (s *state) getAppsInTargetsOnly() map[string]*release {
targetApps := make(map[string]*release)
Expand Down

0 comments on commit cdc70c6

Please sign in to comment.