Skip to content

Commit

Permalink
feat: applying lint and fixing the lint issues (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarlso authored Nov 22, 2023
1 parent e9afedd commit b047f17
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 26 deletions.
121 changes: 121 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
run:
go: "1.21"
timeout: 10m
tests: false
allow-parallel-runners: true
skip-dirs:
- "./*/mock"
skip-files:
- "pkg/ocm/fakes/fakes.go"

linters-settings:
funlen:
lines: 150
statements: 60
staticcheck:
go: "1.21"
stylecheck:
go: "1.21"
cyclop:
max-complexity: 20
skip-tests: true
gosec:
exclude-generated: true
lll:
line-length: 150
misspell:
locale: US
govet:
check-shadowing: true
nilaway:
nolintlint:
allow-unused: false
require-explanation: true
require-specific: false
varnamelen:
ignore-names:
- err
- wg
- fs
- id
- vm
- ns
- ip

issues:
max-same-issues: 0
max-issues-per-linter: 0
exclude-rules:
- text: "should not use dot imports|don't use an underscore in package name"
linters:
- golint
- source: "https://"
linters:
- lll
- path: pkg/defaults/
linters:
- lll
- path: _test\.go
linters:
- goerr113
- gocyclo
- errcheck
- gosec
- dupl
- funlen
- scopelint
- testpackage
- goconst
- godox
- path: internal/version/
linters:
- gochecknoglobals
- path: internal/command/
linters:
- exhaustivestruct
- lll
- wrapcheck
- source: "// .* #\\d+"
linters:
- godox
- path: test/e2e/
linters:
- goerr113
- gomnd
# remove this once https://github.com/golangci/golangci-lint/issues/2649 is closed
- path: /
linters:
- typecheck

linters:
enable-all: true
disable:
- gci
- depguard
- exhaustivestruct
- golint
- interfacer
- ireturn
- maligned
- nilnil
- scopelint
- tagliatelle
- gomoddirectives
- varcheck
- nosnakecase
- structcheck
- ifshort
- deadcode
- forbidigo
- prealloc
- gochecknoinits
- exhaustruct
- goerr113
- govet
- nonamedreturns
- varnamelen
- wrapcheck
- staticcheck
- gochecknoglobals
- paralleltest
- wsl
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ fmt: ## Run go fmt against code.
vet: ## Run go vet against code.
go vet ./...

.PHONY: lint
lint: golangci-lint ## Run golangci-lint.
$(GOLANGCI_LINT) run

.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 ./... -coverprofile cover.out
Expand Down Expand Up @@ -139,11 +143,13 @@ KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
GEN_CRD_API_REFERENCE_DOCS ?= $(LOCALBIN)/gen-crd-api-reference-docs
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint

## Tool Versions
KUSTOMIZE_VERSION ?= v3.8.7
CONTROLLER_TOOLS_VERSION ?= v0.9.2
GEN_API_REF_DOCS_VERSION ?= e327d0730470cbd61b06300f81c5fcf91c23c113
GOLANGCI_LINT_VERSION ?= v1.55.2

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
.PHONY: kustomize
Expand Down Expand Up @@ -176,3 +182,8 @@ generate-license:
$$f \
--skip-unrecognised; \
done

.PHONY: golangci-lint
golangci-lint: $(GOLANGCI_LINT)
$(GOLANGCI_LINT): $(LOCALBIN)
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s $(GOLANGCI_LINT_VERSION)
8 changes: 4 additions & 4 deletions api/v1alpha1/componentsubscription_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type ComponentSubscriptionSpec struct {
Source OCMRepository `json:"source"`

// Destination holds the destination or target OCM Repository details. The ComponentVersion
// will be transfered into this repository.
// will be transferred into this repository.
// +optional
Destination *OCMRepository `json:"destination,omitempty"`

Expand Down Expand Up @@ -79,7 +79,7 @@ type OCMRepository struct {
SecretRef *meta.LocalObjectReference `json:"secretRef,omitempty"`
}

// ComponentSubscriptionStatus defines the observed state of ComponentSubscription
// ComponentSubscriptionStatus defines the observed state of ComponentSubscription.
type ComponentSubscriptionStatus struct {
// LastAttemptedVersion defines the latest version encountered while checking component versions.
// This might be different from last applied version which should be the latest applied/replicated version.
Expand Down Expand Up @@ -165,7 +165,7 @@ func (in ComponentSubscription) GetComponentVersion() Component {
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

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

//+kubebuilder:object:root=true

// ComponentSubscriptionList contains a list of ComponentSubscription
// ComponentSubscriptionList contains a list of ComponentSubscription.
type ComponentSubscriptionList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Expand Down
6 changes: 6 additions & 0 deletions api/v1alpha1/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package v1alpha1

const (
// LevelDebug defines the log level debug messages are put out at.
LevelDebug = 4
)
4 changes: 2 additions & 2 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,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: "delivery.ocm.software", 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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ spec:
schema:
openAPIV3Schema:
description: ComponentSubscription is the Schema for the componentsubscriptions
API
API.
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
Expand All @@ -47,7 +47,7 @@ spec:
type: string
destination:
description: Destination holds the destination or target OCM Repository
details. The ComponentVersion will be transfered into this repository.
details. The ComponentVersion will be transferred into this repository.
properties:
secretRef:
description: SecretRef specifies the credentials used to access
Expand Down Expand Up @@ -142,7 +142,7 @@ spec:
type: object
status:
description: ComponentSubscriptionStatus defines the observed state of
ComponentSubscription
ComponentSubscription.
properties:
conditions:
items:
Expand Down
12 changes: 8 additions & 4 deletions controllers/componentsubscription_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ import (
"github.com/open-component-model/replication-controller/pkg/ocm"
)

// ComponentSubscriptionReconciler reconciles a ComponentSubscription object
const requeueAfter = 10 * time.Second

// ComponentSubscriptionReconciler reconciles a ComponentSubscription object.
type ComponentSubscriptionReconciler struct {
client.Client
Scheme *runtime.Scheme
Expand All @@ -59,6 +61,7 @@ func (r *ComponentSubscriptionReconciler) SetupWithManager(mgr ctrl.Manager) err
}

ns := obj.GetNamespace()

return []string{fmt.Sprintf("%s/%s", ns, obj.Spec.Source.SecretRef.Name)}
}); err != nil {
return fmt.Errorf("failed setting index fields: %w", err)
Expand All @@ -74,6 +77,7 @@ func (r *ComponentSubscriptionReconciler) SetupWithManager(mgr ctrl.Manager) err
}

ns := obj.GetNamespace()

return []string{fmt.Sprintf("%s/%s", ns, obj.Spec.Destination.SecretRef.Name)}
}); err != nil {
return fmt.Errorf("failed setting index fields: %w", err)
Expand Down Expand Up @@ -124,7 +128,7 @@ func (r *ComponentSubscriptionReconciler) findObjects(sourceKey string, destinat
}] = struct{}{}
}

