Skip to content

Commit

Permalink
added metrics for subnamespaces
Browse files Browse the repository at this point in the history
With this PR, there are now metrics showing for each subnamespace
the quantities of the resources it has.

Signed-off-by: mzeevi <meytar80@gmail.com>
  • Loading branch information
mzeevi committed Apr 11, 2024
1 parent 7ec75bd commit 2b8ea37
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 4 deletions.
5 changes: 5 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"flag"
"os"

"github.com/dana-team/hns/internal/metrics"

userv1 "github.com/openshift/api/user/v1"

danav1 "github.com/dana-team/hns/api/v1"
Expand Down Expand Up @@ -108,6 +110,9 @@ func main() {
}
// +kubebuilder:scaffold:builder

// Register the HNS specific metrics
metrics.InitializeHNSMetrics()

setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
Expand Down
69 changes: 69 additions & 0 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package metrics

import (
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)

// InitializeHNSMetrics registers the relevant metrics.
func InitializeHNSMetrics() {
metrics.Registry.MustRegister(
snsAllocatedResources,
snsFreeResources,
snsTotalResources,
)
}

var (
snsAllocatedResources = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "sns_allocated_resources",
Help: "Indication of the quantity of an allocated subnamespace resource",
}, []string{"name", "namespace", "resource"},
)
)

var (
snsFreeResources = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "sns_free_resources",
Help: "Indication of the quantity of a free subnamespace resource",
}, []string{"name", "namespace", "resource"},
)
)

var (
snsTotalResources = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "sns_total_resources",
Help: "Indication of the total quantity of a subnamespace resource",
}, []string{"name", "namespace", "resource"},
)
)

// ObserveSNSAllocatedResource sets the allocated metric as per the quantity.
func ObserveSNSAllocatedResource(name, namespace, resource string, quantity float64) {
snsAllocatedResources.With(prometheus.Labels{
"name": name,
"namespace": namespace,
"resource": resource,
}).Set(quantity)
}

// ObserveSNSFreeResource sets the allocatable metric as per the quantity.
func ObserveSNSFreeResource(name, namespace, resource string, quantity float64) {
snsFreeResources.With(prometheus.Labels{
"name": name,
"namespace": namespace,
"resource": resource,
}).Set(quantity)
}

// ObserveSNSTotalResource sets the total metric as per the quantity.
func ObserveSNSTotalResource(name, namespace, resource string, quantity float64) {
snsTotalResources.With(prometheus.Labels{
"name": name,
"namespace": namespace,
"resource": resource,
}).Set(quantity)
}
26 changes: 22 additions & 4 deletions internal/subnamespace/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package subnamespace
import (
"fmt"

"github.com/dana-team/hns/internal/metrics"

danav1 "github.com/dana-team/hns/api/v1"
"github.com/dana-team/hns/internal/namespace/nsutils"
"github.com/dana-team/hns/internal/namespacedb"
Expand Down Expand Up @@ -138,6 +140,9 @@ func (r *SubnamespaceReconciler) sync(snsParentNS, snsObject *objectcontext.Obje
}
logger.Info("successfully set status for subnamespace", "subnamespace", snsName)

updateSNSMetrics(snsName, snsParentName, free, resourceAllocatedToChildren, snsObject.Object.(*danav1.Subnamespace).Spec.ResourceQuotaSpec.Hard)
logger.Info("successfully set metrics for subnamespace", "subnamespace", snsName)

// trigger reconciliation for parent subnamespace so that it can be aware of
// potential changes in one of its children
r.enqueueSNSEvent(parentSNS.Name(), parentSNS.Object.GetNamespace())
Expand Down Expand Up @@ -389,10 +394,10 @@ func (r *SubnamespaceReconciler) enqueueChildrenSNSToRPConversionEvents(snsObjec

// IsUpdateNeeded gets a subnamespace object, a []danav1.Namespaces and two resource lists and returns whether
// the subnamespace object status has to be updated.
func IsUpdateNeeded(subspace client.Object, childrenRequests []danav1.Namespaces, allocated, free corev1.ResourceList) bool {
if !NamespacesEqual(subspace.(*danav1.Subnamespace).Status.Namespaces, childrenRequests) ||
!quota.ResourceListEqual(subspace.(*danav1.Subnamespace).Status.Total.Allocated, allocated) ||
!quota.ResourceListEqual(subspace.(*danav1.Subnamespace).Status.Total.Free, free) {
func IsUpdateNeeded(sns client.Object, childrenRequests []danav1.Namespaces, allocated, free corev1.ResourceList) bool {
if !NamespacesEqual(sns.(*danav1.Subnamespace).Status.Namespaces, childrenRequests) ||
!quota.ResourceListEqual(sns.(*danav1.Subnamespace).Status.Total.Allocated, allocated) ||
!quota.ResourceListEqual(sns.(*danav1.Subnamespace).Status.Total.Free, free) {
return true
}
return false
Expand All @@ -410,3 +415,16 @@ func NamespacesEqual(nsA, nsB []danav1.Namespaces) bool {
}
return true
}

// updateSNSMetrics updates the metrics for the subnamespace.
func updateSNSMetrics(snsName, snsNS string, allocated, free, total corev1.ResourceList) {
for resourceName := range total {
allocatedResource := allocated[resourceName]
freeResource := free[resourceName]
totalResource := total[resourceName]

metrics.ObserveSNSAllocatedResource(snsName, snsNS, resourceName.String(), allocatedResource.AsApproximateFloat64())
metrics.ObserveSNSFreeResource(snsName, snsNS, resourceName.String(), freeResource.AsApproximateFloat64())
metrics.ObserveSNSTotalResource(snsName, snsNS, resourceName.String(), totalResource.AsApproximateFloat64())
}
}

0 comments on commit 2b8ea37

Please sign in to comment.