Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check the volume before deleting the storage #74

Merged
merged 1 commit into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/storageprovider/nfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (

"github.com/linkernetworks/vortex/src/entity"
"github.com/linkernetworks/vortex/src/serviceprovider"
"gopkg.in/mgo.v2/bson"

appsv1 "k8s.io/api/apps/v1"
"k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
// "gopkg.in/mgo.v2/bson"
)

const NFS_PROVISIONER_PREFIX = "nfs-provisioner-"
Expand Down Expand Up @@ -119,6 +120,19 @@ func (nfs NFSStorageProvider) CreateStorage(sp *serviceprovider.Container, stora
func (nfs NFSStorageProvider) DeleteStorage(sp *serviceprovider.Container, storage *entity.Storage) error {
deployName := NFS_PROVISIONER_PREFIX + storage.ID.Hex()
storageName := NFS_STORAGECLASS_PREFIX + storage.ID.Hex()

//If the storage is used by some volume, we can't delete it.
q := bson.M{"storageName": storage.Name}
session := sp.Mongo.NewSession()
defer session.Close()

count, err := session.Count(entity.VolumeCollectionName, q)
if err != nil {
return err
} else if count > 0 {
return fmt.Errorf("The StorageName %s can't be deleted, since there're some volume still use it", storage.Name)
}

//Delete StorageClass
if err := sp.KubeCtl.DeleteStorageClass(storageName); err != nil {
return err
Expand Down
39 changes: 38 additions & 1 deletion src/storageprovider/nfs_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package storageprovider

import (
"math/rand"
"testing"
"time"

//"github.com/linkernetworks/mongo"
"github.com/linkernetworks/vortex/src/config"
"github.com/linkernetworks/vortex/src/entity"
"github.com/linkernetworks/vortex/src/serviceprovider"
"github.com/moby/moby/pkg/namesgenerator"
"github.com/stretchr/testify/suite"

"gopkg.in/mgo.v2/bson"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

type StorageTestSuite struct {
suite.Suite
sp *serviceprovider.Container
Expand Down Expand Up @@ -52,7 +60,6 @@ func (suite *StorageTestSuite) TestGetStorageClass() {

storageClass := getStorageClass(bson.NewObjectId().Hex(), bson.NewObjectId().Hex(), storage)
suite.NotNil(storageClass)

}

func (suite *StorageTestSuite) TestValidateBeforeCreating() {
Expand Down Expand Up @@ -124,6 +131,36 @@ func (suite *StorageTestSuite) TestDeleteStorage() {
suite.Error(err)
}

func (suite *StorageTestSuite) TestDeleteStorageFail() {
storage := &entity.Storage{
ID: bson.NewObjectId(),
Type: entity.NFSStorageType,
NFS: &entity.NFSStorage{
IP: "1.2.3.4",
PATH: "/exports",
},
Name: namesgenerator.GetRandomName(0),
}

volume := entity.Volume{
ID: bson.NewObjectId(),
StorageName: storage.Name,
}

session := suite.sp.Mongo.NewSession()
defer session.Close()

session.Insert(entity.VolumeCollectionName, volume)
defer session.Remove(entity.VolumeCollectionName, "_id", volume.ID)

sp, err := GetStorageProvider(storage)
suite.NoError(err)
sp = sp.(NFSStorageProvider)

err = sp.DeleteStorage(suite.sp, storage)
suite.Error(err)
}

func (suite *StorageTestSuite) TestValidateBeforeCreatingFail() {
testCases := []struct {
caseName string
Expand Down