Skip to content

Commit 223e934

Browse files
authored
Replace fmt.Errorf with errors.New where possible (#2275)
1 parent 1ead052 commit 223e934

File tree

10 files changed

+29
-21
lines changed

10 files changed

+29
-21
lines changed

‎.golangci.yaml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ linters-settings:
1414
govet:
1515
enable:
1616
- nilness
17+
perfsprint:
18+
int-conversion: false
19+
errorf: true
20+
sprintf1: false
21+
strconcat: false
1722
revive:
1823
enable-all-rules: false
1924
rules:
@@ -32,6 +37,7 @@ linters:
3237
- govet
3338
- loggercheck
3439
- misspell
40+
- perfsprint
3541
- revive
3642
- unconvert
3743

‎cmd/kueuectl/app/create/create_localqueue.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package create
1818

1919
import (
2020
"context"
21-
"fmt"
21+
"errors"
2222

2323
"github.com/spf13/cobra"
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -141,13 +141,13 @@ func (o *LocalQueueOptions) Complete(clientGetter util.ClientGetter, cmd *cobra.
141141
// Validate validates required fields are set to support structured generation
142142
func (o *LocalQueueOptions) Validate(ctx context.Context) error {
143143
if len(o.Name) == 0 {
144-
return fmt.Errorf("name must be specified")
144+
return errors.New("name must be specified")
145145
}
146146
if len(o.ClusterQueue) == 0 {
147-
return fmt.Errorf("clusterqueue must be specified")
147+
return errors.New("clusterqueue must be specified")
148148
}
149149
if len(o.Namespace) == 0 {
150-
return fmt.Errorf("namespace must be specified")
150+
return errors.New("namespace must be specified")
151151
}
152152
if !o.IgnoreUnknownCq {
153153
_, err := o.Client.ClusterQueues().Get(ctx, o.UserSpecifiedClusterQueue, metav1.GetOptions{})

‎cmd/kueuectl/app/list/list_clusterqueue.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package list
1818

1919
import (
2020
"context"
21-
"fmt"
21+
"errors"
2222
"io"
2323
"time"
2424

@@ -139,7 +139,7 @@ func (o *ClusterQueueOptions) Complete(clientGetter util.ClientGetter, cmd *cobr
139139

140140
func (o *ClusterQueueOptions) Validate() error {
141141
if !o.validActiveFlagOptionProvided() {
142-
return fmt.Errorf("only one active flag can be provided")
142+
return errors.New("only one active flag can be provided")
143143
}
144144
return nil
145145
}

‎cmd/kueuectl/app/list/list_localqueue_printer.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package list
1818

1919
import (
20-
"fmt"
20+
"errors"
2121
"io"
2222
"time"
2323

@@ -40,7 +40,7 @@ func (p *listLocalQueuePrinter) PrintObj(obj runtime.Object, out io.Writer) erro
4040

4141
list, ok := obj.(*v1beta1.LocalQueueList)
4242
if !ok {
43-
return fmt.Errorf("invalid object type")
43+
return errors.New("invalid object type")
4444
}
4545

4646
table := &metav1.Table{

‎pkg/cache/cache.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func (c *Cache) AddClusterQueue(ctx context.Context, cq *kueue.ClusterQueue) err
300300
defer c.Unlock()
301301

302302
if _, ok := c.clusterQueues[cq.Name]; ok {
303-
return fmt.Errorf("ClusterQueue already exists")
303+
return errors.New("ClusterQueue already exists")
304304
}
305305
cqImpl, err := c.newClusterQueue(cq)
306306
if err != nil {
@@ -459,7 +459,7 @@ func (c *Cache) UpdateWorkload(oldWl, newWl *kueue.Workload) error {
459459
if workload.HasQuotaReservation(oldWl) {
460460
cq, ok := c.clusterQueues[string(oldWl.Status.Admission.ClusterQueue)]
461461
if !ok {
462-
return fmt.Errorf("old ClusterQueue doesn't exist")
462+
return errors.New("old ClusterQueue doesn't exist")
463463
}
464464
cq.deleteWorkload(oldWl)
465465
}
@@ -470,7 +470,7 @@ func (c *Cache) UpdateWorkload(oldWl, newWl *kueue.Workload) error {
470470
}
471471
cq, ok := c.clusterQueues[string(newWl.Status.Admission.ClusterQueue)]
472472
if !ok {
473-
return fmt.Errorf("new ClusterQueue doesn't exist")
473+
return errors.New("new ClusterQueue doesn't exist")
474474
}
475475
if c.podsReadyTracking {
476476
c.podsReadyCond.Broadcast()
@@ -543,7 +543,7 @@ func (c *Cache) ForgetWorkload(w *kueue.Workload) error {
543543
defer c.Unlock()
544544

545545
if _, assumed := c.assumedWorkloads[workload.Key(w)]; !assumed {
546-
return fmt.Errorf("the workload is not assumed")
546+
return errors.New("the workload is not assumed")
547547
}
548548
c.cleanupAssumedState(w)
549549

‎pkg/cache/cache_test.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package cache
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
2223
"testing"
2324

@@ -1425,7 +1426,7 @@ func TestCacheWorkloadOperations(t *testing.T) {
14251426
ClusterQueue: "three",
14261427
}).Obj()
14271428
if !cache.AddOrUpdateWorkload(w) {
1428-
return fmt.Errorf("failed to add workload")
1429+
return errors.New("failed to add workload")
14291430
}
14301431
return nil
14311432
},
@@ -1454,7 +1455,7 @@ func TestCacheWorkloadOperations(t *testing.T) {
14541455
ClusterQueue: "one",
14551456
}).Obj()
14561457
if !cache.AddOrUpdateWorkload(w) {
1457-
return fmt.Errorf("failed to add workload")
1458+
return errors.New("failed to add workload")
14581459
}
14591460
return nil
14601461
},
@@ -1876,7 +1877,7 @@ func TestCacheWorkloadOperations(t *testing.T) {
18761877

18771878
w := workloads[0]
18781879
if !cache.AddOrUpdateWorkload(w) {
1879-
return fmt.Errorf("failed to add workload")
1880+
return errors.New("failed to add workload")
18801881
}
18811882
return nil
18821883
},

‎pkg/cache/clusterqueue.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package cache
1818

1919
import (
2020
"errors"
21-
"fmt"
2221
"math"
2322
"strings"
2423

@@ -452,7 +451,7 @@ func (c *ClusterQueue) updateWithAdmissionChecks(checks map[string]AdmissionChec
452451
func (c *ClusterQueue) addWorkload(w *kueue.Workload) error {
453452
k := workload.Key(w)
454453
if _, exist := c.Workloads[k]; exist {
455-
return fmt.Errorf("workload already exists in ClusterQueue")
454+
return errors.New("workload already exists in ClusterQueue")
456455
}
457456
wi := workload.NewInfo(w)
458457
c.Workloads[k] = wi

‎pkg/controller/jobs/pod/event_handlers.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pod
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"slices"
78

@@ -20,7 +21,7 @@ import (
2021
)
2122

2223
var (
23-
errFailedRefAPIVersionParse = fmt.Errorf("could not parse single pod OwnerReference APIVersion")
24+
errFailedRefAPIVersionParse = errors.New("could not parse single pod OwnerReference APIVersion")
2425
)
2526

2627
func reconcileRequestForPod(p *corev1.Pod) reconcile.Request {

‎pkg/controller/jobs/pod/pod_controller.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const (
7777

7878
var (
7979
gvk = corev1.SchemeGroupVersion.WithKind("Pod")
80-
errIncorrectReconcileRequest = fmt.Errorf("event handler error: got a single pod reconcile request for a pod group")
80+
errIncorrectReconcileRequest = errors.New("event handler error: got a single pod reconcile request for a pod group")
8181
errPendingOps = jobframework.UnretryableError("waiting to observe previous operations on pods")
8282
errPodNoSupportKubeVersion = errors.New("pod integration only supported in Kubernetes 1.27 or newer")
8383
errPodGroupLabelsMismatch = errors.New("constructing workload: pods have different label values")
@@ -299,7 +299,7 @@ func (p *Pod) Run(ctx context.Context, c client.Client, podSetsInfo []podset.Pod
299299
// RunWithPodSetsInfo will inject the node affinity and podSet counts extracting from workload to job and unsuspend it.
300300
func (p *Pod) RunWithPodSetsInfo(_ []podset.PodSetInfo) error {
301301
// Not implemented because this is not called when JobWithCustomRun is implemented.
302-
return fmt.Errorf("RunWithPodSetsInfo is not implemented for the Pod object")
302+
return errors.New("RunWithPodSetsInfo is not implemented for the Pod object")
303303
}
304304

305305
// RestorePodSetsInfo will restore the original node affinity and podSet counts of the job.

‎pkg/util/testing/core.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package testing
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
2223

2324
corev1 "k8s.io/api/core/v1"
@@ -57,7 +58,7 @@ func CheckLatestEvent(ctx context.Context, k8sClient client.Client,
5758

5859
length := len(events.Items)
5960
if length == 0 {
60-
return false, fmt.Errorf("no events currently exist")
61+
return false, errors.New("no events currently exist")
6162
}
6263

6364
item := events.Items[length-1]

0 commit comments

Comments
 (0)