Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' into release-1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmt committed Jan 13, 2021
2 parents de3f4a3 + 5586bb3 commit bc3b124
Show file tree
Hide file tree
Showing 52 changed files with 850 additions and 401 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Built-in triggers/templates replaced with triggers/templates "catalog" (#56)
* `config.yaml` and `notifiers.yaml` configs split into multiple ConfigMap keys (#76)
* `trigger.enabled` field is replaced with `defaultTriggers` setting
* Replace `template.body`, `template.title` fields with `template.message` and `template.email.subject` fields

## v0.7.0 (2020-05-10)

Expand Down
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ RUN go mod download

# Perform the build
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /dist/argocd-notifications ./cmd
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /app/argocd-notifications ./cmd
RUN ln -s /app/argocd-notifications /app/argocd-notifications-backend

FROM scratch

COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=builder /dist/argocd-notifications /app/argocd-notifications
COPY --from=builder /app/argocd-notifications /app/argocd-notifications
COPY --from=builder /app/argocd-notifications-backend /app/argocd-notifications-backend

# User numeric user so that kubernetes can assert that the user id isn't root (0).
# We are also using the root group (the 0 in 1000:0), it doesn't have any
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ generate: manifests catalog

.PHONY: build
build:
ifeq ($(RELEASE), true)
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o ./dist/argocd-notifications-linux-amd64 ./cmd
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags="-w -s" -o ./dist/argocd-notifications-darwin-amd64 ./cmd
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="-w -s" -o ./dist/argocd-notifications-windows-amd64.exe ./cmd
else
CGO_ENABLED=0 go build -ldflags="-w -s" -o ./dist/argocd-notifications ./cmd
endif

.PHONY: image
image:
Expand Down
58 changes: 45 additions & 13 deletions catalog/install.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
apiVersion: v1
data:
template.app-deployed: |
body: |
email:
subject: New version of an application {{.app.metadata.name}} is up and running.
message: |
{{if eq .serviceType "slack"}}:white_check_mark:{{end}} Application {{.app.metadata.name}} is now running new version of deployments manifests.
slack:
attachments: |
Expand Down Expand Up @@ -36,9 +38,10 @@ data:
{{end}}
]
}]
title: New version of an application {{.app.metadata.name}} is up and running.
template.app-health-degraded: |
body: |
email:
subject: Application {{.app.metadata.name}} has degraded.
message: |
{{if eq .serviceType "slack"}}:exclamation:{{end}} Application {{.app.metadata.name}} has degraded.
Application details: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}.
slack:
Expand Down Expand Up @@ -69,16 +72,44 @@ data:
{{end}}
]
}]
title: Application {{.app.metadata.name}} has degraded.
template.app-sync-failed: |
body: |
email:
subject: Failed to sync application {{.app.metadata.name}}.
message: |
{{if eq .serviceType "slack"}}:exclamation:{{end}} The sync operation of application {{.app.metadata.name}} has failed at {{.app.status.operationState.finishedAt}} with the following error: {{.app.status.operationState.message}}
Sync operation details are available at: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true .
slack:
attachments: "[{\n \"title\": \"{{ .app.metadata.name}}\",\n \"title_link\":\"{{.context.argocdUrl}}/applications/{{.app.metadata.name}}\",\n \"color\": \"#E96D76\",\n \"fields\": [\n {\n \"title\": \"Sync Status\",\n \"value\": \"{{.app.status.sync.status}}\",\n \"short\": true\n },\n {\n \"title\": \"Repository\",\n \"value\": \"{{.app.spec.source.repoURL}}\",\n \"short\": true\n }\n {{range $index, $c := .app.status.conditions}}\n {{if not $index}},{{end}}\n {{if $index}},{{end}}\n {\n \"title\": \"{{$c.type}}\",\n \"value\": \"{{$c.message}}\",\n \"short\": true\n }\n {{end}}\n ]\n}] "
title: Failed to sync application {{.app.metadata.name}}.
attachments: |-
[{
"title": "{{ .app.metadata.name}}",
"title_link":"{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
"color": "#E96D76",
"fields": [
{
"title": "Sync Status",
"value": "{{.app.status.sync.status}}",
"short": true
},
{
"title": "Repository",
"value": "{{.app.spec.source.repoURL}}",
"short": true
}
{{range $index, $c := .app.status.conditions}}
{{if not $index}},{{end}}
{{if $index}},{{end}}
{
"title": "{{$c.type}}",
"value": "{{$c.message}}",
"short": true
}
{{end}}
]
}]
template.app-sync-running: |
body: |
email:
subject: Start syncing application {{.app.metadata.name}}.
message: |
The sync operation of application {{.app.metadata.name}} has started at {{.app.status.operationState.startedAt}}.
Sync operation details are available at: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true .
slack:
Expand Down Expand Up @@ -109,9 +140,10 @@ data:
{{end}}
]
}]
title: Start syncing application {{.app.metadata.name}}.
template.app-sync-status-unknown: |
body: |
email:
subject: Application {{.app.metadata.name}} sync status is 'Unknown'
message: |
{{if eq .serviceType "slack"}}:exclamation:{{end}} Application {{.app.metadata.name}} sync is 'Unknown'.
Application details: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}.
{{if ne .serviceType "slack"}}
Expand Down Expand Up @@ -147,14 +179,14 @@ data:
{{end}}
]
}]
title: Application {{.app.metadata.name}} sync status is 'Unknown'
template.app-sync-succeeded: |
body: |
email:
subject: Application {{.app.metadata.name}} has been successfully synced.
message: |
{{if eq .serviceType "slack"}}:white_check_mark:{{end}} Application {{.app.metadata.name}} has been successfully synced at {{.app.status.operationState.finishedAt}}.
Sync operation details are available at: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true .
slack:
attachments: "[{\n \"title\": \"{{ .app.metadata.name}}\",\n \"title_link\":\"{{.context.argocdUrl}}/applications/{{.app.metadata.name}}\",\n \"color\": \"#18be52\",\n \"fields\": [\n {\n \"title\": \"Sync Status\",\n \"value\": \"{{.app.status.sync.status}}\",\n \"short\": true\n },\n {\n \"title\": \"Repository\",\n \"value\": \"{{.app.spec.source.repoURL}}\",\n \"short\": true\n }\n {{range $index, $c := .app.status.conditions}}\n {{if not $index}},{{end}}\n {{if $index}},{{end}}\n {\n \"title\": \"{{$c.type}}\",\n \"value\": \"{{$c.message}}\",\n \"short\": true\n }\n {{end}}\n ]\n}] "
title: Application {{.app.metadata.name}} has been successfully synced.
trigger.on-deployed: |
- description: Application is synced and healthy. Triggered once per commit.
oncePer: app.status.sync.revision
Expand Down
5 changes: 3 additions & 2 deletions catalog/templates/app-deployed.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
title: "New version of an application {{.app.metadata.name}} is up and running."
body: |
message: |
{{if eq .serviceType "slack"}}:white_check_mark:{{end}} Application {{.app.metadata.name}} is now running new version of deployments manifests.
email:
subject: New version of an application {{.app.metadata.name}} is up and running.
slack:
attachments: |
[{
Expand Down
5 changes: 3 additions & 2 deletions catalog/templates/app-health-degraded.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
title: "Application {{.app.metadata.name}} has degraded."
body: |
message: |
{{if eq .serviceType "slack"}}:exclamation:{{end}} Application {{.app.metadata.name}} has degraded.
Application details: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}.
email:
subject: Application {{.app.metadata.name}} has degraded.
slack:
attachments: |
[{
Expand Down
7 changes: 4 additions & 3 deletions catalog/templates/app-sync-failed.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
title: "Failed to sync application {{.app.metadata.name}}."
body: |
message: |
{{if eq .serviceType "slack"}}:exclamation:{{end}} The sync operation of application {{.app.metadata.name}} has failed at {{.app.status.operationState.finishedAt}} with the following error: {{.app.status.operationState.message}}
Sync operation details are available at: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true .
email:
subject: Failed to sync application {{.app.metadata.name}}.
slack:
attachments: |
[{
Expand Down Expand Up @@ -29,4 +30,4 @@ slack:
}
{{end}}
]
}]
}]
5 changes: 3 additions & 2 deletions catalog/templates/app-sync-running.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
title: "Start syncing application {{.app.metadata.name}}."
body: |
message: |
The sync operation of application {{.app.metadata.name}} has started at {{.app.status.operationState.startedAt}}.
Sync operation details are available at: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true .
email:
subject: "Start syncing application {{.app.metadata.name}}."
slack:
attachments: |
[{
Expand Down
5 changes: 3 additions & 2 deletions catalog/templates/app-sync-status-unknown.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
title: "Application {{.app.metadata.name}} sync status is 'Unknown'"
body: |
message: |
{{if eq .serviceType "slack"}}:exclamation:{{end}} Application {{.app.metadata.name}} sync is 'Unknown'.
Application details: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}.
{{if ne .serviceType "slack"}}
{{range $c := .app.status.conditions}}
* {{$c.message}}
{{end}}
{{end}}
email:
subject: Application {{.app.metadata.name}} sync status is 'Unknown'
slack:
attachments: |
[{
Expand Down
5 changes: 3 additions & 2 deletions catalog/templates/app-sync-succeeded.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
title: "Application {{.app.metadata.name}} has been successfully synced."
body: |
message: |
{{if eq .serviceType "slack"}}:white_check_mark:{{end}} Application {{.app.metadata.name}} has been successfully synced at {{.app.status.operationState.finishedAt}}.
Sync operation details are available at: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true .
email:
subject: Application {{.app.metadata.name}} has been successfully synced.
slack:
attachments: |
[{
Expand Down
3 changes: 2 additions & 1 deletion cmd/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func newBotCommand() *cobra.Command {
port int
)
var command = cobra.Command{
Use: "bot",
Use: "bot",
Short: "Starts Argo CD Notifications bot",
RunE: func(c *cobra.Command, args []string) error {
restConfig, err := clientConfig.ClientConfig()
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func newControllerCommand() *cobra.Command {
argocdRepoServer string
)
var command = cobra.Command{
Use: "controller",
Use: "controller",
Short: "Starts Argo CD Notifications controller",
RunE: func(c *cobra.Command, args []string) error {
restConfig, err := clientConfig.ClientConfig()
if err != nil {
Expand Down
31 changes: 20 additions & 11 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,32 @@ package main
import (
"fmt"
"os"

"github.com/spf13/cobra"
"path/filepath"

"github.com/argoproj-labs/argocd-notifications/cmd/tools"
"github.com/spf13/cobra"
)

func main() {
var command = &cobra.Command{
Use: "argocd-notifications",
Short: "argocd-notifications notifies about Argo CD application changes",
Run: func(c *cobra.Command, args []string) {
c.HelpFunc()(c, args)
},
binaryName := filepath.Base(os.Args[0])
if val := os.Getenv("ARGOCD_NOTIFICATIONS_BINARY"); val != "" {
binaryName = val
}
command.AddCommand(newControllerCommand())
command.AddCommand(newBotCommand())
command.AddCommand(tools.NewToolsCommand())
var command *cobra.Command
switch binaryName {
case "argocd-notifications-backend":
command = &cobra.Command{
Use: "argocd-notifications-backend",
Run: func(c *cobra.Command, args []string) {
c.HelpFunc()(c, args)
},
}
command.AddCommand(newControllerCommand())
command.AddCommand(newBotCommand())
default:
command = tools.NewToolsCommand()
}

if err := command.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
41 changes: 31 additions & 10 deletions cmd/tools/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package tools

import (
"context"
"fmt"
"io"
"io/ioutil"
"path/filepath"
"sync"

"github.com/argoproj/gitops-engine/pkg/utils/kube"
"github.com/ghodss/yaml"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
Expand All @@ -30,6 +34,7 @@ type commandContext struct {
configMapPath string
secretPath string
stdout io.Writer
stdin io.Reader
stderr io.Writer
getK8SClients clientsSource
argocdService *lazyArgocdServiceInitializer
Expand Down Expand Up @@ -86,6 +91,30 @@ func getK8SClients(clientConfig clientcmd.ClientConfig) (kubernetes.Interface, d
return k8sClient, dynamicClient, ns, nil
}

func (c *commandContext) unmarshalFromFile(filePath string, name string, gk schema.GroupKind, result interface{}) error {
var err error
var data []byte
if filePath == "-" {
data, err = ioutil.ReadAll(c.stdin)
} else {
data, err = ioutil.ReadFile(c.configMapPath)
}
if err != nil {
return err
}
objs, err := kube.SplitYAML(data)
if err != nil {
return err
}

for _, obj := range objs {
if obj.GetName() == name && obj.GroupVersionKind().GroupKind() == gk {
return runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, result)
}
}
return fmt.Errorf("file '%s' does not have '%s/%s/%s'", filePath, gk.Group, gk.Kind, name)
}

func (c *commandContext) getConfig() (*settings.Config, error) {
var configMap v1.ConfigMap
if c.configMapPath == "" {
Expand All @@ -99,11 +128,7 @@ func (c *commandContext) getConfig() (*settings.Config, error) {
}
configMap = *cm
} else {
data, err := ioutil.ReadFile(c.configMapPath)
if err != nil {
return nil, err
}
if err = yaml.Unmarshal(data, &configMap); err != nil {
if err := c.unmarshalFromFile(c.configMapPath, k8s.ConfigMapName, schema.GroupKind{Kind: "ConfigMap"}, &configMap); err != nil {
return nil, err
}
}
Expand All @@ -122,11 +147,7 @@ func (c *commandContext) getConfig() (*settings.Config, error) {
}
secret = *s
} else {
data, err := ioutil.ReadFile(c.secretPath)
if err != nil {
return nil, err
}
if err = yaml.Unmarshal(data, &secret); err != nil {
if err := c.unmarshalFromFile(c.secretPath, k8s.SecretName, schema.GroupKind{Kind: "Secret"}, &secret); err != nil {
return nil, err
}
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/tools/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func newTemplateNotifyCommand(cmdContext *commandContext) *cobra.Command {
Use: "notify NAME APPLICATION",
Example: `
# Trigger notification using in-cluster config map and secret
argocd-notifications tools template notify app-sync-succeeded guestbook --recipient slack:argocd-notifications
argocd-notifications template notify app-sync-succeeded guestbook --recipient slack:argocd-notifications
# Render notification render generated notification in console
argocd-notifications tools template notify app-sync-succeeded guestbook
argocd-notifications template notify app-sync-succeeded guestbook
`,
Short: "Generates notification using the specified template and send it to specified recipients",
RunE: func(c *cobra.Command, args []string) error {
Expand Down Expand Up @@ -92,9 +92,9 @@ func newTemplateGetCommand(cmdContext *commandContext) *cobra.Command {
Use: "get",
Example: `
# prints all templates
argocd-notifications tools template get
argocd-notifications template get
# print YAML formatted app-sync-succeeded template definition
argocd-notifications tools template get app-sync-succeeded -o=yaml
argocd-notifications template get app-sync-succeeded -o=yaml
`,
Short: "Prints information about configured templates",
RunE: func(c *cobra.Command, args []string) error {
Expand Down
Loading

0 comments on commit bc3b124

Please sign in to comment.