Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commit status notifications: Add optional prefix to commit status key #340

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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