Skip to content

Commit

Permalink
Provide custom metrics. (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 authored Jul 15, 2024
1 parent aefd9de commit ba443be
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ manager: generate fmt vet
-X 'github.com/metal-stack/v.Revision=$(GITVERSION)' \
-X 'github.com/metal-stack/v.GitSHA1=$(SHA)' \
-X 'github.com/metal-stack/v.BuildDate=$(BUILDDATE)'" \
-o bin/firewall-controller-manager main.go
-o bin/firewall-controller-manager .
strip bin/firewall-controller-manager

# Run against the mini-lab
Expand Down
23 changes: 23 additions & 0 deletions health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"
"log/slog"
"net/http"

v2 "github.com/metal-stack/firewall-controller-manager/api/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func healthCheckFunc(log *slog.Logger, seedClient client.Client, namespace string) func(req *http.Request) error {
return func(req *http.Request) error {
log.Debug("health check called")

fws := &v2.FirewallList{}
err := seedClient.List(req.Context(), fws, client.InNamespace(namespace))
if err != nil {
return fmt.Errorf("unable to list firewalls in namespace %s", namespace)
}
return nil
}
}
20 changes: 4 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"log"
"log/slog"
"net/http"
"os"
"time"

Expand All @@ -21,7 +20,7 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
controllerclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"

Expand All @@ -40,19 +39,6 @@ const (
metalAuthHMACEnvVar = "METAL_AUTH_HMAC"
)

func healthCheckFunc(log *slog.Logger, seedClient controllerclient.Client, namespace string) func(req *http.Request) error {
return func(req *http.Request) error {
log.Debug("health check called")

fws := &v2.FirewallList{}
err := seedClient.List(req.Context(), fws, controllerclient.InNamespace(namespace))
if err != nil {
return fmt.Errorf("unable to list firewalls in namespace %s", namespace)
}
return nil
}
}

func main() {
var (
scheme = helper.MustNewFirewallScheme()
Expand Down Expand Up @@ -150,7 +136,7 @@ func main() {

// cannot use seedMgr.GetClient() because it gets initialized at a later point in time
// we have to create an own client
seedClient, err := controllerclient.New(seedMgr.GetConfig(), controllerclient.Options{
seedClient, err := client.New(seedMgr.GetConfig(), client.Options{
Scheme: scheme,
})
if err != nil {
Expand All @@ -164,6 +150,8 @@ func main() {
log.Fatalf("unable to set up ready check %v", err)
}

mustRegisterCustomMetrics(l.WithGroup("metrics"), seedClient, namespace)

var (
externalShootAccess = &v2.ShootAccess{
GenericKubeconfigSecretName: shootKubeconfigSecret,
Expand Down
75 changes: 75 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"context"
"log/slog"
"time"

v2 "github.com/metal-stack/firewall-controller-manager/api/v2"
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)

var (
firewallDeploymentReadyReplicasDesc = prometheus.NewDesc(
"firewall_deployment_ready_replicas",
"provide information on firewall deployment ready replicas",
[]string{"name", "namespace"},
nil,
)
firewallDeploymentTargetReplicasDesc = prometheus.NewDesc(
"firewall_deployment_target_replicas",
"provide information on firewall deployment target replicas",
[]string{"name", "namespace"},
nil,
)
)

type collector struct {
log *slog.Logger
seedClient client.Client
namespace string
}

func mustRegisterCustomMetrics(log *slog.Logger, seedClient client.Client, namespace string) {
c := &collector{
log: log,
seedClient: seedClient,
namespace: namespace,
}

metrics.Registry.MustRegister(c)
}

func (c *collector) Describe(ch chan<- *prometheus.Desc) {
ch <- firewallDeploymentReadyReplicasDesc
ch <- firewallDeploymentTargetReplicasDesc
}

func (c *collector) Collect(ch chan<- prometheus.Metric) {
c.log.Info("collecting custom metrics")

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

deploys := &v2.FirewallDeploymentList{}
err := c.seedClient.List(ctx, deploys, client.InNamespace(c.namespace))
if err != nil {
c.log.Error("unable to list firewall deployments", "error", err)
return
}

for _, deploy := range deploys.Items {
ch <- prometheus.MustNewConstMetric(firewallDeploymentReadyReplicasDesc, prometheus.GaugeValue,
float64(deploy.Status.ReadyReplicas),
deploy.Name,
deploy.Namespace,
)
ch <- prometheus.MustNewConstMetric(firewallDeploymentTargetReplicasDesc, prometheus.GaugeValue,
float64(deploy.Status.TargetReplicas),
deploy.Name,
deploy.Namespace,
)
}
}

0 comments on commit ba443be

Please sign in to comment.