Skip to content
This repository was archived by the owner on May 24, 2023. It is now read-only.

Add more linters #125

Merged
merged 3 commits into from
Jun 28, 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
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ jobs:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Lint Code
uses: golangci/golangci-lint-action@v2.5.2
uses: golangci/golangci-lint-action@v2
with:
args: --timeout ${{ env.GOLANGCI_TIMEOUT }}
26 changes: 22 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,31 @@ linters-settings:

linters:
enable:
- asciicheck
- deadcode
- errcheck
- errorlint
- gofmt
- gofumpt
- goimports
- gosec
- gosimple
- govet
- ineffassign
- makezero
- misspell
- gofmt
- unparam
- unconvert
- nilerr
- noctx
- predeclared
- staticcheck
- structcheck
- errcheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- wastedassign
disable-all: true
issues:
max-issues-per-linter: 0
max-same-issues: 0
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ fmt: ## Run go fmt against code.
vet: ## Run go vet against code.
go vet ./...

lint: ## Run golangci-lint against code.
docker run --pull always --rm -v $(shell pwd):/nginx-ingress-operator -w /nginx-ingress-operator -v $(shell go env GOCACHE):/cache/go -e GOCACHE=/cache/go -e GOLANGCI_LINT_CACHE=/cache/go -v $(shell go env GOPATH)/pkg:/go/pkg golangci/golangci-lint:latest golangci-lint --color always run


ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: manifests generate fmt vet ## Run tests.
mkdir -p ${ENVTEST_ASSETS_DIR}
Expand Down
12 changes: 6 additions & 6 deletions controllers/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ func kicCRDs() ([]*v1.CustomResourceDefinition, error) {
for _, path := range manifests {
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to open the CRD manifest %v: %v", path, err)
return nil, fmt.Errorf("failed to open the CRD manifest %v: %w", path, err)
}

var crd v1.CustomResourceDefinition

err = yaml.NewYAMLOrJSONDecoder(f, decoderBufferSize).Decode(&crd)

if err != nil {
return nil, fmt.Errorf("failed to parse the CRD manifest %v: %v", path, err)
return nil, fmt.Errorf("failed to parse the CRD manifest %v: %w", path, err)
}

err = f.Close()
if err != nil {
return nil, fmt.Errorf("failed to close the CRD manifest %v: %v", path, err)
return nil, fmt.Errorf("failed to close the CRD manifest %v: %w", path, err)
}

crds = append(crds, &crd)
Expand Down Expand Up @@ -87,18 +87,18 @@ func createKICCustomResourceDefinitions(log logr.Logger, mgr manager.Manager) er
log.V(1).Info(fmt.Sprintf("no previous CRD %v found, creating a new one.", crd.Name))
_, err = crdsClient.Create(context.TODO(), crd, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("error creating CRD %v: %v", crd.Name, err)
return fmt.Errorf("error creating CRD %v: %w", crd.Name, err)
}
} else {
return fmt.Errorf("error getting CRD %v: %v", crd.Name, err)
return fmt.Errorf("error getting CRD %v: %w", crd.Name, err)
}
} else {
// Update CRDs if they already exist
log.V(1).Info(fmt.Sprintf("previous CRD %v found, updating.", crd.Name))
oldCRD.Spec = crd.Spec
_, err = crdsClient.Update(context.TODO(), oldCRD, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("error updating CRD %v: %v", crd.Name, err)
return fmt.Errorf("error updating CRD %v: %w", crd.Name, err)
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions controllers/prerequisites.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,18 @@ func (r *NginxIngressControllerReconciler) createCommonResources(log logr.Logger
log.Info("no previous ClusterRole found, creating a new one.")
err = r.Create(context.TODO(), cr)
if err != nil {
return fmt.Errorf("error creating ClusterRole: %v", err)
return fmt.Errorf("error creating ClusterRole: %w", err)
}
} else {
return fmt.Errorf("error getting ClusterRole: %v", err)
return fmt.Errorf("error getting ClusterRole: %w", err)
}
} else {
// For updates in the ClusterRole permissions (eg new CRDs of the Ingress Controller).
log.Info("previous ClusterRole found, updating.")
cr := clusterRoleForNginxIngressController(clusterRoleName)
err = r.Update(context.TODO(), cr)
if err != nil {
return fmt.Errorf("error updating ClusterRole: %v", err)
return fmt.Errorf("error updating ClusterRole: %w", err)
}
}

Expand All @@ -164,12 +164,12 @@ func (r *NginxIngressControllerReconciler) createCommonResources(log logr.Logger
}

if err != nil {
return fmt.Errorf("error creating ClusterRoleBinding: %v", err)
return fmt.Errorf("error creating ClusterRoleBinding: %w", err)
}

err = createKICCustomResourceDefinitions(log, r.Mgr)
if err != nil {
return fmt.Errorf("error creating KIC CRDs: %v", err)
return fmt.Errorf("error creating KIC CRDs: %w", err)
}

if r.SccAPIExists {
Expand All @@ -184,7 +184,7 @@ func (r *NginxIngressControllerReconciler) createCommonResources(log logr.Logger
}

if err != nil {
return fmt.Errorf("error creating SecurityContextConstraints: %v", err)
return fmt.Errorf("error creating SecurityContextConstraints: %w", err)
}
}

Expand Down
2 changes: 0 additions & 2 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
Expand All @@ -38,7 +37,6 @@ import (
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var (
cfg *rest.Config
k8sClient client.Client
testEnv *envtest.Environment
)
Expand Down
20 changes: 1 addition & 19 deletions controllers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,24 +146,6 @@ func hasDifferentArguments(container corev1.Container, instance *k8sv1alpha1.Ngi
return !reflect.DeepEqual(newArgs, container.Args)
}

func contains(list []string, s string) bool {
for _, v := range list {
if v == s {
return true
}
}
return false
}

func remove(list []string, s string) []string {
for i, v := range list {
if v == s {
list = append(list[:i], list[i+1:]...)
}
}
return list
}

func VerifySCCAPIExists() (bool, error) {
cfg, err := config.GetConfig()
if err != nil {
Expand Down Expand Up @@ -205,7 +187,7 @@ func GetK8sVersion(client kubernetes.Interface) (v *version.Version, err error)

runningVersion, err := version.ParseGeneric(serverVersion.String())
if err != nil {
return nil, fmt.Errorf("unexpected error parsing running Kubernetes version: %v", err)
return nil, fmt.Errorf("unexpected error parsing running Kubernetes version: %w", err)
}

return runningVersion, nil
Expand Down