Skip to content

Commit

Permalink
move address configuration to api/app secrets. Allow future support o…
Browse files Browse the repository at this point in the history
…f multiple key pairs
  • Loading branch information
stephen-harris committed Oct 3, 2020
1 parent d83b92f commit 3d77904
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 38 deletions.
7 changes: 4 additions & 3 deletions docs/features/analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,6 @@ spec:
failureLimit: 3
provider:
datadog:
address: https://api.datadoghq.com
interval: 5m
query: |
sum:requests.error.count{service:{{args.service-name}}} /
Expand All @@ -910,6 +909,8 @@ metadata:
name: datadog
type: Opaque
data:
api-key: <datadog-api-key>
app-key: <datadog-app-key>
default:
address: https://api.datadoghq.com
api-key: <datadog-api-key>
app-key: <datadog-app-key>
```
2 changes: 0 additions & 2 deletions manifests/crds/analysis-run-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ spec:
properties:
datadog:
properties:
address:
type: string
interval:
type: string
query:
Expand Down
2 changes: 0 additions & 2 deletions manifests/crds/analysis-template-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ spec:
properties:
datadog:
properties:
address:
type: string
interval:
type: string
query:
Expand Down
2 changes: 0 additions & 2 deletions manifests/crds/cluster-analysis-template-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ spec:
properties:
datadog:
properties:
address:
type: string
interval:
type: string
query:
Expand Down
29 changes: 18 additions & 11 deletions metricproviders/datadog/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/argoproj/argo-rollouts/utils/evaluate"
metricutil "github.com/argoproj/argo-rollouts/utils/metric"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
Expand All @@ -32,8 +33,7 @@ const (
// Implements the Provider Interface
type Provider struct {
logCtx log.Entry
apiKey string
appKey string
config datadogConfig
}

type datadogResponse struct {
Expand All @@ -42,6 +42,12 @@ type datadogResponse struct {
}
}

type datadogConfig struct {
Address string `yaml:"address,omitempty"`
ApiKey string `yaml:"api-key,omitempty"`
AppKey string `yaml:"app-key,omitempty"`
}

// Type incidates provider is a Datadog provider
func (p *Provider) Type() string {
return ProviderType
Expand All @@ -56,8 +62,8 @@ func (p *Provider) Run(run *v1alpha1.AnalysisRun, metric v1alpha1.Metric) v1alph
}

endpoint := "https://api.datadoghq.com/api/v1/query"
if metric.Provider.Datadog.Address != "" {
endpoint = metric.Provider.Datadog.Address + "/api/v1/query"
if p.config.Address != "" {
endpoint = p.config.Address + "/api/v1/query"
}

url, _ := url.Parse(endpoint)
Expand All @@ -83,8 +89,8 @@ func (p *Provider) Run(run *v1alpha1.AnalysisRun, metric v1alpha1.Metric) v1alph
request.URL = url
request.Header = make(http.Header)
request.Header.Set("Content-Type", "application/json")
request.Header.Set("DD-API-KEY", p.apiKey)
request.Header.Set("DD-APPLICATION-KEY", p.appKey)
request.Header.Set("DD-API-KEY", p.config.ApiKey)
request.Header.Set("DD-APPLICATION-KEY", p.config.AppKey)

// Send Request
httpClient := &http.Client{
Expand Down Expand Up @@ -166,15 +172,16 @@ func NewDatadogProvider(logCtx log.Entry, kubeclientset kubernetes.Interface) (*
if err != nil {
return nil, err
}
config := datadogConfig{}

apiKey := string(secret.Data["api-key"])
appKey := string(secret.Data["app-key"])
if e := yaml.Unmarshal(secret.Data["default"], &config); e != nil {
return nil, e
}

if apiKey != "" && appKey != "" {
if config.ApiKey != "" && config.AppKey != "" {
return &Provider{
logCtx: logCtx,
apiKey: apiKey,
appKey: appKey,
config: config,
}, nil
} else {
return nil, errors.New("API or App token not found")
Expand Down
20 changes: 9 additions & 11 deletions metricproviders/datadog/datadog_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package datadog

import (
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand All @@ -22,16 +23,6 @@ func TestRunSuite(t *testing.T) {
const expectedApiKey = "0123456789abcdef0123456789abcdef"
const expectedAppKey = "0123456789abcdef0123456789abcdef01234567"

tokenSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: DatadogTokensSecretName,
},
Data: map[string][]byte{
"api-key": []byte(expectedApiKey),
"app-key": []byte(expectedAppKey),
},
}

unixNow = func() int64 { return 1599076435 }

// Test Cases
Expand Down Expand Up @@ -204,7 +195,14 @@ func TestRunSuite(t *testing.T) {
}))
defer server.Close()

test.metric.Provider.Datadog.Address = server.URL
tokenSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: DatadogTokensSecretName,
},
Data: map[string][]byte{
"default": []byte(fmt.Sprintf("address: %s\napp-key: %s\napi-key: %s", server.URL, expectedAppKey, expectedApiKey)),
},
}

logCtx := log.WithField("test", "test")

Expand Down
1 change: 0 additions & 1 deletion pkg/apis/rollouts/v1alpha1/analysis_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ type WebMetricHeader struct {
}

type DatadogMetric struct {
Address string `json:"address,omitempty"`
Interval DurationString `json:"interval,omitempty"`
Query string `json:"query"`
}
6 changes: 0 additions & 6 deletions pkg/apis/rollouts/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3d77904

Please sign in to comment.