-
Notifications
You must be signed in to change notification settings - Fork 14
[Autoscaler] Refactor metrics client #83
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
TomerShor
merged 8 commits into
v3io:development
from
weilerN:NUC-709-refactor_metrics_client_in_autoscaler
Jan 8, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
39177e8
Add Metrics client interface
weilerN 1a81301
Refactor custom metrics client
weilerN c8e45f0
Refactor metrics client param in Autoscaler struct
weilerN 150c33b
Use factory to create MetricsClient instance
weilerN 2db6008
Update metrics client options
weilerN 44b998c
Renamings
weilerN ce5c46b
CR1 - renamings and docstring
weilerN 22b5f89
CR2 - init custom metrics client in the proper location
weilerN 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
Some comments aren't visible on the classic Files Changed page.
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
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
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,44 @@ | ||
| /* | ||
| Copyright 2026 Iguazio Systems Ltd. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License") with | ||
| an addition restriction as set forth herein. You may not use this | ||
| file except in compliance with the License. You may obtain a copy of | ||
| the License at http://www.apache.org/licenses/LICENSE-2.0. | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
| implied. See the License for the specific language governing | ||
| permissions and limitations under the License. | ||
|
|
||
| In addition, you may not use the software for any purposes that are | ||
| illegal under applicable law, and the grant of the foregoing license | ||
| under the Apache 2.0 license is conditioned upon your compliance with | ||
| such restriction. | ||
| */ | ||
|
|
||
| package metricsclient | ||
|
|
||
| import ( | ||
| "github.com/v3io/scaler/pkg/scalertypes" | ||
|
|
||
| "github.com/nuclio/errors" | ||
| "github.com/nuclio/logger" | ||
| "k8s.io/client-go/rest" | ||
| ) | ||
|
|
||
| func NewMetricsClient(logger logger.Logger, | ||
| restConfig *rest.Config, | ||
| autoScalerConf scalertypes.AutoScalerOptions) (scalertypes.MetricsClient, error) { | ||
| switch autoScalerConf.MetricsClientOptions.MetricsClientKind { | ||
| case scalertypes.KindK8sMetricsClient: | ||
| return NewCustomMetricsClient( | ||
| logger, | ||
| restConfig, | ||
| autoScalerConf.Namespace, | ||
| autoScalerConf.GroupKind) | ||
| default: | ||
| return nil, errors.Errorf("unsupported metrics client kind: %s", autoScalerConf.MetricsClientOptions.MetricsClientKind) | ||
| } | ||
| } |
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,104 @@ | ||
| /* | ||
| Copyright 2026 Iguazio Systems Ltd. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License") with | ||
| an addition restriction as set forth herein. You may not use this | ||
| file except in compliance with the License. You may obtain a copy of | ||
| the License at http://www.apache.org/licenses/LICENSE-2.0. | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
| implied. See the License for the specific language governing | ||
| permissions and limitations under the License. | ||
|
|
||
| In addition, you may not use the software for any purposes that are | ||
| illegal under applicable law, and the grant of the foregoing license | ||
| under the Apache 2.0 license is conditioned upon your compliance with | ||
| such restriction. | ||
| */ | ||
|
|
||
| package metricsclient | ||
|
|
||
| import ( | ||
| "github.com/nuclio/errors" | ||
| "github.com/nuclio/logger" | ||
| k8serrors "k8s.io/apimachinery/pkg/api/errors" | ||
| "k8s.io/apimachinery/pkg/labels" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/client-go/discovery" | ||
| "k8s.io/client-go/discovery/cached/memory" | ||
| "k8s.io/client-go/rest" | ||
| "k8s.io/client-go/restmapper" | ||
| k8scustommetrics "k8s.io/metrics/pkg/client/custom_metrics" | ||
| ) | ||
|
|
||
| type K8sCustomMetricsClient struct { | ||
| k8scustommetrics.CustomMetricsClient | ||
| namespace string | ||
| groupKind schema.GroupKind | ||
| logger logger.Logger | ||
| } | ||
|
|
||
| func NewCustomMetricsClient( | ||
| parentLogger logger.Logger, | ||
| restConfig *rest.Config, | ||
| namespace string, | ||
| groupKind schema.GroupKind) (*K8sCustomMetricsClient, error) { | ||
| discoveryClient, err := discovery.NewDiscoveryClientForConfig(restConfig) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "Failed to create k8s metrics client") | ||
| } | ||
| availableAPIsGetter := k8scustommetrics.NewAvailableAPIsGetter(discoveryClient) | ||
| restMapper := restmapper.NewDeferredDiscoveryRESTMapper(memory.NewMemCacheClient(discoveryClient)) | ||
| customMetricsClient := k8scustommetrics.NewForConfig(restConfig, restMapper, availableAPIsGetter) | ||
| return &K8sCustomMetricsClient{ | ||
| logger: parentLogger.GetChild("custom-metrics"), | ||
| CustomMetricsClient: customMetricsClient, | ||
| namespace: namespace, | ||
| groupKind: groupKind, | ||
| }, nil | ||
| } | ||
|
|
||
| func (cmw *K8sCustomMetricsClient) GetResourceMetrics(metricNames []string) (map[string]map[string]int, error) { | ||
| resourcesMetricsMap := make(map[string]map[string]int) | ||
| resourceLabels := labels.Everything() | ||
| metricSelectorLabels := labels.Everything() | ||
| metricsClient := cmw.NamespacedMetrics(cmw.namespace) | ||
|
|
||
| for _, metricName := range metricNames { | ||
| // getting the metric values for all object of schema group kind (e.g. deployment) | ||
| metrics, err := metricsClient.GetForObjects(cmw.groupKind, resourceLabels, metricName, metricSelectorLabels) | ||
| if err != nil { | ||
| // No data points have been submitted yet; this is expectedβproceed to the next metric. | ||
| if k8serrors.IsNotFound(err) { | ||
| continue | ||
| } | ||
| return nil, errors.Wrap(err, "Failed to get custom metrics") | ||
| } | ||
|
|
||
| // fill the resourcesMetricsMap with the metrics data we got | ||
| for _, item := range metrics.Items { | ||
| resourceName := item.DescribedObject.Name | ||
| value := int(item.Value.MilliValue()) | ||
|
|
||
| cmw.logger.DebugWith("Got metric entry", | ||
| "resourceName", resourceName, | ||
| "metricName", metricName, | ||
| "value", value) | ||
|
|
||
| if _, found := resourcesMetricsMap[resourceName]; !found { | ||
| resourcesMetricsMap[resourceName] = make(map[string]int) | ||
| } | ||
|
|
||
| // sanity | ||
| if _, found := resourcesMetricsMap[resourceName][metricName]; found { | ||
| return nil, errors.New("Can not have more than one metric value per resource") | ||
| } | ||
|
|
||
| resourcesMetricsMap[resourceName][metricName] = value | ||
| } | ||
| } | ||
|
|
||
| return resourcesMetricsMap, nil | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure I like the name, how about just
custom? Because this are not metrics, the struct is for a client (a custom one), so either custom.go or customclient.goThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it can also just be
k8s.go, and the client nameK8sCustomMetricsClientThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed that the naming here can be improved. fixing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed here - CR1 - renamings and docstring