Skip to content

Commit

Permalink
Add tags to snapshot based on VolumeSnapshotClass
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorJC3 authored and gtxu committed Sep 15, 2022
1 parent 51827cd commit 347ae48
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,29 @@ func (d *controllerService) CreateSnapshot(ctx context.Context, req *csi.CreateS
cloud.SnapshotNameTagKey: snapshotName,
cloud.AwsEbsDriverTagKey: isManagedByDriver,
}

var vscTags []string
for key, value := range req.GetParameters() {
if strings.HasPrefix(key, TagKeyPrefix) {
vscTags = append(vscTags, value)
} else {
return nil, status.Errorf(codes.InvalidArgument, "Invalid parameter key %s for CreateSnapshot", key)
}
}

addTags, err := template.Evaluate(vscTags, nil, d.driverOptions.warnOnInvalidTag)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "Error interpolating the tag value: %v", err)
}

if err := validateExtraTags(addTags, d.driverOptions.warnOnInvalidTag); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "Invalid tag value: %v", err)
}

for k, v := range addTags {
snapshotTags[k] = v
}

if d.driverOptions.kubernetesClusterID != "" {
resourceLifecycleTag := ResourceLifecycleTagPrefix + d.driverOptions.kubernetesClusterID
snapshotTags[resourceLifecycleTag] = ResourceLifecycleOwned
Expand Down
57 changes: 57 additions & 0 deletions pkg/driver/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2295,6 +2295,63 @@ func TestCreateSnapshot(t *testing.T) {
checkExpectedErrorCode(t, err, codes.Aborted)
},
},
{
name: "success with VolumeSnapshotClass tags",
testFunc: func(t *testing.T) {
const (
snapshotName = "test-snapshot"
extraTagKey = "test-key"
extraTagValue = "test-value"
)

req := &csi.CreateSnapshotRequest{
Name: snapshotName,
Parameters: map[string]string{
"tagSpecification_1": fmt.Sprintf("%s=%s", extraTagKey, extraTagValue),
},
SourceVolumeId: "vol-test",
}
expSnapshot := &csi.Snapshot{
ReadyToUse: true,
}

ctx := context.Background()
mockSnapshot := &cloud.Snapshot{
SnapshotID: fmt.Sprintf("snapshot-%d", rand.New(rand.NewSource(time.Now().UnixNano())).Uint64()),
SourceVolumeID: req.SourceVolumeId,
Size: 1,
CreationTime: time.Now(),
}
mockCtl := gomock.NewController(t)
defer mockCtl.Finish()

snapshotOptions := &cloud.SnapshotOptions{
Tags: map[string]string{
cloud.SnapshotNameTagKey: snapshotName,
cloud.AwsEbsDriverTagKey: isManagedByDriver,
extraTagKey: extraTagValue,
},
}

mockCloud := cloud.NewMockCloud(mockCtl)
mockCloud.EXPECT().CreateSnapshot(gomock.Eq(ctx), gomock.Eq(req.SourceVolumeId), gomock.Eq(snapshotOptions)).Return(mockSnapshot, nil)
mockCloud.EXPECT().GetSnapshotByName(gomock.Eq(ctx), gomock.Eq(req.GetName())).Return(nil, cloud.ErrNotFound)

awsDriver := controllerService{
cloud: mockCloud,
inFlight: internal.NewInFlight(),
driverOptions: &DriverOptions{},
}
resp, err := awsDriver.CreateSnapshot(context.Background(), req)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if snap := resp.GetSnapshot(); snap == nil {
t.Fatalf("Expected snapshot %v, got nil", expSnapshot)
}
},
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 347ae48

Please sign in to comment.