Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Versions updates #11

Merged
merged 2 commits into from
May 8, 2024
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
46 changes: 32 additions & 14 deletions .github/workflows/validate.yaml → .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name: Validation
name: Code Checks

on: [push, pull_request]
on: [push]

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Consider adding the pull_request event to the on trigger for the GitHub Actions workflow to ensure that checks are also run on pull requests, not just on pushes to the repository. This is important for catching issues before they are merged into the main branch. [enhancement]

Suggested change
on: [push]
on: [push, pull_request]

defaults:
run:
shell: bash

jobs:
basic:
name: generate and vet
makechecks:
name: Make Checks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -17,16 +17,39 @@ jobs:
with:
go-version-file: './go.mod'

- uses: actions/setup-python@v5
with:
python-version: 3.x

- run: pip install yamllint==1.33.0

- run: |
make generate
make manifests
go mod tidy
make generate
make fmt
make vet
yamllint .
git diff --exit-code

unit-tests:
name: Unit Tests
# A separate job so that it can annotate the code
golangci:
name: golangci-lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version-file: './go.mod'

Comment on lines +39 to +43
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Add a step to cache dependencies in the GitHub Actions workflow to speed up builds. This can significantly reduce build times by avoiding redundant downloads of dependencies that haven't changed. [performance]

Suggested change
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: './go.mod'
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- uses: actions/setup-go@v5
with:
go-version-file: './go.mod'

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setup-go action does this automatically based on the go.sum. I considered doing this for the other binaries that are separately installed, but when the go.sum cache is hit (as in, all dependencies to build the program are already cached), the actions take less than 2 minutes each. And that's good enough for me to not worry about the additional cache setup.

- name: golangci-lint
uses: golangci/golangci-lint-action@v5
with:
version: v1.58
# Automatically uses ./.golangci.yml for configuration

tests:
name: Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -38,15 +61,10 @@ jobs:
- run: |
make test

# Fix things up for the coverage report
head -1 cover.out > nucleus_cover.out
grep 'governance-policy-nucleus' cover.out >> nucleus_cover.out
sudo rm -rf .go

- name: Update coverage report
uses: ncruces/go-coverage-report@v0
with:
coverage-file: nucleus_cover.out
coverage-file: cover.out
output-dir: ${{ github.ref_name }}
report: true
chart: false
Expand Down
40 changes: 0 additions & 40 deletions .github/workflows/lint.yaml

This file was deleted.

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ bin/
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Output of gosec tool
gosec.json

# Dependency directories (remove the comment below to include it)
# vendor/
70 changes: 8 additions & 62 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
# Reference: https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml
run:
# timeout for analysis, e.g. 30s, 5m, default is 1m
deadline: 20m
timeout: 20m

# which dirs to skip: they won't be analyzed;
# can use regexp here: generated.*, regexp is applied on full path;
# default value is empty list, but next dirs are always skipped independently
# from this option's value:
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
skip-dirs:
- genfiles$
- vendor$
- vbh$

# which files to skip: they will be analyzed, but issues from them
# won't be reported. Default value is empty list, but there is
# no need to include all autogenerated files, we confidently recognize
# autogenerated files. If it's not please let us know.
skip-files:
- ".*\\.pb\\.go"
- ".*\\.gen\\.go"

linters:
enable-all: true
disable:
- bodyclose
- copyloopvar # prefer old style for now, more reliable across various scanners
- cyclop
- deadcode #deprecated
Comment on lines 9 to 13
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Re-enable the err113 linter to enforce consistent error handling practices across the codebase. This linter checks for errors that are not created or wrapped with %w in fmt.Errorf, which is a common best practice in Go for error handling. [best practice]

Suggested change
disable:
- bodyclose
- copyloopvar # prefer old style for now, more reliable across various scanners
- cyclop
- deadcode #deprecated
disable:
- bodyclose
- cyclop
- deadcode #deprecated
- depguard

- depguard
Expand All @@ -40,13 +24,14 @@ linters:
- goconst
- gocyclo
- godot
- goerr113
- err113
- golint # replaced by revive
- gomnd
- gomoddirectives
- gosec
- ifshort
- interfacer
- intrange # prefer old style
- ireturn # New linter to consider
- maligned
- maintidx # New linter to consider
Expand Down Expand Up @@ -122,69 +107,26 @@ linters-settings:
gocritic:
enabled-checks:
- appendCombine
- argOrder
- assignOp
- badCond
- boolExprSimplify
- builtinShadow
- captLocal
- caseOrder
- codegenComment
- commentedOutCode
- commentedOutImport
- defaultCaseOrder
- deprecatedComment
- docStub
- dupArg
- dupBranchBody
- dupCase
- dupSubExpr
- elseif
- emptyFallthrough
- equalFold
- flagDeref
- flagName
- hexLiteral
- indexAlloc
- initClause
- methodExprCall
- nilValReturn
- octalLiteral
- offBy1
- rangeExprCopy
- regexpMust
- sloppyLen
- stringXbytes
- switchTrue
- typeAssertChain
- typeSwitchVar
- typeUnparen
- underef
- unlambda
- unnecessaryBlock
- unslice
- valSwap
- weakCond

