Skip to content
This repository has been archived by the owner on Sep 24, 2021. It is now read-only.

Fix validation error when specifying multiple metrics #9

Merged
merged 1 commit into from
Aug 19, 2019

Conversation

homme
Copy link
Contributor

@homme homme commented Aug 7, 2019

This fixes an issue where the loop iterator variable for queries references the last specified MetricStat in the case of multiple metrics. The issue is evident in the following ExternalMetric:

apiVersion: metrics.aws/v1alpha1
kind: ExternalMetric
metadata:
  creationTimestamp: null
  name: rds-db-test
  namespace: default
spec:
  name: rds-db-test
  queries:
  - id: rds_db_test
    metricStat:
      metric:
        dimensions:
        - name: DBInstanceIdentifier
          value: foobar
        metricName: CPUUtilization
        namespace: AWS/RDS
      period: 600
      stat: Average
      unit: Percent
    returnData: false
  - expression: AVG(METRICS())
    id: rds_db_avg_test
    returnData: true
  resource:
    resource: deployment

This produces the following error:

kubectl get --raw "/apis/external.metrics.k8s.io/v1beta1/namespaces/default/rds-db-test"
Error from server (BadRequest): InvalidParameter: 3 validation error(s) found.
- minimum field value of 1, GetMetricDataInput.MetricDataQueries[0].MetricStat.Period.
- minimum field size of 1, GetMetricDataInput.MetricDataQueries[0].MetricStat.Metric.MetricName.
- minimum field size of 1, GetMetricDataInput.MetricDataQueries[0].MetricStat.Metric.Namespace.

This can also be replicated locally with the following program:

package main

import (
	"flag"
	"fmt"
	"log"

	"github.com/awslabs/k8s-cloudwatch-adapter/pkg/aws"
	"github.com/awslabs/k8s-cloudwatch-adapter/pkg/config"
)

func main() {
	flag.Parse()
	c := aws.NewCloudWatchClient()
	queries := []config.MetricDataQuery{
		{
			ID: "rds_db_test",
			MetricStat: config.MetricStat{
				Metric: config.Metric{
					Dimensions: []config.Dimension{
						{
							Name:  "DBInstanceIdentifier",
							Value: "foobar",
						},
					},
					MetricName: "CPUUtilization",
					Namespace:  "AWS/RDS",
				},
				Period: 600,
				Stat:   "Average",
				Unit:   "Percent",
			},
			ReturnData: false,
		},
		{
			ID:         "rds_db_avg_test",
			Expression: "AVG(METRICS())",
			ReturnData: true,
		},
	}
	r, err := c.Query(queries)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(r)
}

Running this produces the following validation error without the fix:

E0807 13:22:22.033542   18945 client.go:98] err: InvalidParameter: 3 validation error(s) found.
- minimum field value of 1, GetMetricDataInput.MetricDataQueries[0].MetricStat.Period.
- minimum field size of 1, GetMetricDataInput.MetricDataQueries[0].MetricStat.Metric.MetricName.
- minimum field size of 1, GetMetricDataInput.MetricDataQueries[0].MetricStat.Metric.Namespace.

The fix allows it to run as expected without any error.

Description of changes:

  • Data is copied when looping over queries (q := q)
  • The loop is refactored to remove redundant logic when handling expressions.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@homme
Copy link
Contributor Author

homme commented Aug 7, 2019

Hmmm, I've built and deployed a version of k8s-cloudwatch-adapter with this fix and am still getting the same errors, so this needs further investigation...

This fixes an issue where the loop iterator variable for queries references the
last specified `MetricStat` in the case of multiple metrics.
@homme
Copy link
Contributor Author

homme commented Aug 8, 2019

Looking into this further I found and fixed where duplicate logic needed to be updated: the adapter now works as expected in the cluster.

@chankh would it make sense to re-use the MetricSeriesSpec struct as the type for MetricsDiscoveryConfig.Series? We can then de-duplicate the query construction logic.

@ghost ghost mentioned this pull request Aug 16, 2019
@chankh chankh merged commit 48ad705 into amazon-archives:master Aug 19, 2019
chankh pushed a commit that referenced this pull request Aug 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants