github.com/argoproj-labs/argocd-operator@v0.10.0/controllers/argocd/notifications_util.go (about)

     1  package argocd
     2  
     3  import argoproj "github.com/argoproj-labs/argocd-operator/api/v1beta1"
     4  
     5  // getDefaultNotificationsTemplates returns a map that contains default template configurations
     6  func getDefaultNotificationsTemplates() map[string]string {
     7  
     8  	notificationsTempaltes := make(map[string]string)
     9  
    10  	// configure default notifications templates
    11  
    12  	notificationsTempaltes["template.app-created"] = `email:
    13    subject: Application {{.app.metadata.name}} has been created.
    14  message: Application {{.app.metadata.name}} has been created.
    15  teams:
    16    title: Application {{.app.metadata.name}} has been created.`
    17  
    18  	notificationsTempaltes["template.app-deleted"] = `email:
    19    subject: Application {{.app.metadata.name}} has been deleted.
    20  message: Application {{.app.metadata.name}} has been deleted.
    21  teams:
    22    title: Application {{.app.metadata.name}} has been deleted.`
    23  
    24  	notificationsTempaltes["template.app-deployed"] = `email:
    25    subject: New version of an application {{.app.metadata.name}} is up and running.
    26  message: |
    27    {{if eq .serviceType "slack"}}:white_check_mark:{{end}} Application {{.app.metadata.name}} is now running new version of deployments manifests.
    28  slack:
    29    attachments: |
    30      [{
    31        "title": "{{ .app.metadata.name}}",
    32        "title_link":"{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
    33        "color": "#18be52",
    34        "fields": [
    35        {
    36          "title": "Sync Status",
    37          "value": "{{.app.status.sync.status}}",
    38          "short": true
    39        },
    40        {
    41          "title": "Repository",
    42          "value": "{{.app.spec.source.repoURL}}",
    43          "short": true
    44        },
    45        {
    46          "title": "Revision",
    47          "value": "{{.app.status.sync.revision}}",
    48          "short": true
    49        }
    50        {{range $index, $c := .app.status.conditions}}
    51        {{if not $index}},{{end}}
    52        {{if $index}},{{end}}
    53        {
    54          "title": "{{$c.type}}",
    55          "value": "{{$c.message}}",
    56          "short": true
    57        }
    58        {{end}}
    59        ]
    60      }]
    61    deliveryPolicy: Post
    62    groupingKey: ""
    63    notifyBroadcast: false
    64  teams:
    65    facts: |
    66      [{
    67        "name": "Sync Status",
    68        "value": "{{.app.status.sync.status}}"
    69      },
    70      {
    71        "name": "Repository",
    72        "value": "{{.app.spec.source.repoURL}}"
    73      },
    74      {
    75        "name": "Revision",
    76        "value": "{{.app.status.sync.revision}}"
    77      }
    78      {{range $index, $c := .app.status.conditions}}
    79        {{if not $index}},{{end}}
    80        {{if $index}},{{end}}
    81        {
    82          "name": "{{$c.type}}",
    83          "value": "{{$c.message}}"
    84        }
    85      {{end}}
    86      ]
    87    potentialAction: |-
    88      [{
    89        "@type":"OpenUri",
    90        "name":"Operation Application",
    91        "targets":[{
    92          "os":"default",
    93          "uri":"{{.context.argocdUrl}}/applications/{{.app.metadata.name}}"
    94        }]
    95      },
    96      {
    97        "@type":"OpenUri",
    98        "name":"Open Repository",
    99        "targets":[{
   100          "os":"default",
   101          "uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
   102        }]
   103      }]
   104    themeColor: '#000080'
   105    title: New version of an application {{.app.metadata.name}} is up and running.`
   106  
   107  	notificationsTempaltes["template.app-health-degraded"] = `email:
   108    subject: Application {{.app.metadata.name}} has degraded.
   109  message: |
   110    {{if eq .serviceType "slack"}}:exclamation:{{end}} Application {{.app.metadata.name}} has degraded.
   111    Application details: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}.
   112  slack:
   113    attachments: |
   114      [{
   115        "title": "{{ .app.metadata.name}}",
   116        "title_link": "{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
   117        "color": "#f4c030",
   118        "fields": [
   119        {
   120          "title": "Health Status",
   121          "value": "{{.app.status.health.status}}",
   122          "short": true
   123        },
   124        {
   125          "title": "Repository",
   126          "value": "{{.app.spec.source.repoURL}}",
   127          "short": true
   128        }
   129        {{range $index, $c := .app.status.conditions}}
   130        {{if not $index}},{{end}}
   131        {{if $index}},{{end}}
   132        {
   133          "title": "{{$c.type}}",
   134          "value": "{{$c.message}}",
   135          "short": true
   136        }
   137        {{end}}
   138        ]
   139      }]
   140    deliveryPolicy: Post
   141    groupingKey: ""
   142    notifyBroadcast: false
   143  teams:
   144    facts: |
   145      [{
   146        "name": "Health Status",
   147        "value": "{{.app.status.health.status}}"
   148      },
   149      {
   150        "name": "Repository",
   151        "value": "{{.app.spec.source.repoURL}}"
   152      }
   153      {{range $index, $c := .app.status.conditions}}
   154        {{if not $index}},{{end}}
   155        {{if $index}},{{end}}
   156        {
   157          "name": "{{$c.type}}",
   158          "value": "{{$c.message}}"
   159        }
   160      {{end}}
   161      ]
   162    potentialAction: |
   163      [{
   164        "@type":"OpenUri",
   165        "name":"Open Application",
   166        "targets":[{
   167          "os":"default",
   168          "uri":"{{.context.argocdUrl}}/applications/{{.app.metadata.name}}"
   169        }]
   170      },
   171      {
   172        "@type":"OpenUri",
   173        "name":"Open Repository",
   174        "targets":[{
   175          "os":"default",
   176          "uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
   177        }]
   178      }]
   179    themeColor: '#FF0000'
   180    title: Application {{.app.metadata.name}} has degraded.`
   181  
   182  	notificationsTempaltes["template.app-sync-failed"] = `email:
   183    subject: Failed to sync application {{.app.metadata.name}}.
   184  message: |
   185    {{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}}
   186    Sync operation details are available at: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true .
   187  slack:
   188    attachments: |
   189      [{
   190        "title": "{{ .app.metadata.name}}",
   191        "title_link":"{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
   192        "color": "#E96D76",
   193        "fields": [
   194        {
   195          "title": "Sync Status",
   196          "value": "{{.app.status.sync.status}}",
   197          "short": true
   198        },
   199        {
   200          "title": "Repository",
   201          "value": "{{.app.spec.source.repoURL}}",
   202          "short": true
   203        }
   204        {{range $index, $c := .app.status.conditions}}
   205        {{if not $index}},{{end}}
   206        {{if $index}},{{end}}
   207        {
   208          "title": "{{$c.type}}",
   209          "value": "{{$c.message}}",
   210          "short": true
   211        }
   212        {{end}}
   213        ]
   214      }]
   215    deliveryPolicy: Post
   216    groupingKey: ""
   217    notifyBroadcast: false
   218  teams:
   219    facts: |
   220      [{
   221        "name": "Sync Status",
   222        "value": "{{.app.status.sync.status}}"
   223      },
   224      {
   225        "name": "Failed at",
   226        "value": "{{.app.status.operationState.finishedAt}}"
   227      },
   228      {
   229        "name": "Repository",
   230        "value": "{{.app.spec.source.repoURL}}"
   231      }
   232      {{range $index, $c := .app.status.conditions}}
   233        {{if not $index}},{{end}}
   234        {{if $index}},{{end}}
   235        {
   236          "name": "{{$c.type}}",
   237          "value": "{{$c.message}}"
   238        }
   239      {{end}}
   240      ]
   241    potentialAction: |-
   242      [{
   243        "@type":"OpenUri",
   244        "name":"Open Operation",
   245        "targets":[{
   246          "os":"default",
   247          "uri":"{{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true"
   248        }]
   249      },
   250      {
   251        "@type":"OpenUri",
   252        "name":"Open Repository",
   253        "targets":[{
   254          "os":"default",
   255          "uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
   256        }]
   257      }]
   258    themeColor: '#FF0000'
   259    title: Failed to sync application {{.app.metadata.name}}.`
   260  
   261  	notificationsTempaltes["template.app-sync-running"] = `email:
   262    subject: Start syncing application {{.app.metadata.name}}.
   263  message: |
   264    The sync operation of application {{.app.metadata.name}} has started at {{.app.status.operationState.startedAt}}.
   265    Sync operation details are available at: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true .
   266  slack:
   267    attachments: |
   268      [{
   269        "title": "{{ .app.metadata.name}}",
   270        "title_link":"{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
   271        "color": "#0DADEA",
   272        "fields": [
   273        {
   274          "title": "Sync Status",
   275          "value": "{{.app.status.sync.status}}",
   276          "short": true
   277        },
   278        {
   279          "title": "Repository",
   280          "value": "{{.app.spec.source.repoURL}}",
   281          "short": true
   282        }
   283        {{range $index, $c := .app.status.conditions}}
   284        {{if not $index}},{{end}}
   285        {{if $index}},{{end}}
   286        {
   287          "title": "{{$c.type}}",
   288          "value": "{{$c.message}}",
   289          "short": true
   290        }
   291        {{end}}
   292        ]
   293      }]
   294    deliveryPolicy: Post
   295    groupingKey: ""
   296    notifyBroadcast: false
   297  teams:
   298    facts: |
   299      [{
   300        "name": "Sync Status",
   301        "value": "{{.app.status.sync.status}}"
   302      },
   303      {
   304        "name": "Started at",
   305        "value": "{{.app.status.operationState.startedAt}}"
   306      },
   307      {
   308        "name": "Repository",
   309        "value": "{{.app.spec.source.repoURL}}"
   310      }
   311      {{range $index, $c := .app.status.conditions}}
   312        {{if not $index}},{{end}}
   313        {{if $index}},{{end}}
   314        {
   315          "name": "{{$c.type}}",
   316          "value": "{{$c.message}}"
   317        }
   318      {{end}}
   319      ]
   320    potentialAction: |-
   321      [{
   322        "@type":"OpenUri",
   323        "name":"Open Operation",
   324        "targets":[{
   325          "os":"default",
   326          "uri":"{{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true"
   327        }]
   328      },
   329      {
   330        "@type":"OpenUri",
   331        "name":"Open Repository",
   332        "targets":[{
   333          "os":"default",
   334          "uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
   335        }]
   336      }]
   337    title: Start syncing application {{.app.metadata.name}}.`
   338  
   339  	notificationsTempaltes["template.app-sync-status-unknown"] = `email:
   340    subject: Application {{.app.metadata.name}} sync status is 'Unknown'
   341  message: |
   342    {{if eq .serviceType "slack"}}:exclamation:{{end}} Application {{.app.metadata.name}} sync is 'Unknown'.
   343    Application details: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}.
   344    {{if ne .serviceType "slack"}}
   345    {{range $c := .app.status.conditions}}
   346        * {{$c.message}}
   347    {{end}}
   348    {{end}}
   349  slack:
   350    attachments: |
   351      [{
   352        "title": "{{ .app.metadata.name}}",
   353        "title_link":"{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
   354        "color": "#E96D76",
   355        "fields": [
   356        {
   357          "title": "Sync Status",
   358          "value": "{{.app.status.sync.status}}",
   359          "short": true
   360        },
   361        {
   362          "title": "Repository",
   363          "value": "{{.app.spec.source.repoURL}}",
   364          "short": true
   365        }
   366        {{range $index, $c := .app.status.conditions}}
   367        {{if not $index}},{{end}}
   368        {{if $index}},{{end}}
   369        {
   370          "title": "{{$c.type}}",
   371          "value": "{{$c.message}}",
   372          "short": true
   373        }
   374        {{end}}
   375        ]
   376      }]
   377    deliveryPolicy: Post
   378    groupingKey: ""
   379    notifyBroadcast: false
   380  teams:
   381    facts: |
   382      [{
   383        "name": "Sync Status",
   384        "value": "{{.app.status.sync.status}}"
   385      },
   386      {
   387        "name": "Repository",
   388        "value": "{{.app.spec.source.repoURL}}"
   389      }
   390      {{range $index, $c := .app.status.conditions}}
   391        {{if not $index}},{{end}}
   392        {{if $index}},{{end}}
   393        {
   394          "name": "{{$c.type}}",
   395          "value": "{{$c.message}}"
   396        }
   397      {{end}}
   398      ]
   399    potentialAction: |-
   400      [{
   401        "@type":"OpenUri",
   402        "name":"Open Application",
   403        "targets":[{
   404          "os":"default",
   405          "uri":"{{.context.argocdUrl}}/applications/{{.app.metadata.name}}"
   406        }]
   407      },
   408      {
   409        "@type":"OpenUri",
   410        "name":"Open Repository",
   411        "targets":[{
   412          "os":"default",
   413          "uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
   414        }]
   415      }]
   416    title: Application {{.app.metadata.name}} sync status is 'Unknown'`
   417  
   418  	notificationsTempaltes["template.app-sync-succeeded"] = `email:
   419    subject: Application {{.app.metadata.name}} has been successfully synced.
   420  message: |
   421    {{if eq .serviceType "slack"}}:white_check_mark:{{end}} Application {{.app.metadata.name}} has been successfully synced at {{.app.status.operationState.finishedAt}}.
   422    Sync operation details are available at: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true .
   423  slack:
   424    attachments: |
   425      [{
   426        "title": "{{ .app.metadata.name}}",
   427        "title_link":"{{.context.argocdUrl}}/applications/{{.app.metadata.name}}",
   428        "color": "#18be52",
   429        "fields": [
   430        {
   431          "title": "Sync Status",
   432          "value": "{{.app.status.sync.status}}",
   433          "short": true
   434        },
   435        {
   436          "title": "Repository",
   437          "value": "{{.app.spec.source.repoURL}}",
   438          "short": true
   439        }
   440        {{range $index, $c := .app.status.conditions}}
   441        {{if not $index}},{{end}}
   442        {{if $index}},{{end}}
   443        {
   444          "title": "{{$c.type}}",
   445          "value": "{{$c.message}}",
   446          "short": true
   447        }
   448        {{end}}
   449        ]
   450      }]
   451    deliveryPolicy: Post
   452    groupingKey: ""
   453    notifyBroadcast: false
   454  teams:
   455    facts: |
   456      [{
   457        "name": "Sync Status",
   458        "value": "{{.app.status.sync.status}}"
   459      },
   460      {
   461        "name": "Synced at",
   462        "value": "{{.app.status.operationState.finishedAt}}"
   463      },
   464      {
   465        "name": "Repository",
   466        "value": "{{.app.spec.source.repoURL}}"
   467      }
   468      {{range $index, $c := .app.status.conditions}}
   469        {{if not $index}},{{end}}
   470        {{if $index}},{{end}}
   471        {
   472          "name": "{{$c.type}}",
   473          "value": "{{$c.message}}"
   474        }
   475      {{end}}
   476      ]
   477    potentialAction: |-
   478      [{
   479        "@type":"OpenUri",
   480        "name":"Operation Details",
   481        "targets":[{
   482          "os":"default",
   483          "uri":"{{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true"
   484        }]
   485      },
   486      {
   487        "@type":"OpenUri",
   488        "name":"Open Repository",
   489        "targets":[{
   490          "os":"default",
   491          "uri":"{{.app.spec.source.repoURL | call .repo.RepoURLToHTTPS}}"
   492        }]
   493      }]
   494    themeColor: '#000080'
   495    title: Application {{.app.metadata.name}} has been successfully synced`
   496  
   497  	return notificationsTempaltes
   498  }
   499  
   500  // getDefaultNotificationsTriggers returns a map that contains default triggers configurations
   501  func getDefaultNotificationsTriggers() map[string]string {
   502  
   503  	notificationsTriggers := make(map[string]string)
   504  
   505  	// configure default notifications triggers
   506  
   507  	notificationsTriggers["trigger.on-created"] = `- description: Application is created.
   508    oncePer: app.metadata.name
   509    send:
   510    - app-created
   511    when: "true"`
   512  
   513  	notificationsTriggers["trigger.on-deleted"] = `- description: Application is deleted.
   514    oncePer: app.metadata.name
   515    send:
   516    - app-deleted
   517    when: app.metadata.deletionTimestamp != nil`
   518  
   519  	notificationsTriggers["trigger.on-deployed"] = `- description: Application is synced and healthy. Triggered once per commit.
   520    oncePer: app.status.operationState.syncResult.revision
   521    send:
   522    - app-deployed
   523    when: app.status.operationState.phase in ['Succeeded'] and app.status.health.status
   524        == 'Healthy'`
   525  
   526  	notificationsTriggers["trigger.on-health-degraded"] = `- description: Application has degraded
   527    send:
   528    - app-health-degraded
   529    when: app.status.health.status == 'Degraded'`
   530  
   531  	notificationsTriggers["trigger.on-sync-failed"] = `- description: Application syncing has failed
   532    send:
   533    - app-sync-failed
   534    when: app.status.operationState.phase in ['Error', 'Failed']`
   535  
   536  	notificationsTriggers["trigger.on-sync-running"] = `- description: Application is being synced
   537    send:
   538    - app-sync-running
   539    when: app.status.operationState.phase in ['Running']`
   540  
   541  	notificationsTriggers["trigger.on-sync-status-unknown"] = `- description: Application status is 'Unknown'
   542    send:
   543    - app-sync-status-unknown
   544    when: app.status.sync.status == 'Unknown'`
   545  
   546  	notificationsTriggers["trigger.on-sync-succeeded"] = `- description: Application syncing has succeeded
   547    send:
   548    - app-sync-succeeded
   549    when: app.status.operationState.phase in ['Succeeded']`
   550  
   551  	return notificationsTriggers
   552  
   553  }
   554  
   555  // getArgoCDNotificationsControllerReplicas will return the size value for the argocd-notifications-controller replica count if it
   556  // has been set in argocd CR. Otherwise, nil is returned if the replicas is not set in the argocd CR or
   557  // replicas value is < 0.
   558  func getArgoCDNotificationsControllerReplicas(cr *argoproj.ArgoCD) *int32 {
   559  	if cr.Spec.Notifications.Replicas != nil && *cr.Spec.Notifications.Replicas >= 0 {
   560  		return cr.Spec.Notifications.Replicas
   561  	}
   562  
   563  	return nil
   564  }