[Autoscaler] Refactor metrics client#83
Conversation
779eb29 to
521e536
Compare
d1ed7fe to
44b998c
Compare
rokatyy
left a comment
There was a problem hiding this comment.
Very well! Just a couple of suggestions and questions
There was a problem hiding this comment.
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.go
There was a problem hiding this comment.
it can also just be k8s.go, and the client name K8sCustomMetricsClient
There was a problem hiding this comment.
Agreed that the naming here can be improved. fixing
There was a problem hiding this comment.
Fixed here - CR1 - renamings and docstring
| type CustomMetricsClient struct { | ||
| k8scustommetrics.CustomMetricsClient | ||
| namespace string | ||
| groupKind schema.GroupKind | ||
| logger logger.Logger | ||
| } |
There was a problem hiding this comment.
I assume some of those values are expected to be common across all the interface implementations? Do we want to have an abstract class or it would be redundant?
There was a problem hiding this comment.
@rokatyy Great question!
At the moment, the only shared values across implementations are the namespace and the logger, so I prefer to keep the current structure as is.
That said, if we start seeing duplicated state or logic (during the Prometheus client implementation), I’ll definitely consider introducing a common abstract struct to avoid repetition.
| autoScalerConf.Namespace, | ||
| autoScalerConf.GroupKind), nil | ||
| default: | ||
| return nil, fmt.Errorf("unsupported metrics client kind: %s", autoScalerConf.MetricsClientOptions.Kind) |
There was a problem hiding this comment.
Fixed here - CR1 - renamings and docstring
pkg/scalertypes/types.go
Outdated
| "k8s.io/client-go/kubernetes" | ||
| ) | ||
|
|
||
| type Kind string |
There was a problem hiding this comment.
Kind is too generic, let's change to MetricsClientKind or something more informative
There was a problem hiding this comment.
Fixed here - CR1 - renamings and docstring
cmd/autoscaler/app/autoscaler.go
Outdated
| Group: metricsResourceGroup, | ||
| }, | ||
| MetricsClientOptions: scalertypes.MetricsClientOptions{ | ||
| Kind: scalertypes.KindCustomMetrics, |
There was a problem hiding this comment.
we not getting this as an input instead of hardcoding it to custom metrics?
There was a problem hiding this comment.
Discussed F2F, comments will be added to improve readiness
There was a problem hiding this comment.
Fixed here - CR1 - renamings and docstring
There was a problem hiding this comment.
it can also just be k8s.go, and the client name K8sCustomMetricsClient
| namespace string, | ||
| groupKind schema.GroupKind) *CustomMetricsClient { | ||
| return &CustomMetricsClient{ | ||
| logger: logger, |
There was a problem hiding this comment.
| logger: logger, | |
| logger: parentLogger.GetChild("custom-metrics"), |
There was a problem hiding this comment.
Fixed here - CR1 - renamings and docstring
| case scalertypes.KindK8sMetricsClient: | ||
| 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 NewCustomMetricsClient( | ||
| logger, | ||
| customMetricsClient, | ||
| autoScalerConf.Namespace, | ||
| autoScalerConf.GroupKind), nil |
There was a problem hiding this comment.
No, Move all of this into NewCustomMetricsClient.
The factory needs to be simple - call the constructor, return the instance.
The constructor tells what it needs the factory to pass as arguments, and it defines any intermediate types etc.
There was a problem hiding this comment.
📝 Description
This change refactors autoscaler metrics handling by introducing a dedicated
MetricsClientabstraction and factory. The autoscaler no longer depends directly on Kubernetes custom metrics APIs and instead consumes metrics via a pluggable client interface.🛠️ Changes Made
MetricsClientinterface inscalertypesto abstract resource metrics retrieval.CustomMetricsClientunderpkg/autoscaler/metricsclient, encapsulating Kubernetes custom metrics logic.pkg/autoscaler/metricsclientto construct the appropriate client based on configuration.MetricsClientinterface instead of directly using Kubernetes custom metrics APIs.✅ Checklist
🧪 Testing
nuclio-scalerand tested STZ🔗 References
🚨 Breaking Changes?
🔍️ Additional Notes