Skip to content

Commit fca5298

Browse files
authored
Merge pull request #4657 from vincepri/golangci-refactor
⚠️ Refactor golangci-lint config to remove false negatives
2 parents d6b962c + af33e43 commit fca5298

File tree

138 files changed

+499
-445
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

138 files changed

+499
-445
lines changed

.github/workflows/golangci-lint.yml

-7
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,9 @@ jobs:
99
golangci:
1010
name: lint
1111
runs-on: ubuntu-latest
12-
strategy:
13-
matrix:
14-
working-directory:
15-
- ""
16-
- test/framework
17-
- test/infrastructure/docker
1812
steps:
1913
- uses: actions/checkout@v2
2014
- name: golangci-lint
2115
uses: golangci/golangci-lint-action@v2
2216
with:
2317
version: v1.38
24-
working-directory: ${{matrix.working-directory}}

.golangci.yml

+36-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ linters:
66
- deadcode
77
- depguard
88
- dogsled
9+
- exportloopref
910
- errcheck
1011
- goconst
1112
- gocritic
@@ -23,11 +24,9 @@ linters:
2324
- nolintlint
2425
- prealloc
2526
- rowserrcheck
26-
- scopelint
2727
- staticcheck
2828
- structcheck
2929
- stylecheck
30-
- testpackage
3130
- typecheck
3231
- unconvert
3332
- unparam
@@ -43,20 +42,44 @@ issues:
4342
exclude-use-default: false
4443
# List of regexps of issue texts to exclude, empty list by default.
4544
exclude:
46-
- Using the variable on range scope `(tc)|(rt)|(tt)|(test)|(testcase)|(testCase)` in function literal
47-
- "G108: Profiling endpoint is automatically exposed on /debug/pprof"
48-
- Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*print(f|ln)?|os\.(Un)?Setenv). is not checked
49-
# The following are being worked on to remove their exclusion. This list should be reduced or go away all together over time.
50-
# If it is decided they will not be addressed they should be moved above this comment.
51-
- Subprocess launch(ed with variable|ing should be audited)
52-
- (Expect directory permissions to be 0750 or less|Expect file permissions to be 0600 or less)
53-
- (G104|G307)
45+
- "G108: Profiling endpoint is automatically exposed on /debug/pprof"
46+
- Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*print(f|ln)?|os\.(Un)?Setenv). is not checked
47+
- exported method `.*.Reconcile` should have comment or be unexported
48+
- exported method `.*.SetupWithManager` should have comment or be unexported
49+
# The following are being worked on to remove their exclusion. This list should be reduced or go away all together over time.
50+
# If it is decided they will not be addressed they should be moved above this comment.
51+
- Subprocess launch(ed with variable|ing should be audited)
52+
- (Expect directory permissions to be 0750 or less|Expect file permissions to be 0600 or less)
53+
- (G104|G307)
54+
- exported (method|function|type|const) (.+) should have comment or be unexported
55+
exclude-rules:
56+
# With Go 1.16, the new embed directive can be used with an un-named import,
57+
# golint only allows these to be imported in a main.go, which wouldn't work for us.
58+
# This directive allows the embed package to be imported with an underscore everywhere.
59+
- linters:
60+
- golint
61+
source: _ "embed"
62+
# Disable unparam "always receives" which might not be really
63+
# useful when building libraries.
64+
- linters:
65+
- unparam
66+
text: always receives
67+
# Dot imports for gomega or ginkgo are allowed
68+
# within test files.
69+
- path: _test\.go
70+
text: should not use dot imports
71+
- path: _test\.go
72+
text: cyclomatic complexity
73+
- path: test/framework.*.go
74+
text: should not use dot imports
75+
- path: test/e2e.*.go
76+
text: should not use dot imports
5477

5578
run:
5679
timeout: 10m
5780
skip-files:
58-
- "zz_generated.*\\.go$"
59-
- ".*conversion.*\\.go$"
81+
- "zz_generated.*\\.go$"
82+
- ".*conversion.*\\.go$"
6083
skip-dirs:
61-
- third_party
84+
- third_party
6285
allow-parallel-runners: true

