forked from kubernetes-retired/hierarchical-namespaces
-
Notifications
You must be signed in to change notification settings - Fork 4
feat: Expose HRQ limit and usage on metrics endpoint. #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
597341a
feat: Expose HRQ limit and usage on metrics endpoint.
ryotarai 58f71bd
docs: Add metrics documentation for HierarchicalResourceQuotas
ryotarai 512aa2b
Use AsApproximateFloat64 to get the value.
ryotarai a43d539
fix: Remove unnecessary error handling for quantity value in HRQ metr…
ryotarai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,27 @@ | ||
| # Metrics | ||
|
|
||
| The metrics endpoint is exposed on `:8080` by default (customizable with the `-metrics-addr` flag). | ||
|
|
||
| ## Metric List | ||
|
|
||
| ### `hnc_hierarchicalresourcequota` | ||
|
|
||
| This metric exposes resource limits and usage for HierarchicalResourceQuotas. | ||
|
|
||
| #### Labels | ||
|
|
||
| - `hrq`: Name of the HierarchicalResourceQuota | ||
| - `namespace`: Namespace of the HierarchicalResourceQuota | ||
| - `resource`: Resource type (e.g., `cpu`, `memory`, `pods`) | ||
| - `type`: Either `hard` (limit) or `used` (current usage) | ||
|
|
||
| #### Example | ||
|
|
||
| ``` | ||
| # HELP hnc_hierarchicalresourcequota HRQ hard/used like kube_resourcequota | ||
| # TYPE hnc_hierarchicalresourcequota gauge | ||
| hnc_hierarchicalresourcequota{hrq="team-quota",namespace="team-a",resource="cpu",type="hard"} 100 | ||
| hnc_hierarchicalresourcequota{hrq="team-quota",namespace="team-a",resource="cpu",type="used"} 45 | ||
| hnc_hierarchicalresourcequota{hrq="team-quota",namespace="team-a",resource="memory",type="hard"} 536870912 | ||
| hnc_hierarchicalresourcequota{hrq="team-quota",namespace="team-a",resource="memory",type="used"} 268435456 | ||
| ``` |
This file contains hidden or 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,81 @@ | ||
| package hrq | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| corev1 "k8s.io/api/core/v1" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/metrics" | ||
|
|
||
| api "sigs.k8s.io/hierarchical-namespaces/api/v1alpha2" | ||
| ) | ||
|
|
||
| func RegisterMetrics(mgr ctrl.Manager) error { | ||
| if err := metrics.Registry.Register(&hrqCollector{ | ||
| client: mgr.GetClient(), | ||
| logger: mgr.GetLogger().WithValues("collector", "hrqCollector"), | ||
| timeout: time.Second * 10, | ||
| }); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| type hrqCollector struct { | ||
| timeout time.Duration | ||
| client client.Client | ||
| logger logr.Logger | ||
| } | ||
|
|
||
| func (c *hrqCollector) desc() *prometheus.Desc { | ||
| return prometheus.NewDesc( | ||
| "hnc_hierarchicalresourcequota", | ||
| "HRQ hard/used like kube_resourcequota", | ||
| []string{"namespace", "hrq", "resource", "type"}, | ||
| nil, | ||
| ) | ||
| } | ||
|
|
||
| func (c *hrqCollector) Describe(ch chan<- *prometheus.Desc) { | ||
| ch <- c.desc() | ||
| } | ||
|
|
||
| func (c *hrqCollector) Collect(ch chan<- prometheus.Metric) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), c.timeout) | ||
| defer cancel() | ||
|
|
||
| var hrqs api.HierarchicalResourceQuotaList | ||
| if err := c.client.List(ctx, &hrqs); err != nil { | ||
| c.logger.Error(err, "Failed to list HRQs during metrics collection") | ||
| return | ||
| } | ||
|
|
||
| for _, hrq := range hrqs.Items { | ||
| for typeLabel, resList := range map[string]corev1.ResourceList{ | ||
| "hard": hrq.Status.Hard, | ||
| "used": hrq.Status.Used, | ||
| } { | ||
| for res, qty := range resList { | ||
| v, ok := qty.AsInt64() | ||
| if !ok { | ||
| c.logger.Info("Failed to get the quantity value", "namespace", hrq.Namespace, "hrq", hrq.Name, "resource", res) | ||
| continue | ||
| } | ||
|
|
||
| ch <- prometheus.MustNewConstMetric( | ||
| c.desc(), | ||
| prometheus.GaugeValue, | ||
| float64(v), | ||
| hrq.Namespace, | ||
| hrq.Name, | ||
| string(res), | ||
| typeLabel, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.