-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
103 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
} | ||
} |