Makefile

-9
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,7 @@ e2e-framework: ## Builds the CAPI e2e framework
215215

216216
.PHONY: lint
217217
lint: $(GOLANGCI_LINT) ## Lint codebase
218-
$(MAKE) -j8 lint-all
219-
220-
.PHONY: lint-all lint-core lint-e2e lint-capd
221-
lint-all: lint-core lint-e2e lint-capd
222-
lint-core:
223218
$(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)
224-
lint-e2e:
225-
cd $(E2E_FRAMEWORK_DIR); $(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)
226-
lint-capd:
227-
cd $(CAPD_DIR); $(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)
228219

229220
.PHONY: lint-fix
230221
lint-fix: $(GOLANGCI_LINT) ## Lint the codebase and run auto-fixers if supported by the linter.

api/v1alpha3/cluster_types.go

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
)
3030

3131
const (
32+
// ClusterFinalizer is the finalizer used by the cluster controller to
33+
// cleanup the cluster resources when a Cluster is being deleted.
3234
ClusterFinalizer = "cluster.cluster.x-k8s.io"
3335
)
3436

@@ -87,6 +89,7 @@ type ClusterNetwork struct {
8789
// ANCHOR_END: ClusterNetwork
8890

8991
// ANCHOR: NetworkRanges
92+
9093
// NetworkRanges represents ranges of network addresses.
9194
type NetworkRanges struct {
9295
CIDRBlocks []string `json:"cidrBlocks"`

api/v1alpha3/common_types.go

+3
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,16 @@ const (
5353
// MachineAddressType describes a valid MachineAddress type.
5454
type MachineAddressType string
5555

56+
// Define all the constants related to MachineAddressType.
5657
const (
5758
MachineHostName MachineAddressType = "Hostname"
5859
MachineExternalIP MachineAddressType = "ExternalIP"
5960
MachineInternalIP MachineAddressType = "InternalIP"
6061
MachineExternalDNS MachineAddressType = "ExternalDNS"
6162
MachineInternalDNS MachineAddressType = "InternalDNS"
63+
)
6264

65+
const (
6366
// MachineNodeNameIndex is used by the Machine Controller to index Machines by Node name, and add a watch on Nodes.
6467
MachineNodeNameIndex = "status.nodeRef.name"
6568
)

api/v1alpha3/condition_consts.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const (
5555
// Conditions and condition Reasons for the Cluster object
5656

5757
const (
58-
// ControlPlaneReady reports the ready condition from the control plane object defined for this cluster.
58+
// ControlPlaneReadyCondition reports the ready condition from the control plane object defined for this cluster.
5959
// This condition is mirrored from the Ready condition in the control plane ref object, and
6060
// the absence of this condition might signal problems in the reconcile external loops or the fact that
6161
// the control plane provider does not not implements the Ready condition yet.
@@ -173,7 +173,7 @@ const (
173173
// allowed to remediate any Machines or whether it is blocked from remediating any further.
174174
RemediationAllowedCondition ConditionType = "RemediationAllowed"
175175

176-
// TooManyUnhealthy is the reason used when too many Machines are unhealthy and the MachineHealthCheck is blocked
176+
// TooManyUnhealthyReason is the reason used when too many Machines are unhealthy and the MachineHealthCheck is blocked
177177
// from making any further remediations.
178178
TooManyUnhealthyReason = "TooManyUnhealthy"
179179
)

api/v1alpha3/machinedeployment_types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
type MachineDeploymentStrategyType string
2525

2626
const (
27-
// Replace the old MachineSet by new one using rolling update
27+
// RollingUpdateMachineDeploymentStrategyType replaces the old MachineSet by new one using rolling update
2828
// i.e. gradually scale down the old MachineSet and scale up the new one.
2929
RollingUpdateMachineDeploymentStrategyType MachineDeploymentStrategyType = "RollingUpdate"
3030

api/v1alpha4/cluster_types.go

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import (
3030
)
3131

3232
const (
33+
// ClusterFinalizer is the finalizer used by the cluster controller to
34+
// cleanup the cluster resources when a Cluster is being deleted.
3335
ClusterFinalizer = "cluster.cluster.x-k8s.io"
3436
)
3537

@@ -88,6 +90,7 @@ type ClusterNetwork struct {
8890
// ANCHOR_END: ClusterNetwork
8991

9092
// ANCHOR: NetworkRanges
93+
9194
// NetworkRanges represents ranges of network addresses.
9295
type NetworkRanges struct {
9396
CIDRBlocks []string `json:"cidrBlocks"`
@@ -286,6 +289,7 @@ func ipFamilyForCIDRStrings(cidrs []string) (ClusterIPFamily, error) {
286289

287290
type ClusterIPFamily int
288291

292+
// Define the ClusterIPFamily constants.
289293
const (
290294
InvalidIPFamily ClusterIPFamily = iota
291295
IPv4IPFamily

api/v1alpha4/common_types.go

+3
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,16 @@ var (
102102
// MachineAddressType describes a valid MachineAddress type.
103103
type MachineAddressType string
104104

105+
// Define the MachineAddressType constants.
105106
const (
106107
MachineHostName MachineAddressType = "Hostname"
107108
MachineExternalIP MachineAddressType = "ExternalIP"
108109
MachineInternalIP MachineAddressType = "InternalIP"
109110
MachineExternalDNS MachineAddressType = "ExternalDNS"
110111
MachineInternalDNS MachineAddressType = "InternalDNS"
112+
)
111113

114+
const (
112115
// MachineNodeNameIndex is used by the Machine Controller to index Machines by Node name, and add a watch on Nodes.
113116
MachineNodeNameIndex = "status.nodeRef.name"
114117
)

api/v1alpha4/condition_consts.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const (
7272
// provider to report successful control plane initialization.
7373
WaitingForControlPlaneProviderInitializedReason = "WaitingForControlPlaneProviderInitialized"
7474

75-
// ControlPlaneReady reports the ready condition from the control plane object defined for this cluster.
75+
// ControlPlaneReadyCondition reports the ready condition from the control plane object defined for this cluster.
7676
// This condition is mirrored from the Ready condition in the control plane ref object, and
7777
// the absence of this condition might signal problems in the reconcile external loops or the fact that
7878
// the control plane provider does not not implements the Ready condition yet.
@@ -197,7 +197,7 @@ const (
197197
// allowed to remediate any Machines or whether it is blocked from remediating any further.
198198
RemediationAllowedCondition ConditionType = "RemediationAllowed"
199199

200-
// TooManyUnhealthy is the reason used when too many Machines are unhealthy and the MachineHealthCheck is blocked
200+
// TooManyUnhealthyReason is the reason used when too many Machines are unhealthy and the MachineHealthCheck is blocked
201201
// from making any further remediations.
202202
TooManyUnhealthyReason = "TooManyUnhealthy"
203203
)

api/v1alpha4/machine_webhook.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ package v1alpha4
1818

1919
import (
2020
"fmt"
21-
"sigs.k8s.io/cluster-api/util/version"
2221
"strings"
2322

23+
"sigs.k8s.io/cluster-api/util/version"
24+
2425
apierrors "k8s.io/apimachinery/pkg/api/errors"
2526
"k8s.io/apimachinery/pkg/runtime"
2627
"k8s.io/apimachinery/pkg/util/validation/field"

api/v1alpha4/machinedeployment_types.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
type MachineDeploymentStrategyType string
2525

2626
const (
27-
// Replace the old MachineSet by new one using rolling update
27+
// RollingUpdateMachineDeploymentStrategyType replaces the old MachineSet by new one using rolling update
2828
// i.e. gradually scale down the old MachineSet and scale up the new one.
2929
RollingUpdateMachineDeploymentStrategyType MachineDeploymentStrategyType = "RollingUpdate"
3030

@@ -33,12 +33,15 @@ const (
3333

3434
// RevisionAnnotation is the revision annotation of a machine deployment's machine sets which records its rollout sequence.
3535
RevisionAnnotation = "machinedeployment.clusters.x-k8s.io/revision"
36+
3637
// RevisionHistoryAnnotation maintains the history of all old revisions that a machine set has served for a machine deployment.
3738
RevisionHistoryAnnotation = "machinedeployment.clusters.x-k8s.io/revision-history"
39+
3840
// DesiredReplicasAnnotation is the desired replicas for a machine deployment recorded as an annotation
3941
// in its machine sets. Helps in separating scaling events from the rollout process and for
4042
// determining if the new machine set for a deployment is really saturated.
4143
DesiredReplicasAnnotation = "machinedeployment.clusters.x-k8s.io/desired-replicas"
44+
4245
// MaxReplicasAnnotation is the maximum replicas a deployment can have at a given point, which
4346
// is machinedeployment.spec.replicas + maxSurge. Used by the underlying machine sets to estimate their
4447
// proportions in case the deployment has surge replicas.

api/v1alpha4/machinedeployment_webhook.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"k8s.io/apimachinery/pkg/labels"
2525
runtime "k8s.io/apimachinery/pkg/runtime"
2626
"k8s.io/apimachinery/pkg/util/intstr"
27-
intstrutil "k8s.io/apimachinery/pkg/util/intstr"
2827
"k8s.io/apimachinery/pkg/util/validation/field"
2928
"k8s.io/utils/pointer"
3029
ctrl "sigs.k8s.io/controller-runtime"
@@ -100,7 +99,7 @@ func (m *MachineDeployment) validate(old *MachineDeployment) error {
10099
}
101100

102101
if m.Spec.Strategy.RollingUpdate.MaxSurge != nil {
103-
if _, err := intstrutil.GetScaledValueFromIntOrPercent(m.Spec.Strategy.RollingUpdate.MaxSurge, total, true); err != nil {
102+
if _, err := intstr.GetScaledValueFromIntOrPercent(m.Spec.Strategy.RollingUpdate.MaxSurge, total, true); err != nil {
104103
allErrs = append(
105104
allErrs,
106105
field.Invalid(field.NewPath("spec", "strategy", "rollingUpdate", "maxSurge"),
@@ -110,7 +109,7 @@ func (m *MachineDeployment) validate(old *MachineDeployment) error {
110109
}
111110

112111
if m.Spec.Strategy.RollingUpdate.MaxUnavailable != nil {
113-
if _, err := intstrutil.GetScaledValueFromIntOrPercent(m.Spec.Strategy.RollingUpdate.MaxUnavailable, total, true); err != nil {
112+
if _, err := intstr.GetScaledValueFromIntOrPercent(m.Spec.Strategy.RollingUpdate.MaxUnavailable, total, true); err != nil {
114113
allErrs = append(
115114
allErrs,
116115
field.Invalid(field.NewPath("spec", "strategy", "rollingUpdate", "maxUnavailable"),

api/v1alpha4/machineset_webhook.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (m *MachineSet) SetupWebhookWithManager(mgr ctrl.Manager) error {
4040
var _ webhook.Defaulter = &MachineSet{}
4141
var _ webhook.Validator = &MachineSet{}
4242

43-
// DefaultingFunction sets default MachineSet field values.
43+
// Default sets default MachineSet field values.
4444
func (m *MachineSet) Default() {
4545
if m.Labels == nil {
4646
m.Labels = make(map[string]string)

bootstrap/kubeadm/api/v1alpha3/kubeadmconfig_types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ type FileSource struct {
211211
Secret SecretFileSource `json:"secret"`
212212
}
213213

214-
// Adapts a Secret into a FileSource.
214+
// SecretFileSource adapts a Secret into a FileSource.
215215
//
216216
// The contents of the target Secret's Data field will be presented
217217
// as files using the keys in the Data field as the file names.

bootstrap/kubeadm/api/v1alpha4/kubeadm_types.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,12 @@ type HostPathMount struct {
412412
PathType corev1.HostPathType `json:"pathType,omitempty"`
413413
}
414414

415-
// +kubebuilder:validation:Type=string
416415
// BootstrapTokenString is a token of the format abcdef.abcdef0123456789 that is used
417416
// for both validation of the practically of the API server from a joining node's point
418417
// of view and as an authentication method for the node in the bootstrap phase of
419418
// "kubeadm join". This token is and should be short-lived.
419+
//
420+
// +kubebuilder:validation:Type=string
420421
type BootstrapTokenString struct {
421422
ID string `json:"-"`
422423
Secret string `json:"-"`

bootstrap/kubeadm/api/v1alpha4/kubeadmconfig_types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ type FileSource struct {
204204
Secret SecretFileSource `json:"secret"`
205205
}
206206

207-
// Adapts a Secret into a FileSource.
207+
// SecretFileSource adapts a Secret into a FileSource.
208208
//
209209
// The contents of the target Secret's Data field will be presented
210210
// as files using the keys in the Data field as the file names.

bootstrap/kubeadm/api/v1alpha4/kubeadmconfig_webhook.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ import (
2727
)
2828

2929
var (
30-
ConflictingFileSourceMsg = "only one of content or contentFrom may be specified for a single file"
31-
MissingSecretNameMsg = "secret file source must specify non-empty secret name"
32-
MissingSecretKeyMsg = "secret file source must specify non-empty secret key"
33-
PathConflictMsg = "path property must be unique among all files"
30+
conflictingFileSourceMsg = "only one of content or contentFrom may be specified for a single file"
31+
missingSecretNameMsg = "secret file source must specify non-empty secret name"
32+
missingSecretKeyMsg = "secret file source must specify non-empty secret key"
33+
pathConflictMsg = "path property must be unique among all files"
3434
)
3535

3636
func (c *KubeadmConfig) SetupWebhookWithManager(mgr ctrl.Manager) error {
@@ -71,7 +71,7 @@ func (c *KubeadmConfigSpec) validate(name string) error {
7171
field.Invalid(
7272
field.NewPath("spec", "files", fmt.Sprintf("%d", i)),
7373
file,
74-
ConflictingFileSourceMsg,
74+
conflictingFileSourceMsg,
7575
),
7676
)
7777
}
@@ -85,7 +85,7 @@ func (c *KubeadmConfigSpec) validate(name string) error {
8585
field.Invalid(
8686
field.NewPath("spec", "files", fmt.Sprintf("%d", i), "contentFrom", "secret", "name"),
8787
file,
88-
MissingSecretNameMsg,
88+
missingSecretNameMsg,
8989
),
9090
)
9191
}
@@ -95,7 +95,7 @@ func (c *KubeadmConfigSpec) validate(name string) error {
9595
field.Invalid(
9696
field.NewPath("spec", "files", fmt.Sprintf("%d", i), "contentFrom", "secret", "key"),
9797
file,
98-
MissingSecretKeyMsg,
98+
missingSecretKeyMsg,
9999
),
100100
)
101101
}
@@ -107,7 +107,7 @@ func (c *KubeadmConfigSpec) validate(name string) error {
107107
field.Invalid(
108108
field.NewPath("spec", "files", fmt.Sprintf("%d", i), "path"),
109109
file,
110-
PathConflictMsg,
110+
pathConflictMsg,
111111
),
112112
)
113113
}

bootstrap/kubeadm/controllers/kubeadmconfig_controller_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func TestKubeadmConfigReconciler_ReturnEarlyIfClusterInfraNotReady(t *testing.T)
190190
machine := newMachine(cluster, "machine")
191191
config := newKubeadmConfig(machine, "cfg")
192192

193-
//cluster infra not ready
193+
// cluster infra not ready
194194
cluster.Status = clusterv1.ClusterStatus{
195195
InfrastructureReady: false,
196196
}

0 commit comments

Comments
 (0)