Skip to content

Commit

Permalink
feat(cluster) Changes after CodeReview (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
gciezkowski-acc committed Aug 26, 2024
1 parent 59e53b4 commit 7267082
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pkg/controllers/cluster/direct_access_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (r *DirectAccessReconciler) Reconcile(ctx context.Context, req ctrl.Request
return ctrl.Result{}, err
}

UpdateMetrics(cluster)
updateMetrics(cluster)
return ctrl.Result{RequeueAfter: defaultRequeueInterval}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/cluster/headscale_access_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (r *HeadscaleAccessReconciler) Reconcile(ctx context.Context, req ctrl.Requ
return ctrl.Result{}, err
}

UpdateMetrics(cluster)
updateMetrics(cluster)
return ctrl.Result{RequeueAfter: defaultRequeueInterval}, nil
}

Expand Down
12 changes: 5 additions & 7 deletions pkg/controllers/cluster/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

var (
kubernetesVersionsCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
kubernetesVersionsGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "greenhouse_cluster_k8s_versions_total",
},
[]string{"cluster", "namespace", "version"})
Expand All @@ -23,19 +23,17 @@ var (
)

func init() {
metrics.Registry.MustRegister(kubernetesVersionsCounter)
metrics.Registry.MustRegister(kubernetesVersionsGauge)
metrics.Registry.MustRegister(secondsToTokenExpiryGauge)
}

//+kubebuilder:rbac:groups=greenhouse.sap,resources=clusters,verbs=get;list

func UpdateMetrics(cluster *greenhousev1alpha1.Cluster) {
func updateMetrics(cluster *greenhousev1alpha1.Cluster) {
kubernetesVersionLabels := prometheus.Labels{
"cluster": cluster.Name,
"namespace": cluster.Namespace,
"version": cluster.Status.KubernetesVersion,
}
kubernetesVersionsCounter.With(kubernetesVersionLabels).Inc()
kubernetesVersionsGauge.With(kubernetesVersionLabels).Set(float64(1))

secondsToExpiry := cluster.Status.BearerTokenExpirationTimestamp.Unix() - time.Now().Unix()
secondsToExpiryLabels := prometheus.Labels{
Expand Down
8 changes: 3 additions & 5 deletions pkg/controllers/cluster/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

var _ = Describe("Metrics controller", Ordered, func() {
var (
//remoteClient client.Client
remoteEnvTest *envtest.Environment
)

Expand All @@ -40,10 +39,9 @@ var _ = Describe("Metrics controller", Ordered, func() {
},
}

counterBefore := prometheusTest.CollectAndCount(kubernetesVersionsCounter)
UpdateMetrics(cluster)
counterAfter := prometheusTest.CollectAndCount(kubernetesVersionsCounter)
Expect(counterAfter).To(BeEquivalentTo(counterBefore + 1))
updateMetrics(cluster)
counterAfter := prometheusTest.ToFloat64(kubernetesVersionsGauge.WithLabelValues(cluster.Name, cluster.Namespace, cluster.Status.KubernetesVersion))
Expect(counterAfter).To(BeEquivalentTo(1))
tokenExpiry := prometheusTest.ToFloat64(secondsToTokenExpiryGauge.WithLabelValues(cluster.Name, cluster.Namespace))
Expect(tokenExpiry).To(BeNumerically(">=", 595))
Expect(tokenExpiry).To(BeNumerically("<=", 600))
Expand Down

0 comments on commit 7267082

Please sign in to comment.