Skip to content

Commit

Permalink
Avoid create tag from metadata req of snapshot (#1575)
Browse files Browse the repository at this point in the history
  • Loading branch information
jichenjc committed Jul 14, 2021
1 parent 4ee2693 commit 3431885
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
8 changes: 6 additions & 2 deletions pkg/csi/cinder/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,12 @@ func (cs *controllerServer) CreateSnapshot(ctx context.Context, req *csi.CreateS
} else {
// Add cluster ID to the snapshot metadata
properties := map[string]string{cinderCSIClusterIDKey: cs.Driver.cluster}
for mKey, mVal := range req.Parameters {
properties[mKey] = mVal

// see https://github.com/kubernetes-csi/external-snapshotter/pull/375/
for _, mKey := range []string{"csi.storage.k8s.io/volumesnapshot/name", "csi.storage.k8s.io/volumesnapshot/namespace", "csi.storage.k8s.io/volumesnapshotcontent/name"} {
if v, ok := req.Parameters[mKey]; ok {
properties[mKey] = v
}
}

// TODO: Delegate the check to openstack itself and ignore the conflict
Expand Down
42 changes: 41 additions & 1 deletion pkg/csi/cinder/controllerserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ func TestListVolumes(t *testing.T) {
// Test CreateSnapshot
func TestCreateSnapshot(t *testing.T) {

osmock.On("CreateSnapshot", FakeSnapshotName, FakeVolID, &map[string]string{cinderCSIClusterIDKey: "cluster", "tag": "tag1"}).Return(&FakeSnapshotRes, nil)
osmock.On("CreateSnapshot", FakeSnapshotName, FakeVolID, &map[string]string{cinderCSIClusterIDKey: "cluster"}).Return(&FakeSnapshotRes, nil)
osmock.On("ListSnapshots", map[string]string{"Name": FakeSnapshotName}).Return(FakeSnapshotListEmpty, "", nil)
osmock.On("WaitSnapshotReady", FakeSnapshotID).Return(nil)

Expand All @@ -433,6 +433,46 @@ func TestCreateSnapshot(t *testing.T) {
assert.NotNil(FakeSnapshotID, actualRes.Snapshot.SnapshotId)
}

// Test CreateSnapshot with extra metadata
func TestCreateSnapshotWithExtraMetadata(t *testing.T) {

properties := map[string]string{
"cinder.csi.openstack.org/cluster": FakeCluster,
"csi.storage.k8s.io/volumesnapshot/name": FakeSnapshotName,
"csi.storage.k8s.io/volumesnapshotcontent/name": FakeSnapshotContentName,
"csi.storage.k8s.io/volumesnapshot/namespace": FakeSnapshotNamespace,
}

osmock.On("CreateSnapshot", FakeSnapshotName, FakeVolID, &properties).Return(&FakeSnapshotRes, nil)
osmock.On("ListSnapshots", map[string]string{"Name": FakeSnapshotName}).Return(FakeSnapshotListEmpty, "", nil)
osmock.On("WaitSnapshotReady", FakeSnapshotID).Return(nil)

// Init assert
assert := assert.New(t)

// Fake request
fakeReq := &csi.CreateSnapshotRequest{
Name: FakeSnapshotName,
SourceVolumeId: FakeVolID,
Parameters: map[string]string{
"csi.storage.k8s.io/volumesnapshot/name": FakeSnapshotName,
"csi.storage.k8s.io/volumesnapshotcontent/name": FakeSnapshotContentName,
"csi.storage.k8s.io/volumesnapshot/namespace": FakeSnapshotNamespace,
},
}

// Invoke CreateSnapshot
actualRes, err := fakeCs.CreateSnapshot(FakeCtx, fakeReq)
if err != nil {
t.Errorf("failed to CreateSnapshot: %v", err)
}

// Assert
assert.Equal(FakeVolID, actualRes.Snapshot.SourceVolumeId)

assert.NotNil(FakeSnapshotID, actualRes.Snapshot.SnapshotId)
}

// Test DeleteSnapshot
func TestDeleteSnapshot(t *testing.T) {

Expand Down
2 changes: 2 additions & 0 deletions pkg/csi/cinder/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ var FakeStagingTargetPath = "/mnt/globalmount"
var FakePVName = "fakepv-1"
var FakePVCName = "fakepvc-1"
var FakePVCNamespace = "fakepvc-ns"
var FakeSnapshotContentName = "fake-content"
var FakeSnapshotNamespace = "fakesnapshot-ns"
var FakeAttachment = volumes.Attachment{
ServerID: FakeNodeID,
}
Expand Down

0 comments on commit 3431885

Please sign in to comment.