Skip to content

Commit c7a98aa

Browse files
authored
🌱 enable errorlint and unused linters (#1890)
* use %w instead of %v for wrapping errors * do not use == or != to compare errors * remove unused code * fix formatting
1 parent 236ad94 commit c7a98aa

File tree

18 files changed

+47
-63
lines changed

18 files changed

+47
-63
lines changed

.golangci.yml

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ linters:
77
- depguard
88
- dogsled
99
- errcheck
10+
- errorlint
1011
- exportloopref
1112
- goconst
1213
- gocritic
@@ -34,6 +35,7 @@ linters:
3435
- typecheck
3536
- unconvert
3637
- unparam
38+
- unused
3739
- varcheck
3840
- whitespace
3941

pkg/cache/multi_namespace_cache.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func MultiNamespacedCacheBuilder(namespaces []string) NewCacheFunc {
5555
// create a cache for cluster scoped resources
5656
gCache, err := New(config, opts)
5757
if err != nil {
58-
return nil, fmt.Errorf("error creating global cache %v", err)
58+
return nil, fmt.Errorf("error creating global cache: %w", err)
5959
}
6060

6161
for _, ns := range namespaces {

pkg/client/config/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func loadConfig(context string) (*rest.Config, error) {
123123
if _, ok := os.LookupEnv("HOME"); !ok {
124124
u, err := user.Current()
125125
if err != nil {
126-
return nil, fmt.Errorf("could not get current user: %v", err)
126+
return nil, fmt.Errorf("could not get current user: %w", err)
127127
}
128128
loadingRules.Precedence = append(loadingRules.Precedence, filepath.Join(u.HomeDir, clientcmd.RecommendedHomeDir, clientcmd.RecommendedFileName))
129129
}

pkg/client/fake/client.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func (t versionedTracker) Add(obj runtime.Object) error {
215215
func (t versionedTracker) Create(gvr schema.GroupVersionResource, obj runtime.Object, ns string) error {
216216
accessor, err := meta.Accessor(obj)
217217
if err != nil {
218-
return fmt.Errorf("failed to get accessor for object: %v", err)
218+
return fmt.Errorf("failed to get accessor for object: %w", err)
219219
}
220220
if accessor.GetName() == "" {
221221
return apierrors.NewInvalid(
@@ -269,7 +269,7 @@ func convertFromUnstructuredIfNecessary(s *runtime.Scheme, o runtime.Object) (ru
269269
func (t versionedTracker) Update(gvr schema.GroupVersionResource, obj runtime.Object, ns string) error {
270270
accessor, err := meta.Accessor(obj)
271271
if err != nil {
272-
return fmt.Errorf("failed to get accessor for object: %v", err)
272+
return fmt.Errorf("failed to get accessor for object: %w", err)
273273
}
274274

275275
if accessor.GetName() == "" {
@@ -315,7 +315,7 @@ func (t versionedTracker) Update(gvr schema.GroupVersionResource, obj runtime.Ob
315315
}
316316
intResourceVersion, err := strconv.ParseUint(oldAccessor.GetResourceVersion(), 10, 64)
317317
if err != nil {
318-
return fmt.Errorf("can not convert resourceVersion %q to int: %v", oldAccessor.GetResourceVersion(), err)
318+
return fmt.Errorf("can not convert resourceVersion %q to int: %w", oldAccessor.GetResourceVersion(), err)
319319
}
320320
intResourceVersion++
321321
accessor.SetResourceVersion(strconv.FormatUint(intResourceVersion, 10))

pkg/client/namespaced_client.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (n *namespacedClient) RESTMapper() meta.RESTMapper {
5656
func (n *namespacedClient) Create(ctx context.Context, obj Object, opts ...CreateOption) error {
5757
isNamespaceScoped, err := objectutil.IsAPINamespaced(obj, n.Scheme(), n.RESTMapper())
5858
if err != nil {
59-
return fmt.Errorf("error finding the scope of the object: %v", err)
59+
return fmt.Errorf("error finding the scope of the object: %w", err)
6060
}
6161

6262
objectNamespace := obj.GetNamespace()
@@ -74,7 +74,7 @@ func (n *namespacedClient) Create(ctx context.Context, obj Object, opts ...Creat
7474
func (n *namespacedClient) Update(ctx context.Context, obj Object, opts ...UpdateOption) error {
7575
isNamespaceScoped, err := objectutil.IsAPINamespaced(obj, n.Scheme(), n.RESTMapper())
7676
if err != nil {
77-
return fmt.Errorf("error finding the scope of the object: %v", err)
77+
return fmt.Errorf("error finding the scope of the object: %w", err)
7878
}
7979

8080
objectNamespace := obj.GetNamespace()
@@ -92,7 +92,7 @@ func (n *namespacedClient) Update(ctx context.Context, obj Object, opts ...Updat
9292
func (n *namespacedClient) Delete(ctx context.Context, obj Object, opts ...DeleteOption) error {
9393
isNamespaceScoped, err := objectutil.IsAPINamespaced(obj, n.Scheme(), n.RESTMapper())
9494
if err != nil {
95-
return fmt.Errorf("error finding the scope of the object: %v", err)
95+
return fmt.Errorf("error finding the scope of the object: %w", err)
9696
}
9797

9898
objectNamespace := obj.GetNamespace()
@@ -110,7 +110,7 @@ func (n *namespacedClient) Delete(ctx context.Context, obj Object, opts ...Delet
110110
func (n *namespacedClient) DeleteAllOf(ctx context.Context, obj Object, opts ...DeleteAllOfOption) error {
111111
isNamespaceScoped, err := objectutil.IsAPINamespaced(obj, n.Scheme(), n.RESTMapper())
112112
if err != nil {
113-
return fmt.Errorf("error finding the scope of the object: %v", err)
113+
return fmt.Errorf("error finding the scope of the object: %w", err)
114114
}
115115

116116
if isNamespaceScoped {
@@ -123,7 +123,7 @@ func (n *namespacedClient) DeleteAllOf(ctx context.Context, obj Object, opts ...
123123
func (n *namespacedClient) Patch(ctx context.Context, obj Object, patch Patch, opts ...PatchOption) error {
124124
isNamespaceScoped, err := objectutil.IsAPINamespaced(obj, n.Scheme(), n.RESTMapper())
125125
if err != nil {
126-
return fmt.Errorf("error finding the scope of the object: %v", err)
126+
return fmt.Errorf("error finding the scope of the object: %w", err)
127127
}
128128

129129
objectNamespace := obj.GetNamespace()
@@ -141,7 +141,7 @@ func (n *namespacedClient) Patch(ctx context.Context, obj Object, patch Patch, o
141141
func (n *namespacedClient) Get(ctx context.Context, key ObjectKey, obj Object) error {
142142
isNamespaceScoped, err := objectutil.IsAPINamespaced(obj, n.Scheme(), n.RESTMapper())
143143
if err != nil {
144-
return fmt.Errorf("error finding the scope of the object: %v", err)
144+
return fmt.Errorf("error finding the scope of the object: %w", err)
145145
}
146146
if isNamespaceScoped {
147147
if key.Namespace != "" && key.Namespace != n.namespace {
@@ -179,7 +179,7 @@ func (nsw *namespacedClientStatusWriter) Update(ctx context.Context, obj Object,
179179
isNamespaceScoped, err := objectutil.IsAPINamespaced(obj, nsw.namespacedclient.Scheme(), nsw.namespacedclient.RESTMapper())
180180

181181
if err != nil {
182-
return fmt.Errorf("error finding the scope of the object: %v", err)
182+
return fmt.Errorf("error finding the scope of the object: %w", err)
183183
}
184184

185185
objectNamespace := obj.GetNamespace()
@@ -198,7 +198,7 @@ func (nsw *namespacedClientStatusWriter) Patch(ctx context.Context, obj Object,
198198
isNamespaceScoped, err := objectutil.IsAPINamespaced(obj, nsw.namespacedclient.Scheme(), nsw.namespacedclient.RESTMapper())
199199

200200
if err != nil {
201-
return fmt.Errorf("error finding the scope of the object: %v", err)
201+
return fmt.Errorf("error finding the scope of the object: %w", err)
202202
}
203203

204204
objectNamespace := obj.GetNamespace()

pkg/envtest/crd.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bufio"
2121
"bytes"
2222
"context"
23+
"errors"
2324
"fmt"
2425
"io"
2526
"io/ioutil"
@@ -437,7 +438,7 @@ func readDocuments(fp string) ([][]byte, error) {
437438
// Read document
438439
doc, err := reader.Read()
439440
if err != nil {
440-
if err == io.EOF {
441+
if errors.Is(err, io.EOF) {
441442
break
442443
}
443444

pkg/envtest/webhook.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (o *WebhookInstallOptions) generateHostPort() (string, error) {
117117
if o.LocalServingPort == 0 {
118118
port, host, err := addr.Suggest(o.LocalServingHost)
119119
if err != nil {
120-
return "", fmt.Errorf("unable to grab random port for serving webhooks on: %v", err)
120+
return "", fmt.Errorf("unable to grab random port for serving webhooks on: %w", err)
121121
}
122122
o.LocalServingPort = port
123123
o.LocalServingHost = host
@@ -180,7 +180,7 @@ func WaitForWebhooks(config *rest.Config,
180180
h := hook
181181
gvk, err := apiutil.GVKForObject(h, scheme.Scheme)
182182
if err != nil {
183-
return fmt.Errorf("unable to get gvk for MutatingWebhookConfiguration %s: %v", hook.GetName(), err)
183+
return fmt.Errorf("unable to get gvk for MutatingWebhookConfiguration %s: %w", hook.GetName(), err)
184184
}
185185

186186
if _, ok := waitingFor[gvk]; !ok {
@@ -193,7 +193,7 @@ func WaitForWebhooks(config *rest.Config,
193193
h := hook
194194
gvk, err := apiutil.GVKForObject(h, scheme.Scheme)
195195
if err != nil {
196-
return fmt.Errorf("unable to get gvk for ValidatingWebhookConfiguration %s: %v", hook.GetName(), err)
196+
return fmt.Errorf("unable to get gvk for ValidatingWebhookConfiguration %s: %w", hook.GetName(), err)
197197
}
198198

199199
if _, ok := waitingFor[gvk]; !ok {
@@ -257,31 +257,31 @@ func (p *webhookPoller) poll() (done bool, err error) {
257257
func (o *WebhookInstallOptions) setupCA() error {
258258
hookCA, err := certs.NewTinyCA()
259259
if err != nil {
260-
return fmt.Errorf("unable to set up webhook CA: %v", err)
260+
return fmt.Errorf("unable to set up webhook CA: %w", err)
261261
}
262262

263263
names := []string{"localhost", o.LocalServingHost, o.LocalServingHostExternalName}
264264
hookCert, err := hookCA.NewServingCert(names...)
265265
if err != nil {
266-
return fmt.Errorf("unable to set up webhook serving certs: %v", err)
266+
return fmt.Errorf("unable to set up webhook serving certs: %w", err)
267267
}
268268

269269
localServingCertsDir, err := ioutil.TempDir("", "envtest-serving-certs-")
270270
o.LocalServingCertDir = localServingCertsDir
271271
if err != nil {
272-
return fmt.Errorf("unable to create directory for webhook serving certs: %v", err)
272+
return fmt.Errorf("unable to create directory for webhook serving certs: %w", err)
273273
}
274274

275275
certData, keyData, err := hookCert.AsBytes()
276276
if err != nil {
277-
return fmt.Errorf("unable to marshal webhook serving certs: %v", err)
277+
return fmt.Errorf("unable to marshal webhook serving certs: %w", err)
278278
}
279279

280280
if err := ioutil.WriteFile(filepath.Join(localServingCertsDir, "tls.crt"), certData, 0640); err != nil { //nolint:gosec
281-
return fmt.Errorf("unable to write webhook serving cert to disk: %v", err)
281+
return fmt.Errorf("unable to write webhook serving cert to disk: %w", err)
282282
}
283283
if err := ioutil.WriteFile(filepath.Join(localServingCertsDir, "tls.key"), keyData, 0640); err != nil { //nolint:gosec
284-
return fmt.Errorf("unable to write webhook serving key to disk: %v", err)
284+
return fmt.Errorf("unable to write webhook serving key to disk: %w", err)
285285
}
286286

287287
o.LocalServingCAData = certData

pkg/finalizer/finalizer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (f finalizers) Finalize(ctx context.Context, obj client.Object) (Result, er
6464
// object (e.g. it may set a condition and need a status update).
6565
res.Updated = res.Updated || finalizerRes.Updated
6666
res.StatusUpdated = res.StatusUpdated || finalizerRes.StatusUpdated
67-
errList = append(errList, fmt.Errorf("finalizer %q failed: %v", key, err))
67+
errList = append(errList, fmt.Errorf("finalizer %q failed: %w", key, err))
6868
} else {
6969
// If the finalizer succeeds, we remove the finalizer from the primary
7070
// object's metadata, so we know it will need an update.

pkg/internal/testing/certs/tinyca.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (k CertPair) AsBytes() (cert []byte, key []byte, err error) {
6464

6565
rawKeyData, err := x509.MarshalPKCS8PrivateKey(k.Key)
6666
if err != nil {
67-
return nil, nil, fmt.Errorf("unable to encode private key: %v", err)
67+
return nil, nil, fmt.Errorf("unable to encode private key: %w", err)
6868
}
6969

7070
key = pem.EncodeToMemory(&pem.Block{
@@ -95,12 +95,12 @@ func newPrivateKey() (crypto.Signer, error) {
9595
func NewTinyCA() (*TinyCA, error) {
9696
caPrivateKey, err := newPrivateKey()
9797
if err != nil {
98-
return nil, fmt.Errorf("unable to generate private key for CA: %v", err)
98+
return nil, fmt.Errorf("unable to generate private key for CA: %w", err)
9999
}
100100
caCfg := certutil.Config{CommonName: "envtest-environment", Organization: []string{"envtest"}}
101101
caCert, err := certutil.NewSelfSignedCACert(caCfg, caPrivateKey)
102102
if err != nil {
103-
return nil, fmt.Errorf("unable to generate certificate for CA: %v", err)
103+
return nil, fmt.Errorf("unable to generate certificate for CA: %w", err)
104104
}
105105

106106
return &TinyCA{
@@ -115,7 +115,7 @@ func (c *TinyCA) makeCert(cfg certutil.Config) (CertPair, error) {
115115

116116
key, err := newPrivateKey()
117117
if err != nil {
118-
return CertPair{}, fmt.Errorf("unable to create private key: %v", err)
118+
return CertPair{}, fmt.Errorf("unable to create private key: %w", err)
119119
}
120120

121121
serial := new(big.Int).Set(c.nextSerial)
@@ -140,12 +140,12 @@ func (c *TinyCA) makeCert(cfg certutil.Config) (CertPair, error) {
140140

141141
certRaw, err := x509.CreateCertificate(crand.Reader, &template, c.CA.Cert, key.Public(), c.CA.Key)
142142
if err != nil {
143-
return CertPair{}, fmt.Errorf("unable to create certificate: %v", err)
143+
return CertPair{}, fmt.Errorf("unable to create certificate: %w", err)
144144
}
145145

146146
cert, err := x509.ParseCertificate(certRaw)
147147
if err != nil {
148-
return CertPair{}, fmt.Errorf("generated invalid certificate, could not parse: %v", err)
148+
return CertPair{}, fmt.Errorf("generated invalid certificate, could not parse: %w", err)
149149
}
150150

151151
return CertPair{

pkg/internal/testing/process/arguments_test.go

-6
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,6 @@ var _ = Describe("Arguments Templates", func() {
225225
})
226226
})
227227

228-
type plainDefaults map[string][]string
229-
230-
func (d plainDefaults) DefaultArgs() map[string][]string {
231-
return d
232-
}
233-
234228
var _ = Describe("Arguments", func() {
235229
Context("when appending", func() {
236230
It("should copy from defaults when appending for the first time", func() {

pkg/manager/internal.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -457,21 +457,21 @@ func (cm *controllerManager) Start(ctx context.Context) (err error) {
457457
// between conversion webhooks and the cache sync (usually initial list) which causes the webhooks
458458
// to never start because no cache can be populated.
459459
if err := cm.runnables.Webhooks.Start(cm.internalCtx); err != nil {
460-
if err != wait.ErrWaitTimeout {
460+
if !errors.Is(err, wait.ErrWaitTimeout) {
461461
return err
462462
}
463463
}
464464

465465
// Start and wait for caches.
466466
if err := cm.runnables.Caches.Start(cm.internalCtx); err != nil {
467-
if err != wait.ErrWaitTimeout {
467+
if !errors.Is(err, wait.ErrWaitTimeout) {
468468
return err
469469
}
470470
}
471471

472472
// Start the non-leaderelection Runnables after the cache has synced.
473473
if err := cm.runnables.Others.Start(cm.internalCtx); err != nil {
474-
if err != wait.ErrWaitTimeout {
474+
if !errors.Is(err, wait.ErrWaitTimeout) {
475475
return err
476476
}
477477
}
@@ -587,7 +587,7 @@ func (cm *controllerManager) engageStopProcedure(stopComplete <-chan struct{}) e
587587
}()
588588

589589
<-cm.shutdownCtx.Done()
590-
if err := cm.shutdownCtx.Err(); err != nil && err != context.Canceled {
590+
if err := cm.shutdownCtx.Err(); err != nil && !errors.Is(err, context.Canceled) {
591591
if errors.Is(err, context.DeadlineExceeded) {
592592
if cm.gracefulShutdownTimeout > 0 {
593593
return fmt.Errorf("failed waiting for all runnables to end within grace period of %s: %w", cm.gracefulShutdownTimeout, err)
@@ -597,6 +597,7 @@ func (cm *controllerManager) engageStopProcedure(stopComplete <-chan struct{}) e
597597
// For any other error, return the error.
598598
return err
599599
}
600+
600601
return nil
601602
}
602603

pkg/manager/manager.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ func defaultHealthProbeListener(addr string) (net.Listener, error) {
533533

534534
ln, err := net.Listen("tcp", addr)
535535
if err != nil {
536-
return nil, fmt.Errorf("error listening on %s: %v", addr, err)
536+
return nil, fmt.Errorf("error listening on %s: %w", addr, err)
537537
}
538538
return ln, nil
539539
}

pkg/manager/manager_test.go

-6
Original file line numberDiff line numberDiff line change
@@ -1806,12 +1806,6 @@ type startSignalingInformer struct {
18061806
cache.Cache
18071807
}
18081808

1809-
func (c *startSignalingInformer) started() bool {
1810-
c.mu.Lock()
1811-
defer c.mu.Unlock()
1812-
return c.wasStarted
1813-
}
1814-
18151809
func (c *startSignalingInformer) Start(ctx context.Context) error {
18161810
c.mu.Lock()
18171811
c.wasStarted = true

pkg/source/source.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (ks *Kind) WaitForSync(ctx context.Context) error {
181181
return err
182182
case <-ctx.Done():
183183
ks.startCancel()
184-
if ctx.Err() == context.Canceled {
184+
if errors.Is(ctx.Err(), context.Canceled) {
185185
return nil
186186
}
187187
return errors.New("timed out waiting for cache to be synced")

pkg/webhook/server.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func (s *Server) Start(ctx context.Context) error {
243243
certPool := x509.NewCertPool()
244244
clientCABytes, err := ioutil.ReadFile(filepath.Join(s.CertDir, s.ClientCAName))
245245
if err != nil {
246-
return fmt.Errorf("failed to read client CA cert: %v", err)
246+
return fmt.Errorf("failed to read client CA cert: %w", err)
247247
}
248248

249249
ok := certPool.AppendCertsFromPEM(clientCABytes)
@@ -305,11 +305,11 @@ func (s *Server) StartedChecker() healthz.Checker {
305305
d := &net.Dialer{Timeout: 10 * time.Second}
306306
conn, err := tls.DialWithDialer(d, "tcp", net.JoinHostPort(s.Host, strconv.Itoa(s.Port)), config)
307307
if err != nil {
308-
return fmt.Errorf("webhook server is not reachable: %v", err)
308+
return fmt.Errorf("webhook server is not reachable: %w", err)
309309
}
310310

311311
if err := conn.Close(); err != nil {
312-
return fmt.Errorf("webhook server is not reachable: closing connection: %v", err)
312+
return fmt.Errorf("webhook server is not reachable: closing connection: %w", err)
313313
}
314314

315315
return nil

0 commit comments

Comments
 (0)