From c6031257928654e8c1dd2a9ed65d4bd9f0e3765b Mon Sep 17 00:00:00 2001 From: Avi Biton Date: Thu, 15 Aug 2024 13:51:35 +0300 Subject: [PATCH] chore(RHTAPWATCH-1236): Add namespace and application to message sent to SNS - Add namespace and application name to the message sent to SNS - Display the message in the pod's log - Add tests Signed-off-by: Avi Biton --- .../notificationservice_controller.go | 2 +- internal/controller/pipelinerun_helper.go | 19 ++++++++++ .../controller/pipelinerun_helper_test.go | 35 ++++++++++++++++--- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/internal/controller/notificationservice_controller.go b/internal/controller/notificationservice_controller.go index c576b29..2fe78cf 100644 --- a/internal/controller/notificationservice_controller.go +++ b/internal/controller/notificationservice_controller.go @@ -84,7 +84,7 @@ func (r *NotificationServiceReconciler) Reconcile(ctx context.Context, req ctrl. notificationsFailures.Inc() return ctrl.Result{}, err } - logger.Info("SNS Notified", "pipelinerun", pipelineRun.Name, "namespace", pipelineRun.Namespace) + logger.Info("SNS Notified", "message", string(results)) err = AddAnnotationToPipelineRun(ctx, pipelineRun, r, NotificationPipelineRunAnnotation, NotificationPipelineRunAnnotationValue) if err != nil { logger.Error(err, "Failed to add annotation", "pipelineRun", pipelineRun.Name) diff --git a/internal/controller/pipelinerun_helper.go b/internal/controller/pipelinerun_helper.go index 256622c..8d9b6af 100644 --- a/internal/controller/pipelinerun_helper.go +++ b/internal/controller/pipelinerun_helper.go @@ -17,6 +17,7 @@ const NotificationPipelineRunAnnotation string = "konflux.ci/notified" const NotificationPipelineRunAnnotationValue string = "true" const PipelineRunTypeLabel string = "pipelinesascode.tekton.dev/event-type" const PushPipelineRunTypeValue string = "push" +const AppLabelKey string = "appstudio.openshift.io/application" // AddFinalizerToPipelineRun adds the finalizer to the PipelineRun. // If finalizer was not added successfully, a non-nil error is returned. @@ -57,6 +58,14 @@ func GetResultsFromPipelineRun(pipelineRun *tektonv1.PipelineRun) ([]byte, error Name: "PIPELINERUN_NAME", Value: *tektonv1.NewStructuredValues(pipelineRun.Name), }, + { + Name: "NAMESPACE", + Value: *tektonv1.NewStructuredValues(pipelineRun.Namespace), + }, + { + Name: "APPLICATION", + Value: *tektonv1.NewStructuredValues(GetApplicationNameFromPipelineRun(pipelineRun)), + }, } fetchedResults := pipelineRun.Status.Results fullResults := append(namedResults, fetchedResults...) @@ -136,3 +145,13 @@ func IsPushPipelineRun(object client.Object) bool { return false } + +// GetApplicationNameFromPipelineRun gets the application name from the application label +// Returns the application name or empty string in case the applicatio name could not retrieved +func GetApplicationNameFromPipelineRun(pipelineRun *tektonv1.PipelineRun) string { + appLabel, err := metadata.GetLabelsWithPrefix(pipelineRun, AppLabelKey) + if err != nil { + return "" + } + return appLabel[AppLabelKey] +} diff --git a/internal/controller/pipelinerun_helper_test.go b/internal/controller/pipelinerun_helper_test.go index 131ae15..ca3e595 100644 --- a/internal/controller/pipelinerun_helper_test.go +++ b/internal/controller/pipelinerun_helper_test.go @@ -35,13 +35,14 @@ var _ = Describe("Unit testing for pipelinerun_helper", func() { testPipelineLookupKey := types.NamespacedName{Name: testResourcesPipelineRunName, Namespace: namespace} testPipelineRun := &tektonv1.PipelineRun{} - Describe("Push Pipelinerun; Ended; with finalizer, annotation and results", func() { + Describe("Push Pipelinerun; Ended; with finalizer, annotation, results and application name", func() { testTruePipelineRun = &tektonv1.PipelineRun{ ObjectMeta: metav1.ObjectMeta{ Name: pushPipelineRunName, Namespace: namespace, Labels: map[string]string{ PipelineRunTypeLabel: PushPipelineRunTypeValue, + AppLabelKey: "myapp", }, Finalizers: []string{ NotificationPipelineRunFinalizer, @@ -109,13 +110,25 @@ var _ = Describe("Unit testing for pipelinerun_helper", func() { got := IsPipelineRunEndedSuccessfully(testTruePipelineRun) Expect(got).To(BeTrue()) }) + It("GetApplicationNameFromPipelineRun should extract the application name", func() { + got := GetApplicationNameFromPipelineRun(testTruePipelineRun) + Expect(got).To(Equal("myapp")) + }) It("GetResultsFromPipelineRun should extract the result and add it the pipelinerunName", func() { got, err := GetResultsFromPipelineRun(testTruePipelineRun) Expect(err).ToNot(HaveOccurred()) result := []tektonv1.PipelineRunResult{ { Name: "PIPELINERUN_NAME", - Value: *tektonv1.NewStructuredValues(testTruePipelineRun.Name), + Value: *tektonv1.NewStructuredValues(pushPipelineRunName), + }, + { + Name: "NAMESPACE", + Value: *tektonv1.NewStructuredValues(namespace), + }, + { + Name: "APPLICATION", + Value: *tektonv1.NewStructuredValues("myapp"), }, { Name: "IMAGE_DIGEST", @@ -140,7 +153,7 @@ var _ = Describe("Unit testing for pipelinerun_helper", func() { }) }) }) - Describe("Pull_request Pipelinerun; Not Ended; without finalizer, annotation or results", func() { + Describe("Pull_request Pipelinerun; Not Ended; without finalizer, annotation, results and application name", func() { testFalsePipelineRun = &tektonv1.PipelineRun{ ObjectMeta: metav1.ObjectMeta{ Name: "testPullRequestPipelineRun", @@ -196,13 +209,25 @@ var _ = Describe("Unit testing for pipelinerun_helper", func() { got := IsPipelineRunEndedSuccessfully(testFalsePipelineRun) Expect(got).To(BeFalse()) }) - It("GetResultsFromPipelineRun should add pipelinerun name to empty results", func() { + It("GetApplicationNameFromPipelineRun should return empty string", func() { + got := GetApplicationNameFromPipelineRun(testFalsePipelineRun) + Expect(got).To(Equal("")) + }) + It("GetResultsFromPipelineRun should add pipelinerun, namespace and application name to empty results", func() { got, err := GetResultsFromPipelineRun(testFalsePipelineRun) Expect(err).ToNot(HaveOccurred()) result := []tektonv1.PipelineRunResult{ { Name: "PIPELINERUN_NAME", - Value: *tektonv1.NewStructuredValues(testFalsePipelineRun.Name), + Value: *tektonv1.NewStructuredValues("testPullRequestPipelineRun"), + }, + { + Name: "NAMESPACE", + Value: *tektonv1.NewStructuredValues(namespace), + }, + { + Name: "APPLICATION", + Value: *tektonv1.NewStructuredValues(""), }, } expectedResult, err := json.Marshal(result)