Skip to content

Commit b1d0604

Browse files
authored
feat: add cache expectations (#68)
* feat: add cache expectations * fix code according to lint on go 1.22 * feat: upgrade golangci-lint to v2 * fix lint * fix: format by gofumpt
1 parent 354982b commit b1d0604

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1072
-627
lines changed

.github/workflows/ci.yaml

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,21 @@ on:
1212
- release-*
1313

1414
env:
15-
GO_VERSION: '1.19'
15+
GO_VERSION: '1.23'
1616

1717
jobs:
1818

1919
Test:
2020
name: Unit Test
2121
runs-on: ubuntu-latest
2222
steps:
23-
- uses: actions/checkout@v3
24-
with:
25-
submodules: true
26-
- name: Fetch History
27-
run: git fetch --prune --unshallow
28-
- name: Setup Go
29-
uses: actions/setup-go@v3
23+
- uses: actions/checkout@v4
24+
- name: Set up Go
25+
uses: actions/setup-go@v5
3026
with:
3127
go-version: ${{ env.GO_VERSION }}
32-
- name: Cache Go Dependencies
33-
uses: actions/cache@v4
34-
with:
35-
path: ~/go/pkg/mod
36-
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
37-
restore-keys: ${{ runner.os }}-go-
28+
check-latest: true
29+
cache-dependency-path: "**/*.sum"
3830
- name: Run Unit Tests
3931
run: |
4032
make test
@@ -45,19 +37,18 @@ jobs:
4537
runs-on: ubuntu-latest
4638
steps:
4739
- name: Checkout
48-
uses: actions/checkout@v3
49-
with:
50-
fetch-depth: 0
40+
uses: actions/checkout@v4
5141
- name: Set up Go
52-
uses: actions/setup-go@v4
42+
uses: actions/setup-go@v5
5343
with:
5444
go-version: ${{ env.GO_VERSION }}
55-
cache: false
45+
check-latest: true
46+
cache-dependency-path: "**/*.sum"
5647
- name: golangci-lint
57-
uses: golangci/golangci-lint-action@v3
48+
uses: golangci/golangci-lint-action@v7
5849
with:
5950
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
60-
version: v1.53
51+
version: v2.0.2
6152
args: --timeout=30m
6253

6354
LicenseCheck:
@@ -66,9 +57,7 @@ jobs:
6657
env:
6758
TERM: xterm
6859
steps:
69-
- uses: actions/checkout@v3
70-
with:
71-
fetch-depth: 0
60+
- uses: actions/checkout@v4
7261
- uses: apache/skywalking-eyes@main
7362

7463
# The TruffleHog OSS Github Action can be used to scan a range of commits for leaked credentials. The action will fail if any results are found.

.golangci.yaml

Lines changed: 81 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,90 @@
1-
# Copyright 2023 The KusionStack Authors
2-
#
3-
# Licensed under the Apache License, Version 2.0 (the "License");
4-
# you may not use this file except in compliance with the License.
5-
# You may obtain a copy of the License at
6-
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
8-
#
9-
# Unless required by applicable law or agreed to in writing, software
10-
# distributed under the License is distributed on an "AS IS" BASIS,
11-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
# See the License for the specific language governing permissions and
13-
# limitations under the License.
14-
15-
linters-settings:
16-
govet:
17-
shadow: true
18-
golint:
19-
min-confidence: 0
20-
gocyclo:
21-
min-complexity: 15
22-
maligned:
23-
suggest-new: true
24-
dupl:
25-
threshold: 100
26-
misspell:
27-
locale: US
28-
lll:
29-
line-length: 140
30-
goimports:
31-
local-prefixes: kusionstack.io/kube-utils
32-
gocritic:
33-
enabled-tags:
34-
- diagnostic
35-
- experimental
36-
- opinionated
37-
- performance
38-
- style
39-
disabled-checks:
40-
- dupImport # https://github.com/go-critic/go-critic/issues/845
41-
- ifElseChain
42-
- octalLiteral
43-
- wrapperFunc
44-
funlen:
45-
lines: 100
46-
statements: 50
1+
version: "2"
472
linters:
48-
disable-all: true
3+
default: standard
494
enable:
50-
## for bug
515
- bodyclose
52-
- errcheck
53-
- govet
54-
- ineffassign
55-
- exportloopref
56-
- staticcheck
57-
- typecheck
58-
59-
## for complexity
60-
# - gocyclo
6+
- copyloopvar
7+
- ginkgolinter
8+
- testifylint
9+
- misspell
6110
- nakedret
62-
- gosimple
63-
64-
## for format
65-
- gofmt
66-
- goimports
67-
68-
## for style
6911
- unconvert
70-
# - goconst
71-
- misspell
7212
- unparam
73-
# - revive
7413
- whitespace
75-
# - goerr113
76-
77-
## for unused
78-
- unused
79-
- unparam
14+
settings:
15+
dupl:
16+
threshold: 100
17+
funlen:
18+
lines: 100
19+
statements: 50
20+
gocritic:
21+
disabled-checks:
22+
- dupImport
23+
- ifElseChain
24+
- octalLiteral
25+
- wrapperFunc
26+
enabled-tags:
27+
- diagnostic
28+
- experimental
29+
- opinionated
30+
- performance
31+
- style
32+
gocyclo:
33+
min-complexity: 15
34+
govet:
35+
enable:
36+
- shadow
37+
misspell:
38+
locale: US
39+
revive:
40+
confidence: 0.1
41+
enable-all-rules: true
42+
staticcheck:
43+
checks:
44+
- "-QF1008"
45+
exclusions:
46+
generated: lax
47+
presets:
48+
- comments
49+
- common-false-positives
50+
- legacy
51+
- std-error-handling
52+
rules:
53+
- linters:
54+
- dupl
55+
- errcheck
56+
- gocyclo
57+
- gosec
58+
- unparam
59+
path: _test.go
60+
- linters:
61+
- govet
62+
text: declaration of "err" shadows declaration at line
63+
paths:
64+
- third_party$
65+
- builtin$
66+
- examples$
67+
formatters:
68+
enable:
69+
- gofumpt # drop in replacement for gofmt
70+
- gci # format imports
71+
settings:
72+
gci:
73+
custom-order: true
74+
sections:
75+
- standard
76+
- blank
77+
- dot
78+
- default
79+
- localmodule
80+
gofumpt:
81+
extra-rules: true
82+
exclusions:
83+
generated: lax
84+
paths:
85+
- third_party$
86+
- builtin$
87+
- examples$
8088

8189
run:
82-
deadline: 10m
83-
# default is true. Enables skipping of directories:
84-
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
85-
skip-dirs-use-default: true
86-
87-
issues:
88-
exclude-use-default: true
89-
exclude-rules:
90-
- path: _test.go
91-
linters:
92-
- errcheck
93-
- unparam
94-
- gocyclo
95-
- dupl
96-
- gosec
97-
98-
# enable shadow declaration for err
99-
- linters:
100-
- govet
101-
text: declaration of "err" shadows declaration at line
102-
103-
# - linters:
104-
# - govet
105-
# text: composite literal uses unkeyed fields
90+
allow-parallel-runners: true

Makefile

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
22
ENVTEST_K8S_VERSION = 1.24.1
33

4-
.PHONY: fmt
5-
fmt: ## Run go fmt against code.
6-
go fmt ./...
7-
8-
.PHONY: vet
9-
vet: ## Run go vet against code.
10-
go vet ./...
4+
# Setting SHELL to bash allows bash commands to be executed by recipes.
5+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
6+
SHELL = /usr/bin/env bash -o pipefail
7+
.SHELLFLAGS = -ec
118

129
.PHONY: test
13-
test: fmt vet envtest ## Run tests.
10+
test: envtest ## Run tests.
1411
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
1512

13+
.PHONY: lint
14+
lint: golangci
15+
$(GOLANGCI) run
16+
1617
##@ Build Dependencies
1718

1819
## Location to install dependencies to
1920
LOCALBIN ?= $(shell pwd)/bin
2021
$(LOCALBIN):
2122
mkdir -p $(LOCALBIN)
2223

24+
2325
ENVTEST ?= $(LOCALBIN)/setup-envtest
26+
GOLANGCI ?= $(LOCALBIN)/golangci-lint
2427

2528
.PHONY: envtest
2629
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
2730
$(ENVTEST): $(LOCALBIN)
2831
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/[email protected]
32+
33+
GOLANGCI_VERSION ?= v2.0.2
34+
35+
.PHONY: golangci
36+
golangci: $(GOLANGCI)
37+
$(GOLANGCI): $(LOCALBIN)
38+
@test -s $(LOCALBIN)/golangci-lint || GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_VERSION)

