Skip to content

Commit

Permalink
Fix templating and configuration conflict issue
Browse files Browse the repository at this point in the history
  • Loading branch information
pkosiec committed Sep 5, 2023
1 parent be131c3 commit 72fa2af
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 43 deletions.
48 changes: 24 additions & 24 deletions helm/botkube/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ sources:
config:
defaultSubscriptions:
applications:
- name: guestbook
- name: hello
namespace: argocd

argoCD:
Expand Down Expand Up @@ -670,7 +670,7 @@ sources:
{
"message": {
"sections": [
{ "header": ":large_green_circle: Application `{{.app.metadata.name}}` has been created" }
{ "header": ":new: Application `{{.app.metadata.name}}` has been created" }
]
},
"context": {
Expand Down Expand Up @@ -703,13 +703,13 @@ sources:
"key": "Sync Status",
"value": "{{.app.status.sync.status}}"
},
{
"key": "Health Status",
"value": "{{.app.status.health.status}}"
},
{
"key": "Revision",
"value": "{{.app.status.sync.revision}}"
},
{
"key": "Repository",
"value": "{{.app.spec.source.repoURL}}"
}
{{range $index, $c := .app.status.conditions}}
,{
Expand Down Expand Up @@ -740,15 +740,11 @@ sources:
"textFields": [
{
"key": "Health Status",
"value": "{{.app.status.health.status}}",
"value": "{{.app.status.health.status}}"
},
{
"key": "Revision",
"value": "{{.app.status.sync.revision}}"
},
{
"key": "Repository",
"value": "{{.app.spec.source.repoURL}}"
}
{{range $index, $c := .app.status.conditions}}
,{
Expand Down Expand Up @@ -789,10 +785,6 @@ sources:
{
"key": "Revision",
"value": "{{.app.status.sync.revision}}"
},
{
"key": "Repository",
"value": "{{.app.spec.source.repoURL}}"
}
{{range $index, $c := .app.status.conditions}}
,{
Expand Down Expand Up @@ -827,9 +819,21 @@ sources:
"key": "Sync Status",
"value": "{{.app.status.sync.status}}"
},
{{ if and (.app.status.operationState) (.app.status.operationState.operation) }}
{
"key": "Initiated by",
"value": "{{.app.status.operationState.operation.initiatedBy.username}}"
},
{{ if .app.status.operationState.operation.initiatedBy.automated }}
{
"key": "Automated",
"value": "{{.app.status.operationState.operation.initiatedBy.automated}}"
},
{{ end }}
{{ end }}
{
"key": "Repository",
"value": "{{.app.spec.source.repoURL}}"
"key": "Revision",
"value": "{{.app.status.sync.revision}}"
}
{{range $index, $c := .app.status.conditions}}
,{
Expand Down Expand Up @@ -861,10 +865,6 @@ sources:
{
"key": "Sync Status",
"value": "{{.app.status.sync.status}}"
},
{
"key": "Repository",
"value": "{{.app.spec.source.repoURL}}"
}
{{range $index, $c := .app.status.conditions}}
,{
Expand Down Expand Up @@ -899,9 +899,9 @@ sources:
"value": "{{.app.status.sync.status}}"
},
{
"key": "Repository",
"value": "{{.app.spec.source.repoURL}}"
}
"key": "Health Status",
"value": "{{.app.status.health.status}}"
}
{{range $index, $c := .app.status.conditions}}
,{
"key": "{{$c.type}}",
Expand Down
21 changes: 19 additions & 2 deletions internal/source/argocd/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"time"

"github.com/avast/retry-go/v4"
"github.com/sirupsen/logrus"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -73,12 +74,24 @@ func (s *Source) Stream(ctx context.Context, input source.StreamInput) (source.S
}

s.log.Info("Preparing configuration...")
err = s.setupArgoNotifications(ctx, k8sCli)

err = retry.Do(
func() error {
return s.setupArgoNotifications(ctx, k8sCli)
},
retry.OnRetry(func(n uint, err error) {
s.log.Errorf("")
}),
retry.DelayType(retry.RandomDelay), // Randomize the retry time as ConfigMap is updated and there might be conflicts when there are multiple plugin configurations
retry.MaxJitter(5*time.Second),
retry.Attempts(5),
retry.LastErrorOnly(true),
)
if err != nil {
return source.StreamOutput{}, fmt.Errorf("while configuring Argo notifications: %w", err)
}

s.log.Info("Set up successful")
s.log.Info("Set up successful for source configuration %q", input.Context.SourceName)
return source.StreamOutput{}, nil
}

Expand All @@ -104,6 +117,10 @@ func (s *Source) HandleExternalRequest(ctx context.Context, input source.Externa
if section != nil {
msg.Sections = append(msg.Sections, *section)
}
} else {
msg.Type = api.NonInteractiveSingleSection
// TODO: Display links to details and repository
// TODO: Support properly emojis for Discord :large_green_circle:
}

return source.ExternalRequestOutput{
Expand Down
35 changes: 18 additions & 17 deletions internal/source/argocd/setup_notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package argocd

import (
"context"
"encoding/json"
"fmt"

"github.com/MakeNowJust/heredoc"
Expand Down Expand Up @@ -49,7 +48,7 @@ func (s *Source) setupArgoNotifications(ctx context.Context, k8sCli *dynamic.Dyn
if err != nil {
return err
}
s.log.Debug("Using webhook %q...", webhookName)
s.log.Debugf("Using webhook %q...", webhookName)

// register webhook
if s.cfg.Webhook.Register {
Expand Down Expand Up @@ -217,7 +216,7 @@ func (s *Source) createTrigger(triggerCfg NewTrigger) (string, []triggers.Condit
if err != nil {
return "", nil, fmt.Errorf("while rendering trigger name: %w", err)
}
s.log.WithField("triggerName", triggerName).Debug("Creating new trigger...")
s.log.Debugf("Creating new trigger %q...", triggerName)

errs := multierror.New()
triggerDetails := triggerCfg.Conditions
Expand Down Expand Up @@ -276,32 +275,34 @@ func (s *Source) registerWebhook(webhookName string) (string, string, error) {
return path, value, nil
}

type webhookConfig struct {
Method string `json:"method"`
Body string `json:"body"`
}

func (s *Source) registerTemplate(webhookName string, tpl Template) (string, string, error) {
s.log.Debugf("Registering template %q...", tpl.Name)
templateName, err := renderStringIfTemplate(tpl.Name, s.srcCtx)
if err != nil {
return "", "", fmt.Errorf("while rendering template name: %w", err)
}

var res interface{}
err = yaml.Unmarshal([]byte(tpl.Body), &res)
if err != nil {
return "", "", fmt.Errorf("while unmarshalling template %q: %w", templateName, err)
s.log.Debugf("Registering template %q...", templateName)

out := map[string]interface{}{
"webhook": map[string]interface{}{
webhookName: webhookConfig{
Method: "POST",
Body: tpl.Body,
},
},
}

bytes, err := json.Marshal(res)
bytes, err := yaml.Marshal(out)
if err != nil {
return "", "", fmt.Errorf("while marshalling template %q: %w", templateName, err)
}

tplPath := fmt.Sprintf("template.%s", templateName)
tplValue := heredoc.Docf(`
webhook:
%s:
method: POST
body: |
%s
`, webhookName, string(bytes))
tplValue := string(bytes)

return tplPath, tplValue, nil
}
1 change: 1 addition & 0 deletions pkg/bot/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,4 +573,5 @@ var emojiMapping = map[string]string{
":cricket:": "🦗",
":no_entry_sign:": "🚫",
":large_green_circle:": "🟢",
":new:": "🆕",
}

0 comments on commit 72fa2af

Please sign in to comment.