Skip to content

⚠️ Refactor golangci-lint config to remove false negatives #4657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,9 @@ jobs:
golangci:
name: lint
runs-on: ubuntu-latest
strategy:
matrix:
working-directory:
- ""
- test/framework
- test/infrastructure/docker
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.38
working-directory: ${{matrix.working-directory}}
49 changes: 36 additions & 13 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ linters:
- deadcode
- depguard
- dogsled
- exportloopref
- errcheck
- goconst
- gocritic
Expand All @@ -23,11 +24,9 @@ linters:
- nolintlint
- prealloc
- rowserrcheck
- scopelint
- staticcheck
- structcheck
- stylecheck
- testpackage
- typecheck
- unconvert
- unparam
Expand All @@ -43,20 +42,44 @@ issues:
exclude-use-default: false
# List of regexps of issue texts to exclude, empty list by default.
exclude:
- Using the variable on range scope `(tc)|(rt)|(tt)|(test)|(testcase)|(testCase)` in function literal
- "G108: Profiling endpoint is automatically exposed on /debug/pprof"
- Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*print(f|ln)?|os\.(Un)?Setenv). is not checked
# The following are being worked on to remove their exclusion. This list should be reduced or go away all together over time.
# If it is decided they will not be addressed they should be moved above this comment.
- Subprocess launch(ed with variable|ing should be audited)
- (Expect directory permissions to be 0750 or less|Expect file permissions to be 0600 or less)
- (G104|G307)
- "G108: Profiling endpoint is automatically exposed on /debug/pprof"
- Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*print(f|ln)?|os\.(Un)?Setenv). is not checked
- exported method `.*.Reconcile` should have comment or be unexported
- exported method `.*.SetupWithManager` should have comment or be unexported
# The following are being worked on to remove their exclusion. This list should be reduced or go away all together over time.
# If it is decided they will not be addressed they should be moved above this comment.
- Subprocess launch(ed with variable|ing should be audited)
- (Expect directory permissions to be 0750 or less|Expect file permissions to be 0600 or less)
- (G104|G307)
- exported (method|function|type|const) (.+) should have comment or be unexported
exclude-rules:
# With Go 1.16, the new embed directive can be used with an un-named import,
# golint only allows these to be imported in a main.go, which wouldn't work for us.
# This directive allows the embed package to be imported with an underscore everywhere.
- linters:
- golint
source: _ "embed"
# Disable unparam "always receives" which might not be really
# useful when building libraries.
- linters:
- unparam
text: always receives
# Dot imports for gomega or ginkgo are allowed
# within test files.
- path: _test\.go
text: should not use dot imports
- path: _test\.go
text: cyclomatic complexity
- path: test/framework.*.go
text: should not use dot imports
- path: test/e2e.*.go
text: should not use dot imports

run:
timeout: 10m
skip-files:
- "zz_generated.*\\.go$"
- ".*conversion.*\\.go$"
- "zz_generated.*\\.go$"
- ".*conversion.*\\.go$"
skip-dirs:
- third_party
- third_party
allow-parallel-runners: true
9 changes: 0 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,7 @@ e2e-framework: ## Builds the CAPI e2e framework

.PHONY: lint
lint: $(GOLANGCI_LINT) ## Lint codebase
$(MAKE) -j8 lint-all

.PHONY: lint-all lint-core lint-e2e lint-capd
lint-all: lint-core lint-e2e lint-capd
lint-core:
$(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)
lint-e2e:
cd $(E2E_FRAMEWORK_DIR); $(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)
lint-capd:
cd $(CAPD_DIR); $(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)

.PHONY: lint-fix
lint-fix: $(GOLANGCI_LINT) ## Lint the codebase and run auto-fixers if supported by the linter.
Expand Down
3 changes: 3 additions & 0 deletions api/v1alpha3/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
)

const (
// ClusterFinalizer is the finalizer used by the cluster controller to
// cleanup the cluster resources when a Cluster is being deleted.
ClusterFinalizer = "cluster.cluster.x-k8s.io"
)

