Skip to content

Commit

Permalink
🍱 [patch] Support more params in image missing reporter (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pohfy123 authored Jun 9, 2020
1 parent f2c17b9 commit 5bf3d02
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 28 deletions.
25 changes: 24 additions & 1 deletion internal/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions internal/reporter/msteams/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 4 additions & 1 deletion internal/reporter/msteams/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions internal/reporter/reportermock/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package reportermock

import (
"github.com/agoda-com/samsahai/internal"
"github.com/agoda-com/samsahai/pkg/samsahai/rpc"
)

const (
Expand Down Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions internal/reporter/rest/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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))
Expand Down
7 changes: 4 additions & 3 deletions internal/reporter/rest/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
})
Expand All @@ -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")
})

Expand Down Expand Up @@ -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))
})
Expand Down
7 changes: 3 additions & 4 deletions internal/reporter/shell/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
9 changes: 5 additions & 4 deletions internal/reporter/shell/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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))
})
Expand Down Expand Up @@ -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 }}"},
},
},
},
Expand Down
6 changes: 3 additions & 3 deletions internal/reporter/slack/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 3 additions & 1 deletion internal/reporter/slack/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
7 changes: 4 additions & 3 deletions internal/samsahai/internal_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 5bf3d02

Please sign in to comment.