Skip to content

Commit

Permalink
[cleanup] rm v2 from function names in metrics forwarder (#1313)
Browse files Browse the repository at this point in the history
  • Loading branch information
celenechang authored and mftoure committed Oct 3, 2024
1 parent 56c06cc commit 52e4952
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
20 changes: 10 additions & 10 deletions pkg/controller/utils/datadog/metrics_forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,15 @@ func (mf *metricsForwarder) setStatus(newStatus *ConditionCommon) {
mf.status = newStatus
}

func (mf *metricsForwarder) setupV2() error {
func (mf *metricsForwarder) setup() error {
// get dda
dda, err := mf.getDatadogAgentV2()
dda, err := mf.getDatadogAgent()
if err != nil {
mf.logger.Error(err, "cannot retrieve DatadogAgent to get Datadog credentials, will retry later...")
return err
}

mf.baseURL = getbaseURLV2(dda)
mf.baseURL = getbaseURL(dda)
mf.logger.V(1).Info("Got API URL for DatadogAgent", "site", mf.baseURL)

if dda.Spec.Global != nil && dda.Spec.Global.ClusterName != nil {
Expand All @@ -258,7 +258,7 @@ func (mf *metricsForwarder) setupV2() error {
mf.ccrStatus = status.ClusterChecksRunner

// set apiKey
apiKey, err := mf.getCredentialsV2(dda)
apiKey, err := mf.getCredentials(dda)
if err != nil {
return err
}
Expand All @@ -270,7 +270,7 @@ func (mf *metricsForwarder) setupV2() error {
// implements wait.ConditionFunc and never returns error to keep retrying
func (mf *metricsForwarder) connectToDatadogAPI() (bool, error) {
var err error
err = mf.setupV2()
err = mf.setup()

defer mf.updateStatusIfNeeded(err)
if err != nil {
Expand All @@ -291,7 +291,7 @@ func (mf *metricsForwarder) connectToDatadogAPI() (bool, error) {
// related to Datadog metrics forwarding by calling updateStatusIfNeeded
func (mf *metricsForwarder) forwardMetrics() error {
var err error
err = mf.setupV2()
err = mf.setup()

defer mf.updateStatusIfNeeded(err)
if err != nil {
Expand Down Expand Up @@ -557,15 +557,15 @@ func (mf *metricsForwarder) initGlobalTags() {
}

// getDatadogAgent retrieves the DatadogAgent using Get client method
func (mf *metricsForwarder) getDatadogAgentV2() (*v2alpha1.DatadogAgent, error) {
func (mf *metricsForwarder) getDatadogAgent() (*v2alpha1.DatadogAgent, error) {
dda := &v2alpha1.DatadogAgent{}
err := mf.k8sClient.Get(context.TODO(), mf.namespacedName, dda)

return dda, err
}

// getCredentialsV2 retrieves the api key configured in the DatadogAgent
func (mf *metricsForwarder) getCredentialsV2(dda *v2alpha1.DatadogAgent) (string, error) {
// getCredentials retrieves the api key configured in the DatadogAgent
func (mf *metricsForwarder) getCredentials(dda *v2alpha1.DatadogAgent) (string, error) {
if dda.Spec.Global == nil || dda.Spec.Global.Credentials == nil {
return "", fmt.Errorf("credentials not configured in the DatadogAgent")
}
Expand Down Expand Up @@ -757,7 +757,7 @@ func (mf *metricsForwarder) isEventChanFull() bool {
return len(mf.eventChan) == cap(mf.eventChan)
}

func getbaseURLV2(dda *v2alpha1.DatadogAgent) string {
func getbaseURL(dda *v2alpha1.DatadogAgent) string {
if dda.Spec.Global != nil && dda.Spec.Global.Endpoint != nil && dda.Spec.Global.Endpoint.URL != nil {
return *dda.Spec.Global.Endpoint.URL
} else if dda.Spec.Global != nil && dda.Spec.Global.Site != nil && *dda.Spec.Global.Site != "" {
Expand Down
34 changes: 17 additions & 17 deletions pkg/controller/utils/datadog/metrics_forwarder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

commonv1 "github.com/DataDog/datadog-operator/apis/datadoghq/common/v1"
datadoghqv2alpha1 "github.com/DataDog/datadog-operator/apis/datadoghq/v2alpha1"
testV2 "github.com/DataDog/datadog-operator/apis/datadoghq/v2alpha1/test"
test "github.com/DataDog/datadog-operator/apis/datadoghq/v2alpha1/test"
apiutils "github.com/DataDog/datadog-operator/apis/utils"
"github.com/DataDog/datadog-operator/pkg/kubernetes"
"github.com/DataDog/datadog-operator/pkg/secrets"
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestMetricsForwarder_updateCredsIfNeeded(t *testing.T) {
}
}

func TestReconcileDatadogAgent_getCredentialsV2(t *testing.T) {
func TestReconcileDatadogAgent_getCredentials(t *testing.T) {
apiKey := "foundAPIKey"

encAPIKey := "ENC[APIKey]"
Expand All @@ -177,7 +177,7 @@ func TestReconcileDatadogAgent_getCredentialsV2(t *testing.T) {
client: fake.NewFakeClient(),
},
args: args{
dda: testV2.NewDatadogAgent("foo", "bar", &datadoghqv2alpha1.GlobalConfig{
dda: test.NewDatadogAgent("foo", "bar", &datadoghqv2alpha1.GlobalConfig{
Credentials: &datadoghqv2alpha1.DatadogCredentials{
APIKey: apiutils.NewStringPointer(apiKey),
},
Expand All @@ -192,7 +192,7 @@ func TestReconcileDatadogAgent_getCredentialsV2(t *testing.T) {
client: fake.NewFakeClient(),
},
args: args{
dda: testV2.NewDatadogAgent("foo", "bar", &datadoghqv2alpha1.GlobalConfig{
dda: test.NewDatadogAgent("foo", "bar", &datadoghqv2alpha1.GlobalConfig{
Credentials: &datadoghqv2alpha1.DatadogCredentials{
APISecret: &commonv1.SecretConfig{
SecretName: "datadog-creds-api",
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestReconcileDatadogAgent_getCredentialsV2(t *testing.T) {
client: fake.NewFakeClient(),
},
args: args{
dda: testV2.NewDatadogAgent("foo", "bar", &datadoghqv2alpha1.GlobalConfig{
dda: test.NewDatadogAgent("foo", "bar", &datadoghqv2alpha1.GlobalConfig{
Credentials: &datadoghqv2alpha1.DatadogCredentials{
APIKey: apiutils.NewStringPointer(encAPIKey),
},
Expand All @@ -248,7 +248,7 @@ func TestReconcileDatadogAgent_getCredentialsV2(t *testing.T) {
client: fake.NewFakeClient(),
},
args: args{
dda: testV2.NewDatadogAgent("foo", "bar", &datadoghqv2alpha1.GlobalConfig{
dda: test.NewDatadogAgent("foo", "bar", &datadoghqv2alpha1.GlobalConfig{
Credentials: &datadoghqv2alpha1.DatadogCredentials{
APIKey: apiutils.NewStringPointer(encAPIKey),
},
Expand All @@ -275,7 +275,7 @@ func TestReconcileDatadogAgent_getCredentialsV2(t *testing.T) {
client: fake.NewFakeClient(),
},
args: args{
dda: testV2.NewDatadogAgent("foo", "bar", &datadoghqv2alpha1.GlobalConfig{}),
dda: test.NewDatadogAgent("foo", "bar", &datadoghqv2alpha1.GlobalConfig{}),
},
wantErr: true,
},
Expand All @@ -291,17 +291,17 @@ func TestReconcileDatadogAgent_getCredentialsV2(t *testing.T) {
if tt.args.loadFunc != nil {
tt.args.loadFunc(mf, d)
}
apiKey, err := mf.getCredentialsV2(tt.args.dda)
apiKey, err := mf.getCredentials(tt.args.dda)
if (err != nil) != tt.wantErr {
t.Errorf("metricsForwarder.getCredentialsV2() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("metricsForwarder.getCredentials() error = %v, wantErr %v", err, tt.wantErr)
return
}
if apiKey != tt.wantAPIKey {
t.Errorf("metricsForwarder.getCredentialsV2() apiKey = %v, want %v", apiKey, tt.wantAPIKey)
t.Errorf("metricsForwarder.getCredentials() apiKey = %v, want %v", apiKey, tt.wantAPIKey)
}
if tt.wantFunc != nil {
if err := tt.wantFunc(mf, d); err != nil {
t.Errorf("metricsForwarder.getCredentialsV2() wantFunc validation error: %v", err)
t.Errorf("metricsForwarder.getCredentials() wantFunc validation error: %v", err)
}
}
})
Expand Down Expand Up @@ -654,7 +654,7 @@ func Test_metricsForwarder_getSecretsFromCache(t *testing.T) {
}
}

func Test_getbaseURLV2(t *testing.T) {
func Test_getbaseURL(t *testing.T) {
euSite := "datadoghq.eu"

type args struct {
Expand All @@ -668,14 +668,14 @@ func Test_getbaseURLV2(t *testing.T) {
{
name: "Get default baseURL",
args: args{
dda: testV2.NewDatadogAgent("foo", "bar", nil),
dda: test.NewDatadogAgent("foo", "bar", nil),
},
want: "https://api.datadoghq.com",
},
{
name: "Compute baseURL from site when passing Site",
args: args{
dda: testV2.NewDatadogAgent("foo", "bar", &datadoghqv2alpha1.GlobalConfig{
dda: test.NewDatadogAgent("foo", "bar", &datadoghqv2alpha1.GlobalConfig{
Site: &euSite,
}),
},
Expand All @@ -684,7 +684,7 @@ func Test_getbaseURLV2(t *testing.T) {
{
name: "Compute baseURL from endpoint.URL when Site is not defined",
args: args{
dda: testV2.NewDatadogAgent("foo", "bar", &datadoghqv2alpha1.GlobalConfig{
dda: test.NewDatadogAgent("foo", "bar", &datadoghqv2alpha1.GlobalConfig{
Endpoint: &datadoghqv2alpha1.Endpoint{
URL: apiutils.NewStringPointer("https://test.url.com"),
},
Expand All @@ -695,7 +695,7 @@ func Test_getbaseURLV2(t *testing.T) {
{
name: "Test that DDUrl takes precedence over Site",
args: args{
dda: testV2.NewDatadogAgent("foo", "bar", &datadoghqv2alpha1.GlobalConfig{
dda: test.NewDatadogAgent("foo", "bar", &datadoghqv2alpha1.GlobalConfig{
Site: &euSite,
Endpoint: &datadoghqv2alpha1.Endpoint{
URL: apiutils.NewStringPointer("https://test.url.com"),
Expand All @@ -707,7 +707,7 @@ func Test_getbaseURLV2(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getbaseURLV2(tt.args.dda); got != tt.want {
if got := getbaseURL(tt.args.dda); got != tt.want {
t.Errorf("getbaseURL() = %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit 52e4952

Please sign in to comment.