Skip to content

Commit

Permalink
feat: Parse metrics port from env (#171)
Browse files Browse the repository at this point in the history
* feat: Parse metrics port from env

* gofmt
  • Loading branch information
lindnerby authored Dec 8, 2023
1 parent 65045b2 commit f79eb40
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
35 changes: 26 additions & 9 deletions runtime-watcher/internal/serverconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ import (
)

const (
minPort, maxPort = 1, 65535
defaultPort = 8443
envWebhookPort = "WEBHOOK_PORT"
envCACert = "CA_CERT"
envTLSCert = "TLS_CERT"
envTLSKey = "TLS_KEY"
envKCPAddress = "KCP_ADDR"
envKCPContract = "KCP_CONTRACT"
minPort, maxPort = 1, 65535
defaultWebhookPort = 8443
defaultMetricsPort = 2112
envWebhookPort = "WEBHOOK_PORT"
envMetricsPort = "METRICS_PORT"
envCACert = "CA_CERT"
envTLSCert = "TLS_CERT"
envTLSKey = "TLS_KEY"
envKCPAddress = "KCP_ADDR"
envKCPContract = "KCP_CONTRACT"
)

type ServerConfig struct {
Port int
MetricsPort int
CACertPath string
TLSCertPath string
TLSKeyPath string
Expand All @@ -34,7 +37,7 @@ func ParseFromEnv(logger logr.Logger) (ServerConfig, error) {

config := ServerConfig{}

config.Port = defaultPort
config.Port = defaultWebhookPort
webhookPort, found := os.LookupEnv(envWebhookPort)
if found {
port, err := strconv.Atoi(webhookPort)
Expand All @@ -48,6 +51,20 @@ func ParseFromEnv(logger logr.Logger) (ServerConfig, error) {
}
}

config.MetricsPort = defaultMetricsPort
metricsPort, found := os.LookupEnv(envMetricsPort)
if found {
port, err := strconv.Atoi(metricsPort)
if err != nil {
logger.Error(err, flagError(envMetricsPort).Error())
}
if err = validatePortRange(port); err != nil {
logger.Error(err, flagError(envMetricsPort).Error())
} else {
config.MetricsPort = port
}
}

config.CACertPath = os.Getenv(envCACert)
if config.CACertPath == "" {
return config, flagError(envCACert)
Expand Down
21 changes: 11 additions & 10 deletions runtime-watcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"

"github.com/prometheus/client_golang/prometheus/promhttp"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"

Expand Down Expand Up @@ -59,16 +60,6 @@ func main() {

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

http.Handle("/metrics", promhttp.Handler())
metricsServer := &http.Server{
Addr: ":2112",
ReadHeaderTimeout: internal.HTTPTimeout,
}
err := metricsServer.ListenAndServe()
if err != nil {
logger.Error(err, "failed to wire up metrics endpoint")
}

logger.Info("starting the Runtime Watcher", "Version:", buildVersion)

config, err := serverconfig.ParseFromEnv(logger)
Expand All @@ -77,6 +68,16 @@ func main() {
return
}

http.Handle("/metrics", promhttp.Handler())
metricsServer := &http.Server{
Addr: fmt.Sprintf(":%d", config.MetricsPort),
ReadHeaderTimeout: internal.HTTPTimeout,
}
err = metricsServer.ListenAndServe()
if err != nil {
logger.Error(err, "failed to wire up metrics endpoint")
}

restConfig := ctrl.GetConfigOrDie()
restClient, err := client.New(restConfig, client.Options{})
if err != nil {
Expand Down

0 comments on commit f79eb40

Please sign in to comment.