Skip to content

Commit 46f3925

Browse files
authored
fix dd_url handling (DataDog#2047)
1 parent b460f35 commit 46f3925

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

pkg/controller/utils/metadata/metadata_forwarder.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,11 @@ func getURL() string {
382382
// check url env var
383383
// example: https://app.datadoghq.com
384384
if urlFromEnvVar := os.Getenv("DD_URL"); urlFromEnvVar != "" {
385-
mdfURL.Host = urlFromEnvVar
385+
tempURL, err := url.Parse(urlFromEnvVar)
386+
if err == nil {
387+
mdfURL.Host = tempURL.Host
388+
mdfURL.Scheme = tempURL.Scheme
389+
}
386390
}
387391

388392
return mdfURL.String()
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Unless explicitly stated otherwise all files in this repository are licensed
2+
// under the Apache License Version 2.0.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
// Copyright 2016-present Datadog, Inc.
5+
6+
package metadata
7+
8+
import (
9+
"os"
10+
"testing"
11+
)
12+
13+
func Test_getURL(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
loadFunc func()
17+
wantURL string
18+
}{
19+
{
20+
name: "default case",
21+
loadFunc: func() {
22+
},
23+
wantURL: "https://app.datadoghq.com/api/v1/metadata",
24+
},
25+
{
26+
name: "set DD_SITE",
27+
loadFunc: func() {
28+
os.Clearenv()
29+
os.Setenv("DD_SITE", "datad0g.com")
30+
},
31+
wantURL: "https://app.datad0g.com/api/v1/metadata",
32+
},
33+
{
34+
name: "set DD_URL",
35+
loadFunc: func() {
36+
os.Clearenv()
37+
os.Setenv("DD_URL", "https://app.datad0g.com")
38+
},
39+
wantURL: "https://app.datad0g.com/api/v1/metadata",
40+
},
41+
}
42+
43+
for _, tt := range tests {
44+
t.Run(tt.name, func(t *testing.T) {
45+
tt.loadFunc()
46+
47+
u := getURL()
48+
49+
if u != tt.wantURL {
50+
t.Errorf("getURL() url = %v, want %v", u, tt.wantURL)
51+
52+
}
53+
})
54+
}
55+
}

0 commit comments

Comments
 (0)