generated from keptn-sandbox/keptn-service-template-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
214 lines (178 loc) · 7.12 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
214
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"
)
const eventbroker = "EVENTBROKER"
var keptnOptions = keptn.KeptnOpts{}
var runlocal = (os.Getenv("env") == "runlocal")
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:"CONFIGURATION_SERVICE" 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())
var shkeptncontext string
event.Context.ExtensionAs("shkeptncontext", &shkeptncontext)
// create a base Keptn Event
keptnEvent := baseKeptnEvent{
event: event.Type(),
source: event.Source(),
context: shkeptncontext,
}
logger := keptn.NewLogger(shkeptncontext, event.Context.GetID(), "gitlab-service")
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 {
logger.Info("Processing Configuration Change Event")
configChangeEventData := &keptn.ConfigurationChangeEventData{}
err := event.DataAs(configChangeEventData)
if err != nil {
log.Printf("Got Data Error: %s", err.Error())
return err
}
keptnEvent.project = configChangeEventData.Project
keptnEvent.stage = configChangeEventData.Stage
keptnEvent.service = configChangeEventData.Service
keptnEvent.labels = configChangeEventData.Labels
return HandleConfigurationChangeEvent(myKeptn, keptnEvent, event, configChangeEventData, logger)
} 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
}
keptnEvent.project = deployFinishEventData.Project
keptnEvent.stage = deployFinishEventData.Stage
keptnEvent.service = deployFinishEventData.Service
keptnEvent.labels = deployFinishEventData.Labels
return HandleDeploymentFinishedEvent(myKeptn, keptnEvent, event, deployFinishEventData, logger)
} 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
}
keptnEvent.project = testsFinishedEventData.Project
keptnEvent.stage = testsFinishedEventData.Stage
keptnEvent.service = testsFinishedEventData.Service
keptnEvent.labels = testsFinishedEventData.Labels
return HandleTestsFinishedEvent(myKeptn, keptnEvent, event, testsFinishedEventData, logger)
} 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
}
keptnEvent.project = startEvaluationEventData.Project
keptnEvent.stage = startEvaluationEventData.Stage
keptnEvent.service = startEvaluationEventData.Service
keptnEvent.labels = startEvaluationEventData.Labels
return HandleStartEvaluationEvent(myKeptn, keptnEvent, event, startEvaluationEventData, logger)
} 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.ProblemOpenEventType || event.Type() == keptn.ProblemEventType {
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)
}
// 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)
}
if runlocal {
log.Println("env=runlocal: Running with local filesystem to fetch resources")
log.Println("Also setting the env variable TESTTOKEN for testing purposes")
// set some test env variables
os.Setenv("TESTTOKEN", "MYTESTTOKENVALUE")
}
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 gitlab-service...")
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
}