From 7f9c4f8670287314dc889183203ae49f82320383 Mon Sep 17 00:00:00 2001 From: patoarvizu Date: Tue, 31 Mar 2020 20:20:39 -0400 Subject: [PATCH] Add metrics endpoint --- cmd/webhook/main.go | 27 +++++++++++++++---- deploy/operator.yaml | 2 ++ go.mod | 1 + .../templates/deployment.yaml | 2 ++ 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/cmd/webhook/main.go b/cmd/webhook/main.go index ea4c0cd..647df6e 100644 --- a/cmd/webhook/main.go +++ b/cmd/webhook/main.go @@ -15,6 +15,10 @@ import ( "github.com/slok/kubewebhook/pkg/log" validatingwh "github.com/slok/kubewebhook/pkg/webhook/validating" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/slok/kubewebhook/pkg/observability/metrics" ) type webhookCfg struct { @@ -115,15 +119,28 @@ func main() { Name: "kms-vault-secret-validator", Obj: &kmsvaultv1alpha1.KMSVaultSecret{}, } - - wh, err := validatingwh.NewWebhook(vhc, v, nil, nil, logger) + reg := prometheus.NewRegistry() + metricsRec := metrics.NewPrometheus(reg) + wh, err := validatingwh.NewWebhook(vhc, v, nil, metricsRec, logger) if err != nil { fmt.Fprintf(os.Stderr, "error creating webhook: %s", err) os.Exit(1) } - err = http.ListenAndServeTLS(cfg.addr, cfg.certFile, cfg.keyFile, whhttp.MustHandlerFor(wh)) - if err != nil { - fmt.Fprintf(os.Stderr, "error serving webhook: %s", err) + webhookError := make(chan error) + go func() { + webhookError <- http.ListenAndServeTLS(cfg.addr, cfg.certFile, cfg.keyFile, whhttp.MustHandlerFor(wh)) + }() + metricsError := make(chan error) + promHandler := promhttp.HandlerFor(reg, promhttp.HandlerOpts{}) + go func() { + metricsError <- http.ListenAndServe(cfg.metricsAddr, promHandler) + }() + if <-webhookError != nil { + fmt.Fprintf(os.Stderr, "error serving webhook: %s", <-webhookError) + os.Exit(1) + } + if <-metricsError != nil { + fmt.Fprintf(os.Stderr, "error serving metrics: %s", <-metricsError) os.Exit(1) } } diff --git a/deploy/operator.yaml b/deploy/operator.yaml index 0a42712..a3b9d2b 100644 --- a/deploy/operator.yaml +++ b/deploy/operator.yaml @@ -57,6 +57,8 @@ spec: ports: - name: https containerPort: 4443 + - name: metrics + containerPort: 8081 volumeMounts: - name: tls mountPath: /tls diff --git a/go.mod b/go.mod index 312bfe5..3dd8dba 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/go-openapi/spec v0.19.4 github.com/hashicorp/vault/api v1.0.4 github.com/operator-framework/operator-sdk v0.15.1 + github.com/prometheus/client_golang v1.2.1 github.com/slok/kubewebhook v0.9.0 github.com/spf13/pflag v1.0.5 k8s.io/api v0.18.0 diff --git a/helm/kms-vault-operator/templates/deployment.yaml b/helm/kms-vault-operator/templates/deployment.yaml index 67b3a5f..0b6ff61 100644 --- a/helm/kms-vault-operator/templates/deployment.yaml +++ b/helm/kms-vault-operator/templates/deployment.yaml @@ -89,6 +89,8 @@ spec: ports: - name: https containerPort: 4443 + - name: metrics + containerPort: 8081 volumeMounts: - name: tls mountPath: /tls