-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.go
193 lines (160 loc) · 5.07 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
package main
import (
"context"
"errors"
"io"
"log"
"net/http"
"os"
"sync"
"cloud.google.com/go/pubsub"
"go.uber.org/zap"
"google.golang.org/api/option"
"github.com/fission/keda-connectors/common"
)
type pubsubConnector struct {
pubsubInfo GCPPubsubConnInfo
connectordata common.ConnectorMetadata
logger *zap.Logger
}
// GCPPubsubConnInfo contains the fields needed to connect to the GCP's pub sub topic queue.
type GCPPubsubConnInfo struct {
ProjectID string
SubscriptionID string
// Creds points to the json value(not file) which has the service account key
Creds string
}
func main() {
logger, err := zap.NewProduction()
if err != nil {
log.Fatalf("can't initialize zap logger: %v", err)
}
defer logger.Sync()
connectordata, err := common.ParseConnectorMetadata()
if err != nil {
logger.Error("Environment variable is missing ", zap.Error(err))
os.Exit(127)
}
pubsubInfo, err := GetGCPInfo()
if err != nil {
logger.Error("failed to find GCP creds or project name or subscription name", zap.Error(err))
return
}
conn := pubsubConnector{
pubsubInfo: *pubsubInfo,
connectordata: connectordata,
logger: logger,
}
logger.Info("Conn: %s", zap.String("Response topic", conn.connectordata.ResponseTopic))
conn.consumeMessage()
}
func (conn pubsubConnector) consumeMessage() {
creds := []byte(conn.pubsubInfo.Creds)
headers := http.Header{
"KEDA-Topic": {conn.connectordata.Topic},
"KEDA-Response-Topic": {conn.connectordata.ResponseTopic},
"KEDA-Error-Topic": {conn.connectordata.ErrorTopic},
"Content-Type": {conn.connectordata.ContentType},
"KEDA-Source-Name": {conn.connectordata.SourceName},
}
ctx := context.Background()
client, err := pubsub.NewClient(ctx, conn.pubsubInfo.ProjectID, option.WithCredentialsJSON(creds))
if err != nil {
conn.logger.Error(err.Error())
}
var mu sync.Mutex
sub := client.Subscription(conn.pubsubInfo.SubscriptionID)
err = sub.Receive(ctx, func(ctx context.Context, msg *pubsub.Message) {
mu.Lock()
defer mu.Unlock()
// Set the attributes as message header came from PubSub record
for k, v := range msg.Attributes {
headers.Add(k, v)
}
// Push the message to the endpoint
resp, err := common.HandleHTTPRequest(string(msg.Data), headers, conn.connectordata, conn.logger)
if err != nil {
if conn.connectordata.ErrorTopic != "" {
conn.responseOrErrorHandler(conn.connectordata.ErrorTopic, err.Error(), headers)
}
conn.logger.Error("Error sending the message to the endpoint %v", zap.Error(err))
} else {
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
if conn.connectordata.ErrorTopic != "" {
conn.responseOrErrorHandler(conn.connectordata.ErrorTopic, string(body), headers)
}
conn.logger.Error("Error reading body", zap.Error(err))
} else {
msg.Ack()
if conn.connectordata.ResponseTopic != "" {
conn.responseOrErrorHandler(conn.connectordata.ResponseTopic, string(body), headers)
}
conn.logger.Info("Success in sending the message", zap.Any("Messsage sent: ", msg))
}
}
})
if err != nil {
conn.logger.Error(err.Error())
}
}
func (conn pubsubConnector) responseOrErrorHandler(topicID string, response string, headers http.Header) {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, conn.pubsubInfo.ProjectID, option.WithCredentialsJSON([]byte(conn.pubsubInfo.Creds)))
if err != nil {
conn.logger.Error("pubsub.NewClient: %v", zap.Error(err))
}
t := client.Topic(topicID)
result := t.Publish(ctx, &pubsub.Message{
Data: []byte(response),
Attributes: convHeadersToAttr(headers),
})
var wg sync.WaitGroup
wg.Add(1)
go func(res *pubsub.PublishResult) {
// The Get method blocks until a server-generated ID or
// an error is returned for the published message.
_, err := res.Get(ctx)
if err != nil {
conn.logger.Error("Failed to publish: %v", zap.Error(err))
return
}
}(result)
wg.Wait()
conn.logger.Info("Published message , topic name: %v\n", zap.String("Topic", topicID))
}
// convHeadersToAttr converts the headers to attributes which can be published by pubsub
func convHeadersToAttr(headers http.Header) map[string]string {
var attr = make(map[string]string)
for k, v := range headers {
for _, d := range v {
attr[k] = d
}
}
return attr
}
// GetGCPInfo gets the configuration required to connect to GCP
func GetGCPInfo() (*GCPPubsubConnInfo, error) {
creds := os.Getenv("CREDENTIALS_FROM_ENV")
projectID := os.Getenv("PROJECT_ID")
subscriptionID := os.Getenv("SUBSCRIPTION_NAME")
triggerCreds := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
if projectID != "" && subscriptionID != "" && triggerCreds != "" {
connInfo := &GCPPubsubConnInfo{
ProjectID: projectID,
SubscriptionID: subscriptionID,
Creds: triggerCreds,
}
return connInfo, nil
}
if subscriptionID != "" && creds != "" {
connInfo := &GCPPubsubConnInfo{
ProjectID: projectID,
SubscriptionID: subscriptionID,
Creds: creds,
}
return connInfo, nil
}
return nil, errors.New("Provide credentials ")
}