cache/cache.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (ip *cacheSupportDisableDeepCopy) objectTypeForListObject(list client.Objec
174174
// http://knowyourmeme.com/memes/this-is-fine
175175
elemType := reflect.Indirect(reflect.ValueOf(itemsPtr)).Type().Elem()
176176
if elemType.Kind() != reflect.Ptr {
177-
elemType = reflect.PtrTo(elemType)
177+
elemType = reflect.PointerTo(elemType)
178178
}
179179

180180
cacheTypeValue := reflect.Zero(elemType)
@@ -262,7 +262,7 @@ const allNamespacesNamespace = "__all_namespaces"
262262

263263
// KeyToNamespacedKey prefixes the given index key with a namespace
264264
// for use in field selector indexes.
265-
func KeyToNamespacedKey(ns string, baseKey string) string {
265+
func KeyToNamespacedKey(ns, baseKey string) string {
266266
if ns != "" {
267267
return ns + "/" + baseKey
268268
}
@@ -286,8 +286,7 @@ type ObjectWithoutDeepCopy struct {
286286

287287
var DisableDeepCopy = &disableDeepCopyListOption{}
288288

289-
type disableDeepCopyListOption struct {
290-
}
289+
type disableDeepCopyListOption struct{}
291290

292291
func (o *disableDeepCopyListOption) ApplyToList(*client.ListOptions) {
293292
}

0 commit comments

Comments
 (0)