@@ -28,10 +28,7 @@ import (
2828
2929 "github.com/nuclio/errors"
3030 "github.com/nuclio/logger"
31- k8serrors "k8s.io/apimachinery/pkg/api/errors"
32- "k8s.io/apimachinery/pkg/labels"
3331 "k8s.io/apimachinery/pkg/runtime/schema"
34- "k8s.io/metrics/pkg/client/custom_metrics"
3532)
3633
3734type Autoscaler struct {
@@ -41,13 +38,13 @@ type Autoscaler struct {
4138 scaleInterval scalertypes.Duration
4239 inScaleToZeroProcessMap map [string ]bool
4340 groupKind schema.GroupKind
44- customMetricsClientSet custom_metrics. CustomMetricsClient
41+ metricsClient scalertypes. MetricsClient
4542 ticker * time.Ticker
4643}
4744
4845func NewAutoScaler (parentLogger logger.Logger ,
4946 resourceScaler scalertypes.ResourceScaler ,
50- customMetricsClientSet custom_metrics. CustomMetricsClient ,
47+ metricsClient scalertypes. MetricsClient ,
5148 options scalertypes.AutoScalerOptions ) (* Autoscaler , error ) {
5249 childLogger := parentLogger .GetChild ("autoscaler" )
5350 childLogger .InfoWith ("Creating Autoscaler" ,
@@ -59,7 +56,7 @@ func NewAutoScaler(parentLogger logger.Logger,
5956 resourceScaler : resourceScaler ,
6057 scaleInterval : options .ScaleInterval ,
6158 groupKind : options .GroupKind ,
62- customMetricsClientSet : customMetricsClientSet ,
59+ metricsClient : metricsClient ,
6360 inScaleToZeroProcessMap : make (map [string ]bool ),
6461 }, nil
6562}
@@ -99,52 +96,6 @@ func (as *Autoscaler) getMetricNames(resources []scalertypes.Resource) []string
9996 return metricNames
10097}
10198
102- func (as * Autoscaler ) getResourceMetrics (metricNames []string ) (map [string ]map [string ]int , error ) {
103- resourcesMetricsMap := make (map [string ]map [string ]int )
104- resourceLabels := labels .Everything ()
105- metricSelectorLabels := labels .Everything ()
106- metricsClient := as .customMetricsClientSet .NamespacedMetrics (as .namespace )
107-
108- for _ , metricName := range metricNames {
109-
110- // getting the metric values for all object of schema group kind (e.g. deployment)
111- metrics , err := metricsClient .GetForObjects (as .groupKind , resourceLabels , metricName , metricSelectorLabels )
112- if err != nil {
113-
114- // if no data points submitted yet it's ok, continue to the next metric
115- if k8serrors .IsNotFound (err ) {
116- continue
117- }
118- return nil , errors .Wrap (err , "Failed to get custom metrics" )
119- }
120-
121- // fill the resourcesMetricsMap with the metrics data we got
122- for _ , item := range metrics .Items {
123-
124- resourceName := item .DescribedObject .Name
125- value := int (item .Value .MilliValue ())
126-
127- as .logger .DebugWith ("Got metric entry" ,
128- "resourceName" , resourceName ,
129- "metricName" , metricName ,
130- "value" , value )
131-
132- if _ , found := resourcesMetricsMap [resourceName ]; ! found {
133- resourcesMetricsMap [resourceName ] = make (map [string ]int )
134- }
135-
136- // sanity
137- if _ , found := resourcesMetricsMap [resourceName ][metricName ]; found {
138- return nil , errors .New ("Can not have more than one metric value per resource" )
139- }
140-
141- resourcesMetricsMap [resourceName ][metricName ] = value
142- }
143- }
144-
145- return resourcesMetricsMap , nil
146- }
147-
14899func (as * Autoscaler ) checkResourceToScale (resource scalertypes.Resource , resourcesMetricsMap map [string ]map [string ]int ) bool {
149100 if _ , found := resourcesMetricsMap [resource .Name ]; ! found {
150101 as .logger .DebugWith ("Resource does not have metrics data yet, keeping up" , "resourceName" , resource .Name )
@@ -198,7 +149,7 @@ func (as *Autoscaler) checkResourcesToScale() error {
198149 }
199150 metricNames := as .getMetricNames (activeResources )
200151 as .logger .DebugWith ("Got metric names" , "metricNames" , metricNames )
201- resourceMetricsMap , err := as .getResourceMetrics (metricNames )
152+ resourceMetricsMap , err := as .metricsClient . GetResourceMetrics (metricNames )
202153 if err != nil {
203154 return errors .Wrap (err , "Failed to get resources metrics" )
204155 }
0 commit comments