generated from keptn-sandbox/keptn-service-template-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
213 lines (174 loc) · 7.02 KB
/
main.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
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
keptn "github.com/keptn/go-utils/pkg/lib"
"github.com/cloudevents/sdk-go/pkg/cloudevents"
"github.com/cloudevents/sdk-go/pkg/cloudevents/client"
cloudeventshttp "github.com/cloudevents/sdk-go/pkg/cloudevents/transport/http"
"github.com/kelseyhightower/envconfig"
)
var keptnOptions = keptn.KeptnOpts{}
type envConfig struct {
// Port on which to listen for cloudevents
Port int `envconfig:"RCV_PORT" default:"8080"`
// Path to which cloudevents are sent
Path string `envconfig:"RCV_PATH" default:"/"`
// Whether we are running locally (e.g., for testing) or on production
Env string `envconfig:"ENV" default:"local"`
// URL of the Keptn configuration service (this is where we can fetch files from the config repo)
ConfigurationServiceUrl string `envconfig:"CONFIGURATION_SERVICE" default:""`
// URL of the Keptn event broker (this is where this service sends cloudevents to)
EventBrokerUrl string `envconfig:"EVENTBROKER" default:""`
}
/**
* This method gets called when a new event is received from the Keptn Event Distributor
* Depending on the Event Type will call the specific event handler functions, e.g: handleDeploymentFinishedEvent
* See https://github.com/keptn/spec/blob/0.1.3/cloudevents.md for details on the payload
*/
func processKeptnCloudEvent(ctx context.Context, event cloudevents.Event) error {
myKeptn, err := keptn.NewKeptn(&event, keptnOptions)
log.Printf("gotEvent(%s): %s - %s", event.Type(), myKeptn.KeptnContext, event.Context.GetID())
if err != nil {
log.Printf("failed to parse incoming cloudevent: %v", err)
return err
}
// ********************************************
// Lets test on each possible Event Type and call the respective handler function
// ********************************************
if event.Type() == keptn.ConfigurationChangeEventType {
log.Printf("Processing Configuration Change Event")
configChangeEventData := &keptn.ConfigurationChangeEventData{}
err := event.DataAs(configChangeEventData)
if err != nil {
log.Printf("Got Data Error: %s", err.Error())
return err
}
return HandleConfigurationChangeEvent(myKeptn, event, configChangeEventData)
} else if event.Type() == keptn.DeploymentFinishedEventType {
log.Printf("Processing Deployment Finished Event")
deployFinishEventData := &keptn.DeploymentFinishedEventData{}
err := event.DataAs(deployFinishEventData)
if err != nil {
log.Printf("Got Data Error: %s", err.Error())
return err
}
return HandleDeploymentFinishedEvent(myKeptn, event, deployFinishEventData)
} else if event.Type() == keptn.TestsFinishedEventType {
log.Printf("Processing Test Finished Event")
testsFinishedEventData := &keptn.TestsFinishedEventData{}
err := event.DataAs(testsFinishedEventData)
if err != nil {
log.Printf("Got Data Error: %s", err.Error())
return err
}
return HandleTestsFinishedEvent(myKeptn, event, testsFinishedEventData)
} else if event.Type() == keptn.StartEvaluationEventType {
log.Printf("Processing Start Evaluation Event")
startEvaluationEventData := &keptn.StartEvaluationEventData{}
err := event.DataAs(startEvaluationEventData)
if err != nil {
log.Printf("Got Data Error: %s", err.Error())
return err
}
return HandleStartEvaluationEvent(myKeptn, event, startEvaluationEventData)
} else if event.Type() == keptn.EvaluationDoneEventType {
log.Printf("Processing Evaluation Done Event")
evaluationDoneEventData := &keptn.EvaluationDoneEventData{}
err := event.DataAs(evaluationDoneEventData)
if err != nil {
log.Printf("Got Data Error: %s", err.Error())
return err
}
return HandleEvaluationDoneEvent(myKeptn, event, evaluationDoneEventData)
} else if event.Type() == keptn.InternalGetSLIEventType {
log.Printf("Processing Internal Get SLI Event")
internalGetSLIEventData := &keptn.InternalGetSLIEventData{}
err := event.DataAs(internalGetSLIEventData)
if err != nil {
log.Printf("Got Data Error: %s", err.Error())
return err
}
return HandleInternalGetSLIEvent(myKeptn, event, internalGetSLIEventData)
} else if event.Type() == keptn.ProblemOpenEventType || event.Type() == keptn.ProblemEventType {
// Subscribing to a problem.open or problem event is deprecated since Keptn 0.7 - subscribe to sh.keptn.event.action.triggered
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")
log.Printf("Processing Problem Event")
problemEventData := &keptn.ProblemEventData{}
err := event.DataAs(problemEventData)
if err != nil {
log.Printf("Got Data Error: %s", err.Error())
return err
}
return HandleProblemEvent(myKeptn, event, problemEventData)
} else if event.Type() == keptn.ActionTriggeredEventType {
log.Printf("Processing Action Triggered Event")
actionTriggeredEventData := &keptn.ActionTriggeredEventData{}
err := event.DataAs(actionTriggeredEventData)
if err != nil {
log.Printf("Got Data Error: %s", err.Error())
return err
}
return HandleActionTriggeredEvent(myKeptn, event, actionTriggeredEventData)
} else if event.Type() == keptn.ConfigureMonitoringEventType {
log.Printf("Processing Configure Monitoring Event")
configureMonitoringEventData := &keptn.ConfigureMonitoringEventData{}
err := event.DataAs(configureMonitoringEventData)
if err != nil {
log.Printf("Got Data Error: %s", err.Error())
return err
}
return HandleConfigureMonitoringEvent(myKeptn, event, configureMonitoringEventData)
}
// Unknown Event -> Throw Error!
var errorMsg string
errorMsg = fmt.Sprintf("Unhandled Keptn Cloud Event: %s", event.Type())
log.Print(errorMsg)
return errors.New(errorMsg)
}
/**
* Usage: ./main
* no args: starts listening for cloudnative events on localhost:port/path
*
* Environment Variables
* env=runlocal -> will fetch resources from local drive instead of configuration service
*/
func main() {
var env envConfig
if err := envconfig.Process("", &env); err != nil {
log.Fatalf("Failed to process env var: %s", err)
}
os.Exit(_main(os.Args[1:], env))
}
/**
* Opens up a listener on localhost:port/path and passes incoming requets to gotEvent
*/
func _main(args []string, env envConfig) int {
ctx := context.Background()
// configure keptn options
if env.Env == "local" {
log.Println("env=local: Running with local filesystem to fetch resources")
keptnOptions.UseLocalFileSystem = true
}
keptnOptions.ConfigurationServiceURL = env.ConfigurationServiceUrl
keptnOptions.EventBrokerURL = env.EventBrokerUrl
// configure http server to receive cloudevents
t, err := cloudeventshttp.New(
cloudeventshttp.WithPort(env.Port),
cloudeventshttp.WithPath(env.Path),
)
log.Println("Starting pac-sliprovider...")
log.Printf(" on Port = %d; Path=%s", env.Port, env.Path)
if err != nil {
log.Fatalf("failed to create transport, %v", err)
}
c, err := client.New(t)
if err != nil {
log.Fatalf("failed to create client, %v", err)
}
log.Fatalf("failed to start receiver: %s", c.StartReceiver(ctx, processKeptnCloudEvent))
return 0
}