Skip to content

Commit

Permalink
fix: metrics/healthz bind port (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
azrod authored Nov 6, 2024
1 parent a42ace9 commit 253cf7a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
7 changes: 7 additions & 0 deletions .changelog/100.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:bug
`metrics` - Fix the issue where the port specified in the `metrics-port` configuration was not being used.
```

```release-note:bug
`healthz` - Fix the issue where the port specified in the `healthz-port` configuration was not being used.
```
3 changes: 2 additions & 1 deletion cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
Expand Down Expand Up @@ -84,7 +85,7 @@ func main() {
},
HealthProbeBindAddress: func() string {
if flag.Lookup(models.HealthzFlagName).Value.String() == "true" {
return httpserver.HealthzPort
return fmt.Sprintf(":%d", httpserver.HealthzPort)
}

return "0" // disable healthz server
Expand Down
18 changes: 7 additions & 11 deletions internal/httpserver/httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@ type (
)

var (
HealthzPort string = "" // expose var to be able to operator
HealthzPort int = 0 // expose var to be able to operator
HealthzPath string = "" // expose var to be able to operator
metricsPort string = ""
metricsPort int = 0
metricsPath string = ""

defaultAddr string = ":8080"
timeoutR = 5 * time.Second
DefaultFuncHealthz HealthzFunc = func() (bool, error) {
_, err := net.DialTimeout("tcp", models.HealthzDefaultAddr, timeoutR)
Expand All @@ -79,12 +78,12 @@ var (
func init() {
// * Healthz
flag.Bool(models.HealthzFlagName, false, "Enable the healthz server.")
flag.StringVar(&HealthzPort, models.HealthzPortFlagName, models.HealthzDefaultAddr, "Healthz server port.")
flag.IntVar(&HealthzPort, models.HealthzPortFlagName, int(models.HealthzDefaultPort), "Healthz server port.")
flag.StringVar(&HealthzPath, models.HealthzPathFlagName, models.HealthzDefaultPath, "Healthz server path.")

// * Metrics
flag.Bool(models.MetricsFlagName, false, "Enable the metrics server.")
flag.StringVar(&metricsPort, models.MetricsPortFlagName, models.MetricsDefaultAddr, "Metrics server port.")
flag.IntVar(&metricsPort, models.MetricsPortFlagName, int(models.MetricsDefaultPort), "Metrics server port.")
flag.StringVar(&metricsPath, models.MetricsPathFlagName, models.MetricsDefaultPath, "Metrics server path.")
}

Expand Down Expand Up @@ -135,13 +134,13 @@ func DisableMetrics() OptionServer {

// Function to create a new server for health
func (a *app) createHealth() *server {
s := a.new(WithAddr(HealthzPort))
s := a.new(WithAddr(fmt.Sprintf(":%d", HealthzPort)))
return s
}

// Function to create a new server for metrics
func (a *app) createMetrics() *server {
s := a.new(WithAddr(metricsPort))
s := a.new(WithAddr(fmt.Sprintf(":%d", metricsPort)))
s.Config.Get(metricsPath, metrics.Handler().ServeHTTP)
return s
}
Expand All @@ -156,7 +155,6 @@ func (a *app) new(opts ...Option) *server {
// create a new server with default parameters
s := &server{
http: &http.Server{
Addr: defaultAddr,
ReadTimeout: timeoutR,
},
Config: r,
Expand All @@ -174,9 +172,7 @@ func (a *app) new(opts ...Option) *server {
func WithAddr(addr string) Option {
_, _, err := net.SplitHostPort(addr)
if err != nil {
return func(s *server) {
s.http.Addr = defaultAddr
}
panic(fmt.Sprintf("invalid address: %s", addr))
}
return func(s *server) {
s.http.Addr = addr
Expand Down
2 changes: 2 additions & 0 deletions tools/env-dev/pod-operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ spec:
args:
- "/main"
- "--metrics"
- "--metrics-port=9080"
- "--healthz"
- "--healthz-port=9081"
- "--leader-elect"
ports:
- containerPort: 9443
Expand Down

0 comments on commit 253cf7a

Please sign in to comment.