Skip to content

Commit

Permalink
Merge pull request #111 from Fedosin/golangci-lint
Browse files Browse the repository at this point in the history
🌱 Implement advanced linting
  • Loading branch information
k8s-ci-robot authored Sep 19, 2023
2 parents a31234e + 26935d6 commit fdd10da
Show file tree
Hide file tree
Showing 23 changed files with 276 additions and 130 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@08e2f20817b15149a52b5b3ebe7de50aff2ba8c5 # tag=v3.4.0
with:
version: v1.52.1
version: v1.54.1
working-directory: ${{matrix.working-directory}}
args: --timeout=5m
args: --timeout=5m
140 changes: 140 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
run:
timeout: 5m
go: "1.20"
skip-files:
- "zz_generated.*\\.go$"
allow-parallel-runners: true

linters:
disable-all: true
# Enable specific linter
# https://golangci-lint.run/usage/linters/#enabled-by-default-linters
enable:
# Default linters
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
# Additional linters
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- contextcheck
- dogsled
- durationcheck
- errchkjson
- errname
- errorlint
- exhaustive
- exportloopref
- forcetypeassert
- gci
- ginkgolinter
- goconst
- gocritic
- gocyclo
- godot
- gofmt
- gofumpt
- goheader
- goimports
- goprintffuncname
- importas
- makezero
- misspell
- nakedret
- nilerr
- nlreturn
- noctx
- nolintlint
- prealloc
- predeclared
- promlinter
- reassign
- rowserrcheck
- sqlclosecheck
- stylecheck
- tenv
- testableexamples
- thelper
- tparallel
- unconvert
- unparam
- usestdlibvars
- wastedassign
- whitespace

linters-settings:
goheader:
values:
regexp:
license-year: (202[0-9]|20[3-9][0-9])
template: |-
Copyright {{license-year}} The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
nlreturn:
block-size: 2
stylecheck:
# https://staticcheck.io/docs/options#checks
checks: ["all", "-ST1000", "-ST1003", "-ST1016"]
dot-import-whitelist:
- "github.com/onsi/gomega"
importas:
no-unaliased: true
alias:
# Kubernetes
- pkg: k8s.io/api/core/v1
alias: corev1
- pkg: k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1
alias: apiextensionsv1
- pkg: k8s.io/apimachinery/pkg/apis/meta/v1
alias: metav1
- pkg: k8s.io/apimachinery/pkg/api/errors
alias: apierrors
- pkg: k8s.io/apimachinery/pkg/util/errors
alias: kerrors
- pkg: k8s.io/apimachinery/pkg/util/runtime
alias: utilruntime
# Controller Runtime
- pkg: sigs.k8s.io/controller-runtime
alias: ctrl
# CAPI
- pkg: sigs.k8s.io/cluster-api/api/v1beta1
alias: clusterv1
- pkg: sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3
alias: clusterctlv1
- pkg: sigs.k8s.io/cluster-api/cmd/clusterctl/client/config
alias: configclient
# CAPI Add-on Provider Helm
- pkg: sigs.k8s.io/cluster-api-addon-provider-helm
alias: addonsv1alpha1
issues:
exclude:
# Not all platforms are supported by this provider, those which aren't
# supported will be caught by the default case in the switches.
- "missing cases in switch of type v1.PlatformType: (\\.*)"
exclude-use-default: false
exclude-rules:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- gosec
- path: hack/boilerplate
linters:
- goheader
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ $(YQ):
$(GOLANGCI_LINT): .github/workflows/golangci-lint.yml # Download golangci-lint using hack script into tools folder.
hack/ensure-golangci-lint.sh \
-b $(TOOLS_BIN_DIR) \
$(shell cat .github/workflows/golangci-lint.yml | grep [[:space:]]version | sed 's/.*version: //')
$(shell yq e '.jobs.golangci.steps[] | select(.name? == "golangci-lint") | .with.version' .github/workflows/golangci-lint.yml )

$(GINKGO): # Build ginkgo from tools folder.
GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) $(GINKGO_PKG) $(GINKGO_BIN) $(GINGKO_VER)
4 changes: 2 additions & 2 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (
)

