mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-14 02:43:36 +00:00
cleanup: move internal/rbd/errors.go to internal/rbd/errors pacakge
Signed-off-by: Praveen M <m.praveen@ibm.com>
This commit is contained in:
@ -24,6 +24,7 @@ import (
|
||||
"github.com/container-storage-interface/spec/lib/go/csi"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
rbderrors "github.com/ceph/ceph-csi/internal/rbd/errors"
|
||||
"github.com/ceph/ceph-csi/internal/rbd/types"
|
||||
"github.com/ceph/ceph-csi/internal/util"
|
||||
"github.com/ceph/ceph-csi/internal/util/log"
|
||||
@ -70,7 +71,7 @@ func GetVolumeGroupSnapshot(
|
||||
|
||||
attrs, err := vgs.getVolumeGroupAttributes(ctx)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrRBDGroupNotFound) {
|
||||
if errors.Is(err, rbderrors.ErrGroupNotFound) {
|
||||
log.ErrorLog(ctx, "%v, returning empty volume group snapshot %q", vgs, err)
|
||||
|
||||
return vgs, err
|
||||
|
@ -26,6 +26,7 @@ import (
|
||||
"github.com/ceph/go-ceph/rados"
|
||||
|
||||
"github.com/ceph/ceph-csi/internal/journal"
|
||||
rbderrors "github.com/ceph/ceph-csi/internal/rbd/errors"
|
||||
"github.com/ceph/ceph-csi/internal/util"
|
||||
"github.com/ceph/ceph-csi/internal/util/log"
|
||||
)
|
||||
@ -126,7 +127,7 @@ func (cvg *commonVolumeGroup) generateVolumeGroupFromMapping(
|
||||
}
|
||||
mcsiID.LocationID = mPID
|
||||
err = cvg.generateVolumeGroup(mcsiID)
|
||||
if util.ShouldRetryVolumeGeneration(err) {
|
||||
if ShouldRetryVolumeGroupGeneration(err) {
|
||||
continue
|
||||
}
|
||||
|
||||
@ -161,13 +162,13 @@ func (cvg *commonVolumeGroup) initCommonVolumeGroup(
|
||||
|
||||
err = cvg.generateVolumeGroup(csiID)
|
||||
// If the error is not a retryable error, return from here.
|
||||
if err != nil && !util.ShouldRetryVolumeGeneration(err) {
|
||||
if err != nil && !ShouldRetryVolumeGroupGeneration(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
// If the error is a retryable error, we should try to get the cluster mapping
|
||||
// and generate the volume group from the mapping.
|
||||
if util.ShouldRetryVolumeGeneration(err) {
|
||||
if ShouldRetryVolumeGroupGeneration(err) {
|
||||
mapping, err := util.GetClusterMappingInfo(csiID.ClusterID)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -229,7 +230,7 @@ func (cvg *commonVolumeGroup) getVolumeGroupAttributes(ctx context.Context) (*jo
|
||||
if attrs.GroupName == "" {
|
||||
log.ErrorLog(ctx, "volume group with id %v not found", cvg.id)
|
||||
|
||||
return nil, ErrRBDGroupNotFound
|
||||
return nil, rbderrors.ErrGroupNotFound
|
||||
}
|
||||
|
||||
cvg.requestName = attrs.RequestName
|
||||
@ -356,12 +357,12 @@ func (cvg *commonVolumeGroup) GetIOContext(ctx context.Context) (*rados.IOContex
|
||||
|
||||
conn, err := cvg.getConnection(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: failed to connect: %w", ErrRBDGroupNotConnected, err)
|
||||
return nil, fmt.Errorf("%w: failed to connect: %w", rbderrors.ErrGroupNotConnected, err)
|
||||
}
|
||||
|
||||
ioctx, err := conn.GetIoctx(cvg.pool)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: failed to get IOContext: %w", ErrRBDGroupNotConnected, err)
|
||||
return nil, fmt.Errorf("%w: failed to get IOContext: %w", rbderrors.ErrGroupNotConnected, err)
|
||||
}
|
||||
|
||||
if cvg.namespace != "" {
|
||||
@ -417,3 +418,28 @@ func (cvg *commonVolumeGroup) GetCreationTime(ctx context.Context) (*time.Time,
|
||||
|
||||
return cvg.creationTime, nil
|
||||
}
|
||||
|
||||
// ShouldRetryVolumeGroupGeneration determines whether the process of finding or generating
|
||||
// volumegroups should continue based on the type of error encountered.
|
||||
//
|
||||
// It checks if the given error matches any of the following known errors:
|
||||
// - ErrPoolNotFound: The rbd pool where the volumegroup/omap is expected doesn't exist.
|
||||
// - ErrRBDGroupNotFound: The volumegroup doesn't exist in the rbd pool.
|
||||
// - rados.ErrPermissionDenied: Permissions to access the pool is denied.
|
||||
//
|
||||
// If any of these errors are encountered, the function returns `true`, indicating
|
||||
// that the volumegroup search should continue because of known error. Otherwise, it
|
||||
// returns `false`, meaning the search should stop.
|
||||
//
|
||||
// This helper function is used in scenarios where multiple attempts may be made
|
||||
// to retrieve or generate volumegroup information, and we want to gracefully handle
|
||||
// specific failure cases while retrying for others.
|
||||
func ShouldRetryVolumeGroupGeneration(err error) bool {
|
||||
if err == nil {
|
||||
return false // No error, do not retry
|
||||
}
|
||||
// Continue searching for specific known errors
|
||||
return (errors.Is(err, util.ErrPoolNotFound) ||
|
||||
errors.Is(err, rbderrors.ErrGroupNotFound) ||
|
||||
errors.Is(err, rados.ErrPermissionDenied))
|
||||
}
|
||||
|
73
internal/rbd/group/util_test.go
Normal file
73
internal/rbd/group/util_test.go
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
Copyright 2025 The Ceph-CSI Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package group
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
rbderrors "github.com/ceph/ceph-csi/internal/rbd/errors"
|
||||
"github.com/ceph/ceph-csi/internal/util"
|
||||
|
||||
"github.com/ceph/go-ceph/rados"
|
||||
)
|
||||
|
||||
func Test_shouldRetryVolumeGroupGeneration(t *testing.T) {
|
||||
t.Parallel()
|
||||
type args struct {
|
||||
err error
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "No error (stop searching)",
|
||||
args: args{err: nil},
|
||||
want: false, // No error, stop searching
|
||||
},
|
||||
{
|
||||
name: "ErrPoolNotFound (continue searching)",
|
||||
args: args{err: util.ErrPoolNotFound},
|
||||
want: true, // Known error, continue searching
|
||||
},
|
||||
{
|
||||
name: "ErrRBDGroupNotFound (continue searching)",
|
||||
args: args{err: rbderrors.ErrGroupNotFound},
|
||||
want: true, // Known error, continue searching
|
||||
},
|
||||
{
|
||||
name: "ErrPermissionDenied (continue searching)",
|
||||
args: args{err: rados.ErrPermissionDenied},
|
||||
want: true, // Known error, continue searching
|
||||
},
|
||||
{
|
||||
name: "Different error (stop searching)",
|
||||
args: args{err: errors.New("unknown error")},
|
||||
want: false, // Unknown error, stop searching
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
if got := ShouldRetryVolumeGroupGeneration(tt.args.err); got != tt.want {
|
||||
t.Errorf("ShouldRetryVolumeGeneration() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -22,21 +22,16 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/ceph/go-ceph/rados"
|
||||
librados "github.com/ceph/go-ceph/rados"
|
||||
librbd "github.com/ceph/go-ceph/rbd"
|
||||
"github.com/container-storage-interface/spec/lib/go/csi"
|
||||
"github.com/csi-addons/spec/lib/go/volumegroup"
|
||||
|
||||
rbderrors "github.com/ceph/ceph-csi/internal/rbd/errors"
|
||||
"github.com/ceph/ceph-csi/internal/rbd/types"
|
||||
"github.com/ceph/ceph-csi/internal/util"
|
||||
"github.com/ceph/ceph-csi/internal/util/log"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrRBDGroupNotConnected = fmt.Errorf("%w: RBD group is not connected", librados.ErrNotConnected)
|
||||
ErrRBDGroupNotFound = fmt.Errorf("%w: RBD group not found", librbd.ErrNotFound)
|
||||
)
|
||||
|
||||
// volumeGroup handles all requests for 'rbd group' operations.
|
||||
type volumeGroup struct {
|
||||
commonVolumeGroup
|
||||
@ -77,7 +72,7 @@ func GetVolumeGroup(
|
||||
|
||||
attrs, err := vg.getVolumeGroupAttributes(ctx)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrRBDGroupNotFound) {
|
||||
if errors.Is(err, rbderrors.ErrGroupNotFound) {
|
||||
log.ErrorLog(ctx, "%v, returning empty volume group %q", vg, err)
|
||||
|
||||
return vg, err
|
||||
|
Reference in New Issue
Block a user