Skip to content

Commit

Permalink
Merge pull request #92 from cybozu-go/kaizen-enable-errorlint
Browse files Browse the repository at this point in the history
enable errorlint of golangci-lint
  • Loading branch information
llamerada-jp authored Jan 17, 2025
2 parents 9dbc316 + 7b56030 commit 2220184
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ issues:
linters:
disable-all: true
enable:
- copyloopvar
- dupl
- errcheck
- copyloopvar
- errorlint
- goconst
- gocyclo
- gofmt
Expand Down
20 changes: 10 additions & 10 deletions internal/ceph/rbd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (c *cephCmdImpl) RBDClone(pool, srcImage, srcSnap, dstImage, features strin
src, dst,
)
if err != nil {
return fmt.Errorf("failed to clone RBD image: %v", err)
return fmt.Errorf("failed to clone RBD image: %w", err)
}

return nil
Expand All @@ -25,13 +25,13 @@ func (c *cephCmdImpl) RBDClone(pool, srcImage, srcSnap, dstImage, features strin
func (c *cephCmdImpl) RBDInfo(pool, image string) (*RBDImageInfo, error) {
out, err := c.command.execute("rbd", "info", "--format", "json", fmt.Sprintf("%s/%s", pool, image))
if err != nil {
return nil, fmt.Errorf("failed to get RBD info: %v", err)
return nil, fmt.Errorf("failed to get RBD info: %w", err)
}

imageInfo := &RBDImageInfo{}
err = json.Unmarshal(out, imageInfo)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal RBD info: %v", err)
return nil, fmt.Errorf("failed to unmarshal RBD info: %w", err)
}
return imageInfo, nil
}
Expand All @@ -40,13 +40,13 @@ func (c *cephCmdImpl) RBDInfo(pool, image string) (*RBDImageInfo, error) {
func (c *cephCmdImpl) RBDLs(pool string) ([]string, error) {
out, err := c.command.execute("rbd", "ls", "-p", pool, "--format", "json")
if err != nil {
return nil, fmt.Errorf("failed to list RBD images: %v", err)
return nil, fmt.Errorf("failed to list RBD images: %w", err)
}

var images []string
err = json.Unmarshal(out, &images)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal RBD images: %v", err)
return nil, fmt.Errorf("failed to unmarshal RBD images: %w", err)
}

return images, nil
Expand All @@ -56,7 +56,7 @@ func (c *cephCmdImpl) RBDLs(pool string) ([]string, error) {
func (c *cephCmdImpl) RBDRm(pool, image string) error {
_, err := c.command.execute("rbd", "rm", fmt.Sprintf("%s/%s", pool, image))
if err != nil {
return fmt.Errorf("failed to remove RBD image: %v", err)
return fmt.Errorf("failed to remove RBD image: %w", err)
}

return nil
Expand Down Expand Up @@ -84,7 +84,7 @@ func (c *cephCmdImpl) CephRBDTaskAddTrashRemove(pool, imageID string) error {
func (c *cephCmdImpl) RBDSnapCreate(pool, image, snap string) error {
_, err := c.command.execute("rbd", "snap", "create", fmt.Sprintf("%s/%s@%s", pool, image, snap))
if err != nil {
return fmt.Errorf("failed to create RBD snapshot: %v", err)
return fmt.Errorf("failed to create RBD snapshot: %w", err)
}

return nil
Expand All @@ -94,13 +94,13 @@ func (c *cephCmdImpl) RBDSnapCreate(pool, image, snap string) error {
func (c *cephCmdImpl) RBDSnapLs(pool, image string) ([]RBDSnapshot, error) {
out, err := c.command.execute("rbd", "snap", "ls", "--format", "json", fmt.Sprintf("%s/%s", pool, image))
if err != nil {
return nil, fmt.Errorf("failed to list RBD snapshots: %v", err)
return nil, fmt.Errorf("failed to list RBD snapshots: %w", err)
}

var snapshots []RBDSnapshot
err = json.Unmarshal(out, &snapshots)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal RBD snapshots: %v", err)
return nil, fmt.Errorf("failed to unmarshal RBD snapshots: %w", err)
}

return snapshots, nil
Expand All @@ -110,7 +110,7 @@ func (c *cephCmdImpl) RBDSnapLs(pool, image string) ([]RBDSnapshot, error) {
func (c *cephCmdImpl) RBDSnapRm(pool, image, snap string) error {
_, err := c.command.execute("rbd", "snap", "rm", fmt.Sprintf("%s/%s@%s", pool, image, snap))
if err != nil {
return fmt.Errorf("failed to remove RBD snapshot: %v", err)
return fmt.Errorf("failed to remove RBD snapshot: %w", err)
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions internal/controller/mantlebackup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (r *MantleBackupReconciler) removeRBDSnapshot(ctx context.Context, poolName
rmErr := r.ceph.RBDSnapRm(poolName, imageName, snapshotName)
if rmErr != nil {
_, findErr := ceph.FindRBDSnapshot(r.ceph, poolName, imageName, snapshotName)
if findErr != nil && findErr != ceph.ErrSnapshotNotFound {
if findErr != nil && errors.Is(findErr, ceph.ErrSnapshotNotFound) {
err := errors.Join(rmErr, findErr)
logger.Error(err, "failed to remove rbd snapshot", "poolName", poolName, "imageName", imageName, "snapshotName", snapshotName)
return fmt.Errorf("failed to remove rbd snapshot: %w", err)
Expand Down Expand Up @@ -229,8 +229,8 @@ type errTargetPVCNotFound struct {
}

func isErrTargetPVCNotFound(err error) bool {
_, ok := err.(errTargetPVCNotFound)
return ok
target := errTargetPVCNotFound{}
return errors.As(err, &target)
}

func (r *MantleBackupReconciler) getSnapshotTarget(ctx context.Context, backup *mantlev1.MantleBackup) (
Expand Down Expand Up @@ -387,7 +387,7 @@ func (r *MantleBackupReconciler) reconcileAsStandalone(ctx context.Context, back

target, result, getSnapshotTargetErr := r.getSnapshotTarget(ctx, backup)
switch {
case getSnapshotTargetErr == errSkipProcessing:
case errors.Is(getSnapshotTargetErr, errSkipProcessing):
return ctrl.Result{}, nil
case isErrTargetPVCNotFound(getSnapshotTargetErr):
// deletion logic may run.
Expand Down Expand Up @@ -456,7 +456,7 @@ func (r *MantleBackupReconciler) reconcileAsSecondary(ctx context.Context, backu

target, result, getSnapshotTargetErr := r.getSnapshotTarget(ctx, backup)
switch {
case getSnapshotTargetErr == errSkipProcessing:
case errors.Is(getSnapshotTargetErr, errSkipProcessing):
return ctrl.Result{}, nil
case isErrTargetPVCNotFound(getSnapshotTargetErr):
// deletion logic may run.
Expand Down
14 changes: 7 additions & 7 deletions internal/controller/mantlerestore_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (r *MantleRestoreReconciler) cloneImageFromBackup(ctx context.Context, rest
pv := corev1.PersistentVolume{}
err := json.Unmarshal([]byte(backup.Status.PVManifest), &pv)
if err != nil {
return fmt.Errorf("failed to unmarshal PV manifest: %v", err)
return fmt.Errorf("failed to unmarshal PV manifest: %w", err)
}

bkImage := pv.Spec.CSI.VolumeAttributes["imageName"]
Expand All @@ -215,14 +215,14 @@ func (r *MantleRestoreReconciler) cloneImageFromBackup(ctx context.Context, rest

images, err := r.ceph.RBDLs(restore.Status.Pool)
if err != nil {
return fmt.Errorf("failed to list RBD images: %v", err)
return fmt.Errorf("failed to list RBD images: %w", err)
}

// check if the image already exists
if slices.Contains(images, r.restoringRBDImageName(restore)) {
info, err := r.ceph.RBDInfo(restore.Status.Pool, r.restoringRBDImageName(restore))
if err != nil {
return fmt.Errorf("failed to get RBD info: %v", err)
return fmt.Errorf("failed to get RBD info: %w", err)
}
if info.Parent == nil {
return fmt.Errorf("failed to get RBD info: parent field is empty")
Expand Down Expand Up @@ -378,15 +378,15 @@ func (r *MantleRestoreReconciler) deleteRestoringPVC(ctx context.Context, restor
if errors.IsNotFound(err) {
return nil
}
return fmt.Errorf("failed to get PVC: %v", err)
return fmt.Errorf("failed to get PVC: %w", err)
}

if pvc.Annotations[PVCAnnotationRestoredBy] != string(restore.UID) {
return fmt.Errorf("PVC is having different MantleRestore UID: %s, %s", pvcName, pvc.Annotations[PVCAnnotationRestoredBy])
}

if err := r.client.Delete(ctx, &pvc); err != nil {
return fmt.Errorf("failed to delete PVC: %v", err)
return fmt.Errorf("failed to delete PVC: %w", err)
}

return nil
Expand All @@ -402,15 +402,15 @@ func (r *MantleRestoreReconciler) deleteRestoringPV(ctx context.Context, restore
// removed by GarbageCollectorRunner.
return nil
}
return fmt.Errorf("failed to get PV: %v", err)
return fmt.Errorf("failed to get PV: %w", err)
}

if pv.Annotations[PVAnnotationRestoredBy] != string(restore.UID) {
return fmt.Errorf("PV is having different MantleRestore UID: %s, %s", pv.Name, pv.Annotations[PVAnnotationRestoredBy])
}

if err := r.client.Delete(ctx, &pv); err != nil {
return fmt.Errorf("failed to delete PV: %v", err)
return fmt.Errorf("failed to delete PV: %w", err)
}

return nil
Expand Down

0 comments on commit 2220184

Please sign in to comment.