Skip to content
This repository was archived by the owner on Jan 19, 2024. It is now read-only.

Commit c24c27e

Browse files
fix(core): Fix event forwarding to localhost (#195) (#203)
* #195 Fix event forwarding to localhost Signed-off-by: Christian Kreuzberger <[email protected]> * Added charts and deploy folder to dockerignore to speed up docker build Signed-off-by: Christian Kreuzberger <[email protected]>
1 parent 36948c6 commit c24c27e

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ README.md
33
.idea
44
skaffold.yaml
55
reviewdog.yml
6+
charts/
7+
deploy/

main.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package main
22

33
import (
4-
"bytes"
5-
"context"
6-
"encoding/json"
7-
"errors"
8-
"fmt"
94
"io/ioutil"
105
"log"
116
"math"
@@ -14,6 +9,12 @@ import (
149
"os"
1510
"strings"
1611

12+
"bytes"
13+
"context"
14+
"encoding/json"
15+
"errors"
16+
"fmt"
17+
1718
"github.com/cloudevents/sdk-go/v2/types"
1819
"github.com/google/uuid"
1920
"github.com/kelseyhightower/envconfig"
@@ -36,7 +37,7 @@ const serviceName = "prometheus-service"
3637

3738
type envConfig struct {
3839
// 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
4041
Path string `envconfig:"RCV_PATH" default:"/"`
4142
ConfigurationServiceURL string `envconfig:"CONFIGURATION_SERVICE" default:""`
4243
}
@@ -54,23 +55,33 @@ type ceTest struct {
5455
var (
5556
namespace = os.Getenv("POD_NAMESPACE")
5657
prometheusEndpoint = os.Getenv("PROMETHEUS_ENDPOINT")
58+
env envConfig
5759
)
5860

5961
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+
*/
6168
logger := keptncommon.NewLogger("", "", "prometheus-service")
6269

63-
logger.Debug("Starting server for receiving events on exposed port 8080")
70+
env = envConfig{}
6471

65-
http.HandleFunc("/", Handler)
66-
go http.ListenAndServe(":8080", nil)
67-
68-
// listen on port 8081 for CloudEvent
69-
var env envConfig
7072
if err := envconfig.Process("", &env); err != nil {
7173
logger.Error(fmt.Sprintf("Failed to process env var: %s", err))
7274
}
75+
7376
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)
7485
os.Exit(_main(env))
7586
}
7687

@@ -122,7 +133,7 @@ func gotEvent(event cloudevents.Event) error {
122133

123134
}
124135

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
126137
func Handler(rw http.ResponseWriter, req *http.Request) {
127138
shkeptncontext := uuid.New().String()
128139
logger := keptncommon.NewLogger(shkeptncontext, "", "prometheus-service")
@@ -139,7 +150,10 @@ func Handler(rw http.ResponseWriter, req *http.Request) {
139150
if json.Unmarshal(body, &event) != nil || event.Specversion == "" {
140151
eventhandling.ProcessAndForwardAlertEvent(rw, body, logger, shkeptncontext)
141152
} 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))
143157
proxyReq.Header.Set("Content-Type", "application/cloudevents+json")
144158
resp, err := http.DefaultClient.Do(proxyReq)
145159
if err != nil {

0 commit comments

Comments
 (0)