Skip to content

Commit

Permalink
Merge pull request #53 from keptn-sandbox/feature/keptn_083_compat
Browse files Browse the repository at this point in the history
Keptn 0.8.3 compat: remove remediation cloudevent, add get-action
  • Loading branch information
christian-kreuzberger-dtx authored May 18, 2021
2 parents 59bf4bb + a4c1885 commit daa2be9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 95 deletions.
16 changes: 4 additions & 12 deletions eventhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,6 @@ func HandleReleaseTriggeredEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudeven
return nil
}

// HandleRemediationTriggeredEvent handles remediation.triggered events
// TODO: add in your handler code
func HandleRemediationTriggeredEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudevents.Event, data *keptnv2.RemediationTriggeredEventData) error {
log.Printf("Handling remediation.triggered Event: %s", incomingEvent.Context.GetID())

return nil
}

// HandleGetSliTriggeredEvent handles get-sli.triggered events if SLIProvider == keptn-service-template-go
// This function acts as an example showing how to handle get-sli events by sending .started and .finished events
// TODO: adapt handler code to your needs
Expand Down Expand Up @@ -164,8 +156,8 @@ func HandleGetSliTriggeredEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudevent
// Step 8 - Build get-sli.finished event data
getSliFinishedEventData := &keptnv2.GetSLIFinishedEventData{
EventData: keptnv2.EventData{
Status: keptnv2.StatusSucceeded,
Result: keptnv2.ResultPass,
Status: keptnv2.StatusSucceeded,
Result: keptnv2.ResultPass,
},
GetSLI: keptnv2.GetSLIFinished{
IndicatorValues: sliResults,
Expand Down Expand Up @@ -219,8 +211,8 @@ func HandleActionTriggeredEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudevent
// 3. Send Action.Finished Cloud-Event
// -----------------------------------------------------
myKeptn.SendTaskFinishedEvent(&keptnv2.EventData{
Status: keptnv2.StatusSucceeded, // alternative: keptnv2.StatusErrored
Result: keptnv2.ResultPass, // alternative: keptnv2.ResultFailed
Status: keptnv2.StatusSucceeded, // alternative: keptnv2.StatusErrored
Result: keptnv2.ResultPass, // alternative: keptnv2.ResultFailed
Message: "Successfully sleeped!",
}, ServiceName)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.16
require (
github.com/cloudevents/sdk-go/v2 v2.3.1
github.com/kelseyhightower/envconfig v1.4.0
github.com/keptn/go-utils v0.8.3
github.com/keptn/go-utils v0.8.4-0.20210518063017-f63f77f4c41e
github.com/mitchellh/mapstructure v1.2.2 // indirect
github.com/onsi/ginkgo v1.12.0 // indirect
github.com/onsi/gomega v1.9.0 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/globalsign/mgo v0.0.0-20180905125535-1ca0a4f7cbcb/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q=
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q=
Expand Down Expand Up @@ -76,7 +75,6 @@ github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfU
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
Expand All @@ -94,8 +92,8 @@ github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8Hm
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/keptn/go-utils v0.8.3 h1:ta2KtNbJLtIX2So/ffnmXi8dYI6JzBN2IGQdlACinj8=
github.com/keptn/go-utils v0.8.3/go.mod h1:LqZZxEeJokovG/lG+J3uKHnPDAqk5m65grNNyNW9d2Q=
github.com/keptn/go-utils v0.8.4-0.20210518063017-f63f77f4c41e h1:neuqa2utYRv7gjlZYCu2sLviV0Z2DyMiUxYvw7pHKjQ=
github.com/keptn/go-utils v0.8.4-0.20210518063017-f63f77f4c41e/go.mod h1:8cm/j/fPLl+qpSIDyyw5WVqT7W6W/ZlsAtMv8dLKtPU=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
Expand Down
123 changes: 45 additions & 78 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,55 +63,39 @@ func processKeptnCloudEvent(ctx context.Context, event cloudevents.Event) error
}

/**
* CloudEvents types in Keptn 0.8.0 follow the following pattern:
* - sh.keptn.event.${EVENTNAME}.triggered
* - sh.keptn.event.${EVENTNAME}.started
* - sh.keptn.event.${EVENTNAME}.status.changed
* - sh.keptn.event.${EVENTNAME}.finished
*
* For convenience, types can be generated using the following methods:
* - triggered: keptnv2.GetTriggeredEventType(${EVENTNAME}) (e.g,. keptnv2.GetTriggeredEventType(keptnv2.DeploymentTaskName))
* - started: keptnv2.GetStartedEventType(${EVENTNAME}) (e.g,. keptnv2.GetStartedEventType(keptnv2.DeploymentTaskName))
* - status.changed: keptnv2.GetStatusChangedEventType(${EVENTNAME}) (e.g,. keptnv2.GetStatusChangedEventType(keptnv2.DeploymentTaskName))
* - finished: keptnv2.GetFinishedEventType(${EVENTNAME}) (e.g,. keptnv2.GetFinishedEventType(keptnv2.DeploymentTaskName))
*
* The following Cloud Events are reserved and specified in the Keptn spec:
* - approval
* - deployment
* - test
* - evaluation
* - release
* - remediation
* - action
* - get-sli (for quality-gate SLI providers)
* - problem / problem.open (both deprecated, use action or remediation instead)
* There are more "internal" Cloud Events that might not have all four status, e.g.:
* - project
* - project.create
* - service
* - service.create
* - configure-monitoring
*
* For those Cloud Events the keptn/go-utils library conveniently provides several data structures
* and strings in github.com/keptn/go-utils/pkg/lib/v0_2_0, e.g.:
* - deployment: DeploymentTaskName, DeploymentTriggeredEventData, DeploymentStartedEventData, DeploymentFinishedEventData
* - test: TestTaskName, TestTriggeredEventData, TestStartedEventData, TestFinishedEventData
* - ... (they all follow the same pattern)
*
*
* In most cases you will be interested in processing .triggered events (e.g., sh.keptn.event.deployment.triggered),
* which you an achieve as follows:
* if event.type() == keptnv2.GetTriggeredEventType(keptnv2.DeploymentTaskName) { ... }
*
* Processing the event payload can be achieved as follows:
*
* eventData := &keptnv2.DeploymentTriggeredEventData{}
* parseKeptnCloudEventPayload(event, eventData)
*
* See https://github.com/keptn/spec/blob/0.2.0-alpha/cloudevents.md for more details of Keptn Cloud Events and their payload
* Also, see https://github.com/keptn-sandbox/echo-service/blob/a90207bc119c0aca18368985c7bb80dea47309e9/pkg/events.go as an example how to create your own CloudEvents
**/
* CloudEvents types in Keptn 0.8.0 follow the following pattern:
* - sh.keptn.event.${EVENTNAME}.triggered
* - sh.keptn.event.${EVENTNAME}.started
* - sh.keptn.event.${EVENTNAME}.status.changed
* - sh.keptn.event.${EVENTNAME}.finished
*
* For convenience, types can be generated using the following methods:
* - triggered: keptnv2.GetTriggeredEventType(${EVENTNAME}) (e.g,. keptnv2.GetTriggeredEventType(keptnv2.DeploymentTaskName))
* - started: keptnv2.GetStartedEventType(${EVENTNAME}) (e.g,. keptnv2.GetStartedEventType(keptnv2.DeploymentTaskName))
* - status.changed: keptnv2.GetStatusChangedEventType(${EVENTNAME}) (e.g,. keptnv2.GetStatusChangedEventType(keptnv2.DeploymentTaskName))
* - finished: keptnv2.GetFinishedEventType(${EVENTNAME}) (e.g,. keptnv2.GetFinishedEventType(keptnv2.DeploymentTaskName))
*
* Keptn reserves some Cloud Event types, please read up on that here: https://keptn.sh/docs/0.8.x/manage/shipyard/
*
* For those Cloud Events the keptn/go-utils library conveniently provides several data structures
* and strings in github.com/keptn/go-utils/pkg/lib/v0_2_0, e.g.:
* - deployment: DeploymentTaskName, DeploymentTriggeredEventData, DeploymentStartedEventData, DeploymentFinishedEventData
* - test: TestTaskName, TestTriggeredEventData, TestStartedEventData, TestFinishedEventData
* - ... (they all follow the same pattern)
*
*
* In most cases you will be interested in processing .triggered events (e.g., sh.keptn.event.deployment.triggered),
* which you an achieve as follows:
* if event.type() == keptnv2.GetTriggeredEventType(keptnv2.DeploymentTaskName) { ... }
*
* Processing the event payload can be achieved as follows:
*
* eventData := &keptnv2.DeploymentTriggeredEventData{}
* parseKeptnCloudEventPayload(event, eventData)
*
* See https://github.com/keptn/spec/blob/0.2.0-alpha/cloudevents.md for more details of Keptn Cloud Events and their payload
* Also, see https://github.com/keptn-sandbox/echo-service/blob/a90207bc119c0aca18368985c7bb80dea47309e9/pkg/events.go as an example how to create your own CloudEvents
**/

/**
* The following code presents a very generic implementation of processing almost all possible
Expand Down Expand Up @@ -326,47 +310,38 @@ func processKeptnCloudEvent(ctx context.Context, event cloudevents.Event) error
return GenericLogKeptnCloudEventHandler(myKeptn, event, eventData)

// -------------------------------------------------------
// sh.keptn.event.remediation
case keptnv2.GetTriggeredEventType(keptnv2.RemediationTaskName): // sh.keptn.event.remediation.triggered
log.Printf("Processing Remediation.Triggered Event")

eventData := &keptnv2.RemediationTriggeredEventData{}
parseKeptnCloudEventPayload(event, eventData)

return HandleRemediationTriggeredEvent(myKeptn, event, eventData)
case keptnv2.GetStartedEventType(keptnv2.RemediationTaskName): // sh.keptn.event.remediation.started
log.Printf("Processing Remediation.Started Event")
// Please note: Processing .started, .status.changed and .finished events is only recommended when you want to
// notify an external service (e.g., for logging purposes).
// sh.keptn.event.get-action (sent by shipyard-controller to extract a remediation action from remediations.yaml)
case keptnv2.GetTriggeredEventType(keptnv2.GetActionTaskName): // sh.keptn.event.action.triggered
log.Printf("Processing Get-Action.Triggered Event")

eventData := &keptnv2.RemediationStartedEventData{}
eventData := &keptnv2.GetActionTriggeredEventData{}
parseKeptnCloudEventPayload(event, eventData)

// Just log this event
return GenericLogKeptnCloudEventHandler(myKeptn, event, eventData)
case keptnv2.GetStatusChangedEventType(keptnv2.RemediationTaskName): // sh.keptn.event.remediation.status.changed
log.Printf("Processing Remediation.Status.Changed Event")
case keptnv2.GetStartedEventType(keptnv2.GetActionTaskName): // sh.keptn.event.action.started
log.Printf("Processing Get-Action.Started Event")
// Please note: Processing .started, .status.changed and .finished events is only recommended when you want to
// notify an external service (e.g., for logging purposes).

eventData := &keptnv2.RemediationStatusChangedEventData{}
eventData := &keptnv2.GetActionStartedEventData{}
parseKeptnCloudEventPayload(event, eventData)

// Just log this event
return GenericLogKeptnCloudEventHandler(myKeptn, event, eventData)
case keptnv2.GetFinishedEventType(keptnv2.RemediationTaskName): // sh.keptn.event.remediation.finished
log.Printf("Processing Remediation.Finished Event")
case keptnv2.GetFinishedEventType(keptnv2.GetActionTaskName): // sh.keptn.event.action.finished
log.Printf("Processing Get-Action.Finished Event")
// Please note: Processing .started, .status.changed and .finished events is only recommended when you want to
// notify an external service (e.g., for logging purposes).

eventData := &keptnv2.RemediationFinishedEventData{}
eventData := &keptnv2.GetActionFinishedEventData{}
parseKeptnCloudEventPayload(event, eventData)

// Just log this event
return GenericLogKeptnCloudEventHandler(myKeptn, event, eventData)

// -------------------------------------------------------
// sh.keptn.event.action
// sh.keptn.event.action (sent by shipyard-controller to execute a remediation action)
case keptnv2.GetTriggeredEventType(keptnv2.ActionTaskName): // sh.keptn.event.action.triggered
log.Printf("Processing Action.Triggered Event")

Expand Down Expand Up @@ -405,18 +380,10 @@ func processKeptnCloudEvent(ctx context.Context, event cloudevents.Event) error
eventData := &keptnlib.ProblemEventData{}
parseKeptnCloudEventPayload(event, eventData)

return HandleProblemEvent(myKeptn, event, eventData)
case keptnlib.ProblemOpenEventType: // sh.keptn.event.problem.open - e.g., sent by dynatrace-service
log.Printf("Processing problem.open Event")
log.Printf("Subscribing to a problem.open or problem event is not recommended since Keptn 0.7. Please subscribe to event of type: sh.keptn.event.action.triggered")

eventData := &keptnlib.ProblemEventData{}
parseKeptnCloudEventPayload(event, eventData)

return HandleProblemEvent(myKeptn, event, eventData)

// -------------------------------------------------------
// sh.keptn.event.get-sli
// sh.keptn.event.get-sli (sent by lighthouse-service to fetch SLIs from the sli provider)
case keptnv2.GetTriggeredEventType(keptnv2.GetSLITaskName): // sh.keptn.event.get-sli.triggered
log.Printf("Processing Get-SLI.Triggered Event")

Expand Down

0 comments on commit daa2be9

Please sign in to comment.