Skip to content
This repository was archived by the owner on Nov 28, 2024. It is now read-only.

Commit

Permalink
feat: applying lint and fixing the lint issues (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarlso authored Nov 22, 2023
1 parent 110b625 commit f86f4da
Show file tree
Hide file tree
Showing 14 changed files with 344 additions and 157 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: tests
name: tests and lint

on:
pull_request:
Expand Down Expand Up @@ -41,5 +41,7 @@ jobs:
restore-keys: |
${{ runner.os }}-go-
- run: git config --global url.https://${{ secrets.OCMBOT_APP_ID }}:${{ steps.generate_token.outputs.token }}@github.com/.insteadOf https://github.com/
- name: Run lint
run: make lint
- name: Run tests
run: make test
119 changes: 119 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
run:
go: "1.21"
timeout: 10m
tests: false
allow-parallel-runners: true
skip-dirs:
- "./*/mock"

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: true
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 @@ -53,6 +53,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 @@ -126,13 +130,15 @@ $(LOCALBIN):
mkdir -p $(LOCALBIN)

## Tool Binaries
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest

## Tool Versions
KUSTOMIZE_VERSION ?= v3.8.7
CONTROLLER_TOOLS_VERSION ?= v0.11.1
GOLANGCI_LINT_VERSION ?= v1.55.2

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
.PHONY: kustomize
Expand All @@ -154,3 +160,8 @@ $(CONTROLLER_GEN): $(LOCALBIN)
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

.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)
2 changes: 1 addition & 1 deletion api/v1alpha1/condition_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
ServiceAccountCreateOrUpdateFailedReason string = "ServiceAccountCreateOrUpdateFailed"

// RBACCreateOrUpdateFailedReason indicates that the project cluster role could not be reconciled.
RBACCreateOrUpdateFailedReason string = "RBACCreateOrUpdateFailed"
RBACCreateOrUpdateFailedReason string = "RBACCreateOrUpdateFailed" //nolint:gosec // not a cred

// CertificateCreateOrUpdateFailedReason indicates that the project certificate could not be reconciled.
CertificateCreateOrUpdateFailedReason string = "CertificateCreateOrUpdateFailed"
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ const (
const (
// ManagedMPASSecretAnnotationKey denotes that the project controller needs to set these secrets
// in the service account of the project.
ManagedMPASSecretAnnotationKey = "mpas.ocm.system/secret.dockerconfig"
ManagedMPASSecretAnnotationKey = "mpas.ocm.system/secret.dockerconfig" //nolint:gosec // not a cred
)
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: "mpas.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
13 changes: 7 additions & 6 deletions api/v1alpha1/project_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
// ExistingRepositoryPolicy defines what to do in case a requested repository already exists.
type ExistingRepositoryPolicy string

// ProjectSpec defines the desired state of Project
// ProjectSpec defines the desired state of Project.
type ProjectSpec struct {
// +required
Git gcv1alpha1.RepositorySpec `json:"git"`
Expand Down Expand Up @@ -51,7 +51,7 @@ type CommitTemplate struct {
Message string
}

// ProjectStatus defines the observed state of Project
// ProjectStatus defines the observed state of Project.
type ProjectStatus struct {
// +optional
Conditions []metav1.Condition `json:"conditions,omitempty"`
Expand All @@ -78,15 +78,17 @@ func (in *Project) GetServiceAccountNamespacedName() (types.NamespacedName, erro
}

var name, namespace string
const nameNamespaceCount = 2
for _, e := range in.Status.Inventory.Entries {
split := strings.Split(e.ID, "_")
if len(split) < 2 {
if len(split) < nameNamespaceCount {
return types.NamespacedName{}, fmt.Errorf("failed to split ID: %s", e.ID)
}

if split[len(split)-1] == "ServiceAccount" {
name = split[1]
namespace = split[0]

break
}
}
Expand All @@ -96,7 +98,6 @@ func (in *Project) GetServiceAccountNamespacedName() (types.NamespacedName, erro
}

return types.NamespacedName{Name: name, Namespace: namespace}, nil

}

// +kubebuilder:object:root=true
Expand All @@ -105,7 +106,7 @@ func (in *Project) GetServiceAccountNamespacedName() (types.NamespacedName, erro
// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].status",description=""
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].message",description=""

// Project is the Schema for the projects API
// Project is the Schema for the projects API.
type Project struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand Down Expand Up @@ -135,7 +136,7 @@ func (in *Project) GetNameWithPrefix(prefix string) string {

//+kubebuilder:object:root=true

// ProjectList contains a list of Project
// ProjectList contains a list of Project.
type ProjectList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Expand Down
6 changes: 3 additions & 3 deletions config/crd/bases/mpas.ocm.software_projects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ spec:
name: v1alpha1
schema:
openAPIV3Schema:
description: Project is the Schema for the projects API
description: Project is the Schema for the projects API.
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
Expand All @@ -42,7 +42,7 @@ spec:
metadata:
type: object
spec:
description: ProjectSpec defines the desired state of Project
description: ProjectSpec defines the desired state of Project.
properties:
flux:
default:
Expand Down Expand Up @@ -143,7 +143,7 @@ spec:
- git
type: object
status:
description: ProjectStatus defines the observed state of Project
description: ProjectStatus defines the observed state of Project.
properties:
conditions:
items:
Expand Down
4 changes: 1 addition & 3 deletions controllers/get_project_in_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import (
"github.com/open-component-model/mpas-project-controller/api/v1alpha1"
)

var (
errNotProjectNamespace = errors.New("not in a namespace that belongs to a project")
)
var errNotProjectNamespace = errors.New("not in a namespace that belongs to a project")

// GetProjectFromObjectNamespace returns the Project from the annotation of the current namespace that an object
// is in.
Expand Down
Loading

0 comments on commit f86f4da

Please sign in to comment.