-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
265 lines (212 loc) · 7.75 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"context"
"fmt"
"io"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/RedHatInsights/sources-api-go/kafka"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/redhatinsights/sources-superkey-worker/config"
l "github.com/redhatinsights/sources-superkey-worker/logger"
"github.com/redhatinsights/sources-superkey-worker/provider"
"github.com/redhatinsights/sources-superkey-worker/superkey"
"github.com/sirupsen/logrus"
)
const (
superkeyRequestedTopic = "platform.sources.superkey-requests"
)
var (
// DisableCreation disabled processing `create_application` sk requests
DisableCreation = os.Getenv("DISABLE_RESOURCE_CREATION")
// DisableDeletion disabled processing `destroy_application` sk requests
DisableDeletion = os.Getenv("DISABLE_RESOURCE_DELETION")
conf = config.Get()
superkeyTopic = conf.KafkaTopic(superkeyRequestedTopic)
)
func main() {
l.InitLogger(conf)
initMetrics()
initHealthCheck()
var brokers strings.Builder
for i, broker := range conf.KafkaBrokerConfig {
brokers.WriteString(broker.Hostname + ":" + strconv.Itoa(*broker.Port))
if i < len(conf.KafkaBrokerConfig)-1 {
brokers.WriteString(",")
}
}
l.Log.Infof("Listening to Kafka at: %s, topic: %v", brokers.String(), superkeyTopic)
l.Log.Infof("Talking to Sources API at: [%v]", fmt.Sprintf("%v://%v:%v", conf.SourcesScheme, conf.SourcesHost, conf.SourcesPort))
reader, err := kafka.GetReader(&kafka.Options{
BrokerConfig: conf.KafkaBrokerConfig,
Topic: superkeyTopic,
GroupID: &conf.KafkaGroupID,
Logger: l.Log.WithField("kafka", ""),
})
if err != nil {
l.Log.Fatalf(`could not get Kafka reader: %s`, err)
}
go func() {
l.Log.Info("SuperKey Worker started.")
kafka.Consume(
reader,
func(msg kafka.Message) {
processSuperkeyRequest(msg)
},
)
}()
interrupts := make(chan os.Signal, 1)
signal.Notify(interrupts, os.Interrupt, syscall.SIGTERM)
// wait for a signal from the OS, gracefully terminating the consumer
// if/when that comes in
s := <-interrupts
l.Log.Infof("Received %v, exiting", s)
kafka.CloseReader(reader, "superkey reader")
os.Exit(0)
}
// processSuperkeyRequest - processes messages.
func processSuperkeyRequest(msg kafka.Message) {
eventType := msg.GetHeader("event_type")
identityHeader := msg.GetHeader("x-rh-identity")
orgIdHeader := msg.GetHeader("x-rh-sources-org-id")
if identityHeader == "" && orgIdHeader == "" {
l.Log.WithFields(logrus.Fields{"kafka_message": string(msg.Value)}).Error(`Skipping Superkey request because no "x-rh-identity" or "x-rh-sources-org-id" headers were found`)
return
}
l.Log.WithFields(logrus.Fields{"org_id": orgIdHeader}).Debugf(`Processing Kafka message: %s`, string(msg.Value))
switch eventType {
case "create_application":
req := &superkey.CreateRequest{}
err := msg.ParseTo(req)
if err != nil {
l.Log.WithFields(logrus.Fields{"org_id": orgIdHeader}).Errorf(`Error parsing "create_application" request "%s": %s`, string(msg.Value), err)
return
}
req.IdentityHeader = identityHeader
req.OrgIdHeader = orgIdHeader
// Define the log context with the fields we want to log.
ctx := l.WithTenantId(context.Background(), req.TenantID)
ctx = l.WithSourceId(ctx, req.SourceID)
ctx = l.WithApplicationId(ctx, req.ApplicationID)
ctx = l.WithApplicationType(ctx, req.ApplicationType)
if DisableCreation == "true" {
l.LogWithContext(ctx).Info(`Skipping "create_application" request because the the resource creation was disabled by the env var`)
l.LogWithContext(ctx).Debugf(`Skipped "create_application" Kafka message: %s`, string(msg.Value))
return
}
l.LogWithContext(ctx).Info(`Processing "create_application" request`)
createResources(ctx, req)
l.LogWithContext(ctx).Info(`Finished processing "create_application"`)
case "destroy_application":
req := &superkey.DestroyRequest{}
err := msg.ParseTo(req)
if err != nil {
l.Log.WithFields(logrus.Fields{"org_id": orgIdHeader}).Errorf(`Error parsing "destroy_application" request "%s": %s`, string(msg.Value), err)
return
}
// Define the log context with the fields we want to log.
ctx := l.WithTenantId(context.Background(), req.TenantID)
if DisableDeletion == "true" {
l.LogWithContext(ctx).Info(`Skipping "create_application"" request because the the resource creation was disabled by the env var`)
l.LogWithContext(ctx).Debugf(`Skipping destroy_application request: %s`, string(msg.Value))
return
}
l.LogWithContext(ctx).Info(`Processing "destroy_application" request`)
destroyResources(ctx, req)
l.LogWithContext(ctx).Info(`Finished processing "destroy_application" request`)
default:
l.Log.WithFields(logrus.Fields{"org_id": orgIdHeader}).Errorf(`Unknown event type "%s" received in the header, skipping request...`, eventType)
}
}
func createResources(ctx context.Context, req *superkey.CreateRequest) {
l.LogWithContext(ctx).Debugf("Forging request: %v", req)
newApp, err := provider.Forge(ctx, req)
if err != nil {
l.LogWithContext(ctx).Errorf(`Tearing down Superkey request due to an error while forging the request \"%v\": %s`, req, err)
errors := provider.TearDown(ctx, newApp)
if len(errors) != 0 {
for _, err := range errors {
l.LogWithContext(ctx).Errorf(`Unable to tear down application: %s`, err)
}
}
err := req.MarkSourceUnavailable(ctx, err, newApp)
if err != nil {
l.LogWithContext(ctx).Errorf(`Error while marking the source and application as "unavailable" in Sources: %s`, err)
}
return
}
l.LogWithContext(ctx).Debug("Finished forging request")
err = newApp.CreateInSourcesAPI(ctx)
if err != nil {
l.LogWithContext(ctx).Errorf(`Error while creating or updating the resources in Sources: %s`, err)
provider.TearDown(ctx, newApp)
}
}
func destroyResources(ctx context.Context, req *superkey.DestroyRequest) {
l.LogWithContext(ctx).Debugf(`Unforging request "%v"`, req)
errors := provider.TearDown(ctx, superkey.ReconstructForgedApplication(req))
if len(errors) != 0 {
for _, err := range errors {
l.LogWithContext(ctx).Errorf(`Error during teardown: %s"`, err)
}
}
l.LogWithContext(ctx).Info("Finished destroying resources")
}
func initMetrics() {
go func() {
http.Handle("/metrics", promhttp.Handler())
err := http.ListenAndServe(fmt.Sprintf(":%d", conf.MetricsPort), nil)
if err != nil {
l.Log.Errorf("Metrics init error: %s", err)
}
}()
}
func initHealthCheck() {
go func() {
healthFile := "/tmp/healthy"
// which endpoints can we hit and get a 200 from without any auth, these are the main 2
// we need anyway.
svcs := []string{"https://iam.amazonaws.com", "https://s3.amazonaws.com"}
// custom http client with timeout
client := http.Client{Timeout: 5 * time.Second}
for {
for _, svc := range svcs {
resp, err := client.Get(svc)
if err == nil && resp.StatusCode == 200 {
// Copying to the bitbucket in order to gc the memory.
_, err := io.Copy(io.Discard, resp.Body)
if err != nil {
l.Log.Errorf("Error discarding response body: %v", err)
}
err = resp.Body.Close()
if err != nil {
l.Log.Errorf("Error closing response body: %v", err)
}
// check if file exists, creating it if not (first run, or recovery)
_, err = os.Stat(healthFile)
if err != nil {
_, err = os.Create(healthFile)
if err != nil {
l.Log.Errorf("Failed to touch healthcheck file")
}
}
} else {
l.Log.Warnf("Error hitting %s, err %v, removing %s", svc, err, healthFile)
_, err = os.Stat(healthFile)
// this is a bit complicated - err will be nil _if the file is there_, so we want
// to remove only if the err is nil.
if err == nil {
_ = os.Remove(healthFile)
}
break
}
}
time.Sleep(10 * time.Second)
}
}()
}