chore(go.d): remove unused resolve hostname functionality (#20922)
Ilya Mashchenko committed
Sep 3, 2025 at 16:29 UTC
20e4fabe46759e4cecbfd679a3f2910d8f3be8bd
3 files changed
+1
-123
src/go/plugin/go.d/agent/confgroup/config.go
+1
-34
@@ -4,11 +4,9 @@ package confgroup
4
5
import (
6
"fmt"
7
- "net/url"
7
"regexp"
8
"strings"
9
11
- "github.com/netdata/netdata/go/plugins/plugin/go.d/agent/hostinfo"
10
"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module"
11
12
"github.com/gohugoio/hashstructure"
@@ -117,11 +115,7 @@ func (c Config) ApplyDefaults(def Default) {
115
if c.Name() == "" {
116
c.Set("name", c.Module())
117
} else {
120
- c.Set("name", cleanName(jobNameResolveHostname(c.Name())))
121
- }
122
-
123
- if v, ok := c.Get("url").(string); ok {
124
- c.Set("url", urlResolveHostname(v))
118
+ c.Set("name", cleanName(c.Name()))
119
}
120
}
121
@@ -149,30 +143,3 @@ func firstPositive(value int, others ...int) int {
143
}
144
return firstPositive(others[0], others[1:]...)
145
}
152
-
153
-func urlResolveHostname(rawURL string) string {
154
- if hostinfo.Hostname == "" || !strings.Contains(rawURL, "hostname") {
155
- return rawURL
156
- }
157
-
158
- u, err := url.Parse(rawURL)
159
- if err != nil || (u.Hostname() != "hostname" && !strings.Contains(u.Hostname(), "hostname.")) {
160
- return rawURL
161
- }
162
-
163
- u.Host = strings.Replace(u.Host, "hostname", hostinfo.Hostname, 1)
164
-
165
- return u.String()
166
-}
167
-
168
-func jobNameResolveHostname(name string) string {
169
- if hostinfo.Hostname == "" || !strings.Contains(name, "hostname") {
170
- return name
171
- }
172
-
173
- if name != "hostname" && !strings.HasPrefix(name, "hostname.") && !strings.HasPrefix(name, "hostname_") {
174
- return name
175
- }
176
-
177
- return strings.Replace(name, "hostname", hostinfo.Hostname, 1)
178
-}
src/go/plugin/go.d/agent/confgroup/config_test.go
-66
@@ -322,69 +322,3 @@ func TestConfig_Apply(t *testing.T) {
322
})
323
}
324
}
325
-
326
-func Test_urlResolveHostname(t *testing.T) {
327
- tests := map[string]struct {
328
- input string
329
- wantChanged bool
330
- }{
331
- "hostname with suffix": {
332
- wantChanged: true,
333
- input: "http://hostname.local:80/metrics",
334
- },
335
- "hostname without suffix": {
336
- wantChanged: true,
337
- input: "http://hostname:80/metrics",
338
- },
339
- "no hostname": {
340
- wantChanged: false,
341
- input: "http://127.0.0.1:80/metrics",
342
- },
343
- }
344
-
345
- for name, test := range tests {
346
- t.Run(name, func(t *testing.T) {
347
-
348
- if test.wantChanged {
349
- assert.NotEqual(t, test.input, urlResolveHostname(test.input))
350
- } else {
351
- assert.Equal(t, test.input, urlResolveHostname(test.input))
352
- }
353
- })
354
- }
355
-}
356
-
357
-func Test_jobNameResolveHostname(t *testing.T) {
358
- tests := map[string]struct {
359
- input string
360
- wantChanged bool
361
- }{
362
- "hostname with dot suffix": {
363
- wantChanged: true,
364
- input: "hostname.local",
365
- },
366
- "hostname with underscore suffix": {
367
- wantChanged: true,
368
- input: "hostname_local",
369
- },
370
- "hostname without suffix": {
371
- wantChanged: true,
372
- input: "hostname",
373
- },
374
- "no hostname": {
375
- wantChanged: false,
376
- input: "name",
377
- },
378
- }
379
-
380
- for name, test := range tests {
381
- t.Run(name, func(t *testing.T) {
382
-
383
- if test.wantChanged {
384
- assert.NotEqual(t, test.input, jobNameResolveHostname(test.input))
385
- } else {
386
- assert.Equal(t, test.input, jobNameResolveHostname(test.input))
387
- }
388
- })
389
- }
390
-}
src/go/plugin/go.d/agent/hostinfo/hostinfo.go
-23
@@ -3,32 +3,9 @@
3
package hostinfo
4
5
import (
6
- "bytes"
7
- "context"
6
"os"
9
- "os/exec"
10
- "time"
7
)
8
13
-var Hostname = getHostname()
14
-
15
-func getHostname() string {
16
- path, err := exec.LookPath("hostname")
17
- if err != nil {
18
- return ""
19
- }
20
-
21
- ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
22
- defer cancel()
23
-
24
- bs, err := exec.CommandContext(ctx, path).Output()
25
- if err != nil {
26
- return ""
27
- }
28
-
29
- return string(bytes.TrimSpace(bs))
30
-}
31
-
9
var (
10
envKubeHost = os.Getenv("KUBERNETES_SERVICE_HOST")
11
envKubePort = os.Getenv("KUBERNETES_SERVICE_PORT")