-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(metrics): provide metrics for tenant quotas
Signed-off-by: Lukas Boettcher <[email protected]>
- Loading branch information
1 parent
0571e41
commit 7f4d55f
Showing
4 changed files
with
115 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2020-2023 Project Capsule Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package tenant | ||
|
||
import ( | ||
"context" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
|
||
capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2" | ||
"github.com/projectcapsule/capsule/pkg/metrics" | ||
) | ||
|
||
func (r *Manager) handleMetricsCleanup(ctx context.Context, tenant *capsulev1beta2.Tenant) (err error) { | ||
metricsFinalizer := "capsule.clastix.io/metrics-cleanup" | ||
|
||
// examine DeletionTimestamp to determine if object is under deletion | ||
if tenant.ObjectMeta.DeletionTimestamp.IsZero() { //nolint:nestif | ||
// The object is not being deleted, so if it does not have our finalizer, | ||
// then lets add the finalizer and update the object. This is equivalent | ||
// to registering our finalizer. | ||
if !controllerutil.ContainsFinalizer(tenant, metricsFinalizer) { | ||
controllerutil.AddFinalizer(tenant, metricsFinalizer) | ||
|
||
if err := r.Update(ctx, tenant); err != nil { | ||
return err | ||
} | ||
} | ||
} else { | ||
// The object is being deleted | ||
if controllerutil.ContainsFinalizer(tenant, metricsFinalizer) { | ||
// our finalizer is present, so handle the removal of the metrics | ||
r.Log.Info("Removing ResourceQuota metrics because Tenant " + tenant.Name + " is being deleted") | ||
|
||
// remove all metrics of the deleted tenant | ||
metrics.TenantResourceUsage.DeletePartialMatch(map[string]string{"tenant": tenant.Name}) | ||
metrics.TenantResourceLimit.DeletePartialMatch(map[string]string{"tenant": tenant.Name}) | ||
|
||
// remove our finalizer from the list and update it. | ||
controllerutil.RemoveFinalizer(tenant, metricsFinalizer) | ||
|
||
if err := r.Update(ctx, tenant); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
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
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,30 @@ | ||
// Copyright 2020-2023 Project Capsule Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package metrics | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"sigs.k8s.io/controller-runtime/pkg/metrics" | ||
) | ||
|
||
var ( | ||
metricsPrefix = "capsule_" | ||
|
||
TenantResourceUsage = prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: metricsPrefix + "tenant_resource_usage", | ||
Help: "Current resource usage for a given resource in a tenant", | ||
}, []string{"tenant", "resource", "resourcequotaindex"}) | ||
|
||
TenantResourceLimit = prometheus.NewGaugeVec(prometheus.GaugeOpts{ | ||
Name: metricsPrefix + "tenant_resource_limit", | ||
Help: "Current resource limit for a given resource in a tenant", | ||
}, []string{"tenant", "resource", "resourcequotaindex"}) | ||
) | ||
|
||
func init() { | ||
metrics.Registry.MustRegister( | ||
TenantResourceUsage, | ||
TenantResourceLimit, | ||
) | ||
} |