From 3e7bf11f709d85177c9eb8b640dd48e4c0f224ff Mon Sep 17 00:00:00 2001 From: "Steven E. Newton" Date: Fri, 15 Feb 2019 10:52:55 -0800 Subject: [PATCH] Allow for posting to multiple slack channels --- ankh/main.go | 8 ++++---- context/context.go | 4 ++-- slack/slack.go | 26 ++++++++++++++++---------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/ankh/main.go b/ankh/main.go index 9658b587..019e271f 100644 --- a/ankh/main.go +++ b/ankh/main.go @@ -299,7 +299,7 @@ func reconcileMissingConfigs(ctx *ankh.ExecutionContext, ankhFile *ankh.AnkhFile } // we should finally have a tag value - if ctx.SlackChannel != "" { + if len(ctx.SlackChannels) > 0 { ctx.SlackDeploymentVersion = *chart.Tag } } @@ -414,7 +414,7 @@ func execute(ctx *ankh.ExecutionContext) { executeContext(ctx, &rootAnkhFile) } - if ctx.SlackChannel != "" { + if len(ctx.SlackChannels) > 0 { if ctx.Mode == ankh.Rollback { ctx.SlackDeploymentVersion = "rollback" } @@ -889,7 +889,7 @@ func main() { ctx.LocalChart = true } ctx.Mode = ankh.Apply - ctx.SlackChannel = *slackChannel + ctx.SlackChannels = append(ctx.SlackChannels, *slackChannel) ctx.SlackMessageOverride = *slackMessageOverride filters := []string{} for _, filter := range *filter { @@ -921,7 +921,7 @@ func main() { ctx.LocalChart = true } ctx.Mode = ankh.Rollback - ctx.SlackChannel = *slackChannel + ctx.SlackChannels = append(ctx.SlackChannels, *slackChannel) ctx.SlackMessageOverride = *slackMessageOverride ctx.Filters = []string{"deployment", "statfulset"} diff --git a/context/context.go b/context/context.go index 13948dd1..32fe05d2 100644 --- a/context/context.go +++ b/context/context.go @@ -53,7 +53,7 @@ type ExecutionContext struct { DataDir string HelmSetValues map[string]string - SlackChannel string + SlackChannels []string SlackMessageOverride string SlackDeploymentVersion string @@ -78,7 +78,7 @@ type Context struct { Release string `yaml:"release,omitempty"` HelmRegistryURL string `yaml:"helm-registry-url,omitempty"` // deprecated in favor of top-level config `helm.registry` ClusterAdminUnused bool `yaml:"cluster-admin,omitempty"` // deprecated - Global map[string]interface{} `yaml:"global",omitempty"` + Global map[string]interface{} `yaml:"global,omitempty"` } // An Environment is a collection of contexts over which operations should be applied diff --git a/slack/slack.go b/slack/slack.go index f9ef8b5a..52a67e23 100644 --- a/slack/slack.go +++ b/slack/slack.go @@ -52,16 +52,22 @@ func PingSlackChannel(ctx *ankh.ExecutionContext) error { Username: username, } - channelId, err := getSlackChannelIDByName(api, ctx.SlackChannel) - if err != nil { - return err - } - - if !ctx.DryRun { - _, _, err = api.PostMessage(channelId, slack.MsgOptionAttachments(attachment), slack.MsgOptionPostMessageParameters(messageParams)) - } else { - ctx.Logger.Infof("--dry-run set so not sending message '%v' to slack channel %v", messageText, ctx.SlackChannel) - } + channels := make(map[string]string, len(ctx.SlackChannels)) + for _, ch := range ctx.SlackChannels { + channelId, err := getSlackChannelIDByName(api, ch) + if err != nil { + return err + } + channels[ch] = channelId + } + + for name, id := range channels { + if !ctx.DryRun { + _, _, err = api.PostMessage(id, slack.MsgOptionAttachments(attachment), slack.MsgOptionPostMessageParameters(messageParams)) + } else { + ctx.Logger.Infof("--dry-run set so not sending message '%v' to slack channel %v", messageText, name) + } + } return err }