Skip to content

Enhance golangci-lint setup #40

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

Merged
merged 5 commits into from
Apr 2, 2025
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
28 changes: 18 additions & 10 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ run:
concurrency: 4
go: "1.24"
linters:
default: none # standard/all/none/fast
enable:
- errcheck
- gocritic
- govet
- nlreturn
- revive
disable:
- staticcheck
- unused
- whitespace
- wsl
exclusions:
generated: lax
rules:
Expand All @@ -27,13 +34,14 @@ linters:
- path: (.+)\.go$
text: '".*" imported but not used'
paths:
- third_party$
- builtin$
- examples$
- hack/.*
formatters:
exclusions:
generated: lax
paths:
- third_party$
- builtin$
- examples$
enable:
- gci
- gofmt
settings:
gci:
sections:
- standard
- default
- prefix(kube-rbac-proxy-watcher)
49 changes: 24 additions & 25 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ BUILD_PLATFORM ?= $(shell uname -s | tr '[:upper:]' '[:lower:]')
BUILD_ARCH ?= $(shell uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')
LD_FLAGS ?= "-s -w $(shell $(REPO_ROOT)/hack/get-ldflags.sh)"

GCI_OPT ?= -s standard -s default -s "prefix($(shell go list -m))" --skip-generated

ifneq ($(strip $(shell git status --porcelain 2>/dev/null)),)
EFFECTIVE_VERSION := $(EFFECTIVE_VERSION)-dirty
endif
IMAGE_TAG := $(EFFECTIVE_VERSION)

.DEFAULT_GOAL := all
all: watcher
all: check watcher

#################################################################
# Rules related to binary build, Docker image build and release #
Expand All @@ -45,7 +46,11 @@ docker-push:
.PHONY: tidy
tidy:
@go mod tidy
@go mod download

.PHONY: gci
gci: tidy
@echo "Running gci..."
@go tool gci write $(GCI_OPT) $(SRC_DIRS)

.PHONY: watcher
watcher: tidy
Expand All @@ -58,25 +63,31 @@ watcher: tidy
-ldflags=$(LD_FLAGS) \
$(REPO_ROOT)/cmd/watcher

.PHONY: format
format:
@gofmt -l -w $(SRC_DIRS)
.PHONY: fmt
fmt: tidy
@echo "Running $@..."
@go tool golangci-lint fmt \
--config=$(REPO_ROOT)/.golangci.yaml \
$(SRC_DIRS)

.PHONY: check
check: tidy format
check: tidy fmt gci lint

.PHONY: lint
lint: tidy
@echo "Running $@..."
@go tool golangci-lint run \
--config=$(REPO_ROOT)/.golangci.yaml \
--timeout 10m \
$(SRC_DIRS)
@go vet \
$(SRC_DIRS)

.PHONY: test
test:
@echo "Running $@..."
@go tool gotest.tools/gotestsum $(SRC_DIRS)

.PHONY: test-cov
test-cov:
@echo "Running $@..."
@$(REPO_ROOT)/hack/test-cover.sh $(SRC_DIRS)

.PHONY: verify
Expand All @@ -87,32 +98,20 @@ verify-extended: check test-cov sast-report

.PHONY: clean
clean:
@echo "Running $@..."
@rm -f $(REPO_ROOT)/build/watcher
@rm -f $(REPO_ROOT)/*.sarif
@rm -f $(REPO_ROOT)/test.coverprofile
@rm -f $(REPO_ROOT)/test.coverage.html

sast: tidy
@echo "Running $@..."
@$(REPO_ROOT)/hack/sast.sh

sast-report: tidy
@echo "Running $@..."
@$(REPO_ROOT)/hack/sast.sh --gosec-report true

add-license-headers: tidy
@echo "Running $@..."
@$(REPO_ROOT)/hack/add-license-header.sh

.PHONY: goimports
goimports: goimports_tool goimports-reviser_tool

.PHONY: goimports_tool
goimports_tool: tidy
@for dir in $(SRC_DIRS); do \
go tool goimports -w $$dir/; \
done

.PHONY: goimports-reviser_tool
goimports-reviser_tool: tidoy
@for dir in $(SRC_DIRS); do \
GOIMPORTS_REVISER_OPTIONS="-imports-order std,project,general,company" \
go tool goimports-reviser -recursive $$dir/; \
done
3 changes: 2 additions & 1 deletion cmd/parameters/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type Parameters struct {

// Parse returns the parameters based on the supplied arguments
func Parse(params []string) Parameters {

parameters := Parameters{
CmdLine: defaultCmdLine,
CmdLineArgs: []string{},
Expand Down Expand Up @@ -57,6 +56,7 @@ func Parse(params []string) Parameters {
if cmdLineIndex != -1 && cmdLineIndex < len(params) {
cmdLineStr := strings.TrimPrefix(params[cmdLineIndex], cmdLineParam)
parameters.CmdLine = cmdLineStr

if watchedDirIndex > cmdLineIndex {
parameters.CmdLineArgs = params[cmdLineIndex+1 : watchedDirIndex]
} else {
Expand All @@ -73,5 +73,6 @@ func indexOf(params []string, str string) int {
return i
}
}

return -1
}
2 changes: 0 additions & 2 deletions cmd/parameters/parametes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
)

func TestParams(t *testing.T) {

tests := []struct {
input []string
expected Parameters
Expand All @@ -33,5 +32,4 @@ func TestParams(t *testing.T) {
t.Logf("running test %d", i)
assert.EqualValues(t, test.expected, Parse(test.input))
}

}
14 changes: 9 additions & 5 deletions cmd/watcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func init() {
// hot configuration reload and needs to be restarted to reflect on configuration changes.

func main() {

params := parameters.Parse(os.Args)
log.Info(
"child process parameters",
Expand All @@ -51,11 +50,11 @@ func main() {
)

proc = process.New(log, params.CmdLine, params.CmdLineArgs...)

sigs := make(chan os.Signal, 1)

signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
done := make(chan bool, 1)

done := make(chan bool, 1)
c, cancel := context.WithCancel(context.Background())
ctx := logr.NewContext(c, log)

Expand All @@ -65,14 +64,16 @@ func main() {
"signal received",
"signal", sig.String(),
)

_ = proc.Stop()

done <- true
}()

hash := watcher.RunTotalHashCalc(ctx, params.WatchedDir)
currentHash := <-hash

//Shall start the processes and maintain the PID
// Shall start the processes and maintain the PID
if err := proc.Start(); err != nil {
log.Error(err, "error starting the child process")
os.Exit(1)
Expand All @@ -91,18 +92,21 @@ func main() {
"old hash", currentHash,
"new hash", h,
)

currentHash = h

if err := proc.Stop(); err != nil {
log.Error(err, "error stopping child process")
os.Exit(1)
}

proc = process.New(log, params.CmdLine, params.CmdLineArgs...)

if err := proc.Start(); err != nil {
log.Error(err, "error starting child process")
os.Exit(1)
}
}
}
}

}
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ module kube-rbac-proxy-watcher
go 1.24.2

tool (
github.com/daixiang0/gci
github.com/golangci/golangci-lint/v2/cmd/golangci-lint
github.com/google/addlicense
github.com/incu6us/goimports-reviser/v3
github.com/onsi/ginkgo/v2/ginkgo
github.com/securego/gosec/v2/cmd/gosec
golang.org/x/tools/cmd/goimports
gotest.tools/gotestsum
)

Expand Down Expand Up @@ -126,7 +125,6 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/incu6us/goimports-reviser/v3 v3.8.2 // indirect
github.com/jgautheron/goconst v1.7.1 // indirect
github.com/jingyugao/rowserrcheck v1.1.1 // indirect
github.com/jjti/go-spancheck v0.6.4 // indirect
Expand Down Expand Up @@ -229,7 +227,6 @@ require (
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.36.0 // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
golang.org/x/exp/typeparams v0.0.0-20250210185358-939b2ce775ac // indirect
golang.org/x/mod v0.24.0 // indirect
golang.org/x/net v0.37.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,6 @@ github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUq
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/incu6us/goimports-reviser/v3 v3.8.2 h1:OYs6hqJ3oaAR0X7jMszIM/tcxMw2l/gkB2C/VGcItdE=
github.com/incu6us/goimports-reviser/v3 v3.8.2/go.mod h1:r0jYpyePwPYiqxl4qjZ0xZgVEPKS/ePqVCT3XNuwR54=
github.com/jgautheron/goconst v1.7.1 h1:VpdAG7Ca7yvvJk5n8dMwQhfEZJh95kl/Hl9S1OI5Jkk=
github.com/jgautheron/goconst v1.7.1/go.mod h1:aAosetZ5zaeC/2EfMeRswtxUFBpe2Hr7HzkgX4fanO4=
github.com/jingyugao/rowserrcheck v1.1.1 h1:zibz55j/MJtLsjP1OF4bSdgXxwL1b+Vn7Tjzq7gFzUs=
Expand Down
6 changes: 4 additions & 2 deletions pkg/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,17 @@ func New(log logr.Logger, cmdLine string, args ...string) *Process {

// Start the child process
func (p *Process) Start() error {

if err := p.Cmd.Start(); err != nil {
return err
}

p.log.Info("Start", "process", p.String(), "pid", p.Process.Pid)

return nil
}

// Stop the child process
func (p *Process) Stop() error {

p.log.Info("Send SIGINT signal", "pid", p.Process.Pid)

err := p.Process.Signal(syscall.SIGINT)
Expand All @@ -63,12 +62,15 @@ func (p *Process) Stop() error {
select {
case <-time.After(terminationTimeout):
p.log.Info("Timeout exceeded, sending SIGKILL signal")

return p.Process.Kill()
case err := <-done:
if exitErr, ok := err.(*exec.ExitError); ok {
p.log.Error(err, "Process exited", "pid", p.Process.Pid, "exitCode", exitErr.ExitCode())

return nil
}

return err
}
}
Loading