# Unused
# - yodaStyleExpr
# - appendAssign
# - commentFormatting
# - emptyStringTest
# - exitAfterDefer
# - ifElseChain
# - hugeParam
# - importShadow
# - nestingReduce
# - paramTypeCombine
# - ptrToRefParam
# - rangeValCopy
# - singleCaseSwitch
# - sloppyReassign
# - unlabelStmt
# - unnamedResult
# - wrapperFunc

issues:
# List of regexps of issue texts to exclude, empty list by default.
# But independently from this option we use default exclude patterns,
Expand Down Expand Up @@ -223,6 +165,10 @@ issues:
- linters:
- lll
source: \/\/ ?https?:\/\/
# Don't enforce whitespace rules on test files
- path: _test\.go$
linters:
- wsl

# Independently from option `exclude` we use default exclude patterns,
# it can be disabled by this option. To list all
Expand Down
57 changes: 42 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ $(LOCAL_BIN):
mkdir -p $(LOCAL_BIN)

# Keep an existing GOPATH, make a private one if it is undefined
GOPATH_DEFAULT := $(ROOTDIR)/.go
export GOPATH ?= $(GOPATH_DEFAULT)
export GOPATH ?= $(shell go env GOPATH)
ifeq ($(GOPATH),)
GOPATH := $(ROOTDIR)/.go
endif
GOBIN_DEFAULT := $(GOPATH)/bin
export GOBIN ?= $(GOBIN_DEFAULT)

Expand All @@ -24,29 +26,51 @@ SHELL = /usr/bin/env bash -o pipefail
# Note: this replaces `go-get-tool`.
go-install = @set -e ; mkdir -p $(LOCAL_BIN) ; GOBIN=$(LOCAL_BIN) go install $(1)

# Define local utilities near the top so they work correctly as targets
# Define local utilities before other targets so they work correctly
# Note: this pattern of variables, paths, and target names allows users to
# override the version used, and helps Make by not using PHONY targets.
# To 'refresh' versions, remove the local bin directory.

CONTROLLER_GEN_VERSION ?= v0.15.0 # https://github.com/kubernetes-sigs/controller-tools/releases/latest
CONTROLLER_GEN ?= $(LOCAL_BIN)/controller-gen
$(CONTROLLER_GEN): $(LOCAL_BIN)
$(call go-install,sigs.k8s.io/controller-tools/cmd/[email protected])

ENVTEST ?= $(LOCAL_BIN)/setup-envtest
$(ENVTEST): $(LOCAL_BIN)
$(call go-install,sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
$(call go-install,sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VERSION))

KUSTOMIZE_VERSION ?= v5.4.1 # https://github.com/kubernetes-sigs/kustomize/releases/latest
KUSTOMIZE ?= $(LOCAL_BIN)/kustomize
$(KUSTOMIZE): $(LOCAL_BIN)
$(call go-install,sigs.k8s.io/kustomize/kustomize/[email protected])
$(call go-install,sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION))

GOFUMPT_VERSION ?= v0.6.0 # https://github.com/mvdan/gofumpt/releases/latest
GOFUMPT ?= $(LOCAL_BIN)/gofumpt
$(GOFUMPT): $(LOCAL_BIN)
$(call go-install,mvdan.cc/gofumpt@$(GOFUMPT_VERSION))

GCI_VERSION ?= v0.13.4 # https://github.com/daixiang0/gci/releases/latest
GCI ?= $(LOCAL_BIN)/gci
$(GCI): $(LOCAL_BIN)
$(call go-install,github.com/daixiang0/gci@$(GCI_VERSION))

GOSEC_VERSION ?= v2.19.0 # https://github.com/securego/gosec/releases/latest
GOSEC ?= $(LOCAL_BIN)/gosec
$(GOSEC): $(LOCAL_BIN)
$(call go-install,github.com/securego/gosec/v2/cmd/gosec@$(GOSEC_VERSION))

