Skip to content

Commit bbb57e0

Browse files
authored
Merge pull request #26 from appuio/separate-healthz-http
Separate health listen address for easier kube-rbac-proxy protection
2 parents bd9a972 + b7d8f21 commit bbb57e0

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

config/default/exporter_auth_proxy_patch.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@ spec:
99
spec:
1010
containers:
1111
- name: exporter
12+
env:
13+
- name: POD_IP
14+
valueFrom:
15+
fieldRef:
16+
apiVersion: v1
17+
fieldPath: status.podIP
1218
args:
1319
- "--listen-addr=127.0.0.1:8080"
20+
- "--health-listen-addr=$(POD_IP):8081"
1421
- name: kube-rbac-proxy
1522
securityContext:
1623
allowPrivilegeEscalation: false

config/exporter/exporter.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ spec:
5757
livenessProbe:
5858
httpGet:
5959
path: /healthz
60-
port: 8080
60+
port: 8081
6161
periodSeconds: 20
6262
initialDelaySeconds: 15
6363
timeoutSeconds: 3

main.go

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package main
22

33
import (
4+
"context"
45
"flag"
56
"fmt"
67
"log"
78
"net/http"
9+
"os"
10+
"os/signal"
11+
"sync"
812

913
alertscollector "github.com/appuio/alerts_exporter/internal/alerts_collector"
1014
"github.com/appuio/alerts_exporter/internal/healthcheck"
@@ -15,7 +19,7 @@ import (
1519
"github.com/prometheus/client_golang/prometheus/promhttp"
1620
)
1721

18-
var listenAddr string
22+
var listenAddr, healthListenAddr string
1923

2024
var host string
2125
var withInhibited, withSilenced, withUnprocessed, withActive bool
@@ -29,6 +33,7 @@ var k8sBearerTokenAuth bool
2933

3034
func main() {
3135
flag.StringVar(&listenAddr, "listen-addr", ":8080", "The addr to listen on")
36+
flag.StringVar(&healthListenAddr, "health-listen-addr", ":8081", "The addr to listen on for the health check endpoint.")
3237

3338
flag.StringVar(&host, "host", "localhost:9093", "The host of the Alertmanager")
3439

@@ -98,12 +103,48 @@ func main() {
98103
Filters: filters,
99104
})
100105

101-
// Expose metrics and custom registry via an HTTP server
102-
// using the HandleFor function. "/metrics" is the usual endpoint for that.
103-
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg}))
104-
http.HandleFunc("/healthz", healthcheck.HealthCheck{GeneralService: ac.General}.HandleHealthz)
105-
log.Printf("Listening on `%s`", listenAddr)
106-
log.Fatal(http.ListenAndServe(listenAddr, nil))
106+
msm := http.NewServeMux()
107+
msm.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg}))
108+
109+
hsm := http.NewServeMux()
110+
hsm.HandleFunc("/healthz", healthcheck.HealthCheck{GeneralService: ac.General}.HandleHealthz)
111+
112+
ms := &http.Server{
113+
Addr: listenAddr,
114+
Handler: msm,
115+
}
116+
117+
hs := &http.Server{
118+
Addr: healthListenAddr,
119+
Handler: hsm,
120+
}
121+
122+
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
123+
go func() {
124+
defer cancel()
125+
log.Printf("Metrics: Listening on `%s`", listenAddr)
126+
log.Println("Metrics:", ms.ListenAndServe())
127+
}()
128+
go func() {
129+
defer cancel()
130+
log.Printf("Healthz: Listening on `%s`", healthListenAddr)
131+
log.Println("Healthz:", hs.ListenAndServe())
132+
}()
133+
134+
var waitShutdown sync.WaitGroup
135+
waitShutdown.Add(2)
136+
go func() {
137+
defer waitShutdown.Done()
138+
<-ctx.Done()
139+
ms.Shutdown(context.Background())
140+
}()
141+
go func() {
142+
defer waitShutdown.Done()
143+
<-ctx.Done()
144+
hs.Shutdown(context.Background())
145+
}()
146+
147+
waitShutdown.Wait()
107148
}
108149

109150
type stringSliceFlag []string

0 commit comments

Comments
 (0)