Add groupSnapshotNamePrefix and groupSnapshotNameUUIDLength CLI options

This commit is contained in:
Raunak Pradip Shah
2023-04-26 13:24:09 +05:30
parent d8d01a38e4
commit 9e2d4f6264
3 changed files with 35 additions and 14 deletions

View File

@@ -87,6 +87,9 @@ var (
retryIntervalMax = flag.Duration("retry-interval-max", 5*time.Minute, "Maximum retry interval of failed volume snapshot creation or deletion. Default is 5 minutes.") retryIntervalMax = flag.Duration("retry-interval-max", 5*time.Minute, "Maximum retry interval of failed volume snapshot creation or deletion. Default is 5 minutes.")
enableNodeDeployment = flag.Bool("node-deployment", false, "Enables deploying the sidecar controller together with a CSI driver on nodes to manage snapshots for node-local volumes.") enableNodeDeployment = flag.Bool("node-deployment", false, "Enables deploying the sidecar controller together with a CSI driver on nodes to manage snapshots for node-local volumes.")
enableVolumeGroupSnapshots = flag.Bool("enable-volume-group-snapshots", false, "Enables the volume group snapshot feature, allowing the user to create snapshots of groups of volumes.") enableVolumeGroupSnapshots = flag.Bool("enable-volume-group-snapshots", false, "Enables the volume group snapshot feature, allowing the user to create snapshots of groups of volumes.")
groupSnapshotNamePrefix = flag.String("groupsnapshot-name-prefix", "groupsnapshot", "Prefix to apply to the name of a created group snapshot")
groupSnapshotNameUUIDLength = flag.Int("groupsnapshot-name-uuid-length", -1, "Length in characters for the generated uuid of a created group snapshot. Defaults behavior is to NOT truncate.")
) )
var ( var (
@@ -227,6 +230,10 @@ func main() {
var groupSnapshotter group_snapshotter.GroupSnapshotter var groupSnapshotter group_snapshotter.GroupSnapshotter
if *enableVolumeGroupSnapshots { if *enableVolumeGroupSnapshots {
groupSnapshotter = group_snapshotter.NewGroupSnapshotter(csiConn) groupSnapshotter = group_snapshotter.NewGroupSnapshotter(csiConn)
if len(*groupSnapshotNamePrefix) == 0 {
klog.Error("group snapshot name prefix cannot be of length 0")
os.Exit(1)
}
} }
ctrl := controller.NewCSISnapshotSideCarController( ctrl := controller.NewCSISnapshotSideCarController(
@@ -241,6 +248,8 @@ func main() {
*resyncPeriod, *resyncPeriod,
*snapshotNamePrefix, *snapshotNamePrefix,
*snapshotNameUUIDLength, *snapshotNameUUIDLength,
*groupSnapshotNamePrefix,
*groupSnapshotNameUUIDLength,
*extraCreateMetadata, *extraCreateMetadata,
workqueue.NewItemExponentialFailureRateLimiter(*retryIntervalStart, *retryIntervalMax), workqueue.NewItemExponentialFailureRateLimiter(*retryIntervalStart, *retryIntervalMax),
*enableVolumeGroupSnapshots, *enableVolumeGroupSnapshots,

View File

@@ -40,11 +40,13 @@ type Handler interface {
// csiHandler is a handler that calls CSI to create/delete volume snapshot. // csiHandler is a handler that calls CSI to create/delete volume snapshot.
type csiHandler struct { type csiHandler struct {
snapshotter snapshotter.Snapshotter snapshotter snapshotter.Snapshotter
groupSnapshotter group_snapshotter.GroupSnapshotter groupSnapshotter group_snapshotter.GroupSnapshotter
timeout time.Duration timeout time.Duration
snapshotNamePrefix string snapshotNamePrefix string
snapshotNameUUIDLength int snapshotNameUUIDLength int
groupSnapshotNamePrefix string
groupSnapshotNameUUIDLength int
} }
// NewCSIHandler returns a handler which includes the csi connection and Snapshot name details // NewCSIHandler returns a handler which includes the csi connection and Snapshot name details
@@ -54,13 +56,17 @@ func NewCSIHandler(
timeout time.Duration, timeout time.Duration,
snapshotNamePrefix string, snapshotNamePrefix string,
snapshotNameUUIDLength int, snapshotNameUUIDLength int,
groupSnapshotNamePrefix string,
groupSnapshotNameUUIDLength int,
) Handler { ) Handler {
return &csiHandler{ return &csiHandler{
snapshotter: snapshotter, snapshotter: snapshotter,
groupSnapshotter: groupSnapshotter, groupSnapshotter: groupSnapshotter,
timeout: timeout, timeout: timeout,
snapshotNamePrefix: snapshotNamePrefix, snapshotNamePrefix: snapshotNamePrefix,
snapshotNameUUIDLength: snapshotNameUUIDLength, snapshotNameUUIDLength: snapshotNameUUIDLength,
groupSnapshotNamePrefix: groupSnapshotNamePrefix,
groupSnapshotNameUUIDLength: groupSnapshotNameUUIDLength,
} }
} }
@@ -152,7 +158,7 @@ func (handler *csiHandler) CreateGroupSnapshot(content *crdv1alpha1.VolumeGroupS
return "", "", nil, time.Time{}, false, fmt.Errorf("cannot create group snapshot. PVCs to be snapshotted not found in group snapshot content %s", content.Name) return "", "", nil, time.Time{}, false, fmt.Errorf("cannot create group snapshot. PVCs to be snapshotted not found in group snapshot content %s", content.Name)
} }
groupSnapshotName, err := makeGroupSnapshotName(handler.snapshotNamePrefix, string(content.Spec.VolumeGroupSnapshotRef.UID)) groupSnapshotName, err := handler.makeGroupSnapshotName(string(content.Spec.VolumeGroupSnapshotRef.UID))
if err != nil { if err != nil {
return "", "", nil, time.Time{}, false, err return "", "", nil, time.Time{}, false, err
} }
@@ -181,9 +187,13 @@ func (handler *csiHandler) GetGroupSnapshotStatus(groupSnapshotContent *crdv1alp
return csiSnapshotStatus, timestamp, nil return csiSnapshotStatus, timestamp, nil
} }
func makeGroupSnapshotName(groupSnapshotUID string) (string, error) { func (handler *csiHandler) makeGroupSnapshotName(groupSnapshotUID string) (string, error) {
if len(groupSnapshotUID) == 0 { if len(groupSnapshotUID) == 0 {
return "", fmt.Errorf("group snapshot object is missing UID") return "", fmt.Errorf("group snapshot object is missing UID")
} }
return fmt.Sprintf("groupsnapshot-%s", strings.Replace(groupSnapshotUID, "-", "", -1)), nil if handler.groupSnapshotNameUUIDLength == -1 {
return fmt.Sprintf("%s-%s", handler.groupSnapshotNamePrefix, groupSnapshotUID), nil
}
return fmt.Sprintf("%s-%s", handler.groupSnapshotNamePrefix, strings.Replace(groupSnapshotUID, "-", "", -1)[0:handler.groupSnapshotNameUUIDLength]), nil
} }

View File

@@ -85,6 +85,8 @@ func NewCSISnapshotSideCarController(
resyncPeriod time.Duration, resyncPeriod time.Duration,
snapshotNamePrefix string, snapshotNamePrefix string,
snapshotNameUUIDLength int, snapshotNameUUIDLength int,
groupSnapshotNamePrefix string,
groupSnapshotNameUUIDLength int,
extraCreateMetadata bool, extraCreateMetadata bool,
contentRateLimiter workqueue.RateLimiter, contentRateLimiter workqueue.RateLimiter,
enableVolumeGroupSnapshots bool, enableVolumeGroupSnapshots bool,
@@ -103,7 +105,7 @@ func NewCSISnapshotSideCarController(
client: client, client: client,
driverName: driverName, driverName: driverName,
eventRecorder: eventRecorder, eventRecorder: eventRecorder,
handler: NewCSIHandler(snapshotter, groupSnapshotter, timeout, snapshotNamePrefix, snapshotNameUUIDLength), handler: NewCSIHandler(snapshotter, groupSnapshotter, timeout, snapshotNamePrefix, snapshotNameUUIDLength, groupSnapshotNamePrefix, groupSnapshotNameUUIDLength),
resyncPeriod: resyncPeriod, resyncPeriod: resyncPeriod,
contentStore: cache.NewStore(cache.DeletionHandlingMetaNamespaceKeyFunc), contentStore: cache.NewStore(cache.DeletionHandlingMetaNamespaceKeyFunc),
contentQueue: workqueue.NewNamedRateLimitingQueue(contentRateLimiter, "csi-snapshotter-content"), contentQueue: workqueue.NewNamedRateLimitingQueue(contentRateLimiter, "csi-snapshotter-content"),