This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
238 lines (191 loc) · 7.62 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
/*
* Reacts to sh.keptn.event.evaluation.finished and sh.keptn.events.problem
*/
import (
"context"
"errors"
"log"
"os"
"strconv"
cloudevents "github.com/cloudevents/sdk-go/v2" // make sure to use v2 cloudevents here
"github.com/kelseyhightower/envconfig"
keptn "github.com/keptn/go-utils/pkg/lib/keptn"
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0"
)
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:""`
}
type JiraDetails struct {
BaseURL string
Username string
APIToken string
AssigneeID string
ReporterID string
ProjectKey string
IssueType string
TicketForProblems bool
TicketForEvaluations bool
}
type KeptnDetails struct {
Domain string
BridgeURL string
}
var JIRA_DETAILS JiraDetails
var KEPTN_DETAILS KeptnDetails
// ServiceName specifies the current services name (e.g., used as source when sending CloudEvents)
const ServiceName = "jira-service"
// This method gets called when a new event is received from the Keptn Event Distributor
func processKeptnCloudEvent(ctx context.Context, event cloudevents.Event) error {
// create keptn handler
log.Printf("[main.go] Initializing Keptn Handler")
myKeptn, err := keptnv2.NewKeptn(&event, keptnOptions)
if err != nil {
return errors.New("Could not create Keptn Handler: " + err.Error())
}
setupAndDebug(myKeptn, event)
log.Println("[main.go] Received Cloud Event Type: " + event.Type())
if event.Type() == "sh.keptn.events.problem" { // sh.keptn.events.problem
log.Println("Processing sh.keptn.events.problem Event")
eventData := &keptnv2.ActionFinishedEventData{}
parseKeptnCloudEventPayload(event, eventData)
HandleProblemEvent(myKeptn, event, eventData)
}
if event.Type() == "sh.keptn.event.evaluation.finished" { // sh.keptn.event.evaluation.finished
log.Println("Processing evaluation.finished Event")
eventData := &keptnv2.EvaluationFinishedEventData{}
parseKeptnCloudEventPayload(event, eventData)
HandleEvaluationFinishedEvent(myKeptn, event, eventData)
}
return nil
}
/**
* 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("[main.go] 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 {
// configure keptn options
if env.Env == "local" {
log.Println("[main.go] env=local: Running with local filesystem to fetch resources")
keptnOptions.UseLocalFileSystem = true
}
keptnOptions.ConfigurationServiceURL = env.ConfigurationServiceUrl
log.Printf("[main.go] Starting %s...", ServiceName)
log.Printf("[main.go] on Port = %d; Path=%s", env.Port, env.Path)
ctx := context.Background()
ctx = cloudevents.WithEncodingStructured(ctx)
log.Printf("[main.go] Creating new http handler")
// configure http server to receive cloudevents
p, err := cloudevents.NewHTTP(cloudevents.WithPath(env.Path), cloudevents.WithPort(env.Port))
if err != nil {
log.Fatalf("[main.go] failed to create client, %v", err)
}
c, err := cloudevents.NewClient(p)
if err != nil {
log.Fatalf("failed to create client, %v", err)
}
log.Printf("[main.go] Starting receiver")
log.Fatal(c.StartReceiver(ctx, processKeptnCloudEvent))
return 0
}
/**
* Parses a Keptn Cloud Event payload (data attribute)
*/
func parseKeptnCloudEventPayload(event cloudevents.Event, data interface{}) error {
err := event.DataAs(data)
if err != nil {
log.Fatalf("Got Data Error: %s", err.Error())
return err
}
return nil
}
func setJIRADetails() {
JIRA_DETAILS.BaseURL = os.Getenv("JIRA_BASE_URL")
JIRA_DETAILS.Username = os.Getenv("JIRA_USERNAME")
JIRA_DETAILS.AssigneeID = os.Getenv("JIRA_ASSIGNEE_ID")
JIRA_DETAILS.ReporterID = os.Getenv("JIRA_REPORTER_ID")
JIRA_DETAILS.APIToken = os.Getenv("JIRA_API_TOKEN")
JIRA_DETAILS.ProjectKey = os.Getenv("JIRA_PROJECT_KEY")
JIRA_DETAILS.IssueType = os.Getenv("JIRA_ISSUE_TYPE")
JIRA_DETAILS.TicketForProblems, _ = strconv.ParseBool(os.Getenv("JIRA_TICKET_FOR_PROBLEMS"))
JIRA_DETAILS.TicketForEvaluations, _ = strconv.ParseBool(os.Getenv("JIRA_TICKET_FOR_EVALUATIONS"))
}
func setKeptnDetails() {
KEPTN_DETAILS.Domain = os.Getenv("KEPTN_DOMAIN")
// If Bridge URL isn't set in YAML file, default to the KEPTN_DOMAIN which is mandatory
if os.Getenv("KEPTN_BRIDGE_URL") == "" {
KEPTN_DETAILS.BridgeURL = os.Getenv("KEPTN_DOMAIN")
} else {
KEPTN_DETAILS.BridgeURL = os.Getenv("KEPTN_BRIDGE_URL")
}
}
func setupAndDebug(myKeptn *keptnv2.Keptn, incomingEvent cloudevents.Event) {
log.Printf("[main.go] gotEvent(%s): %s - %s", incomingEvent.Type(), myKeptn.KeptnContext, incomingEvent.Context.GetID())
// Get Debug Mode
// This is set in the service.yaml as DEBUG "true"
DEBUG, _ := strconv.ParseBool(os.Getenv("DEBUG"))
log.Printf("[main.go] Debug Mode: %v \n", DEBUG)
// Set JIRA Details
setJIRADetails()
// Get Dynatrace Tenant
dynaTraceTenant := os.Getenv("DT_TENANT")
// KEPTN_DOMAIN must be set but KEPTN_BRIDGE_URL is optional in jira-service deployment.yaml file
setKeptnDetails()
if JIRA_DETAILS.BaseURL == "" ||
JIRA_DETAILS.Username == "" ||
JIRA_DETAILS.APIToken == "" ||
JIRA_DETAILS.ProjectKey == "" ||
JIRA_DETAILS.IssueType == "" ||
KEPTN_DETAILS.Domain == "" {
log.Println("[main.go] Missing mandatory input parameters JIRA_BASE_URL and / or JIRA_USERNAME and / or JIRA_API_TOKEN and / or JIRA_PROJECT_KEY and / or JIRA_ISSUE_TYPE and / or KEPTN_DOMAIN.")
}
if DEBUG {
log.Println("[main.go] --- Printing JIRA Input Details ---")
log.Printf("[main.go] Base URL: %s \n", JIRA_DETAILS.BaseURL)
log.Printf("[main.go] Username: %s \n", JIRA_DETAILS.Username)
log.Printf("[main.go] Assignee ID: %s \n", JIRA_DETAILS.AssigneeID)
log.Printf("[main.go] Reporter ID: %s \n", JIRA_DETAILS.ReporterID)
log.Printf("[main.go] API Token: %s \n", JIRA_DETAILS.APIToken)
log.Printf("[main.go] Project Key: %s \n", JIRA_DETAILS.ProjectKey)
log.Printf("[main.go] Issue Type: %s \n", JIRA_DETAILS.IssueType)
log.Printf("[main.go] Ticket For Problems: %v \n", JIRA_DETAILS.TicketForProblems)
log.Printf("[main.go] Ticket For Problems: %v \n", JIRA_DETAILS.TicketForEvaluations)
log.Println("[main.go] --- End Printing JIRA Input Details ---")
log.Printf("[main.go] Dynatrace Tenant: %s \n", dynaTraceTenant)
log.Printf("[main.go] Keptn Domain: %s \n", KEPTN_DETAILS.Domain)
log.Printf("[main.go] Keptn Bridge URL: %s \n", KEPTN_DETAILS.BridgeURL)
// At this point, we have all mandatory input params. Proceed
log.Println("[main.go] Got all input variables. Proceeding...")
if JIRA_DETAILS.TicketForProblems {
log.Println("[main.go] Will create tickets for problems")
} else {
log.Println("[main.go] Will NOT create tickets for problems")
}
if JIRA_DETAILS.TicketForEvaluations {
log.Println("[main.go] Will create tickets for evaluations")
} else {
log.Println("[main.go] Will NOT create tickets for evaluations")
}
}
}