Expand Down Expand Up @@ -87,6 +89,7 @@ type ClusterNetwork struct {
// ANCHOR_END: ClusterNetwork

// ANCHOR: NetworkRanges

// NetworkRanges represents ranges of network addresses.
type NetworkRanges struct {
CIDRBlocks []string `json:"cidrBlocks"`
Expand Down
3 changes: 3 additions & 0 deletions api/v1alpha3/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@ const (
// MachineAddressType describes a valid MachineAddress type.
type MachineAddressType string

// Define all the constants related to MachineAddressType.
const (
MachineHostName MachineAddressType = "Hostname"
MachineExternalIP MachineAddressType = "ExternalIP"
MachineInternalIP MachineAddressType = "InternalIP"
MachineExternalDNS MachineAddressType = "ExternalDNS"
MachineInternalDNS MachineAddressType = "InternalDNS"
)

const (
// MachineNodeNameIndex is used by the Machine Controller to index Machines by Node name, and add a watch on Nodes.
MachineNodeNameIndex = "status.nodeRef.name"
)
Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha3/condition_consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const (
// Conditions and condition Reasons for the Cluster object

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

// TooManyUnhealthy is the reason used when too many Machines are unhealthy and the MachineHealthCheck is blocked
// TooManyUnhealthyReason is the reason used when too many Machines are unhealthy and the MachineHealthCheck is blocked
// from making any further remediations.
TooManyUnhealthyReason = "TooManyUnhealthy"
)
2 changes: 1 addition & 1 deletion api/v1alpha3/machinedeployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
type MachineDeploymentStrategyType string

const (
// Replace the old MachineSet by new one using rolling update
// RollingUpdateMachineDeploymentStrategyType replaces the old MachineSet by new one using rolling update
// i.e. gradually scale down the old MachineSet and scale up the new one.
RollingUpdateMachineDeploymentStrategyType MachineDeploymentStrategyType = "RollingUpdate"

Expand Down
4 changes: 4 additions & 0 deletions api/v1alpha4/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
)

const (
// ClusterFinalizer is the finalizer used by the cluster controller to
// cleanup the cluster resources when a Cluster is being deleted.
ClusterFinalizer = "cluster.cluster.x-k8s.io"
)

Expand Down Expand Up @@ -88,6 +90,7 @@ type ClusterNetwork struct {
// ANCHOR_END: ClusterNetwork

// ANCHOR: NetworkRanges

// NetworkRanges represents ranges of network addresses.
type NetworkRanges struct {
CIDRBlocks []string `json:"cidrBlocks"`
Expand Down Expand Up @@ -286,6 +289,7 @@ func ipFamilyForCIDRStrings(cidrs []string) (ClusterIPFamily, error) {

type ClusterIPFamily int

// Define the ClusterIPFamily constants.
const (
InvalidIPFamily ClusterIPFamily = iota
IPv4IPFamily
Expand Down
3 changes: 3 additions & 0 deletions api/v1alpha4/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,16 @@ var (
// MachineAddressType describes a valid MachineAddress type.
type MachineAddressType string

// Define the MachineAddressType constants.
const (
MachineHostName MachineAddressType = "Hostname"
MachineExternalIP MachineAddressType = "ExternalIP"
MachineInternalIP MachineAddressType = "InternalIP"
MachineExternalDNS MachineAddressType = "ExternalDNS"
MachineInternalDNS MachineAddressType = "InternalDNS"
)

const (
// MachineNodeNameIndex is used by the Machine Controller to index Machines by Node name, and add a watch on Nodes.
MachineNodeNameIndex = "status.nodeRef.name"
)
Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha4/condition_consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const (
// provider to report successful control plane initialization.
WaitingForControlPlaneProviderInitializedReason = "WaitingForControlPlaneProviderInitialized"

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

// TooManyUnhealthy is the reason used when too many Machines are unhealthy and the MachineHealthCheck is blocked
// TooManyUnhealthyReason is the reason used when too many Machines are unhealthy and the MachineHealthCheck is blocked
// from making any further remediations.
TooManyUnhealthyReason = "TooManyUnhealthy"
)
3 changes: 2 additions & 1 deletion api/v1alpha4/machine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ package v1alpha4

import (
"fmt"
"sigs.k8s.io/cluster-api/util/version"
"strings"

"sigs.k8s.io/cluster-api/util/version"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
Expand Down
5 changes: 4 additions & 1 deletion api/v1alpha4/machinedeployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
type MachineDeploymentStrategyType string

const (
// Replace the old MachineSet by new one using rolling update
// RollingUpdateMachineDeploymentStrategyType replaces the old MachineSet by new one using rolling update
// i.e. gradually scale down the old MachineSet and scale up the new one.
RollingUpdateMachineDeploymentStrategyType MachineDeploymentStrategyType = "RollingUpdate"

Expand All @@ -33,12 +33,15 @@ const (

// RevisionAnnotation is the revision annotation of a machine deployment's machine sets which records its rollout sequence.
RevisionAnnotation = "machinedeployment.clusters.x-k8s.io/revision"

// RevisionHistoryAnnotation maintains the history of all old revisions that a machine set has served for a machine deployment.
RevisionHistoryAnnotation = "machinedeployment.clusters.x-k8s.io/revision-history"

// DesiredReplicasAnnotation is the desired replicas for a machine deployment recorded as an annotation
// in its machine sets. Helps in separating scaling events from the rollout process and for
// determining if the new machine set for a deployment is really saturated.
DesiredReplicasAnnotation = "machinedeployment.clusters.x-k8s.io/desired-replicas"

// MaxReplicasAnnotation is the maximum replicas a deployment can have at a given point, which
// is machinedeployment.spec.replicas + maxSurge. Used by the underlying machine sets to estimate their
// proportions in case the deployment has surge replicas.
Expand Down
5 changes: 2 additions & 3 deletions api/v1alpha4/machinedeployment_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"k8s.io/apimachinery/pkg/labels"
runtime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
intstrutil "k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/pointer"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -100,7 +99,7 @@ func (m *MachineDeployment) validate(old *MachineDeployment) error {
}

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

if m.Spec.Strategy.RollingUpdate.MaxUnavailable != nil {
if _, err := intstrutil.GetScaledValueFromIntOrPercent(m.Spec.Strategy.RollingUpdate.MaxUnavailable, total, true); err != nil {
if _, err := intstr.GetScaledValueFromIntOrPercent(m.Spec.Strategy.RollingUpdate.MaxUnavailable, total, true); err != nil {
allErrs = append(
allErrs,
field.Invalid(field.NewPath("spec", "strategy", "rollingUpdate", "maxUnavailable"),
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha4/machineset_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (m *MachineSet) SetupWebhookWithManager(mgr ctrl.Manager) error {
var _ webhook.Defaulter = &MachineSet{}
var _ webhook.Validator = &MachineSet{}

// DefaultingFunction sets default MachineSet field values.
// Default sets default MachineSet field values.
func (m *MachineSet) Default() {
if m.Labels == nil {
m.Labels = make(map[string]string)
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/kubeadm/api/v1alpha3/kubeadmconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ type FileSource struct {
Secret SecretFileSource `json:"secret"`
}

// Adapts a Secret into a FileSource.
// SecretFileSource adapts a Secret into a FileSource.
//
// The contents of the target Secret's Data field will be presented
// as files using the keys in the Data field as the file names.
Expand Down
3 changes: 2 additions & 1 deletion bootstrap/kubeadm/api/v1alpha4/kubeadm_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,12 @@ type HostPathMount struct {
PathType corev1.HostPathType `json:"pathType,omitempty"`
}

// +kubebuilder:validation:Type=string
// BootstrapTokenString is a token of the format abcdef.abcdef0123456789 that is used
// for both validation of the practically of the API server from a joining node's point
// of view and as an authentication method for the node in the bootstrap phase of
// "kubeadm join". This token is and should be short-lived.
//
// +kubebuilder:validation:Type=string
type BootstrapTokenString struct {
ID string `json:"-"`
Secret string `json:"-"`
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/kubeadm/api/v1alpha4/kubeadmconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ type FileSource struct {
Secret SecretFileSource `json:"secret"`
}

// Adapts a Secret into a FileSource.
// SecretFileSource adapts a Secret into a FileSource.
//
// The contents of the target Secret's Data field will be presented
// as files using the keys in the Data field as the file names.
Expand Down
16 changes: 8 additions & 8 deletions bootstrap/kubeadm/api/v1alpha4/kubeadmconfig_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import (
)

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

func (c *KubeadmConfig) SetupWebhookWithManager(mgr ctrl.Manager) error {
Expand Down Expand Up @@ -71,7 +71,7 @@ func (c *KubeadmConfigSpec) validate(name string) error {
field.Invalid(
field.NewPath("spec", "files", fmt.Sprintf("%d", i)),
file,
ConflictingFileSourceMsg,
conflictingFileSourceMsg,
),
)
}
Expand All @@ -85,7 +85,7 @@ func (c *KubeadmConfigSpec) validate(name string) error {
field.Invalid(
field.NewPath("spec", "files", fmt.Sprintf("%d", i), "contentFrom", "secret", "name"),
file,
MissingSecretNameMsg,
missingSecretNameMsg,
),
)
}
Expand All @@ -95,7 +95,7 @@ func (c *KubeadmConfigSpec) validate(name string) error {
field.Invalid(
field.NewPath("spec", "files", fmt.Sprintf("%d", i), "contentFrom", "secret", "key"),
file,
MissingSecretKeyMsg,
missingSecretKeyMsg,
),
)
}
Expand All @@ -107,7 +107,7 @@ func (c *KubeadmConfigSpec) validate(name string) error {
field.Invalid(
field.NewPath("spec", "files", fmt.Sprintf("%d", i), "path"),
file,
PathConflictMsg,
pathConflictMsg,
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func TestKubeadmConfigReconciler_ReturnEarlyIfClusterInfraNotReady(t *testing.T)
machine := newMachine(cluster, "machine")
config := newKubeadmConfig(machine, "cfg")

//cluster infra not ready
// cluster infra not ready
cluster.Status = clusterv1.ClusterStatus{
InfrastructureReady: false,
}
Expand Down
Loading