From 12adcfd0acf3695f0b6402017ce12c8144e080f5 Mon Sep 17 00:00:00 2001 From: Christian Kreuzberger Date: Mon, 29 Aug 2022 16:27:44 +0200 Subject: [PATCH] fix: Deployment Type in remediation.triggered incompatible with Keptn cloud events Signed-off-by: Christian Kreuzberger --- eventhandling/alertEvent.go | 11 ++++++++--- eventhandling/get_sli_event_handler.go | 2 ++ prometheus_alert.json | 7 ++++--- test/e2e/podtatohead_test.go | 23 +++++++++++++++++++++++ test/shipyard/podtatohead.deployment.yaml | 2 ++ 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/eventhandling/alertEvent.go b/eventhandling/alertEvent.go index 9dda5de..2dbd057 100644 --- a/eventhandling/alertEvent.go +++ b/eventhandling/alertEvent.go @@ -63,7 +63,8 @@ type remediationTriggeredEventData struct { // Problem contains details about the problem Problem keptncommons.ProblemEventData `json:"problem"` // Deployment contains the current deployment, that is inferred from the alert event - Deployment string `json:"deployment"` + + Deployment keptnv2.DeploymentFinishedData `json:"deployment"` } // ProcessAndForwardAlertEvent reads the payload from the request and sends a valid Cloud event to the keptn event broker @@ -109,8 +110,12 @@ func ProcessAndForwardAlertEvent(rw http.ResponseWriter, requestBody []byte, log "Problem URL": event.Alerts[0].GeneratorURL, }, }, - Problem: problemData, - Deployment: event.Alerts[0].Labels.Deployment, + Problem: problemData, + Deployment: keptnv2.DeploymentFinishedData{ + DeploymentNames: []string{ + event.Alerts[0].Labels.Deployment, + }, + }, } if event.Alerts[0].Fingerprint != "" { diff --git a/eventhandling/get_sli_event_handler.go b/eventhandling/get_sli_event_handler.go index cf29779..c9998ec 100644 --- a/eventhandling/get_sli_event_handler.go +++ b/eventhandling/get_sli_event_handler.go @@ -60,7 +60,9 @@ func (eh GetSliEventHandler) Execute(k sdk.IKeptn, event sdk.KeptnEvent) (interf return nil, &sdk.Error{Err: err, StatusType: keptnv2.StatusErrored, ResultType: keptnv2.ResultFailed, Message: "failed to get Prometheus API URL: " + err.Error()} } + // determine deployment type based on what lighthouse-service is providing deployment := eventData.Deployment // "canary", "primary" or "" (or "direct" or "user_managed") + // fallback: get deployment type from labels if deploymentLabel, ok := eventData.Labels["deployment"]; deployment == "" && !ok { log.Println("Warning: no deployment type specified in event, defaulting to \"primary\"") deployment = "primary" diff --git a/prometheus_alert.json b/prometheus_alert.json index 2fa606d..5eb201a 100644 --- a/prometheus_alert.json +++ b/prometheus_alert.json @@ -10,16 +10,17 @@ "project": "sockshop", "service": "carts", "severity": "webhook", - "stage": "production" + "stage": "production", + "deployment": "primary" }, "annotations": { "descriptions": "Pod name ", "summary": "response_time_p90" }, - "startsAt": "2021-11-26T10:57:14.057345112Z", + "startsAt": "2022-11-26T10:57:14.057345112Z", "endsAt": "0001-01-01T00:00:00Z", "generatorURL": "http://prometheus-server-6776d8bb7d-pqkln:9090/graph?g0.expr=histogram_quantile%280.9%2C+sum+by%28le%29+%28rate%28http_response_time_milliseconds_bucket%7Bhandler%3D%22ItemsController.addToCart%22%2Cjob%3D%22carts-sockshop-production-primary%22%7D%5B3m%5D%29%29%29+%3E+1000\\u0026g0.tab=1", - "fingerprint": "8dd2c31a47cf2e78" + "fingerprint": "add2c31a47cf2e78" } ], "groupLabels": {}, diff --git a/test/e2e/podtatohead_test.go b/test/e2e/podtatohead_test.go index 10a99da..7afaa6d 100644 --- a/test/e2e/podtatohead_test.go +++ b/test/e2e/podtatohead_test.go @@ -273,6 +273,8 @@ func TestPodtatoheadEvaluation(t *testing.T) { // Check if the following event are in the project: // - remediation.triggered (making sure prometheus-service actually sends a message to Keptn) // - get-action.finished (The message format is compatible with remediation-service) + // - evaluation.triggered (making sure Keptn emits an evaluation.triggered) + // - get-sli.finished (making sure prometheus-service responds with data) requireWaitForFilteredEvent(t, testEnv.API, 1*time.Minute, @@ -307,5 +309,26 @@ func TestPodtatoheadEvaluation(t *testing.T) { }, "remediation-service", ) + + // sleep 60 seconds because of evaluation? + + requireWaitForFilteredEvent(t, + testEnv.API, + 2*time.Minute, + 1*time.Second, + &api.EventFilter{ + Project: testEnv.EventData.Project, + Stage: testEnv.EventData.Stage, + Service: testEnv.EventData.Service, + EventType: "sh.keptn.event.get-sli.finished", + }, + func(event *models.KeptnContextExtendedCE) bool { + responseEventData, err := parseKeptnEventData(event) + require.NoError(t, err) + + return responseEventData.Result == "pass" && responseEventData.Status == "succeeded" + }, + "prometheus-service", + ) }) } diff --git a/test/shipyard/podtatohead.deployment.yaml b/test/shipyard/podtatohead.deployment.yaml index 2878110..8416f39 100644 --- a/test/shipyard/podtatohead.deployment.yaml +++ b/test/shipyard/podtatohead.deployment.yaml @@ -22,3 +22,5 @@ spec: tasks: - name: "get-action" - name: "action" + - name: "evaluation" + triggeredAfter: "1m"