var (
// GroupVersion is group version used to register these objects
// GroupVersion is group version used to register these objects.
GroupVersion = schema.GroupVersion{Group: "addons.cluster.x-k8s.io", Version: "v1alpha1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
// SchemeBuilder is used to add go types to the GroupVersionKind scheme.
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha1/helmchartproxy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ type HelmChartProxyStatus struct {
// +kubebuilder:printcolumn:name="Message",type="string",priority=1,JSONPath=".status.conditions[?(@.type=='Ready')].message"
// +kubebuilder:resource:shortName=hcp

// HelmChartProxy is the Schema for the helmchartproxies API
// HelmChartProxy is the Schema for the helmchartproxies API.
type HelmChartProxy struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand All @@ -206,7 +206,7 @@ type HelmChartProxy struct {

//+kubebuilder:object:root=true

// HelmChartProxyList contains a list of HelmChartProxy
// HelmChartProxyList contains a list of HelmChartProxy.
type HelmChartProxyList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Expand Down
18 changes: 9 additions & 9 deletions api/v1alpha1/helmchartproxy_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"net/url"
"time"

metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -45,9 +45,9 @@ func (r *HelmChartProxy) SetupWebhookWithManager(mgr ctrl.Manager) error {

var _ webhook.Defaulter = &HelmChartProxy{}

const helmTimeoutSeconds = time.Second * 600
const helmTimeout = time.Second * 600

// Default implements webhook.Defaulter so a webhook will be registered for the type
// Default implements webhook.Defaulter so a webhook will be registered for the type.
func (p *HelmChartProxy) Default() {
helmchartproxylog.Info("default", "name", p.Name)

Expand All @@ -57,7 +57,7 @@ func (p *HelmChartProxy) Default() {

// If 'Wait' is set, we need to set default 'Timeout' to make install successful.
if p.Spec.Options != nil && p.Spec.Options.Wait && p.Spec.Options.Timeout == nil {
p.Spec.Options.Timeout = &metaV1.Duration{Duration: helmTimeoutSeconds}
p.Spec.Options.Timeout = &metav1.Duration{Duration: helmTimeout}
}

// If 'CreateNamespace' is not specified by user, set default value to 'true'
Expand All @@ -75,7 +75,7 @@ func (p *HelmChartProxy) Default() {

var _ webhook.Validator = &HelmChartProxy{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (r *HelmChartProxy) ValidateCreate() (admission.Warnings, error) {
helmchartproxylog.Info("validate create", "name", r.Name)

Expand All @@ -86,7 +86,7 @@ func (r *HelmChartProxy) ValidateCreate() (admission.Warnings, error) {
return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (r *HelmChartProxy) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
helmchartproxylog.Info("validate update", "name", r.Name)

Expand All @@ -97,19 +97,19 @@ func (r *HelmChartProxy) ValidateUpdate(old runtime.Object) (admission.Warnings,
return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (r *HelmChartProxy) ValidateDelete() (admission.Warnings, error) {
helmchartproxylog.Info("validate delete", "name", r.Name)

// TODO(user): fill in your validation logic upon object deletion.
return nil, nil
}

// isUrlValid returns true if specifed repoURL is valid as per go doc https://pkg.go.dev/net/url#ParseRequestURI.
// isUrlValid returns true if specified repoURL is valid as per go doc https://pkg.go.dev/net/url#ParseRequestURI.
func isUrlValid(repoURL string) error {
_, err := url.ParseRequestURI(repoURL)
if err != nil {
return fmt.Errorf("specified repoURL: %s is not valid. Error - %s", repoURL, err)
return fmt.Errorf("specified repoURL %s is not valid: %w", repoURL, err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha1/helmreleaseproxy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type HelmReleaseProxyStatus struct {
// +kubebuilder:printcolumn:name="Revision",type="string",JSONPath=".status.revision"
// +kubebuilder:resource:shortName=hrp

// HelmReleaseProxy is the Schema for the helmreleaseproxies API
// HelmReleaseProxy is the Schema for the helmreleaseproxies API.
type HelmReleaseProxy struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand All @@ -108,7 +108,7 @@ type HelmReleaseProxy struct {

//+kubebuilder:object:root=true

// HelmReleaseProxyList contains a list of HelmReleaseProxy
// HelmReleaseProxyList contains a list of HelmReleaseProxy.
type HelmReleaseProxyList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Expand Down
17 changes: 10 additions & 7 deletions api/v1alpha1/helmreleaseproxy_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ limitations under the License.
package v1alpha1

import (
"fmt"
"reflect"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

apierrors "k8s.io/apimachinery/pkg/api/errors"
)

// log is for logging in this package.
Expand All @@ -44,7 +44,7 @@ func (r *HelmReleaseProxy) SetupWebhookWithManager(mgr ctrl.Manager) error {

var _ webhook.Defaulter = &HelmReleaseProxy{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
// Default implements webhook.Defaulter so a webhook will be registered for the type.
func (p *HelmReleaseProxy) Default() {
helmreleaseproxylog.Info("default", "name", p.Name)

Expand All @@ -58,20 +58,23 @@ func (p *HelmReleaseProxy) Default() {

var _ webhook.Validator = &HelmReleaseProxy{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (r *HelmReleaseProxy) ValidateCreate() (admission.Warnings, error) {
helmreleaseproxylog.Info("validate create", "name", r.Name)

// TODO(user): fill in your validation logic upon object creation.
return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (r *HelmReleaseProxy) ValidateUpdate(oldRaw runtime.Object) (admission.Warnings, error) {
helmreleaseproxylog.Info("validate update", "name", r.Name)

var allErrs field.ErrorList
old := oldRaw.(*HelmReleaseProxy)
old, ok := oldRaw.(*HelmReleaseProxy)
if !ok {
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a HelmReleaseProxy but got a %T", old))
}

if !reflect.DeepEqual(r.Spec.RepoURL, old.Spec.RepoURL) {
allErrs = append(allErrs,
Expand Down Expand Up @@ -103,7 +106,7 @@ func (r *HelmReleaseProxy) ValidateUpdate(oldRaw runtime.Object) (admission.Warn
return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (r *HelmReleaseProxy) ValidateDelete() (admission.Warnings, error) {
helmreleaseproxylog.Info("validate delete", "name", r.Name)

Expand Down
18 changes: 8 additions & 10 deletions api/v1alpha1/webhook_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,24 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

admissionv1beta1 "k8s.io/api/admission/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
//+kubebuilder:scaffold:imports
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

// var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
var ctx context.Context
var cancel context.CancelFunc
var (
k8sClient client.Client
testEnv *envtest.Environment
ctx context.Context
cancel context.CancelFunc
)

func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)
Expand Down Expand Up @@ -121,10 +120,9 @@ var _ = BeforeSuite(func() {
if err != nil {
return err
}
conn.Close()
return nil
}).Should(Succeed())

return conn.Close()
}).Should(Succeed())
})

var _ = AfterSuite(func() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ spec:
name: v1alpha1
schema:
openAPIV3Schema:
description: HelmChartProxy is the Schema for the helmchartproxies API
description: HelmChartProxy is the Schema for the helmchartproxies API.
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
Expand Down
Loading

0 comments on commit fdd10da

Please sign in to comment.