Skip to content

Commit

Permalink
notification: Add notification for release container spec
Browse files Browse the repository at this point in the history
Need this for:
 - #2277
 - fluxcd/flux#1472
  • Loading branch information
lelenanam committed Nov 14, 2018
1 parent 2b3344a commit 0107c67
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
47 changes: 34 additions & 13 deletions notification-eventmanager/eventmanager/render/flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

const (
releaseTemplate = `Release {{trim (print .Spec.ImageSpec) "<>"}} to {{with .Spec.ServiceSpecs}}{{range $index, $spec := .}}{{if not (eq $index 0)}}, {{if last $index $.Spec.ServiceSpecs}}and {{end}}{{end}}{{trim (print .) "<>"}}{{end}}{{end}}.`
releaseTemplate = `Release {{trim (print .ImageSpec) "<>"}} to {{with .ServiceSpecs}}{{range $index, $spec := .}}{{if not (eq $index 0)}}, {{if last $index $.ServiceSpecs}}and {{end}}{{end}}{{trim (print .) "<>"}}{{end}}{{end}}.`
autoReleaseTemplate = `Automated release of new image{{if not (last 0 $.Images)}}s{{end}} {{with .Images}}{{range $index, $image := .}}{{if not (eq $index 0)}}, {{if last $index $.Images}}and {{end}}{{end}}{{.}}{{end}}{{end}}.`
)

Expand Down Expand Up @@ -51,11 +51,6 @@ func (r *Render) fluxMessages(ev *types.Event, pd *parsedData, eventURL, eventUR
}

func parseDeployData(data fluxevent.ReleaseEventMetadata) (*parsedData, error) {
text, err := executeTempl(releaseTemplate, data)
if err != nil {
return nil, errors.Wrap(err, "instantiate release template error")
}

releaseError := data.Error
var resText, color string

Expand All @@ -66,13 +61,39 @@ func parseDeployData(data fluxevent.ReleaseEventMetadata) (*parsedData, error) {
}
resText = updateResultText(data.Result)
}
return &parsedData{
Title: "Weave Cloud deploy",
Text: text,
Error: releaseError,
Result: resText,
Color: color,
}, nil

switch data.Spec.Type {
case fluxevent.ReleaseImageSpecType:
text, err := executeTempl(releaseTemplate, data.Spec.ReleaseImageSpec)
if err != nil {
return nil, errors.Wrap(err, "instantiate release template error")
}
return &parsedData{
Title: "Weave Cloud deploy",
Text: text,
Error: releaseError,
Result: resText,
Color: color,
}, nil

case fluxevent.ReleaseContainersSpecType:
servicemap := data.Spec.ReleaseContainersSpec.ContainerSpecs
services := make([]string, 0, len(servicemap))
for service := range servicemap {
services = append(services, service.String())
}
text := fmt.Sprintf("Update image refs in %s.", strings.Join(services, ", "))
return &parsedData{
Title: "Weave Cloud deploy",
Text: text,
Error: releaseError,
Result: resText,
Color: color,
}, nil

default:
return nil, errors.Errorf("unknown deploy data type: %s", data.Spec.Type)
}
}

func parseAutoDeployData(data fluxevent.AutoReleaseEventMetadata) (*parsedData, error) {
Expand Down
19 changes: 6 additions & 13 deletions notification-eventmanager/eventmanager/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/pkg/errors"
fluxevent "github.com/weaveworks/flux/event"
"github.com/weaveworks/flux/update"
"github.com/weaveworks/service/notification-eventmanager/types"
userTemplates "github.com/weaveworks/service/users/templates"
)
Expand Down Expand Up @@ -46,27 +45,21 @@ func (r *Render) Data(ev *types.Event, eventURL, eventURLText, settingsURL strin
case types.DeployType:
var data fluxevent.ReleaseEventMetadata
if err := json.Unmarshal(ev.Data, &data); err != nil {
return errors.Wrap(err, "ummarshaling deploy data error")
}

// Sanity check: we shouldn't get any other kind, but you never know.
if data.Spec.Kind != update.ReleaseKindExecute {
return errors.Errorf("wrong data spec kind %q, should be %q", data.Spec.Kind, update.ReleaseKindExecute)
return errors.Wrap(err, "unmarshaling deploy data error")
}

pd, err := parseDeployData(data)
if err != nil {
return errors.Wrap(err, "cannot parse deploy metadata")
}

if err := r.fluxMessages(ev, pd, eventURL, eventURLText, settingsURL); err != nil {
return errors.Wrapf(err, "cannot get messages for %s", ev.Type)
}

case types.AutoDeployType:
var data fluxevent.AutoReleaseEventMetadata
if err := json.Unmarshal(ev.Data, &data); err != nil {
return errors.Wrap(err, "ummarshaling auto deploy data error")
return errors.Wrap(err, "unmarshaling auto deploy data error")
}

pd, err := parseAutoDeployData(data)
Expand All @@ -80,7 +73,7 @@ func (r *Render) Data(ev *types.Event, eventURL, eventURLText, settingsURL strin
case types.SyncType:
var data types.SyncData
if err := json.Unmarshal(ev.Data, &data); err != nil {
return errors.Wrap(err, "ummarshaling sync data error")
return errors.Wrap(err, "unmarshaling sync data error")
}

pd, err := parseSyncData(data)
Expand All @@ -94,7 +87,7 @@ func (r *Render) Data(ev *types.Event, eventURL, eventURLText, settingsURL strin
case types.PolicyType:
var data fluxevent.CommitEventMetadata
if err := json.Unmarshal(ev.Data, &data); err != nil {
return errors.Wrap(err, "ummarshaling policy data error")
return errors.Wrap(err, "unmarshaling policy data error")
}

pd := &parsedData{
Expand All @@ -108,7 +101,7 @@ func (r *Render) Data(ev *types.Event, eventURL, eventURLText, settingsURL strin
case types.DeployCommitType:
var data fluxevent.CommitEventMetadata
if err := json.Unmarshal(ev.Data, &data); err != nil {
return errors.Wrap(err, "ummarshaling deploy commit data error")
return errors.Wrap(err, "unmarshaling deploy commit data error")
}

pd := &parsedData{
Expand All @@ -122,7 +115,7 @@ func (r *Render) Data(ev *types.Event, eventURL, eventURLText, settingsURL strin
case types.AutoDeployCommitType:
var data fluxevent.CommitEventMetadata
if err := json.Unmarshal(ev.Data, &data); err != nil {
return errors.Wrap(err, "ummarshaling auto deploy commit data error")
return errors.Wrap(err, "unmarshaling auto deploy commit data error")
}

pd := &parsedData{
Expand Down

0 comments on commit 0107c67

Please sign in to comment.