From 5bf3d02b4368c7a1c6bc3acf4e9141748dd9f580 Mon Sep 17 00:00:00 2001 From: Kornchanok Jiravesayakul Date: Tue, 9 Jun 2020 10:40:42 +0700 Subject: [PATCH] :bento: [patch] Support more params in image missing reporter (#38) --- internal/reporter.go | 25 +++++++++++++++++++++- internal/reporter/msteams/reporter.go | 6 +++--- internal/reporter/msteams/reporter_test.go | 5 ++++- internal/reporter/reportermock/reporter.go | 3 +-- internal/reporter/rest/reporter.go | 6 +++--- internal/reporter/rest/reporter_test.go | 7 +++--- internal/reporter/shell/reporter.go | 7 +++--- internal/reporter/shell/reporter_test.go | 9 ++++---- internal/reporter/slack/reporter.go | 6 +++--- internal/reporter/slack/reporter_test.go | 4 +++- internal/samsahai/internal_process.go | 7 +++--- 11 files changed, 57 insertions(+), 28 deletions(-) diff --git a/internal/reporter.go b/internal/reporter.go index 43872356..3429552a 100644 --- a/internal/reporter.go +++ b/internal/reporter.go @@ -125,6 +125,29 @@ func NewActivePromotionReporter(status *s2hv1beta1.ActivePromotionStatus, s2hCon return c } +// ImageMissingReporter manages image missing report +type ImageMissingReporter struct { + TeamName string `json:"teamName,omitempty"` + ComponentName string `json:"componentName,omitempty"` + Envs map[string]string + + *rpc.Image + SamsahaiConfig +} + +// NewImageMissingReporter creates image missing reporter object +func NewImageMissingReporter(image *rpc.Image, s2hConfig SamsahaiConfig, teamName, compName string) *ImageMissingReporter { + c := &ImageMissingReporter{ + SamsahaiConfig: s2hConfig, + TeamName: teamName, + ComponentName: compName, + Image: image, + Envs: listEnv(), + } + + return c +} + // Reporter is the interface of reporter type Reporter interface { // GetName returns type of reporter @@ -137,7 +160,7 @@ type Reporter interface { SendActivePromotionStatus(configCtrl ConfigController, atpRpt *ActivePromotionReporter) error // SendImageMissing sends image missing - SendImageMissing(teamName string, configCtrl ConfigController, image *rpc.Image) error + SendImageMissing(configCtrl ConfigController, imageMissingRpt *ImageMissingReporter) error } func convertIssueType(issueType rpc.ComponentUpgrade_IssueType) IssueType { diff --git a/internal/reporter/msteams/reporter.go b/internal/reporter/msteams/reporter.go index 3df1d3c7..d74978f0 100644 --- a/internal/reporter/msteams/reporter.go +++ b/internal/reporter/msteams/reporter.go @@ -187,13 +187,13 @@ func convertImageListToRPCImageList(images []s2hv1beta1.Image) []*rpc.Image { } // SendImageMissing implements the reporter SendImageMissing function -func (r *reporter) SendImageMissing(teamName string, configCtrl internal.ConfigController, image *rpc.Image) error { - msTeamsConfig, err := r.getMSTeamsConfig(teamName, configCtrl) +func (r *reporter) SendImageMissing(configCtrl internal.ConfigController, imageMissingRpt *internal.ImageMissingReporter) error { + msTeamsConfig, err := r.getMSTeamsConfig(imageMissingRpt.TeamName, configCtrl) if err != nil { return nil } - message := r.makeImageMissingListReport([]*rpc.Image{image}) + message := r.makeImageMissingListReport([]*rpc.Image{imageMissingRpt.Image}) return r.post(msTeamsConfig, message, internal.ImageMissingType) } diff --git a/internal/reporter/msteams/reporter_test.go b/internal/reporter/msteams/reporter_test.go index ae268e9a..c29e3c7b 100644 --- a/internal/reporter/msteams/reporter_test.go +++ b/internal/reporter/msteams/reporter_test.go @@ -388,7 +388,10 @@ var _ = Describe("send ms teams message", func() { mockMSTeamsCli := &mockMSTeams{} r := s2hmsteams.New("tenantID", "clientID", "clientSecret", "user", "pass", s2hmsteams.WithMSTeamsClient(mockMSTeamsCli)) - err := r.SendImageMissing("mock", configCtrl, &rpc.Image{Repository: "registry/comp-1", Tag: "1.0.0"}) + + img := &rpc.Image{Repository: "registry/comp-1", Tag: "1.0.0"} + imageMissingRpt := internal.NewImageMissingReporter(img, internal.SamsahaiConfig{}, "owner", "comp1") + err := r.SendImageMissing(configCtrl, imageMissingRpt) g.Expect(mockMSTeamsCli.accessTokenCalls).Should(Equal(1)) g.Expect(mockMSTeamsCli.getGroupIDCalls).Should(Equal(2)) g.Expect(mockMSTeamsCli.getChannelIDCalls).Should(Equal(3)) diff --git a/internal/reporter/reportermock/reporter.go b/internal/reporter/reportermock/reporter.go index e23238fd..f9273097 100644 --- a/internal/reporter/reportermock/reporter.go +++ b/internal/reporter/reportermock/reporter.go @@ -2,7 +2,6 @@ package reportermock import ( "github.com/agoda-com/samsahai/internal" - "github.com/agoda-com/samsahai/pkg/samsahai/rpc" ) const ( @@ -37,6 +36,6 @@ func (r *reporterMock) SendActivePromotionStatus(configCtrl internal.ConfigContr } // SendImageMissing implements the reporter SendImageMissingList function -func (r *reporterMock) SendImageMissing(teamName string, configCtrl internal.ConfigController, image *rpc.Image) error { +func (r *reporterMock) SendImageMissing(configCtrl internal.ConfigController, imageMissingRpt *internal.ImageMissingReporter) error { return nil } diff --git a/internal/reporter/rest/reporter.go b/internal/reporter/rest/reporter.go index d84b22df..97cef02e 100644 --- a/internal/reporter/rest/reporter.go +++ b/internal/reporter/rest/reporter.go @@ -148,8 +148,8 @@ func (r *reporter) SendActivePromotionStatus(configCtrl internal.ConfigControlle } // SendImageMissing implements the reporter SendImageMissing function -func (r *reporter) SendImageMissing(teamName string, configCtrl internal.ConfigController, img *rpc.Image) error { - config, err := configCtrl.Get(teamName) +func (r *reporter) SendImageMissing(configCtrl internal.ConfigController, imageMissingRpt *internal.ImageMissingReporter) error { + config, err := configCtrl.Get(imageMissingRpt.TeamName) if err != nil { return err } @@ -161,7 +161,7 @@ func (r *reporter) SendImageMissing(teamName string, configCtrl internal.ConfigC } for _, ep := range config.Spec.Reporter.Rest.ImageMissing.Endpoints { - restObj := &imageMissingRest{NewReporterJSON(), img} + restObj := &imageMissingRest{NewReporterJSON(), imageMissingRpt.Image} body, err := json.Marshal(restObj) if err != nil { logger.Error(err, fmt.Sprintf("cannot convert struct to json object, %v", body)) diff --git a/internal/reporter/rest/reporter_test.go b/internal/reporter/rest/reporter_test.go index 8f05f81e..7202d5f2 100644 --- a/internal/reporter/rest/reporter_test.go +++ b/internal/reporter/rest/reporter_test.go @@ -180,7 +180,8 @@ var _ = Describe("send rest message", func() { g.Expect(configCtrl).ShouldNot(BeNil()) client := rest.New(rest.WithRestClient(rest.NewRest(server.URL))) - err := client.SendImageMissing("mock", configCtrl, img) + imageMissingRpt := internal.NewImageMissingReporter(img, internal.SamsahaiConfig{}, "owner", "comp1") + err := client.SendImageMissing(configCtrl, imageMissingRpt) g.Expect(err).To(BeNil(), "request should not thrown any error") }) }) @@ -201,7 +202,7 @@ var _ = Describe("send rest message", func() { err = client.SendActivePromotionStatus(configCtrl, &internal.ActivePromotionReporter{}) g.Expect(err).NotTo(BeNil(), "active promotion request should thrown an error") - err = client.SendImageMissing("mock", configCtrl, &rpc.Image{}) + err = client.SendImageMissing(configCtrl, &internal.ImageMissingReporter{}) g.Expect(err).NotTo(BeNil(), "image missing request should thrown an error") }) @@ -229,7 +230,7 @@ var _ = Describe("send rest message", func() { g.Expect(err).NotTo(HaveOccurred()) g.Expect(calls).To(Equal(0)) - err = client.SendImageMissing("mock", configCtrl, &rpc.Image{}) + err = client.SendImageMissing(configCtrl, &internal.ImageMissingReporter{}) g.Expect(err).NotTo(HaveOccurred()) g.Expect(calls).To(Equal(0)) }) diff --git a/internal/reporter/shell/reporter.go b/internal/reporter/shell/reporter.go index a68ccbea..2d7cd5fe 100644 --- a/internal/reporter/shell/reporter.go +++ b/internal/reporter/shell/reporter.go @@ -10,7 +10,6 @@ import ( s2herrors "github.com/agoda-com/samsahai/internal/errors" s2hlog "github.com/agoda-com/samsahai/internal/log" "github.com/agoda-com/samsahai/internal/util/cmd" - "github.com/agoda-com/samsahai/pkg/samsahai/rpc" ) var logger = s2hlog.Log.WithName(ReporterName) @@ -110,8 +109,8 @@ func (r *reporter) SendActivePromotionStatus(configCtrl internal.ConfigControlle } // SendImageMissing implements the reporter SendImageMissing function -func (r *reporter) SendImageMissing(teamName string, configCtrl internal.ConfigController, image *rpc.Image) error { - config, err := configCtrl.Get(teamName) +func (r *reporter) SendImageMissing(configCtrl internal.ConfigController, imageMissingRpt *internal.ImageMissingReporter) error { + config, err := configCtrl.Get(imageMissingRpt.TeamName) if err != nil { return err } @@ -123,7 +122,7 @@ func (r *reporter) SendImageMissing(teamName string, configCtrl internal.ConfigC } cmdObj := cmd.RenderTemplate(config.Spec.Reporter.Shell.ImageMissing.Command, - config.Spec.Reporter.Shell.ImageMissing.Args, image) + config.Spec.Reporter.Shell.ImageMissing.Args, imageMissingRpt) if err := r.execute(cmdObj, internal.ImageMissingType); err != nil { return err } diff --git a/internal/reporter/shell/reporter_test.go b/internal/reporter/shell/reporter_test.go index b7ca890d..bf9f502c 100644 --- a/internal/reporter/shell/reporter_test.go +++ b/internal/reporter/shell/reporter_test.go @@ -75,11 +75,12 @@ var _ = Describe("shell command reporter", func() { configCtrl := newMockConfigCtrl("") img := &rpc.Image{Repository: "docker.io/hello-a", Tag: "2018.01.01"} - err := r.SendImageMissing("mock", configCtrl, img) + imageMissingRpt := internal.NewImageMissingReporter(img, internal.SamsahaiConfig{}, "owner", "comp1") + err := r.SendImageMissing(configCtrl, imageMissingRpt) g.Expect(err).NotTo(HaveOccurred()) g.Expect(testCmdObj.Command).To(Equal([]string{"/bin/sh", "-c"})) - g.Expect(testCmdObj.Args).To(Equal([]string{"echo image missing docker.io/hello-a:2018.01.01"})) + g.Expect(testCmdObj.Args).To(Equal([]string{"echo image missing docker.io/hello-a:2018.01.01 of comp1"})) }) It("should correctly execute command with environment variables", func() { @@ -129,7 +130,7 @@ var _ = Describe("shell command reporter", func() { g.Expect(err).NotTo(HaveOccurred()) g.Expect(calls).To(Equal(0)) - err = r.SendImageMissing("mock", configCtrl, &rpc.Image{}) + err = r.SendImageMissing(configCtrl, &internal.ImageMissingReporter{}) g.Expect(err).NotTo(HaveOccurred()) g.Expect(calls).To(Equal(0)) }) @@ -186,7 +187,7 @@ func (c *mockConfigCtrl) Get(configName string) (*s2hv1beta1.Config, error) { }, ImageMissing: &s2hv1beta1.CommandAndArgs{ Command: []string{"/bin/sh", "-c"}, - Args: []string{"echo image missing {{ .Repository }}:{{ .Tag }}"}, + Args: []string{"echo image missing {{ .Repository }}:{{ .Tag }} of {{ .ComponentName }}"}, }, }, }, diff --git a/internal/reporter/slack/reporter.go b/internal/reporter/slack/reporter.go index b6f6b55b..4ecd9c06 100644 --- a/internal/reporter/slack/reporter.go +++ b/internal/reporter/slack/reporter.go @@ -183,13 +183,13 @@ func convertImageListToRPCImageList(images []s2hv1beta1.Image) []*rpc.Image { } // SendImageMissing implements the reporter SendImageMissing function -func (r *reporter) SendImageMissing(teamName string, configCtrl internal.ConfigController, image *rpc.Image) error { - slackConfig, err := r.getSlackConfig(teamName, configCtrl) +func (r *reporter) SendImageMissing(configCtrl internal.ConfigController, imageMissingRpt *internal.ImageMissingReporter) error { + slackConfig, err := r.getSlackConfig(imageMissingRpt.TeamName, configCtrl) if err != nil { return nil } - message := r.makeImageMissingListReport([]*rpc.Image{image}) + message := r.makeImageMissingListReport([]*rpc.Image{imageMissingRpt.Image}) return r.post(slackConfig, message, internal.ImageMissingType) } diff --git a/internal/reporter/slack/reporter_test.go b/internal/reporter/slack/reporter_test.go index cc071097..df2630e0 100644 --- a/internal/reporter/slack/reporter_test.go +++ b/internal/reporter/slack/reporter_test.go @@ -359,7 +359,9 @@ var _ = Describe("send slack message", func() { mockSlackCli := &mockSlack{} r := s2hslack.New("mock-token", s2hslack.WithSlackClient(mockSlackCli)) - err := r.SendImageMissing("mock", configCtrl, &rpc.Image{Repository: "registry/comp-1", Tag: "1.0.0"}) + img := &rpc.Image{Repository: "registry/comp-1", Tag: "1.0.0"} + imageMissingRpt := internal.NewImageMissingReporter(img, internal.SamsahaiConfig{}, "owner", "comp1") + err := r.SendImageMissing(configCtrl, imageMissingRpt) g.Expect(mockSlackCli.postMessageCalls).Should(Equal(2)) g.Expect(mockSlackCli.channels).Should(Equal([]string{"chan1", "chan2"})) g.Expect(mockSlackCli.message).Should(ContainSubstring("registry/comp-1:1.0.0")) diff --git a/internal/samsahai/internal_process.go b/internal/samsahai/internal_process.go index db6cd840..84f18724 100644 --- a/internal/samsahai/internal_process.go +++ b/internal/samsahai/internal_process.go @@ -227,7 +227,7 @@ func (c *controller) updateTeamDesiredComponent(updateInfo updateTeamDesiredComp } if vErr != nil && (errors.IsImageNotFound(vErr) || errors.IsErrRequestTimeout(vErr)) { - c.sendImageMissingReport(updateInfo.TeamName, compRepository, version) + c.sendImageMissingReport(updateInfo.TeamName, updateInfo.ComponentName, compRepository, version) return nil } @@ -295,11 +295,12 @@ func (c *controller) updateTeamDesiredComponent(updateInfo updateTeamDesiredComp return nil } -func (c *controller) sendImageMissingReport(teamName, repo, version string) { +func (c *controller) sendImageMissingReport(teamName, compName, repo, version string) { configCtrl := c.GetConfigController() for _, reporter := range c.reporters { img := &rpc.Image{Repository: repo, Tag: version} - if err := reporter.SendImageMissing(teamName, configCtrl, img); err != nil { + imageMissingRpt := internal.NewImageMissingReporter(img, c.configs, teamName, compName) + if err := reporter.SendImageMissing(configCtrl, imageMissingRpt); err != nil { logger.Error(err, "cannot send image missing list report", "team", teamName) } }