Skip to content

Commit

Permalink
fix: hscale out failed for Failed to set default snapshot class (#5428)
Browse files Browse the repository at this point in the history
(cherry picked from commit 77a53f8)
  • Loading branch information
wangyelei authored and ldming committed Oct 13, 2023
1 parent d95b0f3 commit 54bcc5b
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions internal/dataprotection/action/action_create_vs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package action

import (
"context"
"fmt"
"strings"

vsv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -139,8 +141,8 @@ func (c *CreateVolumeSnapshotAction) createVolumeSnapshotIfNotExist(ctx Context,
pvc *corev1.PersistentVolumeClaim,
key client.ObjectKey) error {
var (
err error
vsc *vsv1.VolumeSnapshotClass
err error
vscName string
)

snap := &vsv1.VolumeSnapshot{}
Expand All @@ -167,8 +169,14 @@ func (c *CreateVolumeSnapshotAction) createVolumeSnapshotIfNotExist(ctx Context,
},
}

if vsc != nil {
snap.Spec.VolumeSnapshotClassName = &vsc.Name
if pvc.Spec.StorageClassName != nil && *pvc.Spec.StorageClassName != "" {
if vscName, err = c.getVolumeSnapshotClassName(ctx.Ctx, ctx.Client, vsCli, *pvc.Spec.StorageClassName); err != nil {
return err
}
}

if vscName != "" {
snap.Spec.VolumeSnapshotClassName = &vscName
}

controllerutil.AddFinalizer(snap, dptypes.DataProtectionFinalizerName)
Expand All @@ -184,6 +192,29 @@ func (c *CreateVolumeSnapshotAction) createVolumeSnapshotIfNotExist(ctx Context,
return nil
}

func (c *CreateVolumeSnapshotAction) getVolumeSnapshotClassName(
ctx context.Context,
cli client.Client,
vsCli intctrlutil.VolumeSnapshotCompatClient,
scName string) (string, error) {
scObj := storagev1.StorageClass{}
// ignore if not found storage class, use the default volume snapshot class
if err := cli.Get(ctx, client.ObjectKey{Name: scName}, &scObj); client.IgnoreNotFound(err) != nil {
return "", err
}

vscList := vsv1.VolumeSnapshotClassList{}
if err := vsCli.List(&vscList); err != nil {
return "", err
}
for _, item := range vscList.Items {
if item.Driver == scObj.Provisioner {
return item.Name, nil
}
}
return "", nil
}

func ensureVolumeSnapshotReady(
vsCli intctrlutil.VolumeSnapshotCompatClient,
key client.ObjectKey) (bool, *vsv1.VolumeSnapshot, error) {
Expand Down

0 comments on commit 54bcc5b

Please sign in to comment.