Skip to content

Commit

Permalink
Add support for MetaOriginRevisionKey from the Event API
Browse files Browse the repository at this point in the history
Signed-off-by: Matheus Pimenta <[email protected]>
  • Loading branch information
matheuscscp committed Jan 21, 2025
1 parent 5469e2a commit e0b98ca
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 26 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/containrrr/shoutrrr v0.8.0
github.com/fluxcd/cli-utils v0.36.0-flux.11
github.com/fluxcd/notification-controller/api v1.4.0
github.com/fluxcd/pkg/apis/event v0.13.0
github.com/fluxcd/pkg/apis/event v0.15.0
github.com/fluxcd/pkg/apis/meta v1.9.0
github.com/fluxcd/pkg/git v0.23.0
github.com/fluxcd/pkg/masktoken v0.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ github.com/fluxcd/cli-utils v0.36.0-flux.11 h1:W0y2uvCVkcE8bgV9jgoGSjzWbLFiNq1Aj
github.com/fluxcd/cli-utils v0.36.0-flux.11/go.mod h1:WZ7xUpZbK+O6HBxA5UWqzWTLSSltdmj4wS1LstS5Dqs=
github.com/fluxcd/pkg/apis/acl v0.5.0 h1:+ykKezgerKUlZwSYFUy03lPMOIAyWlqvMNNLIWWqOhk=
github.com/fluxcd/pkg/apis/acl v0.5.0/go.mod h1:IVDZx3MAoDWjlLrJHMF9Z27huFuXAEQlnbWw0M6EcTs=
github.com/fluxcd/pkg/apis/event v0.13.0 h1:m5qHAhYIC0+mRFy5OC8FZxBVBGJM3qxJ/sEg2Vgx4T8=
github.com/fluxcd/pkg/apis/event v0.13.0/go.mod h1:aRK2AONnjjSNW61B6Iy3SW4YHozACntnJeGm3fFqDqA=
github.com/fluxcd/pkg/apis/event v0.15.0 h1:k1suqIfVxnhEeKlGkvlHAbOYXjY8wRixT/OZcIuakqA=
github.com/fluxcd/pkg/apis/event v0.15.0/go.mod h1:aRK2AONnjjSNW61B6Iy3SW4YHozACntnJeGm3fFqDqA=
github.com/fluxcd/pkg/apis/meta v1.9.0 h1:wPgm7bWNJZ/ImS5GqikOxt362IgLPFBG73dZ27uWRiQ=
github.com/fluxcd/pkg/apis/meta v1.9.0/go.mod h1:pMea8eEZcsFSI7ngRnTHFtDZk2CEZGgtrueNgI6Iu70=
github.com/fluxcd/pkg/auth v0.2.0 h1:Df3pHGMDJjpr8AcGKgPvudXF3Lb3SuBlkAmhrkp7U1k=
Expand Down
2 changes: 1 addition & 1 deletion internal/notifier/azure_devops.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (a AzureDevOps) Post(ctx context.Context, event eventv1.Event) error {
return nil
}

