Skip to content

Commit 3e86172

Browse files
authored
Enable additional golangci-lint checks (#3415)
* Enable low-churn golangci-lint checks * Enable staticcheck quick fixes
1 parent 7a23b6a commit 3e86172

40 files changed

Lines changed: 107 additions & 120 deletions

File tree

.golangci.toml

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,12 @@ custom-order = true
1515
[linters]
1616
default = 'all'
1717
disable = [
18-
'bodyclose',
19-
'containedctx',
2018
'contextcheck',
2119
'cyclop',
2220
'dupl',
23-
'dupword',
2421
'embeddedstructfieldcheck',
2522
'err113',
2623
'errcheck',
27-
'errchkjson',
2824
'exhaustive',
2925
'exhaustruct',
3026
'forcetypeassert',
@@ -39,7 +35,6 @@ disable = [
3935
'godoclint',
4036
'godot',
4137
'godox',
42-
'gomoddirectives',
4338
'gosec',
4439
'inamedparam',
4540
'interfacebloat',
@@ -49,27 +44,21 @@ disable = [
4944
'maintidx',
5045
'misspell',
5146
'mnd',
52-
'musttag',
5347
'nakedret',
5448
'nestif',
5549
'nilnil',
5650
'nlreturn',
57-
'nolintlint',
5851
'noinlineerr',
5952
'nonamedreturns',
6053
'paralleltest',
6154
'perfsprint',
6255
'revive',
63-
'rowserrcheck',
64-
'sqlclosecheck',
65-
'staticcheck',
6656
'tagliatelle',
6757
'testpackage',
6858
'thelper',
6959
'unparam',
7060
'usestdlibvars',
7161
'varnamelen',
72-
'wastedassign',
7362
'whitespace',
7463
'wrapcheck',
7564
'wsl',
@@ -108,6 +97,10 @@ enable = [
10897
[linters.settings.misspell]
10998
locale = 'US'
11099

100+
[linters.settings.staticcheck]
101+
# Keep correctness and quick-fix checks while deferring the existing style and dependency-deprecation backlog.
102+
checks = ['all', '-ST*', '-SA1019']
103+
111104
[linters.exclusions]
112105
generated = 'lax'
113106
paths = [
@@ -118,8 +111,5 @@ paths = [
118111
]
119112

120113
[issues]
121-
exclude-use-default = false
122-
max-per-linter = 0
114+
max-issues-per-linter = 0
123115
max-same-issues = 0
124-
exclude = []
125-

api/datadoghq/v2alpha1/datadogagent_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ type MetricMeta struct {
11291129
// LabelsFromPath adds additional labels where the value of the label is taken from a field under Path.
11301130
// +optional
11311131
LabelsFromPath map[string][]string `json:"labelsFromPath,omitempty" yaml:"labelsFromPath,omitempty"`
1132-
// Path is the path to to generate metric(s) for.
1132+
// Path is the path to generate metric(s) for.
11331133
Path []string `json:"path" yaml:"path"`
11341134
}
11351135

cmd/kubectl-datadog/agent/check/check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ func findErrors(statusJSON string) ([]string, bool, error) {
289289
for _, check := range status.RunnerStats.Checks {
290290
for checkName, stat := range check {
291291
if stat.LastError != "" {
292-
errMessage := ""
292+
var errMessage string
293293
lastError := []Error{}
294294
err := json.Unmarshal([]byte(stat.LastError), &lastError)
295295
if err != nil {

cmd/kubectl-datadog/autoscaling/cluster/common/helm/helm.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ func install(ctx context.Context, ac *action.Configuration, releaseName, namespa
5757
installAction.ReleaseName = releaseName
5858
installAction.CreateNamespace = true
5959
installAction.Namespace = namespace
60-
installAction.ChartPathOptions.Version = version
60+
installAction.Version = version
6161
installAction.Wait = true
6262
installAction.WaitForJobs = true
6363
installAction.Timeout = 30 * time.Minute
6464

6565
settings := cli.New()
6666
settings.SetNamespace(namespace)
6767

68-
chartPath, err := installAction.ChartPathOptions.LocateChart(chartRef, settings)
68+
chartPath, err := installAction.LocateChart(chartRef, settings)
6969
if err != nil {
7070
return fmt.Errorf("failed to locate chart %s: %w", chartRef, err)
7171
}
@@ -97,15 +97,15 @@ func upgrade(ctx context.Context, ac *action.Configuration, releaseName, namespa
9797

9898
upgradeAction := action.NewUpgrade(ac)
9999
upgradeAction.Namespace = namespace
100-
upgradeAction.ChartPathOptions.Version = version
100+
upgradeAction.Version = version
101101
upgradeAction.Wait = true
102102
upgradeAction.WaitForJobs = true
103103
upgradeAction.Timeout = 30 * time.Minute
104104

105105
settings := cli.New()
106106
settings.SetNamespace(namespace)
107107

108-
chartPath, err := upgradeAction.ChartPathOptions.LocateChart(chartRef, settings)
108+
chartPath, err := upgradeAction.LocateChart(chartRef, settings)
109109
if err != nil {
110110
return fmt.Errorf("failed to locate chart %s: %w", chartRef, err)
111111
}

cmd/kubectl-datadog/clusteragent/leader/leader.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,16 @@ func (o *options) run(cmd *cobra.Command) error {
109109

110110
// Try to get leader from lease if supported
111111
if useLease {
112-
fmt.Fprintln(o.IOStreams.Out, "Using lease for leader election")
112+
fmt.Fprintln(o.Out, "Using lease for leader election")
113113
leaderName, err = o.getLeaderFromLease(objKey)
114114
}
115115

116116
// Fall back to ConfigMap if lease is not supported or failed
117117
if !useLease || err != nil {
118118
if err != nil {
119-
fmt.Fprintln(o.IOStreams.Out, "Lease lookup failed, falling back to ConfigMap")
119+
fmt.Fprintln(o.Out, "Lease lookup failed, falling back to ConfigMap")
120120
}
121-
fmt.Fprintln(o.IOStreams.Out, "Using ConfigMap for leader election")
121+
fmt.Fprintln(o.Out, "Using ConfigMap for leader election")
122122
leaderName, err = o.getLeaderFromConfigMap(objKey)
123123
}
124124

cmd/kubectl-datadog/flare/flare.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ func (o *options) createCRFiles(dir string, cmd *cobra.Command) error {
254254
}
255255

256256
// Get custom resources yaml
257-
template, err := yaml.Marshal(ddList.Items)
257+
// Kubernetes API types use JSON tags, which this YAML marshaler honors.
258+
template, err := yaml.Marshal(ddList.Items) //nolint:musttag
258259
if err != nil {
259260
return err
260261
}

cmd/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import (
5858
"github.com/DataDog/datadog-operator/pkg/version"
5959

6060
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
61-
// nolint:gci
61+
//nolint:gci // Keep the scaffold marker in its own import section.
6262
// +kubebuilder:scaffold:imports
6363
)
6464

@@ -187,7 +187,7 @@ func (opts *options) Parse() {
187187
flag.BoolVar(&opts.untaintControllerWaitForCSIDriver, "untaintControllerWaitForCSIDriver", false,
188188
"When true (requires --untaintControllerEnabled), the Untaint controller removes the startup taint only after both the node Agent and Datadog CSI node-server pods are Ready. Requires Pod watch coverage of CSI namespaces (DD_CSIDRIVER_WATCH_NAMESPACE).")
189189
flag.BoolVar(&opts.rolloutOnConfigMapChangeEnabled, "rolloutOnConfigMapChangeEnabled", true,
190-
"Automatically roll out Agent/Cluster Agent/Cluster Check Runner/OTel Agent Gateway workloads when a ConfigMap referenced by their pod template changes content out-of-band")
190+
"Automatically roll out Agent, Cluster Agent, Cluster Check Runner, and OTel Agent Gateway workloads when a ConfigMap referenced by their pod template changes content out-of-band")
191191

192192
// DatadogAgentInternal
193193
flag.BoolVar(&opts.createControllerRevisions, "createControllerRevisions", false, "Enable creation of ControllerRevision snapshots on each DDA spec change")

cmd/yaml-mapper/mapper/map_processors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ var mapOverrideType = MappingProcessor{
282282
utils.MergeOrSet(interim, newPath, string(newPathVal))
283283

284284
case newType == "int":
285-
switch {
286-
case pathValType == "string":
285+
switch pathValType {
286+
case "string":
287287
convertedInt, convErr := strconv.Atoi(pathVal.(string))
288288
if convErr != nil {
289289
slog.Error("failed to convert string to int", "error", convErr)

cmd/yaml-mapper/mapper/mapper.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (m *Mapper) loadInputs() (mappingValues chartutil.Values, sourceValues char
113113
if err != nil {
114114
return nil, nil, err
115115
}
116-
m.MapConfig.SourcePath = tmpSourcePath
116+
m.SourcePath = tmpSourcePath
117117

118118
}
119119

@@ -124,11 +124,11 @@ func (m *Mapper) loadInputs() (mappingValues chartutil.Values, sourceValues char
124124

125125
// Ignore error so we can fall back on embedded mapping
126126
tmpMappingPath, _ = utils.FetchYAMLFile(defaultDDAMapUrl, tmpFile.Name())
127-
m.MapConfig.MappingPath = tmpMappingPath
127+
m.MappingPath = tmpMappingPath
128128
}
129129

130130
// Read mapping file
131-
mapping, err := os.ReadFile(m.MapConfig.MappingPath)
131+
mapping, err := os.ReadFile(m.MappingPath)
132132
if err != nil {
133133
// Fall back on embedded default mapping
134134
mapping = defaultDDAMap
@@ -139,7 +139,7 @@ func (m *Mapper) loadInputs() (mappingValues chartutil.Values, sourceValues char
139139
}
140140

141141
// Read source yaml file
142-
source, err := os.ReadFile(m.MapConfig.SourcePath)
142+
source, err := os.ReadFile(m.SourcePath)
143143
if err != nil {
144144
return nil, nil, err
145145
}
@@ -166,18 +166,18 @@ func (m *Mapper) loadInputs() (mappingValues chartutil.Values, sourceValues char
166166
// mapValues maps the Helm source Values to a DDA custom resource based on the provided mapping Values.
167167
func (m *Mapper) mapValues(sourceValues chartutil.Values, mappingValues chartutil.Values) (map[string]any, int) {
168168
var errorCount int
169-
var ddaName = m.MapConfig.DDAName
169+
var ddaName = m.DDAName
170170
var interim = map[string]any{}
171171

172-
if m.MapConfig.HeaderPath == "" {
172+
if m.HeaderPath == "" {
173173
interim = newDefaultFileHeader()
174174
if ddaName == "" {
175175
ddaName = "datadog"
176176
}
177177
utils.MergeOrSet(interim, "metadata.name", ddaName)
178178

179-
if m.MapConfig.Namespace != "" {
180-
utils.MergeOrSet(interim, "metadata.namespace", m.MapConfig.Namespace)
179+
if m.Namespace != "" {
180+
utils.MergeOrSet(interim, "metadata.namespace", m.Namespace)
181181
}
182182
}
183183

@@ -197,7 +197,7 @@ func (m *Mapper) mapValues(sourceValues chartutil.Values, mappingValues chartuti
197197

198198
// Map values.yaml => DDA
199199
for _, sourceKey := range mappingKeys {
200-
destKey, _ := mappingValues[sourceKey]
200+
destKey := mappingValues[sourceKey]
201201
// Only copy a whole table if none of its sub-keys are mapped separately
202202
// (e.g. agents.podSecurity.seLinuxContext.rule has its own mapping entry,
203203
// so seLinuxContext itself shouldn't be copied as one big table).
@@ -370,22 +370,22 @@ func (m *Mapper) updateMapping(sourceValues chartutil.Values, mappingValues char
370370
if e != nil {
371371
return e
372372
}
373-
if strings.HasPrefix(m.MapConfig.MappingPath, constants.DefaultDDAMappingPath) {
373+
if strings.HasPrefix(m.MappingPath, constants.DefaultDDAMappingPath) {
374374
newMapYaml = `# This file maps keys from the Datadog Helm chart (YAML) to the DatadogAgent CustomResource spec (YAML).
375375
` + newMapYaml
376376
}
377377

378-
if m.MapConfig.PrintOutput {
378+
if m.PrintOutput {
379379
os.Stdout.WriteString("\nUpdated mapping file:\n" + newMapYaml)
380380
}
381381

382-
e = os.WriteFile(m.MapConfig.MappingPath, []byte(newMapYaml), 0660)
382+
e = os.WriteFile(m.MappingPath, []byte(newMapYaml), 0660)
383383
if e != nil {
384384
slog.Error("failed to update mapping yaml", "error", e)
385385
return e
386386
}
387387

388-
slog.Info("mapping file successfully updated", "path", m.MapConfig.MappingPath)
388+
slog.Info("mapping file successfully updated", "path", m.MappingPath)
389389

390390
return nil
391391
}

config/crd/bases/v1/datadoghq.com_datadogagentinternals.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1804,7 +1804,7 @@ spec:
18041804
description: NilIsZero indicates that if a value is nil it will be treated as zero value.
18051805
type: boolean
18061806
path:
1807-
description: Path is the path to to generate metric(s) for.
1807+
description: Path is the path to generate metric(s) for.
18081808
items:
18091809
type: string
18101810
type: array
@@ -1830,7 +1830,7 @@ spec:
18301830
description: LabelsFromPath adds additional labels where the value of the label is taken from a field under Path.
18311831
type: object
18321832
path:
1833-
description: Path is the path to to generate metric(s) for.
1833+
description: Path is the path to generate metric(s) for.
18341834
items:
18351835
type: string
18361836
type: array
@@ -1856,7 +1856,7 @@ spec:
18561856
type: string
18571857
type: array
18581858
path:
1859-
description: Path is the path to to generate metric(s) for.
1859+
description: Path is the path to generate metric(s) for.
18601860
items:
18611861
type: string
18621862
type: array
@@ -10612,7 +10612,7 @@ spec:
1061210612
description: NilIsZero indicates that if a value is nil it will be treated as zero value.
1061310613
type: boolean
1061410614
path:
10615-
description: Path is the path to to generate metric(s) for.
10615+
description: Path is the path to generate metric(s) for.
1061610616
items:
1061710617
type: string
1061810618
type: array
@@ -10638,7 +10638,7 @@ spec:
1063810638
description: LabelsFromPath adds additional labels where the value of the label is taken from a field under Path.
1063910639
type: object
1064010640
path:
10641-
description: Path is the path to to generate metric(s) for.
10641+
description: Path is the path to generate metric(s) for.
1064210642
items:
1064310643
type: string
1064410644
type: array
@@ -10664,7 +10664,7 @@ spec:
1066410664
type: string
1066510665
type: array
1066610666
path:
10667-
description: Path is the path to to generate metric(s) for.
10667+
description: Path is the path to generate metric(s) for.
1066810668
items:
1066910669
type: string
1067010670
type: array

0 commit comments

Comments
 (0)