Skip to content

Commit

Permalink
Add optional prefix for commit status notification
Browse files Browse the repository at this point in the history
This commit aims to extend the functionality of commit status
notifications. It adds an optional key `commitStatusPrefix` to the
Provider spec so that notification controllers in different
environments don't overwrite a commit status for a particular resource.

Example use-case: a single object (kustomization/k8s-gitops) is deployed to
multiple clusters. Specifying this (e.g. to the name of the cluster)
ensures that the notification controllers on each cluster don't
overwrite each others' commit statuses.

Signed-off-by: bcarrier <[email protected]>
  • Loading branch information
bcarrier92 committed Mar 2, 2022
1 parent 961b107 commit 7b7ffe8
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 3 deletions.
8 changes: 8 additions & 0 deletions api/v1beta1/provider_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ type ProviderSpec struct {
// +required
Type string `json:"type"`

// A string to prepend to the identifier of a commit status.
// Example use-case: a single object (kustomization/k8s-gitops)
// is deployed to multiple clusters. Specifying this (e.g., to
// the name of the cluster) ensures that the notification controllers
// on each cluster don't overwrite each others' commit statuses.
// +optional
CommitStatusPrefix string `json:"commitStatusPrefix,omitempty"`

// Alert channel for this provider
// +optional
Channel string `json:"channel,omitempty"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ spec:
channel:
description: Alert channel for this provider
type: string
commitStatusPrefix:
description: 'A string to prepend to the identifier of a commit status.
Example use-case: a single object (kustomization/k8s-gitops) is
deployed to multiple clusters. Specifying this (e.g., to the name
of the cluster) ensures that the notification controllers on each
cluster don''t overwrite each others'' commit statuses.'
type: string
proxy:
description: HTTP/S address of the proxy
pattern: ^(http|https)://
Expand Down
32 changes: 32 additions & 0 deletions docs/api/notification.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,22 @@ string
</tr>
<tr>
<td>
<code>commitStatusPrefix</code><br>
<em>
string
</em>
</td>
<td>
<em>(Optional)</em>
<p>A string to prepend to the identifier of a commit status.
Example use-case: a single object (kustomization/k8s-gitops)
is deployed to multiple clusters. Specifying this (e.g., to
the name of the cluster) ensures that the notification controllers
on each cluster don&rsquo;t overwrite each others&rsquo; commit statuses.</p>
</td>
</tr>
<tr>
<td>
<code>channel</code><br>
<em>
string
Expand Down Expand Up @@ -742,6 +758,22 @@ string
</tr>
<tr>
<td>
<code>commitStatusPrefix</code><br>
<em>
string
</em>
</td>
<td>
<em>(Optional)</em>
<p>A string to prepend to the identifier of a commit status.
Example use-case: a single object (kustomization/k8s-gitops)
is deployed to multiple clusters. Specifying this (e.g., to
the name of the cluster) ensures that the notification controllers
on each cluster don&rsquo;t overwrite each others&rsquo; commit statuses.</p>
</td>
</tr>
<tr>
<td>
<code>channel</code><br>
<em>
string
Expand Down
11 changes: 10 additions & 1 deletion internal/notifier/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,16 @@ func parseGitAddress(s string) (string, string, error) {
}

func formatNameAndDescription(event events.Event) (string, string) {
name := fmt.Sprintf("%v/%v", event.InvolvedObject.Kind, event.InvolvedObject.Name)
kind, name := event.InvolvedObject.Kind, event.InvolvedObject.Name
commitStatusPrefix, ok := event.Metadata["commitStatusPrefix"]
if ok && commitStatusPrefix != "" {
// This adds additional information to the identifier of a commit status so that,
// for example, notification controllers deployed on multiple clusters hosting
// the same resource kind/name don't overwrite each others' commit statuses.
name = fmt.Sprintf("%v/%v/%v", commitStatusPrefix, kind, name)
} else {
name = fmt.Sprintf("%v/%v", kind, name)
}
name = strings.ToLower(name)
desc := strings.Join(splitCamelcase(event.Reason), " ")
desc = strings.ToLower(desc)
Expand Down
21 changes: 19 additions & 2 deletions internal/notifier/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,33 @@ import (
)

func TestUtil_NameAndDescription(t *testing.T) {
event := events.Event{
var event events.Event
var name, desc string

event = events.Event{
InvolvedObject: v1.ObjectReference{
Kind: "Kustomization",
Name: "gitops-system",
},
Reason: "ApplySucceeded",
}
name, desc := formatNameAndDescription(event)
name, desc = formatNameAndDescription(event)
require.Equal(t, "kustomization/gitops-system", name)
require.Equal(t, "apply succeeded", desc)

event = events.Event{
InvolvedObject: v1.ObjectReference{
Kind: "Kustomization",
Name: "gitops-system",
},
Reason: "ApplySucceeded",
Metadata: map[string]string{
"commitStatusPrefix": "im-a-cluster",
},
}
name, desc = formatNameAndDescription(event)
require.Equal(t, "im-a-cluster/kustomization/gitops-system", name)
require.Equal(t, "apply succeeded", desc)
}

func TestUtil_ParseRevision(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions internal/server/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,16 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request)
}
}

if provider.Spec.CommitStatusPrefix != "" {
if notification.Metadata == nil {
notification.Metadata = map[string]string{
"commitStatusPrefix": provider.Spec.CommitStatusPrefix,
}
} else {
notification.Metadata["commitStatusPrefix"] = provider.Spec.CommitStatusPrefix
}
}

go func(n notifier.Interface, e events.Event) {
if err := n.Post(e); err != nil {
err = redactTokenFromError(err, token)
Expand Down

0 comments on commit 7b7ffe8

Please sign in to comment.