Skip to content

Commit

Permalink
Add labels to cloned snapshots (#1662)
Browse files Browse the repository at this point in the history
* Add label to cloned snapshots

* fix call CreateFromSource from create_csi_snapshot_static.go

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
AKhoria and mergify[bot] committed Oct 21, 2022
1 parent 94affa6 commit 4dca8c8
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 62 deletions.
2 changes: 1 addition & 1 deletion pkg/function/create_csi_snapshot_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func createCSISnapshotStatic(
Driver: driver,
VolumeSnapshotClassName: snapshotClass,
}
if err := snapshotter.CreateFromSource(ctx, source, name, namespace, wait); err != nil {
if err := snapshotter.CreateFromSource(ctx, source, name, namespace, wait, nil); err != nil {
return nil, err
}

Expand Down
91 changes: 46 additions & 45 deletions pkg/kube/snapshot/mocks/mock_snapshotter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions pkg/kube/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ type Snapshotter interface {
// 'cloneName' is name of the clone.
// 'cloneNamespace' is the namespace where the clone will be created.
// 'waitForReady' will make the function blocks until the clone's status is ready to use.
Clone(ctx context.Context, name, namespace, cloneName, cloneNamespace string, waitForReady bool) error
// 'labels' is the labels to set on the created VSC
Clone(ctx context.Context, name, namespace, cloneName, cloneNamespace string, waitForReady bool, labels map[string]string) error
// GetSource will return the CSI source that backs the volume snapshot.
//
// 'snapshotName' is the name of the Volumesnapshot.
Expand All @@ -94,7 +95,8 @@ type Snapshotter interface {
// 'snapshotName' is the name of the snapshot that will be created.
// 'namespace' is the namespace of the snapshot.
// 'waitForReady' blocks the caller until snapshot is ready to use or context is cancelled.
CreateFromSource(ctx context.Context, source *Source, snapshotName, namespace string, waitForReady bool) error
// 'labels' is the labels to set on the created VSC
CreateFromSource(ctx context.Context, source *Source, snapshotName, namespace string, waitForReady bool, labels map[string]string) error
// CreateContentFromSource will create a 'VolumesnaphotContent' for the underlying snapshot source.
//
// 'source' contains information about CSI snapshot.
Expand Down
8 changes: 4 additions & 4 deletions pkg/kube/snapshot/snapshot_alpha.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (sna *SnapshotAlpha) DeleteContent(ctx context.Context, name string) error

// Clone will clone the VolumeSnapshot to namespace 'cloneNamespace'.
// Underlying VolumeSnapshotContent will be cloned with a different name.
func (sna *SnapshotAlpha) Clone(ctx context.Context, name, namespace, cloneName, cloneNamespace string, waitForReady bool) error {
func (sna *SnapshotAlpha) Clone(ctx context.Context, name, namespace, cloneName, cloneNamespace string, waitForReady bool, labels map[string]string) error {
_, err := sna.Get(ctx, cloneName, cloneNamespace)
if err == nil {
return errors.Errorf("Target snapshot already exists in target namespace, Volumesnapshot: %s, Namespace: %s", cloneName, cloneNamespace)
Expand All @@ -232,7 +232,7 @@ func (sna *SnapshotAlpha) Clone(ctx context.Context, name, namespace, cloneName,
if err != nil {
return errors.Errorf("Failed to get source")
}
return sna.CreateFromSource(ctx, src, cloneName, cloneNamespace, waitForReady)
return sna.CreateFromSource(ctx, src, cloneName, cloneNamespace, waitForReady, labels)
}

// GetSource will return the CSI source that backs the volume snapshot.
Expand Down Expand Up @@ -262,13 +262,13 @@ func (sna *SnapshotAlpha) GetSource(ctx context.Context, snapshotName, namespace
}

// CreateFromSource will create a 'Volumesnapshot' and 'VolumesnaphotContent' pair for the underlying snapshot source.
func (sna *SnapshotAlpha) CreateFromSource(ctx context.Context, source *Source, snapshotName, namespace string, waitForReady bool) error {
func (sna *SnapshotAlpha) CreateFromSource(ctx context.Context, source *Source, snapshotName, namespace string, waitForReady bool, labels map[string]string) error {
deletionPolicy, err := sna.getDeletionPolicyFromClass(source.VolumeSnapshotClassName)
if err != nil {
return errors.Wrap(err, "Failed to get DeletionPolicy from VolumeSnapshotClass")
}
contentName := snapshotName + "-content-" + string(uuid.NewUUID())
snap := UnstructuredVolumeSnapshotAlpha(snapshotName, namespace, "", contentName, source.VolumeSnapshotClassName, nil)
snap := UnstructuredVolumeSnapshotAlpha(snapshotName, namespace, "", contentName, source.VolumeSnapshotClassName, labels)
if err := sna.CreateContentFromSource(ctx, source, contentName, snapshotName, namespace, deletionPolicy); err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/kube/snapshot/snapshot_beta.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (sna *SnapshotBeta) DeleteContent(ctx context.Context, name string) error {

// Clone will clone the VolumeSnapshot to namespace 'cloneNamespace'.
// Underlying VolumeSnapshotContent will be cloned with a different name.
func (sna *SnapshotBeta) Clone(ctx context.Context, name, namespace, cloneName, cloneNamespace string, waitForReady bool) error {
func (sna *SnapshotBeta) Clone(ctx context.Context, name, namespace, cloneName, cloneNamespace string, waitForReady bool, labels map[string]string) error {
_, err := sna.Get(ctx, cloneName, cloneNamespace)
if err == nil {
return errors.Errorf("Target snapshot already exists in target namespace, Volumesnapshot: %s, Namespace: %s", cloneName, cloneNamespace)
Expand All @@ -195,7 +195,7 @@ func (sna *SnapshotBeta) Clone(ctx context.Context, name, namespace, cloneName,
if err != nil {
return errors.Errorf("Failed to get source")
}
return sna.CreateFromSource(ctx, src, cloneName, cloneNamespace, waitForReady)
return sna.CreateFromSource(ctx, src, cloneName, cloneNamespace, waitForReady, labels)
}

// GetSource will return the CSI source that backs the volume snapshot.
Expand Down Expand Up @@ -230,13 +230,13 @@ func getSnapshotSource(ctx context.Context, dynCli dynamic.Interface, snapGVR, s
}

// CreateFromSource will create a 'Volumesnapshot' and 'VolumesnaphotContent' pair for the underlying snapshot source.
func (sna *SnapshotBeta) CreateFromSource(ctx context.Context, source *Source, snapshotName, namespace string, waitForReady bool) error {
func (sna *SnapshotBeta) CreateFromSource(ctx context.Context, source *Source, snapshotName, namespace string, waitForReady bool, labels map[string]string) error {
deletionPolicy, err := getDeletionPolicyFromClass(sna.dynCli, v1beta1.VolSnapClassGVR, source.VolumeSnapshotClassName)
if err != nil {
return errors.Wrap(err, "Failed to get DeletionPolicy from VolumeSnapshotClass")
}
contentName := snapshotName + "-content-" + string(uuid.NewUUID())
snap := UnstructuredVolumeSnapshot(v1beta1.VolSnapGVR, snapshotName, namespace, "", contentName, source.VolumeSnapshotClassName, nil)
snap := UnstructuredVolumeSnapshot(v1beta1.VolSnapGVR, snapshotName, namespace, "", contentName, source.VolumeSnapshotClassName, labels)

if err := sna.CreateContentFromSource(ctx, source, contentName, snapshotName, namespace, deletionPolicy); err != nil {
return err
Expand Down
Loading

0 comments on commit 4dca8c8

Please sign in to comment.