generated from keptn-sandbox/keptn-service-template-go
-
Notifications
You must be signed in to change notification settings - Fork 5
/
eventhandlers.go
219 lines (173 loc) · 7.42 KB
/
eventhandlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"time"
cloudevents "github.com/cloudevents/sdk-go/v2" // make sure to use v2 cloudevents here
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0"
)
/**
* Here are all the handler functions for the individual event
* See https://github.com/keptn/spec/blob/0.8.0-alpha/cloudevents.md for details on the payload
**/
const EngineNotFound = "not found"
// GenericLogKeptnCloudEventHandler is a generic handler for Keptn Cloud Events that logs the CloudEvent
func GenericLogKeptnCloudEventHandler(myKeptn *keptnv2.Keptn, incomingEvent cloudevents.Event, data interface{}) error {
log.Printf("Handling %s Event: %s", incomingEvent.Type(), incomingEvent.Context.GetID())
log.Printf("CloudEvent %T: %v", data, data)
return nil
}
// HandleTestsTriggered hanldes test.triggered (used to be sh.keptn.events.deployment-finished)
func HandleTestsTriggered(myKeptn *keptnv2.Keptn, incomingEvent cloudevents.Event, data *keptnv2.TestTriggeredEventData) error {
log.Printf("Handling Tests Triggered Event: %s", incomingEvent.Context.GetID())
myKeptn.SendTaskStartedEvent(data, ServiceName)
// run tests
log.Printf("Looking for Litmus chaos experiment in Keptn git repo...")
keptnResourceContent, err := myKeptn.GetKeptnResource(LitmusExperimentFileName)
if err != nil {
logMessage := fmt.Sprintf("No %s file found for service %s in stage %s in project %s", LitmusExperimentFileName, data.Service, data.Stage, data.Project)
log.Printf(logMessage)
_, err = myKeptn.SendTaskFinishedEvent(&keptnv2.EventData{
Status: keptnv2.StatusErrored,
Result: keptnv2.ResultFailed,
Message: logMessage,
}, ServiceName)
return err
}
_ = os.Mkdir("litmus", 0644)
err = ioutil.WriteFile(LitmusExperimentFileName, []byte(keptnResourceContent), 0644)
if err != nil {
log.Printf("Could not store experiment file locally: %s", err.Error())
}
// construct the namespace of the chaos resources
projectAndNamespace := data.Project + "-" + data.Stage
// Obtain the ChaosEngine name from the Keptn experiment manifest
chaosEngineName, err := ExecuteCommand("kubectl", []string{"apply", "-f", LitmusExperimentFileName, "--dry-run", "-o", "jsonpath='{.metadata.name}'"})
if err != nil {
logMessage := fmt.Sprintf("Error while extracting chaosengine name from manifest: %s", err.Error())
log.Printf(logMessage)
_, err = myKeptn.SendTaskFinishedEvent(&keptnv2.EventData{
Status: keptnv2.StatusErrored,
Result: keptnv2.ResultFailed,
Message: logMessage,
}, ServiceName)
return err
}
chaosEngineName = strings.Trim(chaosEngineName, `'"`)
log.Printf("Name of ChaosEngine: %s", chaosEngineName)
log.Printf("Executing Litmus chaos experiment...")
output, err := ExecuteCommand("kubectl", []string{"apply", "-f", LitmusExperimentFileName})
if err != nil {
logMessage := fmt.Sprintf("Error execute kubectl apply command: %s", err.Error())
log.Printf(logMessage)
_, err = myKeptn.SendTaskFinishedEvent(&keptnv2.EventData{
Status: keptnv2.StatusErrored,
Result: keptnv2.ResultFailed,
Message: logMessage,
}, ServiceName)
return err
}
log.Printf("ChaosEngine create command finished with: %s", output)
// Allow the chaos-operator to patch the engine with the initial status
time.Sleep(2 * time.Second)
// Extract the chaosUID for use in result extraction
uid, err := ExecuteCommand("kubectl", []string{"get", "chaosengine", chaosEngineName, "-o", "jsonpath='{.metadata.uid}'", "-n", projectAndNamespace})
if err != nil {
log.Printf("Error while retrieving chaosengine UID: %s", err.Error())
}
chaosUID := strings.Trim(string(uid), `'"`)
log.Printf("UID of ChaosEngine %s: %s", chaosEngineName, chaosUID)
var chaosStatus string
for chaosStatus != "completed" {
log.Printf("Waiting for completion of chaos experiment..")
chaosStatus, err = CheckIfChaosIsStillRunning(chaosEngineName, projectAndNamespace)
if err != nil {
if strings.Contains(strings.ToLower(err.Error()), EngineNotFound) {
log.Printf("chaosengine %v is deleted!", chaosEngineName)
break
}
logMessage := fmt.Sprintf("Error while retrieving chaos status: %s", err.Error())
log.Printf(logMessage)
// we will consider this a WARNING (e.g., the experiment was aborted)
_, err = myKeptn.SendTaskFinishedEvent(&keptnv2.EventData{
Status: keptnv2.StatusSucceeded,
Result: keptnv2.ResultWarning,
Message: logMessage,
}, ServiceName)
return err
}
// interval before we check the chaosengine status again
time.Sleep(5 * time.Second)
}
log.Printf("Chaos experiment is completed")
// Construct the jsonpath filter to extract verdict of the chaosresult
jsonPathFilterForResult := fmt.Sprintf("jsonpath='{.items[?(@.metadata.labels.chaosUID==\"%s\")].status.experimentStatus.verdict}'", chaosUID)
log.Println("jsonPathFilterForResult: " + jsonPathFilterForResult)
// Getting ChaosResult Data
verdict, err := ExecuteCommand("kubectl", []string{"get", "chaosresult", "-o", jsonPathFilterForResult, "-n", projectAndNamespace})
if err != nil {
logMessage := fmt.Sprintf("Error while retrieving chaos result: %s", err.Error())
log.Printf(logMessage)
_, err = myKeptn.SendTaskFinishedEvent(&keptnv2.EventData{
Status: keptnv2.StatusErrored,
Result: keptnv2.ResultFailed,
Message: logMessage,
}, ServiceName)
return err
}
verdict = strings.Trim(verdict, `'"`)
log.Println("ChaosExperiment Verdict: " + verdict)
testResult := keptnv2.ResultFailed
// check verdict
log.Println("Final Result: " + verdict)
if verdict == "Pass" {
testResult = keptnv2.ResultPass
} else if verdict == "Stopped" {
// Not clear if it failed or if someone stopped it, so we will warn
testResult = keptnv2.ResultWarning
}
// send tests.finished
_, err = myKeptn.SendTaskFinishedEvent(&keptnv2.EventData{
Status: keptnv2.StatusSucceeded,
Result: testResult,
Message: "Chaos tests finished",
}, ServiceName)
return err
}
// HandleTestFinished handles test.finished event coming from other services, such that we kill the currently running litmus experiment
func HandleTestFinished(myKeptn *keptnv2.Keptn, incomingEvent cloudevents.Event, data *keptnv2.TestFinishedEventData) error {
log.Printf("Handling Tests Finished Event: %s", incomingEvent.Context.GetID())
if incomingEvent.Source() == ServiceName {
// skip test.finished, it has been send out by litmus service
return nil
}
// delete chaos experiment
log.Printf("Deleting chaos experiment resources")
_, err := ExecuteCommand("kubectl", []string{"delete", "-f", LitmusExperimentFileName})
if err != nil {
log.Printf("Error execute kubectl delete command: %s", err.Error())
}
return nil
}
// ExecuteCommand exectues the command using the args
func ExecuteCommand(command string, args []string) (string, error) {
cmd := exec.Command(command, args...)
out, err := cmd.CombinedOutput()
if err != nil {
return string(out), fmt.Errorf("Error executing command %s %s: %s\n%s", command, strings.Join(args, " "), err.Error(), string(out))
}
return string(out), nil
}
// CheckIfChaosIsStillRunning checks if Chaos is still running in the specified namespace
func CheckIfChaosIsStillRunning(chaosEngineName string, projectAndNamespace string) (string, error) {
chaosStatus, err := ExecuteCommand("kubectl", []string{"get", "chaosengine", chaosEngineName, "-o", "jsonpath='{.status.engineStatus}'", "-n", projectAndNamespace})
if err != nil {
return "", err
}
chaosStatus = strings.Trim(chaosStatus, `'"`)
return chaosStatus, nil
}