GOLANGCI_VERSION ?= v1.58.0 # https://github.com/golangci/golangci-lint/releases/latest
GOLANGCI ?= $(LOCAL_BIN)/golangci-lint
$(GOLANGCI): $(LOCAL_BIN)
$(call go-install,github.com/golangci/golangci-lint/cmd/golangci-lint@v1.55.2)
$(call go-install,github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_VERSION))

# To change this version, adjust it in the go.mod file
# https://github.com/onsi/ginkgo/releases/latest
GINKGO_VERSION := $(shell awk '/github.com\/onsi\/ginkgo\/v2/ {print $$2}' go.mod)
GINKGO ?= $(LOCAL_BIN)/ginkgo
$(GINKGO): $(LOCAL_BIN)
$(call go-install,github.com/onsi/ginkgo/v2/ginkgo@$(shell awk '/github.com\/onsi\/ginkgo\/v2/ {print $$2}' go.mod))
$(call go-install,github.com/onsi/ginkgo/v2/ginkgo@$(GINKGO_VERSION))

ENVTEST ?= $(LOCAL_BIN)/setup-envtest
$(ENVTEST): $(LOCAL_BIN)
$(call go-install,sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)

.PHONY: manifests
manifests: $(CONTROLLER_GEN) $(KUSTOMIZE) ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
Comment on lines 75 to 76
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Add a clean-up target in the Makefile to remove the generated binaries and other build artifacts. This helps in maintaining a clean workspace and can be useful before rebuilds to ensure that no stale artifacts are used. [maintainability]

Suggested change
.PHONY: manifests
manifests: $(CONTROLLER_GEN) $(KUSTOMIZE) ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
.PHONY: clean
clean:
rm -rf $(LOCAL_BIN)/*
echo "Cleaned up build artifacts."
.PHONY: manifests
manifests: $(CONTROLLER_GEN) $(KUSTOMIZE) ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it was any more complicated than one directory to rm, I would consider this.

Expand All @@ -62,12 +86,15 @@ generate: $(CONTROLLER_GEN) ## Generate code containing DeepCopy, DeepCopyInto,
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

.PHONY: fmt
fmt: ## Run go fmt against code.
go fmt ./...
fmt: $(GOFUMPT) $(GCI)
go mod tidy
find . -not \( -path "./.go" -prune \) -name "*.go" | xargs $(GOFUMPT) -l -w
find . -not \( -path "./.go" -prune \) -name "*.go" | xargs $(GCI) write --skip-generated -s standard -s default -s localmodule
Comment on lines +89 to +92
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Use more specific patterns for find commands in the fmt target to avoid unnecessary processing of non-Go files and directories, improving the efficiency of the formatting process. [performance]

Suggested change
fmt: $(GOFUMPT) $(GCI)
go mod tidy
find . -not \( -path "./.go" -prune \) -name "*.go" | xargs $(GOFUMPT) -l -w
find . -not \( -path "./.go" -prune \) -name "*.go" | xargs $(GCI) write --skip-generated -s standard -s default -s localmodule
fmt: $(GOFUMPT) $(GCI)
go mod tidy
find . -type f -name '*.go' ! -path './.go/*' | xargs $(GOFUMPT) -l -w
find . -type f -name '*.go' ! -path './.go/*' | xargs $(GCI) write --skip-generated -s standard -s default -s localmodule

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not touching these unless I have to.


.PHONY: vet
vet: ## Run go vet against code.
vet: $(GOSEC)
go vet ./...
$(GOSEC) -fmt sonarqube -out gosec.json -stdout -exclude-dir=.go -exclude-dir=test -exclude-generated ./...

# Note: this target is not used by Github Actions. Instead, each linter is run
# separately to automatically decorate the code with the linting errors.
Expand All @@ -77,7 +104,7 @@ lint: $(GOLANGCI)
$(GOLANGCI) run
yamllint -c $(ROOTDIR)/.yamllint.yaml .

ENVTEST_K8S_VERSION ?= 1.26
ENVTEST_K8S_VERSION ?= 1.29
.PHONY: test
test: manifests generate $(GINKGO) $(ENVTEST) ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" $(GINKGO) \
Expand Down
3 changes: 2 additions & 1 deletion api/v1alpha1/reflectiveResourceList.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"fmt"
"reflect"

"open-cluster-management.io/governance-policy-nucleus/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"

"open-cluster-management.io/governance-policy-nucleus/api/v1beta1"
)

//+kubebuilder:object:generate=false
Expand Down
1 change: 0 additions & 1 deletion api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading