Skip to content

Commit 8db57b9

Browse files
authored
ci(lint): add two more linters gocritic and errorlint (#69)
* ci(lint): add two more linters gocritic and errorlint * fix
1 parent b1d0604 commit 8db57b9

26 files changed

+98
-122
lines changed

.golangci.yaml

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,27 @@ linters:
1111
- unconvert
1212
- unparam
1313
- whitespace
14+
- gocritic
15+
- errorlint
1416
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
3417
govet:
3518
enable:
3619
- shadow
3720
misspell:
3821
locale: US
39-
revive:
40-
confidence: 0.1
41-
enable-all-rules: true
4222
staticcheck:
4323
checks:
4424
- "-QF1008"
25+
gocritic:
26+
enable-all: true
27+
disabled-checks:
28+
- emptyStringTest
29+
- ifElseChain
30+
- singleCaseSwitch
31+
- hugeParam
32+
- unnamedResult
33+
- whyNoLint
34+
- tooManyResultsChecker
4535
exclusions:
4636
generated: lax
4737
presets:
@@ -51,11 +41,8 @@ linters:
5141
- std-error-handling
5242
rules:
5343
- linters:
54-
- dupl
55-
- errcheck
56-
- gocyclo
57-
- gosec
58-
- unparam
44+
- errcheck
45+
- unparam
5946
path: _test.go
6047
- linters:
6148
- govet
@@ -88,3 +75,4 @@ formatters:
8875

8976
run:
9077
allow-parallel-runners: true
78+
timeout: 10m

cache/cache.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"context"
2222
"fmt"
2323
"reflect"
24+
"slices"
2425
"strings"
2526

2627
apimeta "k8s.io/apimachinery/pkg/api/meta"
@@ -92,7 +93,7 @@ func (c *cacheSupportDisableDeepCopy) ListWithoutDeepCopy(ctx context.Context, l
9293
}
9394

9495
indexer := sharedInformer.GetIndexer()
95-
var objs []interface{}
96+
var objs []any
9697

9798
switch {
9899
case listOpts.FieldSelector != nil:
@@ -149,8 +150,8 @@ func (c *cacheSupportDisableDeepCopy) ListWithoutDeepCopy(ctx context.Context, l
149150
// objectTypeForListObject tries to find the runtime.Object and associated GVK
150151
// for a single object corresponding to the passed-in list type. We need them
151152
// because they are used as cache map key.
152-
func (ip *cacheSupportDisableDeepCopy) objectTypeForListObject(list client.ObjectList) (*schema.GroupVersionKind, runtime.Object, error) {
153-
gvk, err := apiutil.GVKForObject(list, ip.scheme)
153+
func (c *cacheSupportDisableDeepCopy) objectTypeForListObject(list client.ObjectList) (*schema.GroupVersionKind, runtime.Object, error) {
154+
gvk, err := apiutil.GVKForObject(list, c.scheme)
154155
if err != nil {
155156
return nil, nil, err
156157
}
@@ -203,10 +204,10 @@ func RequiresExactMatch(sel fields.Selector) bool {
203204
return true
204205
}
205206

206-
func byIndexes(indexer clienttoolcache.Indexer, requires fields.Requirements, namespace string) ([]interface{}, error) {
207+
func byIndexes(indexer clienttoolcache.Indexer, requires fields.Requirements, namespace string) ([]any, error) {
207208
var (
208209
err error
209-
objs []interface{}
210+
objs []any
210211
vals []string
211212
)
212213
indexers := indexer.GetIndexers()
@@ -230,17 +231,14 @@ func byIndexes(indexer clienttoolcache.Indexer, requires fields.Requirements, na
230231
if !exist {
231232
return nil, fmt.Errorf("index with name %s does not exist", indexName)
232233
}
233-
filteredObjects := make([]interface{}, 0, len(objs))
234+
filteredObjects := make([]any, 0, len(objs))
234235
for _, obj := range objs {
235236
vals, err = fn(obj)
236237
if err != nil {
237238
return nil, err
238239
}
239-
for _, val := range vals {
240-
if val == indexedValue {
241-
filteredObjects = append(filteredObjects, obj)
242-
break
243-
}
240+
if slices.Contains(vals, indexedValue) {
241+
filteredObjects = append(filteredObjects, obj)
244242
}
245243
}
246244
if len(filteredObjects) == 0 {

cert/cert.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ import (
2424
"net"
2525
"time"
2626

27-
"github.com/zoumo/golib/cert"
2827
certutil "github.com/zoumo/golib/cert"
2928
)
3029

3130
type (
32-
Config = cert.Config
33-
AltNames = cert.AltNames
31+
Config = certutil.Config
32+
AltNames = certutil.AltNames
3433
)
3534

3635
// ServingCerts is a set of serving certificates.
@@ -56,7 +55,7 @@ func (c *ServingCerts) Validate(host string) error {
5655
return fmt.Errorf("CA certificate is empty")
5756
}
5857

59-
tlsCert, err := cert.X509KeyPair(c.Cert, c.Key)
58+
tlsCert, err := certutil.X509KeyPair(c.Cert, c.Key)
6059
if err != nil {
6160
return fmt.Errorf("invalid x509 keypair: %w", err)
6261
}
@@ -99,7 +98,7 @@ func GenerateSelfSignedCerts(cfg Config) (*ServingCerts, error) {
9998

10099
// GenerateSelfSignedCertKeyIfNotExist generates a self-signed certificate and
101100
// write them to the given path if not exist.
102-
func GenerateSelfSignedCertKeyIfNotExist(path string, cfg cert.Config) error {
101+
func GenerateSelfSignedCertKeyIfNotExist(path string, cfg Config) error {
103102
fscerts, err := NewFSCertProvider(path, FSOptions{})
104103
if err != nil {
105104
return err

cert/error.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
var errNotFound = errors.New("not found")
2727

2828
func newNotFound(name string, err error) error {
29-
return fmt.Errorf("%s %w: %v", name, errNotFound, err)
29+
return fmt.Errorf("%s %w: %w", name, errNotFound, err)
3030
}
3131

3232
// IsNotFound returns true if certificate not found.

cert/fs.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,28 +182,28 @@ func (p *FSCertProvider) Overwrite(certs *ServingCerts) (bool, error) {
182182
var updated bool
183183
changed, err := p.writeFile(keyPath, certs.Key)
184184
if err != nil {
185-
return false, fmt.Errorf("failed to write key to %s: %v", keyPath, err)
185+
return false, fmt.Errorf("failed to write key to %s: %w", keyPath, err)
186186
}
187187
updated = changed || updated
188188

189189
certPath := path.Join(p.path, p.CertName)
190190
changed, err = p.writeFile(certPath, certs.Cert)
191191
if err != nil {
192-
return false, fmt.Errorf("failed to write cert to %s: %v", certPath, err)
192+
return false, fmt.Errorf("failed to write cert to %s: %w", certPath, err)
193193
}
194194
updated = changed || updated
195195

196196
caKeyPath := path.Join(p.path, p.CAKeyName)
197197
changed, err = p.writeFile(caKeyPath, certs.CAKey)
198198
if err != nil {
199-
return false, fmt.Errorf("failed to write ca key to %s: %v", caKeyPath, err)
199+
return false, fmt.Errorf("failed to write ca key to %s: %w", caKeyPath, err)
200200
}
201201
updated = changed || updated
202202

203203
caCertPath := path.Join(p.path, p.CACertName)
204204
changed, err = p.writeFile(caCertPath, certs.CACert)
205205
if err != nil {
206-
return false, fmt.Errorf("failed to write ca cert to %s: %v", caCertPath, err)
206+
return false, fmt.Errorf("failed to write ca cert to %s: %w", caCertPath, err)
207207
}
208208
updated = changed || updated
209209
return updated, nil

client/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func UpdateOnConflict[T client.Object](
5656
err = retry.RetryOnConflict(retry.DefaultBackoff, func() error {
5757
if !first {
5858
// refresh object
59-
if innerErr := reader.Get(ctx, key, original); err != nil {
59+
if innerErr := reader.Get(ctx, key, original); innerErr != nil {
6060
return innerErr
6161
}
6262
} else {

condition/helper.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func SetCondition(conditions []metav1.Condition, condition metav1.Condition) []m
6060

6161
func RejectConditionByType(conditions []metav1.Condition, ctype string) []metav1.Condition {
6262
result := []metav1.Condition{}
63+
6364
for i := range conditions {
6465
c := conditions[i]
6566
if c.Type == ctype {

controller/expectations/controller_expectation.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const (
5454
// * Controllers that don't set expectations will get woken up for every matching controllee
5555

5656
// ExpKeyFunc to parse out the key from a ControlleeExpectation
57-
var ExpKeyFunc = func(obj interface{}) (string, error) {
57+
var ExpKeyFunc = func(obj any) (string, error) {
5858
if e, ok := obj.(ExpectationKey); ok {
5959
return e.Key(), nil
6060
}
@@ -138,13 +138,6 @@ func (r *ControllerExpectations) SatisfiedExpectations(controllerKey string) boo
138138
return true
139139
}
140140

141-
// TODO: Extend ExpirationCache to support explicit expiration.
142-
// TODO: Make this possible to disable in tests.
143-
// TODO: Support injection of clock.
144-
func (exp *ControlleeExpectations) isExpired() bool {
145-
return clock.RealClock{}.Since(exp.timestamp) > ExpectationsTimeout
146-
}
147-
148141
// SetExpectations registers new expectations for the given controller. Forgets existing expectations.
149142
func (r *ControllerExpectations) SetExpectations(controllerKey string, add, del int) error {
150143
exp := &ControlleeExpectations{add: int64(add), del: int64(del), key: controllerKey, timestamp: clock.RealClock{}.Now()}
@@ -200,6 +193,11 @@ type ControlleeExpectations struct {
200193
timestamp time.Time
201194
}
202195

196+
// NewControllerExpectations returns a store for ControllerExpectations.
197+
func NewControllerExpectations() *ControllerExpectations {
198+
return &ControllerExpectations{cache.NewStore(ExpKeyFunc)}
199+
}
200+
203201
// Key implements ExpectationKey interface
204202
func (e *ControlleeExpectations) Key() string {
205203
return e.key
@@ -222,7 +220,9 @@ func (e *ControlleeExpectations) GetExpectations() (int64, int64) {
222220
return atomic.LoadInt64(&e.add), atomic.LoadInt64(&e.del)
223221
}
224222

225-
// NewControllerExpectations returns a store for ControllerExpectations.
226-
func NewControllerExpectations() *ControllerExpectations {
227-
return &ControllerExpectations{cache.NewStore(ExpKeyFunc)}
223+
// TODO: Extend ExpirationCache to support explicit expiration.
224+
// TODO: Make this possible to disable in tests.
225+
// TODO: Support injection of clock.
226+
func (exp *ControlleeExpectations) isExpired() bool {
227+
return clock.RealClock{}.Since(exp.timestamp) > ExpectationsTimeout
228228
}

controller/history/revision_control.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323

2424
"github.com/go-logr/logr"
2525
"github.com/samber/lo"
26-
apps "k8s.io/api/apps/v1"
2726
appsv1 "k8s.io/api/apps/v1"
2827
"k8s.io/apimachinery/pkg/api/errors"
2928
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -52,22 +51,22 @@ type RevisionControlInterface interface {
5251
// error is not nil, creation failed. If the returned error is nil, the returned ControllerRevision has been
5352
// created.
5453
// Callers must make sure that collisionCount is not nil. An error is returned if it is.
55-
CreateControllerRevision(ctx context.Context, parent metav1.Object, revision *apps.ControllerRevision, collisionCount *int32) (*apps.ControllerRevision, error)
54+
CreateControllerRevision(ctx context.Context, parent metav1.Object, revision *appsv1.ControllerRevision, collisionCount *int32) (*appsv1.ControllerRevision, error)
5655
// DeleteControllerRevision attempts to delete revision. If the returned error is not nil, deletion has failed.
57-
DeleteControllerRevision(ctx context.Context, revision *apps.ControllerRevision) error
56+
DeleteControllerRevision(ctx context.Context, revision *appsv1.ControllerRevision) error
5857
// UpdateControllerRevision updates revision such that its Revision is equal to newRevision. Implementations
5958
// may retry on conflict. If the returned error is nil, the update was successful and returned ControllerRevision
6059
// is valid. If the returned error is not nil, the update failed and the returned ControllerRevision is invalid.
61-
UpdateControllerRevision(ctx context.Context, revision *apps.ControllerRevision, newRevision int64) (*apps.ControllerRevision, error)
60+
UpdateControllerRevision(ctx context.Context, revision *appsv1.ControllerRevision, newRevision int64) (*appsv1.ControllerRevision, error)
6261
// AdoptControllerRevision attempts to adopt revision by adding a ControllerRef indicating that the parent
6362
// Object of parentKind is the owner of revision. If revision is already owned, an error is returned. If the
6463
// resource patch fails, an error is returned. If no error is returned, the returned ControllerRevision is
6564
// valid.
66-
AdoptControllerRevision(ctx context.Context, parent metav1.Object, parentKind schema.GroupVersionKind, revision *apps.ControllerRevision) (*apps.ControllerRevision, error)
65+
AdoptControllerRevision(ctx context.Context, parent metav1.Object, parentKind schema.GroupVersionKind, revision *appsv1.ControllerRevision) (*appsv1.ControllerRevision, error)
6766
// ReleaseControllerRevision attempts to release parent's ownership of revision by removing parent from the
6867
// OwnerReferences of revision. If an error is returned, parent remains the owner of revision. If no error is
6968
// returned, the returned ControllerRevision is valid.
70-
ReleaseControllerRevision(ctx context.Context, parent metav1.Object, revision *apps.ControllerRevision) (*apps.ControllerRevision, error)
69+
ReleaseControllerRevision(ctx context.Context, parent metav1.Object, revision *appsv1.ControllerRevision) (*appsv1.ControllerRevision, error)
7170
}
7271

7372
var _ RevisionControlInterface = &realRevisionControl{}
@@ -103,7 +102,7 @@ func (h *realRevisionControl) ListControllerRevisions(ctx context.Context, paren
103102
return result, nil
104103
}
105104

106-
func (h *realRevisionControl) CreateControllerRevision(ctx context.Context, parent metav1.Object, revision *apps.ControllerRevision, collisionCount *int32) (*apps.ControllerRevision, error) {
105+
func (h *realRevisionControl) CreateControllerRevision(ctx context.Context, parent metav1.Object, revision *appsv1.ControllerRevision, collisionCount *int32) (*appsv1.ControllerRevision, error) {
107106
if collisionCount == nil {
108107
return nil, fmt.Errorf("collisionCount should not be nil")
109108
}
@@ -126,7 +125,7 @@ func (h *realRevisionControl) CreateControllerRevision(ctx context.Context, pare
126125
clone.Name = ControllerRevisionName(parent.GetName(), hash)
127126
err = h.writer.Create(ctx, clone)
128127
if errors.IsAlreadyExists(err) {
129-
exists := &apps.ControllerRevision{}
128+
exists := &appsv1.ControllerRevision{}
130129
err := h.reader.Get(ctx, types.NamespacedName{Namespace: clone.Namespace, Name: clone.Name}, exists)
131130
if err != nil {
132131
return nil, err
@@ -151,7 +150,7 @@ func (h *realRevisionControl) CreateControllerRevision(ctx context.Context, pare
151150
func (h *realRevisionControl) UpdateControllerRevision(ctx context.Context, revision *appsv1.ControllerRevision, newRevision int64) (*appsv1.ControllerRevision, error) {
152151
clone := revision.DeepCopy()
153152
oldRevision := clone.Revision
154-
changed, err := clientutil.UpdateOnConflict(ctx, h.reader, h.writer, clone, func(obj *apps.ControllerRevision) error {
153+
changed, err := clientutil.UpdateOnConflict(ctx, h.reader, h.writer, clone, func(obj *appsv1.ControllerRevision) error {
155154
oldRevision = obj.Revision
156155
obj.Revision = newRevision
157156
return nil
@@ -163,7 +162,7 @@ func (h *realRevisionControl) UpdateControllerRevision(ctx context.Context, revi
163162
return clone, err
164163
}
165164

166-
func (h *realRevisionControl) DeleteControllerRevision(ctx context.Context, revision *apps.ControllerRevision) error {
165+
func (h *realRevisionControl) DeleteControllerRevision(ctx context.Context, revision *appsv1.ControllerRevision) error {
167166
err := h.writer.Delete(ctx, revision)
168167
if err == nil {
169168
logger := logr.FromContextOrDiscard(ctx)
@@ -173,7 +172,7 @@ func (h *realRevisionControl) DeleteControllerRevision(ctx context.Context, revi
173172
return client.IgnoreNotFound(err)
174173
}
175174

176-
func (h *realRevisionControl) AdoptControllerRevision(ctx context.Context, parent metav1.Object, parentKind schema.GroupVersionKind, revision *apps.ControllerRevision) (*apps.ControllerRevision, error) {
175+
func (h *realRevisionControl) AdoptControllerRevision(ctx context.Context, parent metav1.Object, parentKind schema.GroupVersionKind, revision *appsv1.ControllerRevision) (*appsv1.ControllerRevision, error) {
177176
oldOwner := metav1.GetControllerOfNoCopy(revision)
178177
newOwner := metav1.NewControllerRef(parent, parentKind)
179178

@@ -194,7 +193,7 @@ func (h *realRevisionControl) AdoptControllerRevision(ctx context.Context, paren
194193
return revision, err
195194
}
196195

197-
func (h *realRevisionControl) ReleaseControllerRevision(ctx context.Context, parent metav1.Object, revision *apps.ControllerRevision) (*apps.ControllerRevision, error) {
196+
func (h *realRevisionControl) ReleaseControllerRevision(ctx context.Context, parent metav1.Object, revision *appsv1.ControllerRevision) (*appsv1.ControllerRevision, error) {
198197
w := refmanager.NewOwnerRefWriter(h.writer)
199198
err := w.Release(ctx, parent, revision)
200199
return revision, err

controller/history/revision_control_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ func (s *revisionControlTestSuite) TestCreateRevisionsConflict1() {
123123
s.EqualValues(1, collisionCount, "create revision conflict, collisionCount should be 1")
124124
s.NotEqual(revision.Name, created.Name, "create revision conflict, revision name should be changed")
125125
s.Equal(revision.Labels[ControllerRevisionHashLabel], created.Labels[ControllerRevisionHashLabel], "the hash label should be the same")
126-
// s.True(strings.HasSuffix(created.Name, created.Labels[ControllerRevisionHashLabel]), "the suffix of revision name should be the same with hash")
127126

128127
list := &appsv1.ControllerRevisionList{}
129128
err = s.client.List(context.Background(), list, client.InNamespace(s.testObj.Namespace))
@@ -146,7 +145,6 @@ func (s *revisionControlTestSuite) TestCreateRevisionsConflict2() {
146145
s.EqualValues(1, collisionCount, "create revision conflict, collisionCount should be 1")
147146
s.NotEqual(revision.Name, created.Name)
148147
s.Equal(revision.Labels[ControllerRevisionHashLabel], created.Labels[ControllerRevisionHashLabel], "the hash label should be the same")
149-
// s.True(strings.HasSuffix(created.Name, created.Labels[ControllerRevisionHashLabel]), "the suffix of revision name should be the same with hash")
150148

151149
list := &appsv1.ControllerRevisionList{}
152150
err = s.client.List(context.Background(), list, client.InNamespace(s.testObj.Namespace))

controller/history/revision_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"testing"
2121

2222
"github.com/stretchr/testify/assert"
23-
apps "k8s.io/api/apps/v1"
2423
appsv1 "k8s.io/api/apps/v1"
2524
"k8s.io/utils/ptr"
2625
)
@@ -78,8 +77,8 @@ func TestHashControllerRevisionRawData(t *testing.T) {
7877
func TestEqualRevision(t *testing.T) {
7978
tests := []struct {
8079
name string
81-
lrv *apps.ControllerRevision
82-
rrv *apps.ControllerRevision
80+
lrv *appsv1.ControllerRevision
81+
rrv *appsv1.ControllerRevision
8382
want bool
8483
}{
8584
{

0 commit comments

Comments
 (0)