revString, ok := event.Metadata[eventv1.MetaRevisionKey]
revString, ok := event.GetRevision()
if !ok {
return errors.New("missing revision metadata")
}
Expand Down
28 changes: 28 additions & 0 deletions internal/notifier/azure_devops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,34 @@ func TestAzureDevOps_Post(t *testing.T) {
},
},
},
{
name: "event with origin revision",
event: eventv1.Event{
Severity: eventv1.EventSeverityInfo,
InvolvedObject: corev1.ObjectReference{
Kind: "Kustomization",
Name: "gitops-system",
},
Metadata: map[string]string{
eventv1.MetaRevisionKey: "main@sha1:69b59063470310ebbd88a9156325322a124e55a3",
eventv1.MetaOriginRevisionKey: "main@sha1:bd88a9156325322a124e55a369b59063470310eb",
},
Reason: "ApplySucceeded",
},
want: git.CreateCommitStatusArgs{
CommitId: strPtr("bd88a9156325322a124e55a369b59063470310eb"),
Project: strPtr("bar"),
RepositoryId: strPtr("baz"),
GitCommitStatusToCreate: &git.GitStatus{
Description: strPtr("apply succeeded"),
State: &git.GitStatusStateValues.Succeeded,
Context: &git.GitStatusContext{
Genre: strPtr("fluxcd"),
Name: strPtr("kustomization/gitops-system/0c9c2e41"),
},
},
},
},
{
name: "event with summary",
event: eventv1.Event{
Expand Down
2 changes: 1 addition & 1 deletion internal/notifier/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (b Bitbucket) Post(ctx context.Context, event eventv1.Event) error {
return nil
}

revString, ok := event.Metadata[eventv1.MetaRevisionKey]
revString, ok := event.GetRevision()
if !ok {
return errors.New("missing revision metadata")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/notifier/bitbucketserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (b BitbucketServer) Post(ctx context.Context, event eventv1.Event) error {
if event.HasReason(meta.ProgressingReason) {
return nil
}
revString, ok := event.Metadata[eventv1.MetaRevisionKey]
revString, ok := event.GetRevision()
if !ok {
return errors.New("missing revision metadata")
}
Expand Down
30 changes: 29 additions & 1 deletion internal/notifier/bitbucketserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"testing"

Expand Down Expand Up @@ -183,6 +184,7 @@ func TestBitBucketServerPostValidateRequest(t *testing.T) {
event eventv1.Event
provideruid string
key string
uriHash string
}{
{
name: "Validate Token Auth ",
Expand All @@ -199,6 +201,25 @@ func TestBitBucketServerPostValidateRequest(t *testing.T) {
key: sha1String(generateCommitStatusID("0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a", generateTestEventKustomization("info", map[string]string{
eventv1.MetaRevisionKey: "main@sha1:5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
}))),
uriHash: "5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
},
{
name: "Event with origin revision",
token: "goodtoken",
provideruid: "0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a",
headers: map[string]string{
"Authorization": "Bearer goodtoken",
"x-atlassian-token": "no-check",
"x-requested-with": "XMLHttpRequest",
},
event: generateTestEventKustomization("info", map[string]string{
eventv1.MetaRevisionKey: "main@sha1:5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
eventv1.MetaOriginRevisionKey: "main@sha1:e7c17dd8b8384bbc84b7e7385394cb7f48332b2d",
}),
key: sha1String(generateCommitStatusID("0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a", generateTestEventKustomization("info", map[string]string{
eventv1.MetaRevisionKey: "main@sha1:e7c17dd8b8384bbc84b7e7385394cb7f48332b2d",
}))),
uriHash: "e7c17dd8b8384bbc84b7e7385394cb7f48332b2d",
},
{
name: "Validate Basic Auth and Post State=Successful",
Expand All @@ -216,6 +237,7 @@ func TestBitBucketServerPostValidateRequest(t *testing.T) {
key: sha1String(generateCommitStatusID("0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a", generateTestEventKustomization("info", map[string]string{
eventv1.MetaRevisionKey: "main@sha1:5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
}))),
uriHash: "5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
},
{
name: "Validate Post State=Failed",
Expand All @@ -233,6 +255,7 @@ func TestBitBucketServerPostValidateRequest(t *testing.T) {
key: sha1String(generateCommitStatusID("0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a", generateTestEventKustomization("error", map[string]string{
eventv1.MetaRevisionKey: "main@sha1:5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
}))),
uriHash: "5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
},
{
name: "Fail if bad json response in existing commit status",
Expand All @@ -252,6 +275,7 @@ func TestBitBucketServerPostValidateRequest(t *testing.T) {
key: sha1String(generateCommitStatusID("0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a", generateTestEventKustomization("error", map[string]string{
eventv1.MetaRevisionKey: "main@sha1:5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
}))),
uriHash: "5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
},
{
name: "Fail if status code is non-200 in existing commit status",
Expand All @@ -271,6 +295,7 @@ func TestBitBucketServerPostValidateRequest(t *testing.T) {
key: sha1String(generateCommitStatusID("0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a", generateTestEventKustomization("error", map[string]string{
eventv1.MetaRevisionKey: "main@sha1:5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
}))),
uriHash: "5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
},
{
name: "Bad post- Unauthorized",
Expand All @@ -290,6 +315,7 @@ func TestBitBucketServerPostValidateRequest(t *testing.T) {
key: sha1String(generateCommitStatusID("0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a", generateTestEventKustomization("error", map[string]string{
eventv1.MetaRevisionKey: "main@sha1:5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
}))),
uriHash: "5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
},
{
name: "Validate duplicate commit status successful match",
Expand All @@ -307,6 +333,7 @@ func TestBitBucketServerPostValidateRequest(t *testing.T) {
key: sha1String(generateCommitStatusID("0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a", generateTestEventKustomization("info", map[string]string{
eventv1.MetaRevisionKey: "main@sha1:5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
}))),
uriHash: "5394cb7f48332b2de7c17dd8b8384bbc84b7e738",
},
}

Expand All @@ -320,7 +347,8 @@ func TestBitBucketServerPostValidateRequest(t *testing.T) {
}

// Validate URI
require.Equal(t, r.URL.Path, "/rest/api/latest/projects/projectfoo/repos/repobar/commits/5394cb7f48332b2de7c17dd8b8384bbc84b7e738/builds")
path := fmt.Sprintf("/rest/api/latest/projects/projectfoo/repos/repobar/commits/%s/builds", tt.uriHash)
require.Equal(t, r.URL.Path, path)

// Validate Get Build Status call
if r.Method == http.MethodGet {
Expand Down
2 changes: 1 addition & 1 deletion internal/notifier/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func NewGitea(providerUID string, addr string, token string, certPool *x509.Cert
}

func (g *Gitea) Post(ctx context.Context, event eventv1.Event) error {
revString, ok := event.Metadata[eventv1.MetaRevisionKey]
revString, ok := event.GetRevision()
if !ok {
return errors.New("missing revision metadata")
}
Expand Down
65 changes: 50 additions & 15 deletions internal/notifier/gitea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func newTestServer(t *testing.T) *httptest.Server {
fmt.Fprintf(w, "[]")
case "/api/v1/repos/foo/bar/statuses/69b59063470310ebbd88a9156325322a124e55a3":
fmt.Fprintf(w, "{}")
case "/api/v1/repos/foo/bar/commits/8a9156325322a124e55a369b59063470310ebbd8/statuses":
fmt.Fprintf(w, "[]")
case "/api/v1/repos/foo/bar/statuses/8a9156325322a124e55a369b59063470310ebbd8":
fmt.Fprintf(w, "{}")
default:
t.Logf("unknown %s request at %s", r.Method, r.URL.Path)
}
Expand Down Expand Up @@ -83,22 +87,53 @@ func TestGitea_Post(t *testing.T) {
g, err := NewGitea("0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a", srv.URL+"/foo/bar", "foobar", nil)
assert.Nil(t, err)

event := eventv1.Event{
InvolvedObject: corev1.ObjectReference{
Kind: "Kustomization",
Namespace: "flux-system",
Name: "podinfo-repo",
for _, tt := range []struct {
name string
event eventv1.Event
}{
{
name: "revision key",
event: eventv1.Event{
InvolvedObject: corev1.ObjectReference{
Kind: "Kustomization",
Namespace: "flux-system",
Name: "podinfo-repo",
},
Severity: "info",
Timestamp: metav1.Time{
Time: time.Now(),
},
Metadata: map[string]string{
eventv1.MetaRevisionKey: "main@sha1:69b59063470310ebbd88a9156325322a124e55a3",
},
Message: "Service/podinfo/podinfo configured",
Reason: "",
},
},
Severity: "info",
Timestamp: metav1.Time{
Time: time.Now(),
{
name: "origin revision key",
event: eventv1.Event{
InvolvedObject: corev1.ObjectReference{
Kind: "Kustomization",
Namespace: "flux-system",
Name: "podinfo-repo",
},
Severity: "info",
Timestamp: metav1.Time{
Time: time.Now(),
},
Metadata: map[string]string{
eventv1.MetaRevisionKey: "main@sha1:69b59063470310ebbd88a9156325322a124e55a3",
eventv1.MetaOriginRevisionKey: "main@sha1:8a9156325322a124e55a369b59063470310ebbd8",
},
Message: "Service/podinfo/podinfo configured",
Reason: "",
},
},
Metadata: map[string]string{
eventv1.MetaRevisionKey: "main@sha1:69b59063470310ebbd88a9156325322a124e55a3",
},
Message: "Service/podinfo/podinfo configured",
Reason: "",
} {
t.Run(tt.name, func(t *testing.T) {
err := g.Post(context.Background(), tt.event)
assert.NoError(t, err)
})
}
err = g.Post(context.Background(), event)
assert.NoError(t, err)
}
2 changes: 1 addition & 1 deletion internal/notifier/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (g *GitHub) Post(ctx context.Context, event eventv1.Event) error {
return nil
}

revString, ok := event.Metadata[eventv1.MetaRevisionKey]
revString, ok := event.GetRevision()
if !ok {
return errors.New("missing revision metadata")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/notifier/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"fmt"
"net/http"

"gitlab.com/gitlab-org/api/client-go"
gitlab "gitlab.com/gitlab-org/api/client-go"

eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
"github.com/fluxcd/pkg/apis/meta"
Expand Down Expand Up @@ -77,7 +77,7 @@ func (g *GitLab) Post(ctx context.Context, event eventv1.Event) error {
return nil
}

revString, ok := event.Metadata[eventv1.MetaRevisionKey]
revString, ok := event.GetRevision()
if !ok {
return errors.New("missing revision metadata")
}
Expand Down
6 changes: 6 additions & 0 deletions internal/server/event_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ func eventKeyFunc(r *http.Request) (string, error) {

objectGroup := event.InvolvedObject.GetObjectKind().GroupVersionKind().Group

originRevisionKey := fmt.Sprintf("%s/%s", objectGroup, eventv1.MetaOriginRevisionKey)
originRevision, ok := event.Metadata[originRevisionKey]
if ok {
comps = append(comps, "originRevision="+originRevision)
}

revisionKey := fmt.Sprintf("%s/%s", objectGroup, eventv1.MetaRevisionKey)
revision, ok := event.Metadata[revisionKey]
if ok {
Expand Down
42 changes: 42 additions & 0 deletions internal/server/event_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,48 @@ func TestEventKeyFunc(t *testing.T) {
},
rateLimit: false,
},
{
involvedObject: corev1.ObjectReference{
APIVersion: "kustomize.toolkit.fluxcd.io/v1",
Kind: "Kustomization",
Name: "4",
Namespace: "4",
},
severity: eventv1.EventSeverityInfo,
message: "Health check passed",
metadata: map[string]string{
fmt.Sprintf("%s/%s", "kustomize.toolkit.fluxcd.io", eventv1.MetaOriginRevisionKey): "orev1",
},
rateLimit: false,
},
{
involvedObject: corev1.ObjectReference{
APIVersion: "kustomize.toolkit.fluxcd.io/v1",
Kind: "Kustomization",
Name: "4",
Namespace: "4",
},
severity: eventv1.EventSeverityInfo,
message: "Health check passed",
metadata: map[string]string{
fmt.Sprintf("%s/%s", "kustomize.toolkit.fluxcd.io", eventv1.MetaOriginRevisionKey): "orev1",
},
rateLimit: true,
},
{
involvedObject: corev1.ObjectReference{
APIVersion: "kustomize.toolkit.fluxcd.io/v1",
Kind: "Kustomization",
Name: "4",
Namespace: "4",
},
severity: eventv1.EventSeverityInfo,
message: "Health check passed",
metadata: map[string]string{
fmt.Sprintf("%s/%s", "kustomize.toolkit.fluxcd.io", eventv1.MetaOriginRevisionKey): "orev2",
},
rateLimit: false,
},
{
involvedObject: corev1.ObjectReference{
APIVersion: "kustomize.toolkit.fluxcd.io/v1",
Expand Down

0 comments on commit e0b98ca

Please sign in to comment.