From 8654c1557c5f30ad3abfb1d359a6728a54cd2514 Mon Sep 17 00:00:00 2001 From: Mateusz Kubaczyk Date: Sat, 1 Feb 2020 16:18:57 +0100 Subject: [PATCH 1/3] Reuse -target flag flow for -group passed --- internal/app/decision_maker_test.go | 40 ++++++++++++++++++++++------- internal/app/helm_helpers.go | 7 +++-- internal/app/main.go | 3 +++ internal/app/release.go | 6 ----- internal/app/state.go | 10 ++++++++ 5 files changed, 47 insertions(+), 19 deletions(-) diff --git a/internal/app/decision_maker_test.go b/internal/app/decision_maker_test.go index 5345f94b..b88b2de3 100644 --- a/internal/app/decision_maker_test.go +++ b/internal/app/decision_maker_test.go @@ -228,6 +228,7 @@ func Test_decide_group(t *testing.T) { r *release s *state currentState *map[string]helmRelease + curContext string } tests := []struct { name string @@ -245,13 +246,26 @@ func Test_decide_group(t *testing.T) { Namespace: "namespace", Enabled: true, }, - s: &state{}, + s: &state{ + Apps: map[string]*release{ + "app": { + Name: "release1", + Namespace: "namespace", + Group: "run-me", + Enabled: true, + }, + }, + }, currentState: &map[string]helmRelease{ - "release1-namespace": { - Namespace: "namespace", - Chart: "chart-1.0.0", + "release2-namespace": { + Name: "release2", + Namespace: "namespace", + Chart: "chart-1.0.0", + Status: "deployed", + HelmsmanContext: "some-other-context", }, }, + curContext: "some-other-context", }, want: ignored, }, @@ -266,16 +280,25 @@ func Test_decide_group(t *testing.T) { Group: "run-me", }, s: &state{ - Context: "default", + Apps: map[string]*release{ + "app": { + Name: "release1", + Namespace: "namespace", + Group: "run-me", + Enabled: true, + }, + }, }, currentState: &map[string]helmRelease{ "release2-namespace": { Name: "release2", Namespace: "namespace", Chart: "chart-1.0.0", + Status: "deployed", HelmsmanContext: "some-other-context", }, }, + curContext: "some-other-context", }, want: create, }, @@ -285,13 +308,12 @@ func Test_decide_group(t *testing.T) { t.Run(tt.name, func(t *testing.T) { tt.args.s.GroupMap = make(map[string]bool) cs := currentState{releases: *tt.args.currentState} - + curContext = tt.args.curContext + tt.args.s.Context = tt.args.curContext for _, target := range tt.targetFlag { tt.args.s.GroupMap[target] = true } - for _, group := range tt.groupFlag { - tt.args.s.GroupMap[group] = true - } + tt.args.s.GroupMap = tt.args.s.getAppsInGroupsAsTargetMap() outcome := plan{} cs.decide(tt.args.r, tt.args.s, &outcome) got := outcome.Decisions[0].Type diff --git a/internal/app/helm_helpers.go b/internal/app/helm_helpers.go index 942dd149..4cc79bc1 100644 --- a/internal/app/helm_helpers.go +++ b/internal/app/helm_helpers.go @@ -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{ @@ -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) } } diff --git a/internal/app/main.go b/internal/app/main.go index eb67d9ea..4974c125 100644 --- a/internal/app/main.go +++ b/internal/app/main.go @@ -32,6 +32,9 @@ func Main() { defer s.cleanup() flags.readState(&s) + if len(s.GroupMap) > 0 { + s.TargetMap = s.getAppsInGroupsAsTargetMap() + } if len(s.TargetMap) > 0 { s.TargetApps = s.getAppsInTargetsOnly() s.TargetNamespaces = s.getNamespacesInTargetsOnly() diff --git a/internal/app/release.go b/internal/app/release.go index 234e20ce..4f4fe5a3 100644 --- a/internal/app/release.go +++ b/internal/app/release.go @@ -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 } diff --git a/internal/app/state.go b/internal/app/state.go index 24daa741..30baa04a 100644 --- a/internal/app/state.go +++ b/internal/app/state.go @@ -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) From e29d3e7fc36305176cb34ff3f2c7471790c02acd Mon Sep 17 00:00:00 2001 From: Mateusz Kubaczyk Date: Sat, 1 Feb 2020 16:44:16 +0100 Subject: [PATCH 2/3] Fix -group flag tests --- internal/app/decision_maker_test.go | 74 +++++++---------------------- 1 file changed, 16 insertions(+), 58 deletions(-) diff --git a/internal/app/decision_maker_test.go b/internal/app/decision_maker_test.go index b88b2de3..87813e07 100644 --- a/internal/app/decision_maker_test.go +++ b/internal/app/decision_maker_test.go @@ -225,30 +225,21 @@ func Test_decide(t *testing.T) { func Test_decide_group(t *testing.T) { type args struct { - r *release - s *state - currentState *map[string]helmRelease - curContext string + 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{ Apps: map[string]*release{ - "app": { + "release1": { Name: "release1", Namespace: "namespace", Group: "run-me", @@ -256,32 +247,16 @@ func Test_decide_group(t *testing.T) { }, }, }, - currentState: &map[string]helmRelease{ - "release2-namespace": { - Name: "release2", - Namespace: "namespace", - Chart: "chart-1.0.0", - Status: "deployed", - HelmsmanContext: "some-other-context", - }, - }, - curContext: "some-other-context", }, - 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{ Apps: map[string]*release{ - "app": { + "release1": { Name: "release1", Namespace: "namespace", Group: "run-me", @@ -289,39 +264,22 @@ func Test_decide_group(t *testing.T) { }, }, }, - currentState: &map[string]helmRelease{ - "release2-namespace": { - Name: "release2", - Namespace: "namespace", - Chart: "chart-1.0.0", - Status: "deployed", - HelmsmanContext: "some-other-context", - }, - }, - curContext: "some-other-context", }, - 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} - curContext = tt.args.curContext - tt.args.s.Context = tt.args.curContext - for _, target := range tt.targetFlag { - tt.args.s.GroupMap[target] = true + for _, group := range tt.groupFlag { + tt.args.s.GroupMap[group] = true } - tt.args.s.GroupMap = tt.args.s.getAppsInGroupsAsTargetMap() - 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)) } }) } From 450c15f17503e5e0274377dfd51ce82f12ea79f8 Mon Sep 17 00:00:00 2001 From: Mateusz Kubaczyk Date: Sat, 1 Feb 2020 16:44:31 +0100 Subject: [PATCH 3/3] Add exiting on empty apps list for -target or -group flag --- internal/app/main.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/internal/app/main.go b/internal/app/main.go index 4974c125..f710195e 100644 --- a/internal/app/main.go +++ b/internal/app/main.go @@ -34,10 +34,18 @@ func Main() { 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