From a3301255bc613df05ddbaa50a78de17cf3784aa5 Mon Sep 17 00:00:00 2001 From: Daniel Steininger Date: Wed, 7 Jul 2021 19:20:18 +0200 Subject: [PATCH 1/3] Offers the possibility to use an internal proxy. --- config.sample.toml | 1 + helm/templates/configmap.yaml | 1 + helm/values.yaml | 1 + kustomize/overlays/prod/raw/config.toml | 1 + main.go | 28 ++++++++++++++++++++----- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/config.sample.toml b/config.sample.toml index db742e4..6a839e4 100644 --- a/config.sample.toml +++ b/config.sample.toml @@ -18,6 +18,7 @@ max_size = 4000 [app.http_client] max_idle_conns = 100 request_timeout = 8000 +proxy_url = "http://internal-squid-proxy.com:3128" [app.chat.alertManagerTestRoom] notification_url = "https://chat.googleapis.com/v1/spaces/xxx/messages?key=abc-xyz&token=token-unique-key%3D" diff --git a/helm/templates/configmap.yaml b/helm/templates/configmap.yaml index b589f4f..bae9331 100644 --- a/helm/templates/configmap.yaml +++ b/helm/templates/configmap.yaml @@ -30,6 +30,7 @@ data: [app.http_client] max_idle_conns = {{ .Values.configmap.app.http_client.max_idle_conns }} request_timeout = {{ .Values.configmap.app.http_client.request_timeout }} + proxy_url = {{ .Values.configmap.app.http_client.proxy_url }} {{- with .Values.configmap.rooms }} {{ tpl . $ | indent 4 }} diff --git a/helm/values.yaml b/helm/values.yaml index 6ec51b7..d4a458e 100644 --- a/helm/values.yaml +++ b/helm/values.yaml @@ -18,6 +18,7 @@ configmap: http_client: max_idle_conns: "100" request_timeout: "8000" + proxy_url: "http://internal-squid-proxy.com:3128" rooms: | [app.chat.alertManagerTestRoom] diff --git a/kustomize/overlays/prod/raw/config.toml b/kustomize/overlays/prod/raw/config.toml index 0827b81..51b6295 100644 --- a/kustomize/overlays/prod/raw/config.toml +++ b/kustomize/overlays/prod/raw/config.toml @@ -17,6 +17,7 @@ template_file = "message.tmpl" [app.http_client] max_idle_conns = 100 request_timeout = 8000 +proxy_url = "http://115.124.92.42:8080" [app.chat.alertManagerTestRoom] notification_url = "https://chat.googleapis.com/v1/spaces/xxx/messages?key=abc-xyz&token=token-unique-key%3D" diff --git a/main.go b/main.go index e999016..7da88ac 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "net/http" + "net/url" "os" "time" @@ -70,6 +71,7 @@ func initConfig() { viper.SetDefault("server.read_timeout", 1000) viper.SetDefault("server.write_timeout", 5000) viper.SetDefault("server.keepalive_timeout", 30000) + viper.SetDefault("app.http_client.proxy_url", "") viper.SetDefault("app.max_size", 4000) // Process flags. flagSet.Parse(os.Args[1:]) @@ -92,13 +94,29 @@ func initConfig() { } func initClient() *http.Client { + + transport := &http.Transport{ + MaxIdleConnsPerHost: viper.GetInt("app.http_client.max_idle_conns"), + ResponseHeaderTimeout: time.Duration(viper.GetDuration("app.http_client.request_timeout") * time.Millisecond), + } + + proxyURLString := viper.GetString("app.http_client.proxy_url") + + if proxyURLString != "" { + + proxyURL, err := url.Parse(proxyURLString) + if err != nil { + errLog.Fatalf("Error enter proxy_url: %s", err) + } + + transport.Proxy = http.ProxyURL(proxyURL) + } + // Generic HTTP handler for communicating with the Chat webhook endpoint. return &http.Client{ - Timeout: time.Duration(viper.GetDuration("app.http_client.request_timeout") * time.Millisecond), - Transport: &http.Transport{ - MaxIdleConnsPerHost: viper.GetInt("app.http_client.max_idle_conns"), - ResponseHeaderTimeout: time.Duration(viper.GetDuration("app.http_client.request_timeout") * time.Millisecond), - }} + Timeout: time.Duration(viper.GetDuration("app.http_client.request_timeout") * time.Millisecond), + Transport: transport} + } // prog initialisation. From 4d127985e3d75e1aa73d7e4a1bc58854793e81ce Mon Sep 17 00:00:00 2001 From: dreamblack86 Date: Thu, 8 Jul 2021 08:18:12 +0200 Subject: [PATCH 2/3] Adjust error message; Replace placeholder/example proxy_url. --- kustomize/overlays/prod/raw/config.toml | 2 +- main.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kustomize/overlays/prod/raw/config.toml b/kustomize/overlays/prod/raw/config.toml index 51b6295..be36d7d 100644 --- a/kustomize/overlays/prod/raw/config.toml +++ b/kustomize/overlays/prod/raw/config.toml @@ -17,7 +17,7 @@ template_file = "message.tmpl" [app.http_client] max_idle_conns = 100 request_timeout = 8000 -proxy_url = "http://115.124.92.42:8080" +proxy_url = "http://internal-squid-proxy.com:3128" [app.chat.alertManagerTestRoom] notification_url = "https://chat.googleapis.com/v1/spaces/xxx/messages?key=abc-xyz&token=token-unique-key%3D" diff --git a/main.go b/main.go index 7da88ac..8903270 100644 --- a/main.go +++ b/main.go @@ -106,7 +106,7 @@ func initClient() *http.Client { proxyURL, err := url.Parse(proxyURLString) if err != nil { - errLog.Fatalf("Error enter proxy_url: %s", err) + errLog.Fatalf("Unable to parse `proxy_url`: %s", err) } transport.Proxy = http.ProxyURL(proxyURL) From eb3a2fb51949da6de0da6ae4ed060c48da2466f9 Mon Sep 17 00:00:00 2001 From: dreamblack86 Date: Thu, 8 Jul 2021 12:02:37 +0200 Subject: [PATCH 3/3] Added missing quote pipe --- helm/templates/configmap.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helm/templates/configmap.yaml b/helm/templates/configmap.yaml index bae9331..79328a9 100644 --- a/helm/templates/configmap.yaml +++ b/helm/templates/configmap.yaml @@ -30,7 +30,7 @@ data: [app.http_client] max_idle_conns = {{ .Values.configmap.app.http_client.max_idle_conns }} request_timeout = {{ .Values.configmap.app.http_client.request_timeout }} - proxy_url = {{ .Values.configmap.app.http_client.proxy_url }} + proxy_url = {{ .Values.configmap.app.http_client.proxy_url | quote }} {{- with .Values.configmap.rooms }} {{ tpl . $ | indent 4 }}