Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Parse metrics port from env #171

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading