From 5a543781d779e143a9fa1dd7baca52bbf4e16536 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Larsen Date: Tue, 21 May 2024 14:00:31 +0200 Subject: [PATCH] Introduce context for collector interface Signed-off-by: Mikkel Oscar Lyderik Larsen --- pkg/collector/aws_collector.go | 10 +++--- pkg/collector/collector.go | 21 ++++++------ pkg/collector/collector_test.go | 7 ++-- pkg/collector/external_rps_collector.go | 8 +++-- pkg/collector/external_rps_collector_test.go | 11 ++++--- pkg/collector/fake_collector_test.go | 4 ++- pkg/collector/http_collector.go | 5 +-- pkg/collector/http_collector_test.go | 5 +-- pkg/collector/influxdb_collector.go | 14 ++++---- pkg/collector/influxdb_collector_test.go | 7 ++-- pkg/collector/nakadi_collector.go | 12 +++---- pkg/collector/pod_collector.go | 20 +++++------ pkg/collector/pod_collector_test.go | 20 +++++------ pkg/collector/prometheus_collector.go | 8 ++--- pkg/collector/prometheus_collector_test.go | 3 +- pkg/collector/scaling_schedule_collector.go | 9 ++--- .../scaling_schedule_collector_test.go | 33 ++++++++++--------- pkg/collector/skipper_collector.go | 18 +++++----- pkg/collector/skipper_collector_test.go | 8 ++--- pkg/collector/zmon_collector.go | 5 +-- pkg/collector/zmon_collector_test.go | 7 ++-- pkg/provider/hpa.go | 4 +-- pkg/provider/hpa_test.go | 4 +-- 23 files changed, 130 insertions(+), 113 deletions(-) diff --git a/pkg/collector/aws_collector.go b/pkg/collector/aws_collector.go index 5837ece..251a3cc 100644 --- a/pkg/collector/aws_collector.go +++ b/pkg/collector/aws_collector.go @@ -32,8 +32,8 @@ func NewAWSCollectorPlugin(configs map[string]aws.Config) *AWSCollectorPlugin { } // NewCollector initializes a new skipper collector from the specified HPA. -func (c *AWSCollectorPlugin) NewCollector(hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { - return NewAWSSQSCollector(c.configs, hpa, config, interval) +func (c *AWSCollectorPlugin) NewCollector(ctx context.Context, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { + return NewAWSSQSCollector(ctx, c.configs, hpa, config, interval) } type sqsiface interface { @@ -50,7 +50,7 @@ type AWSSQSCollector struct { metricType autoscalingv2.MetricSourceType } -func NewAWSSQSCollector(configs map[string]aws.Config, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (*AWSSQSCollector, error) { +func NewAWSSQSCollector(ctx context.Context, configs map[string]aws.Config, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (*AWSSQSCollector, error) { if config.Metric.Selector == nil { return nil, fmt.Errorf("selector for queue is not specified") } @@ -90,13 +90,13 @@ func NewAWSSQSCollector(configs map[string]aws.Config, hpa *autoscalingv2.Horizo }, nil } -func (c *AWSSQSCollector) GetMetrics() ([]CollectedMetric, error) { +func (c *AWSSQSCollector) GetMetrics(ctx context.Context) ([]CollectedMetric, error) { params := &sqs.GetQueueAttributesInput{ QueueUrl: aws.String(c.queueURL), AttributeNames: []types.QueueAttributeName{types.QueueAttributeNameApproximateNumberOfMessages}, } - resp, err := c.sqs.GetQueueAttributes(context.TODO(), params) + resp, err := c.sqs.GetQueueAttributes(ctx, params) if err != nil { return nil, err } diff --git a/pkg/collector/collector.go b/pkg/collector/collector.go index 2e09ecd..4a68bf0 100644 --- a/pkg/collector/collector.go +++ b/pkg/collector/collector.go @@ -1,6 +1,7 @@ package collector import ( + "context" "fmt" "time" @@ -50,7 +51,7 @@ func NewCollectorFactory() *CollectorFactory { } type CollectorPlugin interface { - NewCollector(hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) + NewCollector(ctx context.Context, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) } type PluginNotFoundError struct { @@ -120,38 +121,38 @@ func (c *CollectorFactory) RegisterExternalCollector(metrics []string, plugin Co } } -func (c *CollectorFactory) NewCollector(hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { +func (c *CollectorFactory) NewCollector(ctx context.Context, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { switch config.Type { case autoscalingv2.PodsMetricSourceType: // first try to find a plugin by format if plugin, ok := c.podsPlugins.Named[config.CollectorType]; ok { - return plugin.NewCollector(hpa, config, interval) + return plugin.NewCollector(ctx, hpa, config, interval) } // else try to use the default plugin if set if c.podsPlugins.Any != nil { - return c.podsPlugins.Any.NewCollector(hpa, config, interval) + return c.podsPlugins.Any.NewCollector(ctx, hpa, config, interval) } case autoscalingv2.ObjectMetricSourceType: // first try to find a plugin by kind if kinds, ok := c.objectPlugins.Named[config.ObjectReference.Kind]; ok { if plugin, ok := kinds.Named[config.CollectorType]; ok { - return plugin.NewCollector(hpa, config, interval) + return plugin.NewCollector(ctx, hpa, config, interval) } if kinds.Any != nil { - return kinds.Any.NewCollector(hpa, config, interval) + return kinds.Any.NewCollector(ctx, hpa, config, interval) } break } // else try to find a default plugin for this kind if plugin, ok := c.objectPlugins.Any.Named[config.CollectorType]; ok { - return plugin.NewCollector(hpa, config, interval) + return plugin.NewCollector(ctx, hpa, config, interval) } if c.objectPlugins.Any.Any != nil { - return c.objectPlugins.Any.Any.NewCollector(hpa, config, interval) + return c.objectPlugins.Any.Any.NewCollector(ctx, hpa, config, interval) } case autoscalingv2.ExternalMetricSourceType: // First type to get metric type from the `type` label, @@ -169,7 +170,7 @@ func (c *CollectorFactory) NewCollector(hpa *autoscalingv2.HorizontalPodAutoscal } if plugin, ok := c.externalPlugins[pluginKey]; ok { - return plugin.NewCollector(hpa, config, interval) + return plugin.NewCollector(ctx, hpa, config, interval) } } @@ -189,7 +190,7 @@ type CollectedMetric struct { } type Collector interface { - GetMetrics() ([]CollectedMetric, error) + GetMetrics(ctx context.Context) ([]CollectedMetric, error) Interval() time.Duration } diff --git a/pkg/collector/collector_test.go b/pkg/collector/collector_test.go index 5cbdf62..f2b14ca 100644 --- a/pkg/collector/collector_test.go +++ b/pkg/collector/collector_test.go @@ -1,6 +1,7 @@ package collector import ( + "context" "testing" "time" @@ -13,7 +14,7 @@ type mockCollectorPlugin struct { Name string } -func (c *mockCollectorPlugin) NewCollector(hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { +func (c *mockCollectorPlugin) NewCollector(_ context.Context, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { return &mockCollector{Name: c.Name}, nil } @@ -21,7 +22,7 @@ type mockCollector struct { Name string } -func (c *mockCollector) GetMetrics() ([]CollectedMetric, error) { +func (c *mockCollector) GetMetrics(_ context.Context) ([]CollectedMetric, error) { return nil, nil } @@ -114,7 +115,7 @@ func TestNewCollector(t *testing.T) { require.NoError(t, err) require.Len(t, configs, 1) - collector, err := collectorFactory.NewCollector(tc.hpa, configs[0], 0) + collector, err := collectorFactory.NewCollector(context.Background(), tc.hpa, configs[0], 0) if tc.expectedCollector == "" { require.Error(t, err) } else { diff --git a/pkg/collector/external_rps_collector.go b/pkg/collector/external_rps_collector.go index 0eb02cd..b510f01 100644 --- a/pkg/collector/external_rps_collector.go +++ b/pkg/collector/external_rps_collector.go @@ -1,6 +1,7 @@ package collector import ( + "context" "fmt" "regexp" "strconv" @@ -48,6 +49,7 @@ func NewExternalRPSCollectorPlugin( // NewCollector initializes a new skipper collector from the specified HPA. func (p *ExternalRPSCollectorPlugin) NewCollector( + ctx context.Context, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration, @@ -95,7 +97,7 @@ func (p *ExternalRPSCollectorPlugin) NewCollector( ), } - c, err := p.promPlugin.NewCollector(hpa, &confCopy, interval) + c, err := p.promPlugin.NewCollector(ctx, hpa, &confCopy, interval) if err != nil { return nil, err } @@ -107,8 +109,8 @@ func (p *ExternalRPSCollectorPlugin) NewCollector( } // GetMetrics gets hostname metrics from Prometheus -func (c *ExternalRPSCollector) GetMetrics() ([]CollectedMetric, error) { - v, err := c.promCollector.GetMetrics() +func (c *ExternalRPSCollector) GetMetrics(ctx context.Context) ([]CollectedMetric, error) { + v, err := c.promCollector.GetMetrics(ctx) if err != nil { return nil, err } diff --git a/pkg/collector/external_rps_collector_test.go b/pkg/collector/external_rps_collector_test.go index afaf354..c2f7450 100644 --- a/pkg/collector/external_rps_collector_test.go +++ b/pkg/collector/external_rps_collector_test.go @@ -1,6 +1,7 @@ package collector import ( + "context" "fmt" "regexp" "testing" @@ -100,6 +101,7 @@ func TestExternalRPSPluginNewCollector(tt *testing.T) { } { tt.Run(testcase.msg, func(t *testing.T) { c, err := plugin.NewCollector( + context.Background(), &autoscalingv2.HorizontalPodAutoscaler{}, testcase.config, interval, @@ -156,7 +158,7 @@ func TestExternalRPSCollectorGetMetrics(tt *testing.T) { tt.Run(testcase.msg, func(t *testing.T) { fake := makeCollectorWithStub(testcase.stub) c := &ExternalRPSCollector{promCollector: fake} - m, err := c.GetMetrics() + m, err := c.GetMetrics(context.Background()) if testcase.shouldWork { require.Nil(t, err) @@ -182,6 +184,7 @@ func TestExternalRPSCollectorInterval(t *testing.T) { pattern: pattern, } c, err := plugin.NewCollector( + context.Background(), &autoscalingv2.HorizontalPodAutoscaler{}, &MetricConfig{Config: map[string]string{"hostnames": "foo.bar.baz"}}, interval, @@ -227,7 +230,7 @@ func TestExternalRPSCollectorAndCollectorFabricInteraction(t *testing.T) { require.NoError(t, err) require.Len(t, conf, 1) - c, err := factory.NewCollector(hpa, conf[0], 0) + c, err := factory.NewCollector(context.Background(), hpa, conf[0], 0) require.NoError(t, err) _, ok := c.(*ExternalRPSCollector) @@ -288,9 +291,9 @@ func TestExternalRPSPrometheusCollectorInteraction(t *testing.T) { require.Len(t, conf, 2) collectors := make(map[string]Collector) - collectors["hostname"], err = factory.NewCollector(hpa, conf[0], 0) + collectors["hostname"], err = factory.NewCollector(context.Background(), hpa, conf[0], 0) require.NoError(t, err) - collectors["prom"], err = factory.NewCollector(hpa, conf[1], 0) + collectors["prom"], err = factory.NewCollector(context.Background(), hpa, conf[1], 0) require.NoError(t, err) prom, ok := collectors["prom"].(*PrometheusCollector) diff --git a/pkg/collector/fake_collector_test.go b/pkg/collector/fake_collector_test.go index 7b22719..54ad5cf 100644 --- a/pkg/collector/fake_collector_test.go +++ b/pkg/collector/fake_collector_test.go @@ -3,6 +3,7 @@ package collector import ( "time" + "golang.org/x/net/context" autoscalingv2 "k8s.io/api/autoscaling/v2" "k8s.io/apimachinery/pkg/api/resource" "k8s.io/metrics/pkg/apis/custom_metrics" @@ -19,7 +20,7 @@ type FakeCollector struct { stub func() ([]CollectedMetric, error) } -func (c *FakeCollector) GetMetrics() ([]CollectedMetric, error) { +func (c *FakeCollector) GetMetrics(_ context.Context) ([]CollectedMetric, error) { if c.stub != nil { v, err := c.stub() return v, err @@ -33,6 +34,7 @@ func (FakeCollector) Interval() time.Duration { } func (p *FakeCollectorPlugin) NewCollector( + _ context.Context, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration, diff --git a/pkg/collector/http_collector.go b/pkg/collector/http_collector.go index fd41aeb..7537f69 100644 --- a/pkg/collector/http_collector.go +++ b/pkg/collector/http_collector.go @@ -1,6 +1,7 @@ package collector import ( + "context" "fmt" "net/url" "time" @@ -26,7 +27,7 @@ func NewHTTPCollectorPlugin() (*HTTPCollectorPlugin, error) { return &HTTPCollectorPlugin{}, nil } -func (p *HTTPCollectorPlugin) NewCollector(hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { +func (p *HTTPCollectorPlugin) NewCollector(_ context.Context, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { collector := &HTTPCollector{ namespace: hpa.Namespace, } @@ -78,7 +79,7 @@ type HTTPCollector struct { metric autoscalingv2.MetricIdentifier } -func (c *HTTPCollector) GetMetrics() ([]CollectedMetric, error) { +func (c *HTTPCollector) GetMetrics(ctx context.Context) ([]CollectedMetric, error) { metric, err := c.metricsGetter.GetMetric(*c.endpoint) if err != nil { return nil, err diff --git a/pkg/collector/http_collector_test.go b/pkg/collector/http_collector_test.go index efdbede..df4c784 100644 --- a/pkg/collector/http_collector_test.go +++ b/pkg/collector/http_collector_test.go @@ -1,6 +1,7 @@ package collector import ( + "context" "encoding/json" "net/http" "net/http/httptest" @@ -66,9 +67,9 @@ func TestHTTPCollector(t *testing.T) { Namespace: "default", }, } - collector, err := plugin.NewCollector(hpa, testConfig, testInterval) + collector, err := plugin.NewCollector(context.Background(), hpa, testConfig, testInterval) require.NoError(t, err) - metrics, err := collector.GetMetrics() + metrics, err := collector.GetMetrics(context.Background()) require.NoError(t, err) require.NotNil(t, metrics) require.Len(t, metrics, 1) diff --git a/pkg/collector/influxdb_collector.go b/pkg/collector/influxdb_collector.go index 2e3cd3d..7e01e97 100644 --- a/pkg/collector/influxdb_collector.go +++ b/pkg/collector/influxdb_collector.go @@ -38,8 +38,8 @@ func NewInfluxDBCollectorPlugin(client kubernetes.Interface, address, token, org }, nil } -func (p *InfluxDBCollectorPlugin) NewCollector(hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { - return NewInfluxDBCollector(hpa, p.address, p.token, p.org, config, interval) +func (p *InfluxDBCollectorPlugin) NewCollector(ctx context.Context, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { + return NewInfluxDBCollector(ctx, hpa, p.address, p.token, p.org, config, interval) } type InfluxDBCollector struct { @@ -55,7 +55,7 @@ type InfluxDBCollector struct { namespace string } -func NewInfluxDBCollector(hpa *autoscalingv2.HorizontalPodAutoscaler, address string, token string, org string, config *MetricConfig, interval time.Duration) (*InfluxDBCollector, error) { +func NewInfluxDBCollector(_ context.Context, hpa *autoscalingv2.HorizontalPodAutoscaler, address string, token string, org string, config *MetricConfig, interval time.Duration) (*InfluxDBCollector, error) { collector := &InfluxDBCollector{ interval: interval, metric: config.Metric, @@ -107,9 +107,9 @@ type queryResult struct { } // getValue returns the first result gathered from an InfluxDB instance. -func (c *InfluxDBCollector) getValue() (resource.Quantity, error) { +func (c *InfluxDBCollector) getValue(ctx context.Context) (resource.Quantity, error) { queryAPI := c.influxDBClient.QueryAPI(c.org) - res, err := queryAPI.Query(context.Background(), c.query) + res, err := queryAPI.Query(ctx, c.query) if err != nil { return resource.Quantity{}, err } @@ -125,8 +125,8 @@ func (c *InfluxDBCollector) getValue() (resource.Quantity, error) { return resource.Quantity{}, fmt.Errorf("empty result returned") } -func (c *InfluxDBCollector) GetMetrics() ([]CollectedMetric, error) { - v, err := c.getValue() +func (c *InfluxDBCollector) GetMetrics(ctx context.Context) ([]CollectedMetric, error) { + v, err := c.getValue(ctx) if err != nil { return nil, err } diff --git a/pkg/collector/influxdb_collector_test.go b/pkg/collector/influxdb_collector_test.go index acbecb0..910516d 100644 --- a/pkg/collector/influxdb_collector_test.go +++ b/pkg/collector/influxdb_collector_test.go @@ -1,6 +1,7 @@ package collector import ( + "context" "strings" "testing" "time" @@ -38,7 +39,7 @@ func TestInfluxDBCollector_New(t *testing.T) { "query-name": "range2m", }, } - c, err := NewInfluxDBCollector(hpa, "http://localhost:9999", "secret", "deadbeef", m, time.Second) + c, err := NewInfluxDBCollector(context.Background(), hpa, "http://localhost:9999", "secret", "deadbeef", m, time.Second) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -79,7 +80,7 @@ func TestInfluxDBCollector_New(t *testing.T) { "query-name": "range3m", }, } - c, err := NewInfluxDBCollector(hpa, "http://localhost:8888", "secret", "deadbeef", m, time.Second) + c, err := NewInfluxDBCollector(context.Background(), hpa, "http://localhost:8888", "secret", "deadbeef", m, time.Second) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -149,7 +150,7 @@ func TestInfluxDBCollector_New(t *testing.T) { CollectorType: "influxdb", Config: tc.config, } - _, err := NewInfluxDBCollector(hpa, "http://localhost:9999", "secret", "deadbeef", m, time.Second) + _, err := NewInfluxDBCollector(context.Background(), hpa, "http://localhost:9999", "secret", "deadbeef", m, time.Second) if err == nil { t.Fatal("expected error got none") } diff --git a/pkg/collector/nakadi_collector.go b/pkg/collector/nakadi_collector.go index 39d87ef..2ced5c4 100644 --- a/pkg/collector/nakadi_collector.go +++ b/pkg/collector/nakadi_collector.go @@ -36,8 +36,8 @@ func NewNakadiCollectorPlugin(nakadi nakadi.Nakadi) (*NakadiCollectorPlugin, err } // NewCollector initializes a new Nakadi collector from the specified HPA. -func (c *NakadiCollectorPlugin) NewCollector(hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { - return NewNakadiCollector(c.nakadi, hpa, config, interval) +func (c *NakadiCollectorPlugin) NewCollector(ctx context.Context, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { + return NewNakadiCollector(ctx, c.nakadi, hpa, config, interval) } // NakadiCollector defines a collector that is able to collect metrics from @@ -53,7 +53,7 @@ type NakadiCollector struct { } // NewNakadiCollector initializes a new NakadiCollector. -func NewNakadiCollector(nakadi nakadi.Nakadi, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (*NakadiCollector, error) { +func NewNakadiCollector(_ context.Context, nakadi nakadi.Nakadi, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (*NakadiCollector, error) { if config.Metric.Selector == nil { return nil, fmt.Errorf("selector for nakadi is not specified") } @@ -84,17 +84,17 @@ func NewNakadiCollector(nakadi nakadi.Nakadi, hpa *autoscalingv2.HorizontalPodAu } // GetMetrics returns a list of collected metrics for the Nakadi subscription ID. -func (c *NakadiCollector) GetMetrics() ([]CollectedMetric, error) { +func (c *NakadiCollector) GetMetrics(ctx context.Context) ([]CollectedMetric, error) { var value int64 var err error switch c.nakadiMetricType { case nakadiMetricTypeConsumerLagSeconds: - value, err = c.nakadi.ConsumerLagSeconds(context.TODO(), c.subscriptionID) + value, err = c.nakadi.ConsumerLagSeconds(ctx, c.subscriptionID) if err != nil { return nil, err } case nakadiMetricTypeUnconsumedEvents: - value, err = c.nakadi.UnconsumedEvents(context.TODO(), c.subscriptionID) + value, err = c.nakadi.UnconsumedEvents(ctx, c.subscriptionID) if err != nil { return nil, err } diff --git a/pkg/collector/pod_collector.go b/pkg/collector/pod_collector.go index 1f3bca0..0df7128 100644 --- a/pkg/collector/pod_collector.go +++ b/pkg/collector/pod_collector.go @@ -30,8 +30,8 @@ func NewPodCollectorPlugin(client kubernetes.Interface, argoRolloutsClient argoR } } -func (p *PodCollectorPlugin) NewCollector(hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { - return NewPodCollector(p.client, p.argoRolloutsClient, hpa, config, interval) +func (p *PodCollectorPlugin) NewCollector(ctx context.Context, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { + return NewPodCollector(ctx, p.client, p.argoRolloutsClient, hpa, config, interval) } type PodCollector struct { @@ -46,9 +46,9 @@ type PodCollector struct { logger *log.Entry } -func NewPodCollector(client kubernetes.Interface, argoRolloutsClient argoRolloutsClient.Interface, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (*PodCollector, error) { +func NewPodCollector(ctx context.Context, client kubernetes.Interface, argoRolloutsClient argoRolloutsClient.Interface, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (*PodCollector, error) { // get pod selector based on HPA scale target ref - selector, err := getPodLabelSelector(client, argoRolloutsClient, hpa) + selector, err := getPodLabelSelector(ctx, client, argoRolloutsClient, hpa) if err != nil { return nil, fmt.Errorf("failed to get pod label selector: %v", err) } @@ -81,12 +81,12 @@ func NewPodCollector(client kubernetes.Interface, argoRolloutsClient argoRollout return c, nil } -func (c *PodCollector) GetMetrics() ([]CollectedMetric, error) { +func (c *PodCollector) GetMetrics(ctx context.Context) ([]CollectedMetric, error) { opts := metav1.ListOptions{ LabelSelector: labels.Set(c.podLabelSelector.MatchLabels).String(), } - pods, err := c.client.CoreV1().Pods(c.namespace).List(context.TODO(), opts) + pods, err := c.client.CoreV1().Pods(c.namespace).List(ctx, opts) if err != nil { return nil, err } @@ -156,22 +156,22 @@ func (c *PodCollector) getPodMetric(pod corev1.Pod, ch chan CollectedMetric, err } } -func getPodLabelSelector(client kubernetes.Interface, argoRolloutsClient argoRolloutsClient.Interface, hpa *autoscalingv2.HorizontalPodAutoscaler) (*metav1.LabelSelector, error) { +func getPodLabelSelector(ctx context.Context, client kubernetes.Interface, argoRolloutsClient argoRolloutsClient.Interface, hpa *autoscalingv2.HorizontalPodAutoscaler) (*metav1.LabelSelector, error) { switch hpa.Spec.ScaleTargetRef.Kind { case "Deployment": - deployment, err := client.AppsV1().Deployments(hpa.Namespace).Get(context.TODO(), hpa.Spec.ScaleTargetRef.Name, metav1.GetOptions{}) + deployment, err := client.AppsV1().Deployments(hpa.Namespace).Get(ctx, hpa.Spec.ScaleTargetRef.Name, metav1.GetOptions{}) if err != nil { return nil, err } return deployment.Spec.Selector, nil case "StatefulSet": - sts, err := client.AppsV1().StatefulSets(hpa.Namespace).Get(context.TODO(), hpa.Spec.ScaleTargetRef.Name, metav1.GetOptions{}) + sts, err := client.AppsV1().StatefulSets(hpa.Namespace).Get(ctx, hpa.Spec.ScaleTargetRef.Name, metav1.GetOptions{}) if err != nil { return nil, err } return sts.Spec.Selector, nil case "Rollout": - rollout, err := argoRolloutsClient.ArgoprojV1alpha1().Rollouts(hpa.Namespace).Get(context.TODO(), hpa.Spec.ScaleTargetRef.Name, metav1.GetOptions{}) + rollout, err := argoRolloutsClient.ArgoprojV1alpha1().Rollouts(hpa.Namespace).Get(ctx, hpa.Spec.ScaleTargetRef.Name, metav1.GetOptions{}) if err != nil { return nil, err } diff --git a/pkg/collector/pod_collector_test.go b/pkg/collector/pod_collector_test.go index d72acc8..4ed1d2e 100644 --- a/pkg/collector/pod_collector_test.go +++ b/pkg/collector/pod_collector_test.go @@ -56,9 +56,9 @@ func TestPodCollector(t *testing.T) { makeTestPods(t, host, port, "test-metric", client, 5, podCondition, podDeletionTimestamp) testHPA := makeTestHPA(t, client) testConfig := makeTestConfig(port, minPodReadyAge) - collector, err := plugin.NewCollector(testHPA, testConfig, testInterval) + collector, err := plugin.NewCollector(context.Background(), testHPA, testConfig, testInterval) require.NoError(t, err) - metrics, err := collector.GetMetrics() + metrics, err := collector.GetMetrics(context.Background()) require.NoError(t, err) require.Equal(t, len(metrics), int(metricsHandler.calledCounter)) var values []int64 @@ -97,9 +97,9 @@ func TestPodCollectorWithMinPodReadyAge(t *testing.T) { makeTestPods(t, host, port, "test-metric", client, 5, podCondition, podDeletionTimestamp) testHPA := makeTestHPA(t, client) testConfig := makeTestConfig(port, minPodReadyAge) - collector, err := plugin.NewCollector(testHPA, testConfig, testInterval) + collector, err := plugin.NewCollector(context.Background(), testHPA, testConfig, testInterval) require.NoError(t, err) - metrics, err := collector.GetMetrics() + metrics, err := collector.GetMetrics(context.Background()) require.NoError(t, err) require.Equal(t, len(metrics), int(metricsHandler.calledCounter)) var values []int64 @@ -137,9 +137,9 @@ func TestPodCollectorWithPodCondition(t *testing.T) { makeTestPods(t, host, port, "test-metric", client, 5, podCondition, podDeletionTimestamp) testHPA := makeTestHPA(t, client) testConfig := makeTestConfig(port, minPodReadyAge) - collector, err := plugin.NewCollector(testHPA, testConfig, testInterval) + collector, err := plugin.NewCollector(context.Background(), testHPA, testConfig, testInterval) require.NoError(t, err) - metrics, err := collector.GetMetrics() + metrics, err := collector.GetMetrics(context.Background()) require.NoError(t, err) require.Equal(t, len(metrics), int(metricsHandler.calledCounter)) var values []int64 @@ -177,9 +177,9 @@ func TestPodCollectorWithPodTerminatingCondition(t *testing.T) { makeTestPods(t, host, port, "test-metric", client, 5, podCondition, podDeletionTimestamp) testHPA := makeTestHPA(t, client) testConfig := makeTestConfig(port, minPodReadyAge) - collector, err := plugin.NewCollector(testHPA, testConfig, testInterval) + collector, err := plugin.NewCollector(context.Background(), testHPA, testConfig, testInterval) require.NoError(t, err) - metrics, err := collector.GetMetrics() + metrics, err := collector.GetMetrics(context.Background()) require.NoError(t, err) require.Equal(t, len(metrics), int(metricsHandler.calledCounter)) var values []int64 @@ -217,9 +217,9 @@ func TestPodCollectorWithRollout(t *testing.T) { makeTestPods(t, host, port, "test-metric", client, 5, podCondition, podDeletionTimestamp) testHPA := makeTestHPAForRollout(t, client) testConfig := makeTestConfig(port, minPodReadyAge) - collector, err := plugin.NewCollector(testHPA, testConfig, testInterval) + collector, err := plugin.NewCollector(context.Background(), testHPA, testConfig, testInterval) require.NoError(t, err) - metrics, err := collector.GetMetrics() + metrics, err := collector.GetMetrics(context.Background()) require.NoError(t, err) require.Equal(t, len(metrics), int(metricsHandler.calledCounter)) var values []int64 diff --git a/pkg/collector/prometheus_collector.go b/pkg/collector/prometheus_collector.go index 9c6cc1d..d985399 100644 --- a/pkg/collector/prometheus_collector.go +++ b/pkg/collector/prometheus_collector.go @@ -55,7 +55,7 @@ func NewPrometheusCollectorPlugin(client kubernetes.Interface, prometheusServer }, nil } -func (p *PrometheusCollectorPlugin) NewCollector(hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { +func (p *PrometheusCollectorPlugin) NewCollector(_ context.Context, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { return NewPrometheusCollector(p.client, p.promAPI, hpa, config, interval) } @@ -133,9 +133,9 @@ func NewPrometheusCollector(client kubernetes.Interface, promAPI promv1.API, hpa return c, nil } -func (c *PrometheusCollector) GetMetrics() ([]CollectedMetric, error) { +func (c *PrometheusCollector) GetMetrics(ctx context.Context) ([]CollectedMetric, error) { // TODO: use real context - value, _, err := c.promAPI.Query(context.Background(), c.query, time.Now().UTC()) + value, _, err := c.promAPI.Query(ctx, c.query, time.Now().UTC()) if err != nil { return nil, err } @@ -163,7 +163,7 @@ func (c *PrometheusCollector) GetMetrics() ([]CollectedMetric, error) { // calculate an average metric instead of total. // targetAverageValue will be available in Kubernetes v1.12 // https://github.com/kubernetes/kubernetes/pull/64097 - replicas, err := targetRefReplicas(c.client, c.hpa) + replicas, err := targetRefReplicas(ctx, c.client, c.hpa) if err != nil { return nil, err } diff --git a/pkg/collector/prometheus_collector_test.go b/pkg/collector/prometheus_collector_test.go index 30f67d4..f14623a 100644 --- a/pkg/collector/prometheus_collector_test.go +++ b/pkg/collector/prometheus_collector_test.go @@ -1,6 +1,7 @@ package collector import ( + "context" "testing" "github.com/stretchr/testify/require" @@ -179,7 +180,7 @@ func TestNewPrometheusCollector(t *testing.T) { require.NoError(t, err) require.Len(t, configs, 1) - collector, err := collectorFactory.NewCollector(tc.hpa, configs[0], 0) + collector, err := collectorFactory.NewCollector(context.Background(), tc.hpa, configs[0], 0) if tc.expectedQuery != "" { require.NoError(t, err) c, ok := collector.(*PrometheusCollector) diff --git a/pkg/collector/scaling_schedule_collector.go b/pkg/collector/scaling_schedule_collector.go index 294f30e..55a2f30 100644 --- a/pkg/collector/scaling_schedule_collector.go +++ b/pkg/collector/scaling_schedule_collector.go @@ -1,6 +1,7 @@ package collector import ( + "context" "errors" "fmt" "math" @@ -93,14 +94,14 @@ func NewClusterScalingScheduleCollectorPlugin(store Store, now Now, defaultScali // NewCollector initializes a new scaling schedule collector from the // specified HPA. It's the only required method to implement the // collector.CollectorPlugin interface. -func (c *ScalingScheduleCollectorPlugin) NewCollector(hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { +func (c *ScalingScheduleCollectorPlugin) NewCollector(_ context.Context, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { return NewScalingScheduleCollector(c.store, c.defaultScalingWindow, c.defaultTimeZone, c.rampSteps, c.now, hpa, config, interval) } // NewCollector initializes a new cluster wide scaling schedule // collector from the specified HPA. It's the only required method to // implement the collector.CollectorPlugin interface. -func (c *ClusterScalingScheduleCollectorPlugin) NewCollector(hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { +func (c *ClusterScalingScheduleCollectorPlugin) NewCollector(_ context.Context, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { return NewClusterScalingScheduleCollector(c.store, c.defaultScalingWindow, c.defaultTimeZone, c.rampSteps, c.now, hpa, config, interval) } @@ -169,7 +170,7 @@ func NewClusterScalingScheduleCollector(store Store, defaultScalingWindow time.D } // GetMetrics is the main implementation for collector.Collector interface -func (c *ScalingScheduleCollector) GetMetrics() ([]CollectedMetric, error) { +func (c *ScalingScheduleCollector) GetMetrics(_ context.Context) ([]CollectedMetric, error) { scalingScheduleInterface, exists, err := c.store.GetByKey(fmt.Sprintf("%s/%s", c.objectReference.Namespace, c.objectReference.Name)) if !exists { return nil, ErrScalingScheduleNotFound @@ -186,7 +187,7 @@ func (c *ScalingScheduleCollector) GetMetrics() ([]CollectedMetric, error) { } // GetMetrics is the main implementation for collector.Collector interface -func (c *ClusterScalingScheduleCollector) GetMetrics() ([]CollectedMetric, error) { +func (c *ClusterScalingScheduleCollector) GetMetrics(_ context.Context) ([]CollectedMetric, error) { clusterScalingScheduleInterface, exists, err := c.store.GetByKey(c.objectReference.Name) if !exists { return nil, ErrClusterScalingScheduleNotFound diff --git a/pkg/collector/scaling_schedule_collector_test.go b/pkg/collector/scaling_schedule_collector_test.go index 4194596..daf3105 100644 --- a/pkg/collector/scaling_schedule_collector_test.go +++ b/pkg/collector/scaling_schedule_collector_test.go @@ -1,6 +1,7 @@ package collector import ( + "context" "errors" "fmt" "testing" @@ -626,17 +627,17 @@ func TestScalingScheduleCollector(t *testing.T) { err = collectorFactoryFirstRun.RegisterObjectCollector("ClusterScalingSchedule", "", clusterPluginFirstRun) require.NoError(t, err) - collector, err := collectorFactory.NewCollector(hpa, configs[0], 0) + collector, err := collectorFactory.NewCollector(context.Background(), hpa, configs[0], 0) require.NoError(t, err) collector, ok := collector.(*ScalingScheduleCollector) require.True(t, ok) - clusterCollector, err := collectorFactory.NewCollector(hpa, configs[1], 0) + clusterCollector, err := collectorFactory.NewCollector(context.Background(), hpa, configs[1], 0) require.NoError(t, err) clusterCollector, ok = clusterCollector.(*ClusterScalingScheduleCollector) require.True(t, ok) - clusterCollectorFirstRun, err := collectorFactoryFirstRun.NewCollector(hpa, configs[1], 0) + clusterCollectorFirstRun, err := collectorFactoryFirstRun.NewCollector(context.Background(), hpa, configs[1], 0) require.NoError(t, err) clusterCollectorFirstRun, ok = clusterCollectorFirstRun.(*ClusterScalingScheduleCollector) require.True(t, ok) @@ -659,13 +660,13 @@ func TestScalingScheduleCollector(t *testing.T) { } } - collected, err := collector.GetMetrics() + collected, err := collector.GetMetrics(context.Background()) checkCollectedMetrics(t, collected, "ScalingSchedule") - clusterCollected, err := clusterCollector.GetMetrics() + clusterCollected, err := clusterCollector.GetMetrics(context.Background()) checkCollectedMetrics(t, clusterCollected, "ClusterScalingSchedule") - clusterCollectedFirstRun, err := clusterCollectorFirstRun.GetMetrics() + clusterCollectedFirstRun, err := clusterCollectorFirstRun.GetMetrics(context.Background()) checkCollectedMetrics(t, clusterCollectedFirstRun, "ClusterScalingSchedule") }) } @@ -698,21 +699,21 @@ func TestScalingScheduleObjectNotPresentReturnsError(t *testing.T) { err = collectorFactory.RegisterObjectCollector("ClusterScalingSchedule", "", clusterPlugin) require.NoError(t, err) - collector, err := collectorFactory.NewCollector(hpa, configs[0], 0) + collector, err := collectorFactory.NewCollector(context.Background(), hpa, configs[0], 0) require.NoError(t, err) collector, ok := collector.(*ScalingScheduleCollector) require.True(t, ok) - clusterCollector, err := collectorFactory.NewCollector(hpa, configs[1], 0) + clusterCollector, err := collectorFactory.NewCollector(context.Background(), hpa, configs[1], 0) require.NoError(t, err) clusterCollector, ok = clusterCollector.(*ClusterScalingScheduleCollector) require.True(t, ok) - _, err = collector.GetMetrics() + _, err = collector.GetMetrics(context.Background()) require.Error(t, err) require.Equal(t, ErrScalingScheduleNotFound, err) - _, err = clusterCollector.GetMetrics() + _, err = clusterCollector.GetMetrics(context.Background()) require.Error(t, err) require.Equal(t, ErrClusterScalingScheduleNotFound, err) @@ -721,11 +722,11 @@ func TestScalingScheduleObjectNotPresentReturnsError(t *testing.T) { store.d["namespace/scalingScheduleName"] = invalidObject clusterStore.d["scalingScheduleName"] = invalidObject - _, err = collector.GetMetrics() + _, err = collector.GetMetrics(context.Background()) require.Error(t, err) require.Equal(t, ErrNotScalingScheduleFound, err) - _, err = clusterCollector.GetMetrics() + _, err = clusterCollector.GetMetrics(context.Background()) require.Error(t, err) require.Equal(t, ErrNotClusterScalingScheduleFound, err) } @@ -755,20 +756,20 @@ func TestReturnsErrorWhenStoreDoes(t *testing.T) { err = collectorFactory.RegisterObjectCollector("ClusterScalingSchedule", "", clusterPlugin) require.NoError(t, err) - collector, err := collectorFactory.NewCollector(hpa, configs[0], 0) + collector, err := collectorFactory.NewCollector(context.Background(), hpa, configs[0], 0) require.NoError(t, err) collector, ok := collector.(*ScalingScheduleCollector) require.True(t, ok) - clusterCollector, err := collectorFactory.NewCollector(hpa, configs[1], 0) + clusterCollector, err := collectorFactory.NewCollector(context.Background(), hpa, configs[1], 0) require.NoError(t, err) clusterCollector, ok = clusterCollector.(*ClusterScalingScheduleCollector) require.True(t, ok) - _, err = collector.GetMetrics() + _, err = collector.GetMetrics(context.Background()) require.Error(t, err) - _, err = clusterCollector.GetMetrics() + _, err = clusterCollector.GetMetrics(context.Background()) require.Error(t, err) } diff --git a/pkg/collector/skipper_collector.go b/pkg/collector/skipper_collector.go index 92dd47d..961a388 100644 --- a/pkg/collector/skipper_collector.go +++ b/pkg/collector/skipper_collector.go @@ -49,7 +49,7 @@ func NewSkipperCollectorPlugin(client kubernetes.Interface, rgClient rginterface } // NewCollector initializes a new skipper collector from the specified HPA. -func (c *SkipperCollectorPlugin) NewCollector(hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { +func (c *SkipperCollectorPlugin) NewCollector(_ context.Context, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { if strings.HasPrefix(config.Metric.Name, rpsMetricName) { backend, ok := config.Config["backend"] if !ok { @@ -204,7 +204,7 @@ func (c *SkipperCollector) getCollector(ctx context.Context) (Collector, error) } config.PerReplica = false // per replica is handled outside of the prometheus collector - collector, err := c.plugin.NewCollector(c.hpa, &config, c.interval) + collector, err := c.plugin.NewCollector(ctx, c.hpa, &config, c.interval) if err != nil { return nil, err } @@ -213,13 +213,13 @@ func (c *SkipperCollector) getCollector(ctx context.Context) (Collector, error) } // GetMetrics gets skipper metrics from prometheus. -func (c *SkipperCollector) GetMetrics() ([]CollectedMetric, error) { - collector, err := c.getCollector(context.TODO()) +func (c *SkipperCollector) GetMetrics(ctx context.Context) ([]CollectedMetric, error) { + collector, err := c.getCollector(ctx) if err != nil { return nil, err } - values, err := collector.GetMetrics() + values, err := collector.GetMetrics(ctx) if err != nil { return nil, err } @@ -236,7 +236,7 @@ func (c *SkipperCollector) GetMetrics() ([]CollectedMetric, error) { // calculate an average metric instead of total. // targetAverageValue will be available in Kubernetes v1.12 // https://github.com/kubernetes/kubernetes/pull/64097 - replicas, err := targetRefReplicas(c.client, c.hpa) + replicas, err := targetRefReplicas(ctx, c.client, c.hpa) if err != nil { return nil, err } @@ -257,17 +257,17 @@ func (c *SkipperCollector) Interval() time.Duration { return c.interval } -func targetRefReplicas(client kubernetes.Interface, hpa *autoscalingv2.HorizontalPodAutoscaler) (int32, error) { +func targetRefReplicas(ctx context.Context, client kubernetes.Interface, hpa *autoscalingv2.HorizontalPodAutoscaler) (int32, error) { var replicas int32 switch hpa.Spec.ScaleTargetRef.Kind { case "Deployment": - deployment, err := client.AppsV1().Deployments(hpa.Namespace).Get(context.TODO(), hpa.Spec.ScaleTargetRef.Name, metav1.GetOptions{}) + deployment, err := client.AppsV1().Deployments(hpa.Namespace).Get(ctx, hpa.Spec.ScaleTargetRef.Name, metav1.GetOptions{}) if err != nil { return 0, err } replicas = deployment.Status.Replicas case "StatefulSet": - sts, err := client.AppsV1().StatefulSets(hpa.Namespace).Get(context.TODO(), hpa.Spec.ScaleTargetRef.Name, metav1.GetOptions{}) + sts, err := client.AppsV1().StatefulSets(hpa.Namespace).Get(ctx, hpa.Spec.ScaleTargetRef.Name, metav1.GetOptions{}) if err != nil { return 0, err } diff --git a/pkg/collector/skipper_collector_test.go b/pkg/collector/skipper_collector_test.go index 846dbc0..4856cfa 100644 --- a/pkg/collector/skipper_collector_test.go +++ b/pkg/collector/skipper_collector_test.go @@ -38,7 +38,7 @@ func TestTargetRefReplicasDeployments(t *testing.T) { Create(context.TODO(), newHPA(defaultNamespace, name, "Deployment"), metav1.CreateOptions{}) require.NoError(t, err) - replicas, err := targetRefReplicas(client, hpa) + replicas, err := targetRefReplicas(context.Background(), client, hpa) require.NoError(t, err) require.Equal(t, deployment.Status.Replicas, replicas) } @@ -55,7 +55,7 @@ func TestTargetRefReplicasStatefulSets(t *testing.T) { Create(context.TODO(), newHPA(defaultNamespace, name, "StatefulSet"), metav1.CreateOptions{}) require.NoError(t, err) - replicas, err := targetRefReplicas(client, hpa) + replicas, err := targetRefReplicas(context.Background(), client, hpa) require.NoError(t, err) require.Equal(t, statefulSet.Status.Replicas, replicas) } @@ -340,7 +340,7 @@ func TestSkipperCollectorIngress(t *testing.T) { require.NoError(t, err) collector, err := NewSkipperCollector(client, nil, plugin, hpa, config, time.Minute, tc.backendAnnotations, tc.backend) require.NoError(t, err, "failed to create skipper collector: %v", err) - collected, err := collector.GetMetrics() + collected, err := collector.GetMetrics(context.Background()) if tc.expectError { require.Error(t, err) } else { @@ -522,7 +522,7 @@ func TestSkipperCollector(t *testing.T) { require.NoError(t, err) collector, err := NewSkipperCollector(client, rgClient, plugin, hpa, config, time.Minute, []string{testBackendWeightsAnnotation}, tc.backend) require.NoError(t, err, "failed to create skipper collector: %v", err) - collected, err := collector.GetMetrics() + collected, err := collector.GetMetrics(context.Background()) if tc.expectError { require.Error(t, err, "%s", kind) } else { diff --git a/pkg/collector/zmon_collector.go b/pkg/collector/zmon_collector.go index a7a6f86..e91326f 100644 --- a/pkg/collector/zmon_collector.go +++ b/pkg/collector/zmon_collector.go @@ -7,6 +7,7 @@ import ( "time" "github.com/zalando-incubator/kube-metrics-adapter/pkg/zmon" + "golang.org/x/net/context" autoscalingv2 "k8s.io/api/autoscaling/v2" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -40,7 +41,7 @@ func NewZMONCollectorPlugin(zmon zmon.ZMON) (*ZMONCollectorPlugin, error) { } // NewCollector initializes a new ZMON collector from the specified HPA. -func (c *ZMONCollectorPlugin) NewCollector(hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { +func (c *ZMONCollectorPlugin) NewCollector(_ context.Context, hpa *autoscalingv2.HorizontalPodAutoscaler, config *MetricConfig, interval time.Duration) (Collector, error) { return NewZMONCollector(c.zmon, hpa, config, interval) } @@ -121,7 +122,7 @@ func NewZMONCollector(zmon zmon.ZMON, hpa *autoscalingv2.HorizontalPodAutoscaler } // GetMetrics returns a list of collected metrics for the ZMON check. -func (c *ZMONCollector) GetMetrics() ([]CollectedMetric, error) { +func (c *ZMONCollector) GetMetrics(ctx context.Context) ([]CollectedMetric, error) { dataPoints, err := c.zmon.Query(c.checkID, c.key, c.tags, c.aggregators, c.duration) if err != nil { return nil, err diff --git a/pkg/collector/zmon_collector_test.go b/pkg/collector/zmon_collector_test.go index ef9d028..fa24abf 100644 --- a/pkg/collector/zmon_collector_test.go +++ b/pkg/collector/zmon_collector_test.go @@ -1,6 +1,7 @@ package collector import ( + "context" "testing" "time" @@ -38,7 +39,7 @@ func TestZMONCollectorNewCollector(t *testing.T) { hpa := &autoscalingv2.HorizontalPodAutoscaler{} - collector, err := collectPlugin.NewCollector(hpa, config, 1*time.Second) + collector, err := collectPlugin.NewCollector(context.Background(), hpa, config, 1*time.Second) require.NoError(t, err) require.NotNil(t, collector) zmonCollector := collector.(*ZMONCollector) @@ -52,7 +53,7 @@ func TestZMONCollectorNewCollector(t *testing.T) { // should fail if the check id is not specified. delete(config.Config, zmonCheckIDLabelKey) config.Metric.Name = "foo-check" - _, err = collectPlugin.NewCollector(nil, config, 1*time.Second) + _, err = collectPlugin.NewCollector(context.Background(), nil, config, 1*time.Second) require.Error(t, err) } @@ -124,7 +125,7 @@ func TestZMONCollectorGetMetrics(tt *testing.T) { zmonCollector, err := NewZMONCollector(z, hpa, config, 1*time.Second) require.NoError(t, err) - metrics, _ := zmonCollector.GetMetrics() + metrics, _ := zmonCollector.GetMetrics(context.Background()) require.Equal(t, ti.collectedMetrics, metrics) }) } diff --git a/pkg/provider/hpa.go b/pkg/provider/hpa.go index 4da0dd7..c2bf22f 100644 --- a/pkg/provider/hpa.go +++ b/pkg/provider/hpa.go @@ -161,7 +161,7 @@ func (p *HPAProvider) updateHPAs() error { interval = p.collectorInterval } - c, err := p.collectorFactory.NewCollector(&hpa, config, interval) + c, err := p.collectorFactory.NewCollector(context.TODO(), &hpa, config, interval) if err != nil { // Only log when it's not a PluginNotFoundError AND flag disregardIncompatibleHPAs is true @@ -347,7 +347,7 @@ func (t *CollectorScheduler) Add(resourceRef resourceReference, typeName collect // context is canceled the collection will be stopped. func collectorRunner(ctx context.Context, collector collector.Collector, metricsc chan<- metricCollection) { for { - values, err := collector.GetMetrics() + values, err := collector.GetMetrics(ctx) metricsc <- metricCollection{ Values: values, diff --git a/pkg/provider/hpa_test.go b/pkg/provider/hpa_test.go index 1ac2636..c7df48e 100644 --- a/pkg/provider/hpa_test.go +++ b/pkg/provider/hpa_test.go @@ -17,13 +17,13 @@ import ( type mockCollectorPlugin struct{} -func (m mockCollectorPlugin) NewCollector(hpa *autoscaling.HorizontalPodAutoscaler, config *collector.MetricConfig, interval time.Duration) (collector.Collector, error) { +func (m mockCollectorPlugin) NewCollector(_ context.Context, hpa *autoscaling.HorizontalPodAutoscaler, config *collector.MetricConfig, interval time.Duration) (collector.Collector, error) { return mockCollector{}, nil } type mockCollector struct{} -func (c mockCollector) GetMetrics() ([]collector.CollectedMetric, error) { +func (c mockCollector) GetMetrics(_ context.Context) ([]collector.CollectedMetric, error) { return nil, nil }