From fa99576832c655fb1533bd913e1f734a348b3fc2 Mon Sep 17 00:00:00 2001 From: "Brandt, Sebastian (ITO-PC)" Date: Mon, 8 Jan 2024 11:24:02 +0100 Subject: [PATCH 1/3] feat: add function to create a more speaking id for commit status Signed-off-by: Brandt, Sebastian (ITO-PC) --- docs/spec/v1beta3/providers.md | 28 ++++++++++++++++++++++++++++ internal/notifier/util.go | 30 +++++++++++++++++++++++++++++- internal/notifier/util_test.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) diff --git a/docs/spec/v1beta3/providers.md b/docs/spec/v1beta3/providers.md index b67478065..a086e6de5 100644 --- a/docs/spec/v1beta3/providers.md +++ b/docs/spec/v1beta3/providers.md @@ -1474,6 +1474,34 @@ spec: name: flux-system ``` +##### Construction of Git Commit Status ID + +The commit status ID is constructed through the `eventSources.kind`, `eventSources.name` and the +[`UID`](https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids) of the `Provider`. + +To generate a speakable prefix, instead of relying on the `UID` of the `Provider`, `eventMetadata` can be set. + +```yaml +apiVersion: notification.toolkit.fluxcd.io/v1beta3 +kind: Alert +metadata: + name: github-status + namespace: flux-system +spec: + providerRef: + name: github-status + eventMetadata: + app.kubernetes.io/env: "production" + app.kubernetes.io/cluster: "my-cluster" + app.kubernetes.io/region: "us-east-1" + eventSources: + - kind: Kustomization + name: flux-system +``` + +In this case the Git Commit Status ID will be extended by a suffix which is a combination of `app.kubernetes.io/` +string sorted labels. The example above constructs a suffix as follows: "/my-cluster/production/us-east-1". + #### GitHub When `.spec.type` is set to `github`, the referenced secret must contain a key called `token` with the value set to a diff --git a/internal/notifier/util.go b/internal/notifier/util.go index 4bdd5c6f6..f006f949d 100644 --- a/internal/notifier/util.go +++ b/internal/notifier/util.go @@ -20,6 +20,7 @@ import ( "crypto/sha1" "encoding/base64" "fmt" + "sort" "strings" "unicode" "unicode/utf8" @@ -59,10 +60,37 @@ func formatNameAndDescription(event eventv1.Event) (string, string) { // involved object kind and name. func generateCommitStatusID(providerUID string, event eventv1.Event) string { uidParts := strings.Split(providerUID, "-") - id := fmt.Sprintf("%s/%s/%s", event.InvolvedObject.Kind, event.InvolvedObject.Name, uidParts[0]) + metadataLabelsSuffix := createMetadataLabelsSuffix(event) + id := fmt.Sprintf("%s/%s/%s%s", event.InvolvedObject.Kind, event.InvolvedObject.Name, uidParts[0], metadataLabelsSuffix) return strings.ToLower(id) } +// createMetadataLabelsSuffix returns a concated string depending on app.kubernetes.io/, +// prefixed common labels https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/ +func createMetadataLabelsSuffix(event eventv1.Event) string { + metadataKeys := make([]string, 0, len(event.Metadata)) + metadataLabels := make([]string, 0) + prefixedMetadataLabelsSuffix := "" + + // order Metadata keys + for labelKey := range event.Metadata { + metadataKeys = append(metadataKeys, labelKey) + } + sort.Strings(metadataKeys) + // iteratore over ordered Metadata keys + for _, key := range metadataKeys { + if strings.Contains(key, "app.kubernetes.io/") { + metadataLabels = append(metadataLabels, event.Metadata[key]) + } + } + metadataLabelsSuffix := strings.Join(metadataLabels, "/") + if len(metadataLabelsSuffix) > 0 { + prefixedMetadataLabelsSuffix = fmt.Sprintf("/%s", metadataLabelsSuffix) + } + + return strings.ToLower(prefixedMetadataLabelsSuffix) +} + func splitCamelcase(src string) (entries []string) { // don't split invalid utf8 if !utf8.ValidString(src) { diff --git a/internal/notifier/util_test.go b/internal/notifier/util_test.go index 247d39e23..72eebd29f 100644 --- a/internal/notifier/util_test.go +++ b/internal/notifier/util_test.go @@ -170,6 +170,38 @@ func TestUtil_GenerateCommitStatusID(t *testing.T) { }, want: "kustomization/gitops-system/0c9c2e41", }, + { + name: "test with non related metadata event case", + providerUID: "0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a", + event: eventv1.Event{ + Metadata: map[string]string{ + "foobar/batz": "test", + }, + InvolvedObject: corev1.ObjectReference{ + Kind: "Kustomization", + Name: "gitops-system", + }, + Reason: "ApplySucceeded", + }, + want: "kustomization/gitops-system/0c9c2e41", + }, + { + name: "test with related metadata event case", + providerUID: "0c9c2e41-d2f9-4f9b-9c41-bebc1984d67a", + event: eventv1.Event{ + Metadata: map[string]string{ + "app.kubernetes.io/cluster": "testcluster", + "app.kubernetes.io/env": "test", + "app.kubernetes.io/region": "europe-west4", + }, + InvolvedObject: corev1.ObjectReference{ + Kind: "Kustomization", + Name: "gitops-system", + }, + Reason: "ApplySucceeded", + }, + want: "kustomization/gitops-system/0c9c2e41/testcluster/test/europe-west4", + }, } for _, tt := range statusIDTests { From ac2cd57521712c7d6a635597515361913621de9f Mon Sep 17 00:00:00 2001 From: Sebastian Brandt Date: Thu, 11 Jan 2024 16:59:52 +0100 Subject: [PATCH 2/3] Update docs/spec/v1beta3/providers.md Co-authored-by: Max Jonas Werner Signed-off-by: Sebastian Brandt --- docs/spec/v1beta3/providers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/spec/v1beta3/providers.md b/docs/spec/v1beta3/providers.md index a086e6de5..8316b1217 100644 --- a/docs/spec/v1beta3/providers.md +++ b/docs/spec/v1beta3/providers.md @@ -1476,7 +1476,7 @@ spec: ##### Construction of Git Commit Status ID -The commit status ID is constructed through the `eventSources.kind`, `eventSources.name` and the +By default the commit status ID is constructed through the `eventSources.kind`, `eventSources.name` and the [`UID`](https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids) of the `Provider`. To generate a speakable prefix, instead of relying on the `UID` of the `Provider`, `eventMetadata` can be set. From 9f6736437e342586dde10d4589e9dd86b5c3fc81 Mon Sep 17 00:00:00 2001 From: Sebastian Brandt Date: Thu, 11 Jan 2024 17:00:16 +0100 Subject: [PATCH 3/3] Update docs/spec/v1beta3/providers.md Co-authored-by: Max Jonas Werner Signed-off-by: Sebastian Brandt --- docs/spec/v1beta3/providers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/spec/v1beta3/providers.md b/docs/spec/v1beta3/providers.md index 8316b1217..17840e23b 100644 --- a/docs/spec/v1beta3/providers.md +++ b/docs/spec/v1beta3/providers.md @@ -1479,7 +1479,7 @@ spec: By default the commit status ID is constructed through the `eventSources.kind`, `eventSources.name` and the [`UID`](https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids) of the `Provider`. -To generate a speakable prefix, instead of relying on the `UID` of the `Provider`, `eventMetadata` can be set. +To generate a descriptive prefix, instead of relying on the `UID` of the `Provider`, `eventMetadata` can be set. ```yaml apiVersion: notification.toolkit.fluxcd.io/v1beta3