Skip to content

Commit

Permalink
Linter: add golangci-linter and fix issues (opendatahub-io#540)
Browse files Browse the repository at this point in the history
- Update Makefile
- Add linter in GH action and make target to download binary
 - set timeout for 5m for GH aciton when cache is not hit
 - set to use 1.54.0 of golangci-lint
 - add lll linter and set to 180, skip lines out of range
 - set max-complicity back to default 30, skip lines out of scope
 - disable degarud linter
Fix linter after adding function test on DSCI

Signed-off-by: Wen Zhou <[email protected]>
Co-authored-by: Bartosz Majsak <[email protected]>
  • Loading branch information
zdtsw and bartoszmajsak authored Oct 3, 2023
1 parent dbab421 commit aacee48
Show file tree
Hide file tree
Showing 35 changed files with 372 additions and 294 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/linter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: linters
on:
push:
branches:
- main
- incubation
pull_request:
jobs:
golangci:
name: golangci-lint
runs-on: ubuntu-latest
env:
REPOSITORY: ${{ github.repository }}
steps:
- name: Set up Go env
uses: actions/setup-go@v4
with:
go-version: 1.18
- uses: actions/checkout@v4
with:
path: go/src/github.com/${{ env.REPOSITORY }}
- name: Set $GOPATH
run: |
echo "GOPATH=${{ github.workspace }}/go" >> $GITHUB_ENV
echo "${{ github.workspace }}/go/bin" >> $GITHUB_PATH
shell: bash
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54.0
working-directory: go/src/github.com/${{ env.REPOSITORY }}
args: --timeout 5m0s
39 changes: 39 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
linters-settings:
gocyclo:
min-complexity: 30
lll:
line-length: 180

linters:
disable-all: true
enable:
- bodyclose
- dogsled
- dupl
- errcheck
- exportloopref
- goconst
- gocritic
- gocyclo
- gofmt
- goprintffuncname
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- nolintlint
- staticcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- whitespace
- lll

issues:
exclude-rules:
- path: tests/e2e/(.+)_test\.go
linters:
- typecheck
14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
OPERATOR_SDK ?= $(LOCALBIN)/operator-sdk
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint

## Tool Versions
KUSTOMIZE_VERSION ?= v3.8.7
CONTROLLER_GEN_VERSION ?= v0.9.2
OPERATOR_SDK_VERSION ?= v1.24.1
GOLANGCI_LINT_VERSION ?= v1.54.0
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.24.2

Expand Down Expand Up @@ -140,6 +142,10 @@ fmt: ## Run go fmt against code.
vet: ## Run go vet against code.
go vet ./...

.PHONY: lint
lint: golangci-lint ## Run golangci-lint against code.
$(GOLANGCI_LINT) run --fix --sort-results

.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./controllers/... -v -coverprofile cover.out
Expand Down Expand Up @@ -204,7 +210,7 @@ KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/k
.PHONY: kustomize
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
$(KUSTOMIZE): $(LOCALBIN)
test -s $(LOCALBIN)/kustomize || { curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }
test -s $(LOCALBIN)/kustomize || { curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | sh -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }

.PHONY: controller-gen
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
Expand All @@ -231,6 +237,12 @@ else
OPERATOR_SDK = $(shell which operator-sdk)
endif

GOLANGCI_LINT_INSTALL_SCRIPT ?= 'https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh'
.PHONY: golangci-lint
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
$(GOLANGCI_LINT): $(LOCALBIN)
test -s $(LOCALBIN)/golangci-lint || { curl -sSfL $(GOLANGCI_LINT_INSTALL_SCRIPT) | bash -s $(GOLANGCI_LINT_VERSION); }

BUNDLE_DIR ?= "bundle"
.PHONY: bundle
bundle: manifests kustomize operator-sdk ## Generate bundle manifests and metadata, then validate generated files.
Expand Down
15 changes: 7 additions & 8 deletions apis/datasciencecluster/v1/datasciencecluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ package v1

import (
"github.com/opendatahub-io/opendatahub-operator/v2/components/codeflare"
"github.com/opendatahub-io/opendatahub-operator/v2/components/dashboard"
"github.com/opendatahub-io/opendatahub-operator/v2/components/datasciencepipelines"
"github.com/opendatahub-io/opendatahub-operator/v2/components/kserve"
"github.com/opendatahub-io/opendatahub-operator/v2/components/modelmeshserving"
"github.com/opendatahub-io/opendatahub-operator/v2/components/ray"
"github.com/opendatahub-io/opendatahub-operator/v2/components/workbenches"
conditionsv1 "github.com/openshift/custom-resource-status/conditions/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/opendatahub-io/opendatahub-operator/v2/components/dashboard"
"github.com/opendatahub-io/opendatahub-operator/v2/components/datasciencepipelines"
"github.com/opendatahub-io/opendatahub-operator/v2/components/modelmeshserving"
"github.com/opendatahub-io/opendatahub-operator/v2/components/workbenches"
)

// DataScienceCluster defines the desired state of the cluster.
Expand Down Expand Up @@ -67,7 +66,7 @@ type Components struct {
Ray ray.Ray `json:"ray,omitempty"`
}

// DataScienceClusterStatus defines the observed state of DataScienceCluster
// DataScienceClusterStatus defines the observed state of DataScienceCluster.
type DataScienceClusterStatus struct {
// Phase describes the Phase of DataScienceCluster reconciliation state
// This is used by OLM UI to provide status information to the user
Expand All @@ -92,7 +91,7 @@ type DataScienceClusterStatus struct {
//+kubebuilder:resource:scope=Cluster
//+kubebuilder:storageversion

// DataScienceCluster is the Schema for the datascienceclusters API
// DataScienceCluster is the Schema for the datascienceclusters API.
type DataScienceCluster struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand All @@ -103,7 +102,7 @@ type DataScienceCluster struct {

//+kubebuilder:object:root=true

// DataScienceClusterList contains a list of DataScienceCluster
// DataScienceClusterList contains a list of DataScienceCluster.
type DataScienceClusterList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions apis/datasciencecluster/v1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,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: "datasciencecluster.opendatahub.io", Version: "v1"}

// 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
10 changes: 5 additions & 5 deletions apis/dscinitialization/v1/dscinitialization_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

// +operator-sdk:csv:customresourcedefinitions:order=1

// DSCInitializationSpec defines the desired state of DSCInitialization
// DSCInitializationSpec defines the desired state of DSCInitialization.
type DSCInitializationSpec struct {
// Namespace for applications to be installed, non-configurable, default to "opendatahub"
// +kubebuilder:default:=opendatahub
Expand Down Expand Up @@ -60,10 +60,10 @@ type Monitoring struct {
type DevFlags struct {
// Custom manifests uri for odh-manifests
// +optional
ManifestsUri string `json:"manifestsUri,omitempty"`
ManifestsUri string `json:"manifestsUri,omitempty"` //nolint
}

// DSCInitializationStatus defines the observed state of DSCInitialization
// DSCInitializationStatus defines the observed state of DSCInitialization.
type DSCInitializationStatus struct {
// Phase describes the Phase of DSCInitializationStatus
// This is used by OLM UI to provide status information to the user
Expand All @@ -89,7 +89,7 @@ type DSCInitializationStatus struct {
//+kubebuilder:printcolumn:name="Created At",type=string,JSONPath=.metadata.creationTimestamp
//+operator-sdk:csv:customresourcedefinitions:displayName="DSC Initialization"

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

//+kubebuilder:object:root=true

// DSCInitializationList contains a list of DSCInitialization
// DSCInitializationList contains a list of DSCInitialization.
type DSCInitializationList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions apis/dscinitialization/v1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,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: "dscinitialization.opendatahub.io", Version: "v1"}

// 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
8 changes: 4 additions & 4 deletions components/codeflare/codeflare.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Package codeflare provides utility functions to config CodeFlare as part of the stack which makes managing distributed compute infrastructure in the cloud easy and intuitive for Data Scientists
// Package codeflare provides utility functions to config CodeFlare as part of the stack
// which makes managing distributed compute infrastructure in the cloud easy and intuitive for Data Scientists
package codeflare

import (
"fmt"

dsci "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/components"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
Expand Down Expand Up @@ -38,7 +40,6 @@ func (c *CodeFlare) OverrideManifests(_ string) error {
defaultKustomizePath = manifestConfig.SourcePath
}
CodeflarePath = filepath.Join(deploy.DefaultManifestPath, ComponentName, defaultKustomizePath)

}
return nil
}
Expand All @@ -56,7 +57,7 @@ func (c *CodeFlare) GetComponentName() string {
return ComponentName
}

// Verifies that CodeFlare implements ComponentInterface
// Verifies that CodeFlare implements ComponentInterface.
var _ components.ComponentInterface = (*CodeFlare)(nil)

func (c *CodeFlare) ReconcileComponent(cli client.Client, owner metav1.Object, dscispec *dsci.DSCInitializationSpec) error {
Expand Down Expand Up @@ -97,7 +98,6 @@ func (c *CodeFlare) ReconcileComponent(cli client.Client, owner metav1.Object, d
// Deploy Codeflare
err = deploy.DeployManifestsFromPath(cli, owner, CodeflarePath, dscispec.ApplicationsNamespace, c.GetComponentName(), enabled)
return err

}

func (c *CodeFlare) DeepCopyInto(target *CodeFlare) {
Expand Down
22 changes: 10 additions & 12 deletions components/dashboard/dashboard.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
// Package dashboard provides utility functions to config Open Data Hub Dashboard: A web dashboard that displays installed Open Data Hub components with easy access to component UIs and documentation
// Package dashboard provides utility functions to config Open Data Hub Dashboard: A web dashboard that displays
// installed Open Data Hub components with easy access to component UIs and documentation
package dashboard

import (
"context"
"fmt"
operatorv1 "github.com/openshift/api/operator/v1"
"path/filepath"
"strings"

operatorv1 "github.com/openshift/api/operator/v1"

dsci "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/components"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/common"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"

routev1 "github.com/openshift/api/route/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -62,7 +65,6 @@ func (d *Dashboard) OverrideManifests(platform string) error {
}
Path = filepath.Join(deploy.DefaultManifestPath, ComponentName, defaultKustomizePath)
}

}
return nil
}
Expand All @@ -80,13 +82,10 @@ func (d *Dashboard) GetComponentName() string {
return ComponentName
}

// Verifies that Dashboard implements ComponentInterface
// Verifies that Dashboard implements ComponentInterface.
var _ components.ComponentInterface = (*Dashboard)(nil)

func (d *Dashboard) ReconcileComponent(cli client.Client, owner metav1.Object, dscispec *dsci.DSCInitializationSpec) error {

// TODO: Add any additional tasks if required when reconciling component

func (d *Dashboard) ReconcileComponent(cli client.Client, owner metav1.Object, dscispec *dsci.DSCInitializationSpec) error { //nolint
platform, err := deploy.GetPlatform(cli)
if err != nil {
return err
Expand Down Expand Up @@ -114,7 +113,6 @@ func (d *Dashboard) ReconcileComponent(cli client.Client, owner metav1.Object, d
if err != nil {
return fmt.Errorf("failed to deploy dashboard crds %s: %v", PathCRDs, err)
}

}
if platform == deploy.SelfManagedRhods || platform == deploy.ManagedRhods {
err := common.UpdatePodSecurityRolebinding(cli, []string{"rhods-dashboard"}, dscispec.ApplicationsNamespace)
Expand Down Expand Up @@ -256,7 +254,7 @@ func (d *Dashboard) ReconcileComponent(cli client.Client, owner metav1.Object, d
}
}

func (in *Dashboard) DeepCopyInto(out *Dashboard) {
*out = *in
out.Component = in.Component
func (d *Dashboard) DeepCopyInto(out *Dashboard) {
*out = *d
out.Component = d.Component
}
5 changes: 3 additions & 2 deletions components/datasciencepipelines/datasciencepipelines.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Package datasciencepipelines provides utility functions to config Data Science Pipelines: Pipeline solution for end to end MLOps workflows that support the Kubeflow Pipelines SDK and Tekton
// Package datasciencepipelines provides utility functions to config Data Science Pipelines:
// Pipeline solution for end to end MLOps workflows that support the Kubeflow Pipelines SDK and Tekton
package datasciencepipelines

import (
Expand Down Expand Up @@ -59,7 +60,7 @@ func (d *DataSciencePipelines) GetComponentName() string {
return ComponentName
}

// Verifies that Dashboard implements ComponentInterface
// Verifies that Dashboard implements ComponentInterface.
var _ components.ComponentInterface = (*DataSciencePipelines)(nil)

func (d *DataSciencePipelines) ReconcileComponent(cli client.Client, owner metav1.Object, dscispec *dsci.DSCInitializationSpec) error {
Expand Down
Loading

0 comments on commit aacee48

Please sign in to comment.