1
1
package main
2
2
3
3
import (
4
- "bytes"
5
- "context"
6
- "encoding/json"
7
- "errors"
8
- "fmt"
9
4
"io/ioutil"
10
5
"log"
11
6
"math"
@@ -14,6 +9,12 @@ import (
14
9
"os"
15
10
"strings"
16
11
12
+ "bytes"
13
+ "context"
14
+ "encoding/json"
15
+ "errors"
16
+ "fmt"
17
+
17
18
"github.com/cloudevents/sdk-go/v2/types"
18
19
"github.com/google/uuid"
19
20
"github.com/kelseyhightower/envconfig"
@@ -36,7 +37,7 @@ const serviceName = "prometheus-service"
36
37
37
38
type envConfig struct {
38
39
// Port on which to listen for cloudevents
39
- Port int `envconfig:"RCV_PORT" default:"8080"`
40
+ Port int `envconfig:"RCV_PORT" default:"8082"` // Note: must not be 8080 and not 8081
40
41
Path string `envconfig:"RCV_PATH" default:"/"`
41
42
ConfigurationServiceURL string `envconfig:"CONFIGURATION_SERVICE" default:""`
42
43
}
@@ -54,23 +55,33 @@ type ceTest struct {
54
55
var (
55
56
namespace = os .Getenv ("POD_NAMESPACE" )
56
57
prometheusEndpoint = os .Getenv ("PROMETHEUS_ENDPOINT" )
58
+ env envConfig
57
59
)
58
60
59
61
func main () {
60
- // listen on port 8080 for any event
62
+ /**
63
+ Note that prometheus-service requires to open multiple ports:
64
+ * 8080 (default port; exposed) - acts as the ingest for prometheus alerts, and also proxies CloudEvents to port 8082
65
+ * 8081 (Keptn distributor) - Port that keptn/distributor is listening too (default port for Keptn distributor)
66
+ * 8082 (CloudEvents; env.Port) - Port that the CloudEvents sdk is listening to; this port is not exposed, but will be used for internal communication
67
+ */
61
68
logger := keptncommon .NewLogger ("" , "" , "prometheus-service" )
62
69
63
- logger . Debug ( "Starting server for receiving events on exposed port 8080" )
70
+ env = envConfig {}
64
71
65
- http .HandleFunc ("/" , Handler )
66
- go http .ListenAndServe (":8080" , nil )
67
-
68
- // listen on port 8081 for CloudEvent
69
- var env envConfig
70
72
if err := envconfig .Process ("" , & env ); err != nil {
71
73
logger .Error (fmt .Sprintf ("Failed to process env var: %s" , err ))
72
74
}
75
+
73
76
logger .Debug (fmt .Sprintf ("Configuration service: %s" , env .ConfigurationServiceURL ))
77
+ logger .Debug (fmt .Sprintf ("Port: %d, Path: %s" , env .Port , env .Path ))
78
+
79
+ // listen on port 8080 for any HTTP request (cloudevents are also handled, but forwarded to env.Port internally)
80
+ logger .Debug ("Starting server on port 8080..." )
81
+ http .HandleFunc ("/" , Handler )
82
+ go http .ListenAndServe (":8080" , nil )
83
+
84
+ // start internal CloudEvents handler (on port env.Port)
74
85
os .Exit (_main (env ))
75
86
}
76
87
@@ -122,7 +133,7 @@ func gotEvent(event cloudevents.Event) error {
122
133
123
134
}
124
135
125
- // Handler takes request and forwards it to the corresponding event handler
136
+ // Handler takes request and forwards it to the corresponding event handler; Note: cloudevents are also forwarded
126
137
func Handler (rw http.ResponseWriter , req * http.Request ) {
127
138
shkeptncontext := uuid .New ().String ()
128
139
logger := keptncommon .NewLogger (shkeptncontext , "" , "prometheus-service" )
@@ -139,7 +150,10 @@ func Handler(rw http.ResponseWriter, req *http.Request) {
139
150
if json .Unmarshal (body , & event ) != nil || event .Specversion == "" {
140
151
eventhandling .ProcessAndForwardAlertEvent (rw , body , logger , shkeptncontext )
141
152
} else {
142
- proxyReq , err := http .NewRequest (req .Method , "http://localhost:8080" , bytes .NewReader (body ))
153
+ forwardPath := fmt .Sprintf ("http://localhost:%d%s" , env .Port , env .Path )
154
+ logger .Debug ("Forwarding cloudevent to " + forwardPath )
155
+ // forward cloudevent to cloudevents lister on env.Port (see main())
156
+ proxyReq , err := http .NewRequest (req .Method , forwardPath , bytes .NewReader (body ))
143
157
proxyReq .Header .Set ("Content-Type" , "application/cloudevents+json" )
144
158
resp , err := http .DefaultClient .Do (proxyReq )
145
159
if err != nil {
0 commit comments