chore(go.d): fix some golangcilint warning (#20360)
Ilya Mashchenko committed
May 28, 2025 at 07:10 UTC
6e0a46f5036de58a50f5a341dc2f35db3d3ec63c
21 files changed
+67
-54
src/go/plugin/go.d/agent/confgroup/config.go
+1
-1
@@ -40,7 +40,7 @@ type Config map[string]any
40
41
func (c Config) HashIncludeMap(_ string, k, _ any) (bool, error) {
42
s := k.(string)
43
- return !(strings.HasPrefix(s, "__") || strings.HasSuffix(s, "__")), nil
43
+ return !strings.HasPrefix(s, "__") && !strings.HasSuffix(s, "__"), nil
44
}
45
46
func (c Config) Set(key string, value any) Config { c[key] = value; return c }
src/go/plugin/go.d/agent/discovery/sd/discoverer/dockersd/docker.go
+3
-4
@@ -16,7 +16,6 @@ import (
16
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/confopt"
17
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/dockerhost"
18
19
- "github.com/docker/docker/api/types"
19
typesContainer "github.com/docker/docker/api/types/container"
20
docker "github.com/docker/docker/client"
21
"github.com/gohugoio/hashstructure"
@@ -88,7 +87,7 @@ type (
87
}
88
dockerClient interface {
89
NegotiateAPIVersion(context.Context)
91
- ContainerList(context.Context, typesContainer.ListOptions) ([]types.Container, error)
90
+ ContainerList(context.Context, typesContainer.ListOptions) ([]typesContainer.Summary, error)
91
Close() error
92
}
93
)
@@ -168,7 +167,7 @@ func (d *Discoverer) listContainers(ctx context.Context, in chan<- []model.Targe
167
return nil
168
}
169
171
-func (d *Discoverer) buildTargetGroup(cntr types.Container) model.TargetGroup {
170
+func (d *Discoverer) buildTargetGroup(cntr typesContainer.Summary) model.TargetGroup {
171
if len(cntr.Names) == 0 || cntr.NetworkSettings == nil || len(cntr.NetworkSettings.Networks) == 0 {
172
return nil
173
}
@@ -220,7 +219,7 @@ func (d *Discoverer) cleanup() {
219
}
220
}
221
223
-func cntrSource(cntr types.Container) string {
222
+func cntrSource(cntr typesContainer.Summary) string {
223
name := strings.TrimPrefix(cntr.Names[0], "/")
224
return fmt.Sprintf("discoverer=docker,container=%s,image=%s", name, cntr.Image)
225
}
src/go/plugin/go.d/agent/discovery/sd/discoverer/dockersd/dockerd_test.go
+6
-5
@@ -6,9 +6,10 @@ import (
6
"testing"
7
"time"
8
9
+ typesContainer "github.com/docker/docker/api/types/container"
10
+
11
"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/discovery/sd/model"
12
11
- "github.com/docker/docker/api/types"
13
typesNetwork "github.com/docker/docker/api/types/network"
14
)
15
@@ -124,14 +125,14 @@ func TestDiscoverer_Discover(t *testing.T) {
125
}
126
}
127
127
-func prepareNginxContainer(name string) types.Container {
128
- return types.Container{
128
+func prepareNginxContainer(name string) typesContainer.Summary {
129
+ return typesContainer.Summary{
130
ID: "id-" + name,
131
Names: []string{"/" + name},
132
Image: "nginx-image",
133
ImageID: "nginx-image-id",
134
Command: "nginx-command",
134
- Ports: []types.Port{
135
+ Ports: []typesContainer.Port{
136
{
137
IP: "0.0.0.0",
138
PrivatePort: 80,
@@ -146,7 +147,7 @@ func prepareNginxContainer(name string) types.Container {
147
}{
148
NetworkMode: "default",
149
},
149
- NetworkSettings: &types.SummaryNetworkSettings{
150
+ NetworkSettings: &typesContainer.NetworkSettingsSummary{
151
Networks: map[string]*typesNetwork.EndpointSettings{
152
"bridge": {IPAddress: "192.0.2.0"},
153
},
src/go/plugin/go.d/agent/discovery/sd/discoverer/k8ssd/kubernetes.go
+8
-8
@@ -179,12 +179,12 @@ func (d *KubeDiscoverer) Discover(ctx context.Context, in chan<- []model.TargetG
179
func (d *KubeDiscoverer) setupPodDiscoverer(ctx context.Context, ns string) *podDiscoverer {
180
pod := d.client.CoreV1().Pods(ns)
181
podLW := &cache.ListWatch{
182
- ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) {
182
+ ListWithContextFunc: func(_ context.Context, opts metav1.ListOptions) (runtime.Object, error) {
183
opts.FieldSelector = d.selectorField
184
opts.LabelSelector = d.selectorLabel
185
return pod.List(ctx, opts)
186
},
187
- WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) {
187
+ WatchFuncWithContext: func(_ context.Context, opts metav1.ListOptions) (watch.Interface, error) {
188
opts.FieldSelector = d.selectorField
189
opts.LabelSelector = d.selectorLabel
190
return pod.Watch(ctx, opts)
@@ -193,20 +193,20 @@ func (d *KubeDiscoverer) setupPodDiscoverer(ctx context.Context, ns string) *pod
193
194
cmap := d.client.CoreV1().ConfigMaps(ns)
195
cmapLW := &cache.ListWatch{
196
- ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) {
196
+ ListWithContextFunc: func(_ context.Context, opts metav1.ListOptions) (runtime.Object, error) {
197
return cmap.List(ctx, opts)
198
},
199
- WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) {
199
+ WatchFuncWithContext: func(_ context.Context, opts metav1.ListOptions) (watch.Interface, error) {
200
return cmap.Watch(ctx, opts)
201
},
202
}
203
204
secret := d.client.CoreV1().Secrets(ns)
205
secretLW := &cache.ListWatch{
206
- ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) {
206
+ ListWithContextFunc: func(_ context.Context, opts metav1.ListOptions) (runtime.Object, error) {
207
return secret.List(ctx, opts)
208
},
209
- WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) {
209
+ WatchFuncWithContext: func(_ context.Context, opts metav1.ListOptions) (watch.Interface, error) {
210
return secret.Watch(ctx, opts)
211
},
212
}
@@ -225,12 +225,12 @@ func (d *KubeDiscoverer) setupServiceDiscoverer(ctx context.Context, namespace s
225
svc := d.client.CoreV1().Services(namespace)
226
227
svcLW := &cache.ListWatch{
228
- ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) {
228
+ ListWithContextFunc: func(_ context.Context, opts metav1.ListOptions) (runtime.Object, error) {
229
opts.FieldSelector = d.selectorField
230
opts.LabelSelector = d.selectorLabel
231
return svc.List(ctx, opts)
232
},
233
- WatchFunc: func(opts metav1.ListOptions) (watch.Interface, error) {
233
+ WatchFuncWithContext: func(_ context.Context, opts metav1.ListOptions) (watch.Interface, error) {
234
opts.FieldSelector = d.selectorField
235
opts.LabelSelector = d.selectorLabel
236
return svc.Watch(ctx, opts)
src/go/plugin/go.d/agent/discovery/sd/discoverer/netlistensd/netlisteners.go
+3
-2
@@ -231,9 +231,10 @@ func (d *Discoverer) parseLocalListeners(bs []byte) ([]model.Target, error) {
231
continue
232
}
233
234
- if tgt.IPAddress == "0.0.0.0" {
234
+ switch tgt.IPAddress {
235
+ case "0.0.0.0":
236
tgt.IPAddress = local4
236
- } else if tgt.IPAddress == "::" {
237
+ case "::":
238
tgt.IPAddress = local6
239
}
240
src/go/plugin/go.d/agent/discovery/sd/discoverer/snmpsd/sim_test.go
-1
@@ -121,7 +121,6 @@ func sortTargetGroups(tggs []model.TargetGroup) {
121
}
122
123
type mockSnmpHandler struct {
124
- mu sync.Mutex
124
*snmpmock.MockHandler
125
skipOnConnect func(ip string) bool
126
}
src/go/plugin/go.d/agent/discovery/sd/model/tags.go
+1
-3
@@ -38,9 +38,7 @@ func (t Tags) Merge(tags Tags) {
38
func (t Tags) Add(tags Tags) {
39
for tag := range tags {
40
v := strings.TrimPrefix(tag, "-")
41
- if _, ok := t[v]; ok {
42
- delete(t, v)
43
- }
41
+ delete(t, v)
42
t[tag] = struct{}{}
43
}
44
}
src/go/plugin/go.d/collector/apcupsd/collect.go
+1
-1
@@ -130,7 +130,7 @@ func battdateSecondsAgo(battdate string) (int64, error) {
130
return 0, err
131
}
132
133
- secsAgo := int64(time.Now().Sub(date).Seconds())
133
+ secsAgo := int64(time.Since(date).Seconds())
134
135
return secsAgo, nil
136
}
src/go/plugin/go.d/collector/bind/collect.go
+1
-1
@@ -107,7 +107,7 @@ func (c *Collector) collectServerStats(metrics map[string]int64, stats *serverSt
107
}
108
}
109
110
- if !(c.permitView != nil && len(stats.Views) > 0) {
110
+ if c.permitView == nil || len(stats.Views) == 0 {
111
return
112
}
113
src/go/plugin/go.d/collector/boinc/client.go
+1
-1
@@ -129,7 +129,7 @@ func (c *boincClient) send(req *boincRequest) (*boincReply, error) {
129
}
130
131
if logger.Level.Enabled(slog.LevelDebug) {
132
- c.Debugf("received response: %s", string(b.Bytes()))
132
+ c.Debugf("received response: %s", b.String())
133
}
134
135
respData := cleanReplyData(b.Bytes())
src/go/plugin/go.d/collector/dnsquery/init.go
+3
-1
@@ -16,7 +16,9 @@ func (c *Collector) verifyConfig() error {
16
return errors.New("no domains specified")
17
}
18
19
- if !(c.Network == "" || c.Network == "udp" || c.Network == "tcp" || c.Network == "tcp-tls") {
19
+ switch c.Network {
20
+ case "", "udp", "tcp", "tcp-tls":
21
+ default:
22
return fmt.Errorf("wrong network transport : %s", c.Network)
23
}
24
src/go/plugin/go.d/collector/docker/collect.go
+3
-3
@@ -104,7 +104,7 @@ var (
104
)
105
106
func (c *Collector) collectContainers(mx map[string]int64) error {
107
- containerSet := make(map[string][]types.Container)
107
+ containerSet := make(map[string][]typesContainer.Summary)
108
109
for _, status := range containerHealthStatuses {
110
if err := func() error {
@@ -207,7 +207,7 @@ func (c *Collector) negotiateAPIVersion() {
207
c.client.NegotiateAPIVersion(ctx)
208
}
209
210
-func hasIgnoreLabel(cntr types.Container) bool {
211
- v, _ := cntr.Labels["netdata.cloud/ignore"]
210
+func hasIgnoreLabel(cntr typesContainer.Summary) bool {
211
+ v := cntr.Labels["netdata.cloud/ignore"]
212
return strings.EqualFold(v, "true") || strings.EqualFold(v, "yes")
213
}
src/go/plugin/go.d/collector/dockerhub/charts.go
+1
-1
@@ -64,7 +64,7 @@ var charts = Charts{
64
65
func addReposToCharts(repositories []string, cs *Charts) {
66
for _, name := range repositories {
67
- dimName := strings.Replace(name, "/", "_", -1)
67
+ dimName := strings.ReplaceAll(name, "/", "_")
68
_ = cs.Get("pulls").AddDim(&Dim{
69
ID: "pull_count_" + name,
70
Name: dimName,
src/go/plugin/go.d/collector/elasticsearch/collect.go
+5
-6
@@ -5,13 +5,12 @@ package elasticsearch
5
import (
6
"errors"
7
"fmt"
8
- "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/metrix"
9
- "math"
8
"slices"
9
"strconv"
10
"strings"
11
"sync"
12
13
+ "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/metrix"
14
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/stm"
15
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/web"
16
)
@@ -234,16 +233,16 @@ func convertIndexStoreSizeToBytes(size string) int64 {
233
switch {
234
case strings.HasSuffix(size, "kb"):
235
num, _ = strconv.ParseFloat(size[:len(size)-2], 64)
237
- num *= math.Pow(1024, 1)
236
+ num *= 1024
237
case strings.HasSuffix(size, "mb"):
238
num, _ = strconv.ParseFloat(size[:len(size)-2], 64)
240
- num *= math.Pow(1024, 2)
239
+ num *= 1024 * 1024
240
case strings.HasSuffix(size, "gb"):
241
num, _ = strconv.ParseFloat(size[:len(size)-2], 64)
243
- num *= math.Pow(1024, 3)
242
+ num *= 1024 * 1024 * 1024
243
case strings.HasSuffix(size, "tb"):
244
num, _ = strconv.ParseFloat(size[:len(size)-2], 64)
246
- num *= math.Pow(1024, 4)
245
+ num *= 1024 * 1024 * 1024 * 1024
246
case strings.HasSuffix(size, "b"):
247
num, _ = strconv.ParseFloat(size[:len(size)-1], 64)
248
}
src/go/plugin/go.d/collector/k8s_state/discover_kubernetes.go
+26
-10
@@ -114,19 +114,23 @@ var (
114
func (d *kubeDiscovery) setupDiscoverers(ctx context.Context) []discoverer {
115
node := d.client.CoreV1().Nodes()
116
nodeWatcher := &cache.ListWatch{
117
- ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { return node.List(ctx, options) },
118
- WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { return node.Watch(ctx, options) },
117
+ ListWithContextFunc: func(_ context.Context, options metav1.ListOptions) (runtime.Object, error) {
118
+ return node.List(ctx, options)
119
+ },
120
+ WatchFuncWithContext: func(_ context.Context, options metav1.ListOptions) (watch.Interface, error) {
121
+ return node.Watch(ctx, options)
122
+ },
123
}
124
125
pod := d.client.CoreV1().Pods(corev1.NamespaceAll)
126
podWatcher := &cache.ListWatch{
123
- ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
127
+ ListWithContextFunc: func(_ context.Context, options metav1.ListOptions) (runtime.Object, error) {
128
if myNodeName != "" {
129
options.FieldSelector = "spec.nodeName=" + myNodeName
130
}
131
return pod.List(ctx, options)
132
},
129
- WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
133
+ WatchFuncWithContext: func(_ context.Context, options metav1.ListOptions) (watch.Interface, error) {
134
if myNodeName != "" {
135
options.FieldSelector = "spec.nodeName=" + myNodeName
136
}
@@ -136,20 +140,32 @@ func (d *kubeDiscovery) setupDiscoverers(ctx context.Context) []discoverer {
140
141
deploy := d.client.AppsV1().Deployments(corev1.NamespaceAll)
142
deployWatcher := &cache.ListWatch{
139
- ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { return deploy.List(ctx, options) },
140
- WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { return deploy.Watch(ctx, options) },
143
+ ListWithContextFunc: func(_ context.Context, options metav1.ListOptions) (runtime.Object, error) {
144
+ return deploy.List(ctx, options)
145
+ },
146
+ WatchFuncWithContext: func(_ context.Context, options metav1.ListOptions) (watch.Interface, error) {
147
+ return deploy.Watch(ctx, options)
148
+ },
149
}
150
151
cj := d.client.BatchV1().CronJobs(corev1.NamespaceAll)
152
cjWatcher := &cache.ListWatch{
145
- ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { return cj.List(ctx, options) },
146
- WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { return cj.Watch(ctx, options) },
153
+ ListWithContextFunc: func(_ context.Context, options metav1.ListOptions) (runtime.Object, error) {
154
+ return cj.List(ctx, options)
155
+ },
156
+ WatchFuncWithContext: func(_ context.Context, options metav1.ListOptions) (watch.Interface, error) {
157
+ return cj.Watch(ctx, options)
158
+ },
159
}
160
161
jobs := d.client.BatchV1().Jobs(corev1.NamespaceAll)
162
jobsWatcher := &cache.ListWatch{
151
- ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { return jobs.List(ctx, options) },
152
- WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { return jobs.Watch(ctx, options) },
163
+ ListWithContextFunc: func(_ context.Context, options metav1.ListOptions) (runtime.Object, error) {
164
+ return jobs.List(ctx, options)
165
+ },
166
+ WatchFuncWithContext: func(_ context.Context, options metav1.ListOptions) (watch.Interface, error) {
167
+ return jobs.Watch(ctx, options)
168
+ },
169
}
170
171
return []discoverer{
src/go/plugin/go.d/collector/mysql/collector.go
-1
@@ -14,7 +14,6 @@ import (
14
15
"github.com/blang/semver/v4"
16
"github.com/go-sql-driver/mysql"
17
- _ "github.com/go-sql-driver/mysql"
17
18
"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module"
19
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/confopt"
src/go/plugin/go.d/collector/postgres/collector.go
-1
@@ -17,7 +17,6 @@ import (
17
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/metrix"
18
19
"github.com/jackc/pgx/v5/stdlib"
20
- _ "github.com/jackc/pgx/v5/stdlib"
20
)
21
22
//go:embed "config_schema.json"
src/go/plugin/go.d/collector/prometheus/charts.go
+1
-1
@@ -253,7 +253,7 @@ func getChartTitle(name, help string) string {
253
return fmt.Sprintf("Metric \"%s\"", name)
254
}
255
256
- help = strings.Replace(help, "'", "", -1)
256
+ help = strings.ReplaceAll(help, "'", "")
257
help = strings.TrimSuffix(help, ".")
258
259
return help
src/go/plugin/go.d/collector/samba/charts.go
+1
-1
@@ -115,7 +115,7 @@ func (c *Collector) addSmb2CallChart(smb2Call string, chart *module.Chart) {
115
}
116
117
func extractCallName(s, prefix, suffix string) (string, bool) {
118
- if !(strings.HasPrefix(s, prefix) && strings.HasSuffix(s, suffix)) {
118
+ if !strings.HasPrefix(s, prefix) || !strings.HasSuffix(s, suffix) {
119
return "", false
120
}
121
name := strings.TrimPrefix(s, prefix)
src/go/plugin/go.d/collector/samba/collect.go
+1
-1
@@ -50,7 +50,7 @@ func (c *Collector) collectSmbStatusProfile(mx map[string]int64, profileData []b
50
51
key, value = strings.TrimSpace(key), strings.TrimSpace(value)
52
53
- if !(strings.HasSuffix(key, "count") || strings.HasSuffix(key, "bytes")) {
53
+ if !strings.HasSuffix(key, "count") && !strings.HasSuffix(key, "bytes") {
54
continue
55
}
56
src/go/plugin/go.d/collector/varnish/collect.go
+1
-1
@@ -107,7 +107,7 @@ func (c *Collector) collectStatistics(mx map[string]int64, bs []byte) error {
107
for name := range c.seenStorages {
108
if !seenStorages[name] {
109
delete(c.seenStorages, name)
110
- c.removeBackendCharts(name)
110
+ c.removeStorageCharts(name)
111
}
112
}
113