Skip to content

Commit

Permalink
Fetch Site or DdUrl before initializing MetricsForwarder (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
mantoine96 committed May 25, 2020
1 parent 607e3ce commit e44716e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pkg/apis/datadoghq/v1alpha1/test/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type NewDatadogAgentOptions struct {
ClusterChecksRunnerVolumeMounts []corev1.VolumeMount
ClusterChecksRunnerEnvVars []corev1.EnvVar
APIKeyExistingSecret string
Site string
}

// NewDefaultedDatadogAgent returns an initialized and defaulted DatadogAgent for testing purpose
Expand Down Expand Up @@ -104,6 +105,7 @@ func NewDefaultedDatadogAgent(ns, name string, options *NewDatadogAgentOptions)
}

ad.Spec.Agent.DaemonsetName = options.AgentDaemonsetName
ad.Spec.Site = options.Site

if options.Status != nil {
ad.Status = *options.Status
Expand All @@ -123,6 +125,7 @@ func NewDefaultedDatadogAgent(ns, name string, options *NewDatadogAgentOptions)
},
DeploymentName: options.ClusterAgentDeploymentName,
}

if options.MetricsServerEnabled {
ad.Spec.ClusterAgent.Config.MetricsProviderEnabled = datadoghqv1alpha1.NewBoolPointer(true)

Expand Down
19 changes: 17 additions & 2 deletions pkg/controller/utils/datadog/metrics_forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const (
reconcileMetricFormat = "%s.reconcile.success"
reconcileErrTagFormat = "reconcile_err:%s"
datadogOperatorSourceType = "datadog_operator"
defaultbaseURL = "https://api.datadoghq.com"
)

var (
Expand Down Expand Up @@ -81,6 +82,7 @@ type metricsForwarder struct {
delegator delegatedAPI
decryptor secrets.Decryptor
creds sync.Map
baseURL string
sync.Mutex
status *datadoghqv1alpha1.DatadogAgentCondition
}
Expand Down Expand Up @@ -108,6 +110,7 @@ func newMetricsForwarder(k8sClient client.Client, decryptor secrets.Decryptor, o
lastReconcileErr: errInitValue,
decryptor: decryptor,
creds: sync.Map{},
baseURL: defaultbaseURL,
logger: log.WithValues("CustomResource.Namespace", obj.GetNamespace(), "CustomResource.Name", obj.GetName()),
}
}
Expand All @@ -133,7 +136,7 @@ func (mf *metricsForwarder) start(wg *sync.WaitGroup) {
return
}

mf.logger.Info("Datadog metrics forwarder initilized successfully")
mf.logger.Info("Datadog metrics forwarder initialized successfully")

// Send CR detection event
crEvent := crDetected(mf.id)
Expand Down Expand Up @@ -200,6 +203,8 @@ func (mf *metricsForwarder) connectToDatadogAPI() (bool, error) {
}
mf.logger.Info("Getting Datadog credentials")
apiKey, appKey, err := mf.getCredentials(dda)
mf.baseURL = getbaseURL(dda)
mf.logger.Info("Got Datadog Site", "site", mf.baseURL)
defer mf.updateStatusIfNeeded(err)
if err != nil {
mf.logger.Error(err, "cannot get Datadog credentials, will retry later...")
Expand Down Expand Up @@ -350,12 +355,13 @@ func (mf *metricsForwarder) validateCreds(apiKey, appKey string) (*api.Client, e
// delegatedValidateCreds is separated from validateCreds to facilitate mocking the Datadog API
func (mf *metricsForwarder) delegatedValidateCreds(apiKey, appKey string) (*api.Client, error) {
datadogClient := api.NewClient(apiKey, appKey)
datadogClient.SetBaseUrl(mf.baseURL)
valid, err := datadogClient.Validate()
if err != nil {
return nil, fmt.Errorf("cannot validate datadog credentials: %v", err)
}
if !valid {
return nil, errors.New("invalid datadog credentials")
return nil, fmt.Errorf("invalid datadog credentials on %s", mf.baseURL)
}
return datadogClient, nil
}
Expand Down Expand Up @@ -653,3 +659,12 @@ func (mf *metricsForwarder) isErrChanFull() bool {
func (mf *metricsForwarder) isEventChanFull() bool {
return len(mf.eventChan) == cap(mf.eventChan)
}

func getbaseURL(dda *datadoghqv1alpha1.DatadogAgent) string {
if dda.Spec.Agent != nil && dda.Spec.Agent.Config.DDUrl != nil {
return *dda.Spec.Agent.Config.DDUrl
} else if dda.Spec.Site != "" {
return fmt.Sprintf("https://api.%s", dda.Spec.Site)
}
return defaultbaseURL
}
56 changes: 56 additions & 0 deletions pkg/controller/utils/datadog/metrics_forwarder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,3 +1029,59 @@ func Test_metricsForwarder_getSecretsFromCache(t *testing.T) {
})
}
}

func Test_getbaseURL(t *testing.T) {
type args struct {
dda *datadoghqv1alpha1.DatadogAgent
}
tests := []struct {
name string
args args
want string
}{
{
name: "Get default baseURL",
args: args{
dda: test.NewDefaultedDatadogAgent("foo", "bar", &test.NewDatadogAgentOptions{}),
},
want: "https://api.datadoghq.com",
},
{
name: "Compute baseURL from site when passing Site",
args: args{
dda: test.NewDefaultedDatadogAgent("foo", "bar", &test.NewDatadogAgentOptions{
Site: "datadoghq.eu",
}),
},
want: "https://api.datadoghq.eu",
},
{
name: "Compute baseURL from ddUrl when Site is not defined",
args: args{
dda: test.NewDefaultedDatadogAgent("foo", "bar", &test.NewDatadogAgentOptions{
NodeAgentConfig: &datadoghqv1alpha1.NodeAgentConfig{
DDUrl: datadoghqv1alpha1.NewStringPointer("https://test.url.com"),
}}),
},
want: "https://test.url.com",
},
{
name: "Test that DDUrl takes precedence over Site",
args: args{
dda: test.NewDefaultedDatadogAgent("foo", "bar", &test.NewDatadogAgentOptions{
Site: "datadoghq.eu",
NodeAgentConfig: &datadoghqv1alpha1.NodeAgentConfig{
DDUrl: datadoghqv1alpha1.NewStringPointer("https://test.url.com"),
}}),
},
want: "https://test.url.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getbaseURL(tt.args.dda); got != tt.want {
t.Errorf("getbaseURL() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit e44716e

Please sign in to comment.