Skip to content

Commit

Permalink
Merge pull request #42 from patoarvizu/add_metrics_endpoint_to_webhook
Browse files Browse the repository at this point in the history
Add metrics endpoint
  • Loading branch information
patoarvizu authored Apr 1, 2020
2 parents 9a3bf6a + 7f9c4f8 commit f289591
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
27 changes: 22 additions & 5 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
2 changes: 2 additions & 0 deletions deploy/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ spec:
ports:
- name: https
containerPort: 4443
- name: metrics
containerPort: 8081
volumeMounts:
- name: tls
mountPath: /tls
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions helm/kms-vault-operator/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ spec:
ports:
- name: https
containerPort: 4443
- name: metrics
containerPort: 8081
volumeMounts:
- name: tls
mountPath: /tls
Expand Down

0 comments on commit f289591

Please sign in to comment.