Skip to content

Commit

Permalink
pkg/metrics/providers: add region filed for MetricTemplate.Provider
Browse files Browse the repository at this point in the history
and make Address not required
  • Loading branch information
mathetake committed Mar 2, 2020
1 parent 0a2235b commit 337f627
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 26 deletions.
4 changes: 3 additions & 1 deletion artifacts/flagger/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,6 @@ spec:
type: object
required:
- type
- address
properties:
type:
description: Type of this provider
Expand All @@ -752,6 +751,9 @@ spec:
name:
description: Name of the Kubernetes secret
type: string
region:
description: Region of the provider
type: string
query:
description: Query of this metric template
type: string
Expand Down
4 changes: 3 additions & 1 deletion charts/flagger/crds/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,6 @@ spec:
type: object
required:
- type
- address
properties:
type:
description: Type of this provider
Expand All @@ -752,6 +751,9 @@ spec:
name:
description: Name of the Kubernetes secret
type: string
region:
description: Region of the provider
type: string
query:
description: Query of this metric template
type: string
Expand Down
4 changes: 3 additions & 1 deletion kustomize/base/flagger/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,6 @@ spec:
type: object
required:
- type
- address
properties:
type:
description: Type of this provider
Expand All @@ -752,6 +751,9 @@ spec:
name:
description: Name of the Kubernetes secret
type: string
region:
description: Region of the provider
type: string
query:
description: Query of this metric template
type: string
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/flagger/v1beta1/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,16 @@ type MetricTemplateProvider struct {
Type string `json:"type,omitempty"`

// HTTP(S) address of this provider
// +optional
Address string `json:"address,omitempty"`

// Secret reference containing the provider credentials
// +optional
SecretRef *corev1.LocalObjectReference `json:"secretRef,omitempty"`

// Region of the provider
// +optional
Region string `json:"region,omitempty"`
}

// MetricTemplateModel is the query template model
Expand Down
19 changes: 10 additions & 9 deletions pkg/metrics/providers/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -33,14 +32,16 @@ type cloudWatchClient interface {
// NewCloudWatchProvider takes a metricInterval, a provider spec and the credentials map, and
// returns a cloudWatchProvider ready to execute queries against the AWS CloudWatch metrics
func NewCloudWatchProvider(metricInterval string, provider flaggerv1.MetricTemplateProvider) (*CloudWatchProvider, error) {
region := strings.TrimLeft(provider.Address, "monitoring.")
region = strings.TrimRight(region, ".amazonaws.com")
sess, err := session.NewSession(
aws.NewConfig().
WithRegion(region).
WithMaxRetries(cloudWatchMaxRetries).
WithEndpoint(provider.Address),
)
if provider.Region == "" {
return nil, fmt.Errorf("region not specified")
}

sess, err := session.NewSession(aws.NewConfig().
WithRegion(provider.Region).WithMaxRetries(cloudWatchMaxRetries))

if err != nil {
return nil, fmt.Errorf("error creating aws session: %s", err.Error())
}

md, err := time.ParseDuration(metricInterval)
if err != nil {
Expand Down
36 changes: 23 additions & 13 deletions pkg/metrics/providers/cloudwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/costandusagereportservice"

flaggerv1 "github.com/weaveworks/flagger/pkg/apis/flagger/v1beta1"
)
Expand All @@ -23,19 +24,28 @@ func (c cloudWatchClientMock) GetMetricData(_ *cloudwatch.GetMetricDataInput) (*
}

func TestNewCloudWatchProvider(t *testing.T) {
p, err := NewCloudWatchProvider(
"5m",
flaggerv1.MetricTemplateProvider{
Address: "monitoring.ap-northeast-1.amazonaws.com",
})

if err != nil {
t.Fatal(err)
}

if exp := 5 * 60 * time.Second * cloudWatchStartDeltaMultiplierOnMetricInterval; p.startDelta != exp {
t.Fatalf("expected %d but got %d", exp, p.startDelta)
}
t.Run("ok", func(t *testing.T) {
p, err := NewCloudWatchProvider(
"5m",
flaggerv1.MetricTemplateProvider{
Region: costandusagereportservice.AWSRegionApEast1,
})

if err != nil {
t.Fatal(err)
}

if exp := 5 * 60 * time.Second * cloudWatchStartDeltaMultiplierOnMetricInterval; p.startDelta != exp {
t.Fatalf("expected %d but got %d", exp, p.startDelta)
}
})

t.Run("ng", func(t *testing.T) {
_, err := NewCloudWatchProvider("5m", flaggerv1.MetricTemplateProvider{})
if err == nil {
t.Fatal("error expected since region was not specified")
}
})
}

func TestCloudWatchProvider_IsOnline(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/metrics/providers/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type prometheusResponse struct {
// returns a Prometheus client ready to execute queries against the API
func NewPrometheusProvider(provider flaggerv1.MetricTemplateProvider, credentials map[string][]byte) (*PrometheusProvider, error) {
promURL, err := url.Parse(provider.Address)
if err != nil {
if provider.Address == "" || err != nil {
return nil, fmt.Errorf("%s address %s is not a valid URL", provider.Type, provider.Address)
}

Expand Down

0 comments on commit 337f627

Please sign in to comment.