requests := make([]reconcile.Request, len(requestMap))
requests := make([]reconcile.Request, 0, len(requestMap))
for k := range requestMap {
requests = append(requests, k)
}
Expand All @@ -149,11 +153,11 @@ func (r *ComponentSubscriptionReconciler) Reconcile(ctx context.Context, req ctr
return ctrl.Result{}, nil
}

return ctrl.Result{RequeueAfter: 10 * time.Second}, nil
return ctrl.Result{RequeueAfter: requeueAfter}, nil
}

if obj.DeletionTimestamp != nil {
return
return ctrl.Result{}, nil
}

// The replication controller doesn't need a shouldReconcile, because it should always reconcile,
Expand Down
9 changes: 4 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,19 @@ import (
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

deliveryv1alpha1 "github.com/open-component-model/replication-controller/api/v1alpha1"
"github.com/open-component-model/replication-controller/api/v1alpha1"
"github.com/open-component-model/replication-controller/controllers"
//+kubebuilder:scaffold:imports
)

var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
ociAgent = "replication-controller/v1alpha1"
)

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

utilruntime.Must(deliveryv1alpha1.AddToScheme(scheme))
utilruntime.Must(v1alpha1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}

Expand All @@ -55,10 +53,11 @@ func main() {

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

const metricsServerPort = 9443
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
Port: metricsServerPort,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "e34a5c60.ocm.software",
Expand Down
Loading

0 comments on commit b047f17

Please sign in to comment.