Skip to content

Commit 6d1afbe

Browse files
committed
add DeleteVolumeGroupSnapshot API to delete group snapshot
This commit adds logic to DeleteVolumeGroupSnapshot API which will further delete the VolumeGroupSnapshot. Signed-off-by: yati1998 <ypadia@redhat.com>
1 parent 304f4bf commit 6d1afbe

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

pkg/group_snapshotter/group_snapshotter.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ package group_snapshotter
1818

1919
import (
2020
"context"
21+
"time"
22+
2123
"github.com/container-storage-interface/spec/lib/go/csi"
2224
csirpc "github.com/kubernetes-csi/csi-lib-utils/rpc"
2325
"google.golang.org/grpc"
2426
klog "k8s.io/klog/v2"
25-
"time"
2627
)
2728

2829
// GroupSnapshotter implements CreateGroupSnapshot/DeleteGroupSnapshot operations against a CSI driver.
@@ -31,7 +32,7 @@ type GroupSnapshotter interface {
3132
CreateGroupSnapshot(ctx context.Context, groupSnapshotName string, volumeIDs []string, parameters map[string]string, snapshotterCredentials map[string]string) (driverName string, groupSnapshotId string, snapshots []*csi.Snapshot, timestamp time.Time, readyToUse bool, err error)
3233

3334
// DeleteGroupSnapshot deletes a group snapshot of multiple volumes
34-
DeleteGroupSnapshot(ctx context.Context, groupSnapshotID string, snapshotterCredentials map[string]string) (err error)
35+
DeleteGroupSnapshot(ctx context.Context, groupSnapshotID string, snapshotIDs []string, snapshotterCredentials map[string]string) (err error)
3536

3637
// GetGroupSnapshotStatus returns if a group snapshot is ready to use, its creation time, etc
3738
GetGroupSnapshotStatus(ctx context.Context, groupSnapshotID string, snapshotterListCredentials map[string]string) (bool, time.Time, error)
@@ -74,8 +75,20 @@ func (gs *groupSnapshot) CreateGroupSnapshot(ctx context.Context, groupSnapshotN
7475

7576
}
7677

77-
func (gs *groupSnapshot) DeleteGroupSnapshot(ctx context.Context, groupSnapshotID string, snapshotterCredentials map[string]string) error {
78-
// TODO: Implement DeleteGroupSnapshot
78+
func (gs *groupSnapshot) DeleteGroupSnapshot(ctx context.Context, groupSnapshotID string, snapshotIds []string, snapshotterCredentials map[string]string) error {
79+
client := csi.NewGroupControllerClient(gs.conn)
80+
81+
req := csi.DeleteVolumeGroupSnapshotRequest{
82+
Secrets: snapshotterCredentials,
83+
GroupSnapshotId: groupSnapshotID,
84+
SnapshotIds: snapshotIds,
85+
}
86+
87+
_, err := client.DeleteVolumeGroupSnapshot(ctx, &req)
88+
if err != nil {
89+
return err
90+
}
91+
7992
return nil
8093
}
8194

pkg/sidecar-controller/csi_handler.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ package sidecar_controller
1919
import (
2020
"context"
2121
"fmt"
22+
"strings"
23+
"time"
24+
2225
"github.com/container-storage-interface/spec/lib/go/csi"
2326
crdv1alpha1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumegroupsnapshot/v1alpha1"
2427
"github.com/kubernetes-csi/external-snapshotter/v6/pkg/group_snapshotter"
25-
"strings"
26-
"time"
2728

2829
crdv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
2930
"github.com/kubernetes-csi/external-snapshotter/v6/pkg/snapshotter"
@@ -36,6 +37,7 @@ type Handler interface {
3637
GetSnapshotStatus(content *crdv1.VolumeSnapshotContent, snapshotterListCredentials map[string]string) (bool, time.Time, int64, error)
3738
CreateGroupSnapshot(content *crdv1alpha1.VolumeGroupSnapshotContent, volumeIDs []string, parameters map[string]string, snapshotterCredentials map[string]string) (string, string, []*csi.Snapshot, time.Time, bool, error)
3839
GetGroupSnapshotStatus(content *crdv1alpha1.VolumeGroupSnapshotContent, snapshotterListCredentials map[string]string) (bool, time.Time, error)
40+
DeleteGroupSnapshot(content *crdv1alpha1.VolumeGroupSnapshotContent, SanpshotID []string, snapshotterCredentials map[string]string) error
3941
}
4042

4143
// csiHandler is a handler that calls CSI to create/delete volume snapshot.
@@ -165,6 +167,30 @@ func (handler *csiHandler) CreateGroupSnapshot(content *crdv1alpha1.VolumeGroupS
165167
return handler.groupSnapshotter.CreateGroupSnapshot(ctx, groupSnapshotName, volumeIDs, parameters, snapshotterCredentials)
166168
}
167169

170+
func (handler *csiHandler) DeleteGroupSnapshot(content *crdv1alpha1.VolumeGroupSnapshotContent, snapshotIDs []string, snapshotterCredentials map[string]string) error {
171+
ctx, cancel := context.WithTimeout(context.Background(), handler.timeout)
172+
defer cancel()
173+
174+
if content.Spec.VolumeGroupSnapshotRef.UID == "" {
175+
return fmt.Errorf("cannot delete group snapshot. Group snapshot content %s not bound to a group snapshot", content.Name)
176+
}
177+
178+
if len(snapshotIDs) != 0 {
179+
return fmt.Errorf("cannot delete group snapshot. Snapshots found in the group snapshot%s", content.Name)
180+
}
181+
var groupSnapshotHandle string
182+
183+
if content.Status != nil && content.Status.VolumeGroupSnapshotHandle != nil {
184+
groupSnapshotHandle = *content.Status.VolumeGroupSnapshotHandle
185+
} else if content.Spec.Source.VolumeGroupSnapshotHandle != nil {
186+
groupSnapshotHandle = *content.Spec.Source.VolumeGroupSnapshotHandle
187+
} else {
188+
return fmt.Errorf("failed to delete group snapshot content %s: groupsnapshotHandle is missing", content.Name)
189+
}
190+
191+
return handler.groupSnapshotter.DeleteGroupSnapshot(ctx, groupSnapshotHandle, snapshotIDs, snapshotterCredentials)
192+
}
193+
168194
func (handler *csiHandler) GetGroupSnapshotStatus(groupSnapshotContent *crdv1alpha1.VolumeGroupSnapshotContent, snapshotterListCredentials map[string]string) (bool, time.Time, error) {
169195
ctx, cancel := context.WithTimeout(context.Background(), handler.timeout)
170196
defer cancel()

0 commit comments

Comments
 (0)