Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hscale out failed for Failed to set default snapshot class #5428

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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