Skip to content

Commit facefba

Browse files
committed
add DeleteVolumeGroupSnapshot API to delete group snapshot
1 parent 49193ef commit facefba

File tree

11 files changed

+648
-51
lines changed

11 files changed

+648
-51
lines changed

cmd/snapshot-controller/main.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ var (
8181
var version = "unknown"
8282

8383
// Checks that the VolumeSnapshot v1 CRDs exist.
84-
func ensureCustomResourceDefinitionsExist(client *clientset.Clientset) error {
84+
func ensureCustomResourceDefinitionsExist(client *clientset.Clientset, enableVolumeGroupSnapshots bool) error {
8585
condition := func() (bool, error) {
8686
var err error
8787

@@ -102,6 +102,25 @@ func ensureCustomResourceDefinitionsExist(client *clientset.Clientset) error {
102102
klog.Errorf("Failed to list v1 volumesnapshotcontents with error=%+v", err)
103103
return false, nil
104104
}
105+
if enableVolumeGroupSnapshots {
106+
_, err = client.GroupsnapshotV1alpha1().VolumeGroupSnapshots("").List(context.TODO(), metav1.ListOptions{})
107+
if err != nil {
108+
klog.Errorf("Failed to list v1alpha1 volumegroupsnapshots with error=%+v", err)
109+
return false, nil
110+
}
111+
112+
_, err = client.GroupsnapshotV1alpha1().VolumeGroupSnapshotClasses().List(context.TODO(), metav1.ListOptions{})
113+
if err != nil {
114+
klog.Errorf("Failed to list v1alpha1 volumegroupsnapshotclasses with error=%+v", err)
115+
return false, nil
116+
}
117+
_, err = client.GroupsnapshotV1alpha1().VolumeGroupSnapshotContents().List(context.TODO(), metav1.ListOptions{})
118+
if err != nil {
119+
klog.Errorf("Failed to list v1alpha1 volumegroupsnapshotcontents with error=%+v", err)
120+
return false, nil
121+
}
122+
}
123+
105124
return true, nil
106125
}
107126

@@ -214,7 +233,7 @@ func main() {
214233
*enableVolumeGroupSnapshots,
215234
)
216235

217-
if err := ensureCustomResourceDefinitionsExist(snapClient); err != nil {
236+
if err := ensureCustomResourceDefinitionsExist(snapClient, *enableVolumeGroupSnapshots); err != nil {
218237
klog.Errorf("Exiting due to failure to ensure CRDs exist during startup: %+v", err)
219238
os.Exit(1)
220239
}

deploy/kubernetes/csi-snapshotter/rbac-csi-snapshotter.yaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,19 @@ rules:
3636
verbs: ["get", "list", "watch"]
3737
- apiGroups: ["snapshot.storage.k8s.io"]
3838
resources: ["volumesnapshotcontents"]
39-
verbs: ["get", "list", "watch", "update", "patch"]
39+
verbs: ["get", "list", "watch", "update", "patch", "create"]
4040
- apiGroups: ["snapshot.storage.k8s.io"]
4141
resources: ["volumesnapshotcontents/status"]
4242
verbs: ["update", "patch"]
43-
43+
- apiGroups: ["groupsnapshot.storage.k8s.io"]
44+
resources: ["volumegroupsnapshotclasses"]
45+
verbs: ["get", "list", "watch"]
46+
- apiGroups: ["groupsnapshot.storage.k8s.io"]
47+
resources: ["volumegroupsnapshotcontents"]
48+
verbs: ["get", "list", "watch", "update", "patch"]
49+
- apiGroups: ["groupsnapshot.storage.k8s.io"]
50+
resources: ["volumegroupsnapshotcontents/status"]
51+
verbs: ["update", "patch"]
4452
---
4553
kind: ClusterRoleBinding
4654
apiVersion: rbac.authorization.k8s.io/v1

deploy/kubernetes/snapshot-controller/rbac-snapshot-controller.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ rules:
4242
- apiGroups: ["snapshot.storage.k8s.io"]
4343
resources: ["volumesnapshots/status"]
4444
verbs: ["update", "patch"]
45+
46+
- apiGroups: ["groupsnapshot.storage.k8s.io"]
47+
resources: ["volumegroupsnapshotclasses"]
48+
verbs: ["get", "list", "watch"]
49+
- apiGroups: ["groupsnapshot.storage.k8s.io"]
50+
resources: ["volumegroupsnapshotcontents"]
51+
verbs: ["create", "get", "list", "watch", "update", "delete", "patch"]
52+
- apiGroups: ["groupsnapshot.storage.k8s.io"]
53+
resources: ["volumegroupsnapshotcontents/status"]
54+
verbs: ["patch"]
55+
- apiGroups: ["groupsnapshot.storage.k8s.io"]
56+
resources: ["volumegroupsnapshots"]
57+
verbs: ["get", "list", "watch", "update", "patch"]
58+
- apiGroups: ["groupsnapshot.storage.k8s.io"]
59+
resources: ["volumegroupsnapshots/status"]
60+
verbs: ["update", "patch"]
61+
4562
# Enable this RBAC rule only when using distributed snapshotting, i.e. when the enable-distributed-snapshotting flag is set to true
4663
# - apiGroups: [""]
4764
# resources: ["nodes"]

pkg/common-controller/groupsnapshot_controller_helper.go

Lines changed: 312 additions & 17 deletions
Large diffs are not rendered by default.

pkg/common-controller/snapshot_controller.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,6 @@ func (ctrl *csiSnapshotCommonController) updateSnapshotStatus(snapshot *crdv1.Vo
11651165
updated = true
11661166
} else {
11671167
newStatus = snapshotObj.Status.DeepCopy()
1168-
klog.Infof("Raunak 1 %s", newStatus.VolumeGroupSnapshotName)
11691168
if newStatus.BoundVolumeSnapshotContentName == nil {
11701169
newStatus.BoundVolumeSnapshotContentName = &boundContentName
11711170
updated = true

pkg/common-controller/snapshot_controller_base.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ func (ctrl *csiSnapshotCommonController) updateGroupSnapshotContent(content *crd
836836

837837
// deleteGroupSnapshotContent runs in worker thread and handles "groupsnapshotcontent deleted" event.
838838
func (ctrl *csiSnapshotCommonController) deleteGroupSnapshotContent(content *crdv1alpha1.VolumeGroupSnapshotContent) {
839-
_ = ctrl.contentStore.Delete(content)
839+
_ = ctrl.groupSnapshotContentStore.Delete(content)
840840
klog.V(4).Infof("group snapshot content %q deleted", content.Name)
841841

842842
groupSnapshotName := utils.GroupSnapshotRefKey(&content.Spec.VolumeGroupSnapshotRef)

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: 25 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, SnapshotID []string, snapshotterCredentials map[string]string) error
3941
}
4042

4143
// csiHandler is a handler that calls CSI to create/delete volume snapshot.
@@ -165,6 +167,27 @@ 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+
// NOTE: snapshotIDs are required for DeleteGroupSnapshot
175+
if len(snapshotIDs) == 0 {
176+
return fmt.Errorf("cannot delete group snapshot content %s. No snapshots found in the group snapshot", content.Name)
177+
}
178+
var groupSnapshotHandle string
179+
180+
if content.Status != nil && content.Status.VolumeGroupSnapshotHandle != nil {
181+
groupSnapshotHandle = *content.Status.VolumeGroupSnapshotHandle
182+
} else if content.Spec.Source.VolumeGroupSnapshotHandle != nil {
183+
groupSnapshotHandle = *content.Spec.Source.VolumeGroupSnapshotHandle
184+
} else {
185+
return fmt.Errorf("failed to delete group snapshot content %s: groupsnapshotHandle is missing", content.Name)
186+
}
187+
188+
return handler.groupSnapshotter.DeleteGroupSnapshot(ctx, groupSnapshotHandle, snapshotIDs, snapshotterCredentials)
189+
}
190+
168191
func (handler *csiHandler) GetGroupSnapshotStatus(groupSnapshotContent *crdv1alpha1.VolumeGroupSnapshotContent, snapshotterListCredentials map[string]string) (bool, time.Time, error) {
169192
ctx, cancel := context.WithTimeout(context.Background(), handler.timeout)
170193
defer cancel()

0 commit comments

Comments
 (0)