Skip to content

Commit

Permalink
Merge pull request #80 from linkernetworks/hwchiu/VX-158
Browse files Browse the repository at this point in the history
Flat the storage entity and change the meta name of volume.

Former-commit-id: 16e8069190839641593528541d6c52f3e873c056 [formerly 89308e0]
Former-commit-id: 324d0b124528da2da137c5172b7ee28ac8248afe
  • Loading branch information
Hung-Wei Chiu authored Jul 11, 2018
2 parents c14aebd + 02830c4 commit f214af0
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 91 deletions.
16 changes: 6 additions & 10 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,17 @@ Example:

```
curl -X POST -H "Content-Type: application/json" \
-d '{"type":"nfs","name":"My First Storage","nfs":{"ip":"172.17.8.100","path":"/nfs"}}' \
-d '{"type":"nfs","name":"My First Storage","ip":"172.17.8.100","path":"/nfs"}' \
http://localhost:7890/v1/storage
```

Request Data:
```json
{
"type": "nfs",
"name": "My First Storage",
"nfs": {
"ip":"172.17.8.100",
"path":"/nfs"
}
"name": "My First Storage",
"ip":"172.17.8.100",
"path":"/nfs"
}

Response Data:
Expand Down Expand Up @@ -179,10 +177,8 @@ Response Data:
"name": "My First Storage",
"createdAt": "2018-07-09T03:42:12.708Z",
"storageClassName": "nfs-storageclass-5b42d9944807c52e1c804fbb",
"nfs": {
"ip": "172.17.8.100",
"path": "/nfs"
}
"ip": "172.17.8.100",
"path": "/nfs"
}
]
```
Expand Down
3 changes: 2 additions & 1 deletion src/entity/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ type Storage struct {
Name string `bson:"name" json:"name"`
CreatedAt *time.Time `bson:"createdAt,omitempty" json:"createdAt,omitempty"`
StorageClassName string `bson:"storageClassName" json:"storageClassName"`
NFS *NFSStorage `bson:"nfs,omitempty" json:"nfs,omitempty"`
IP string `bson:"ip" json:"ip"`
PATH string `bson:"path" json:"path"`
Fake *FakeStorage `bson:"fake,omitempty" json:"fake,omitempty"` //FakeStorage, for restful testing.
}

Expand Down
6 changes: 0 additions & 6 deletions src/entity/storage_nfs.go

This file was deleted.

7 changes: 3 additions & 4 deletions src/entity/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

const (
VolumeCollectionName string = "volume"
PVCNamePrefix string = "pvc-"
)

/*
Expand All @@ -22,7 +23,6 @@ type Volume struct {
StorageName string `bson:"storageName" json:"storageName"`
AccessMode corev1.PersistentVolumeAccessMode `bson:"accessMode" json:"accessMode"`
Capacity string `bson:"capacity" json:"capacity"`
MetaName string `bson:"metaName" json:"metaName"` //For PVC metaname
CreatedAt *time.Time `bson:"createdAt,omitempty" json:"createdAt,omitempty"`
}

Expand All @@ -31,7 +31,6 @@ func (m Volume) GetCollection() string {
return VolumeCollectionName
}

//GenerateMetaName - Generate a metaname for kubernetes PVC object
func (m Volume) GenerateMetaName() string {
return "pvc-" + m.ID.Hex()
func (m Volume) GetPVCName() string {
return PVCNamePrefix + m.ID.Hex()
}
1 change: 0 additions & 1 deletion src/server/handler_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func createVolume(ctx *web.Context) {
// Check whether this name has been used
v.ID = bson.NewObjectId()
v.CreatedAt = timeutils.Now()
v.MetaName = v.GenerateMetaName()
//Generate the metaName for PVC meta name and we will use it future
if err := volume.CreateVolume(sp, &v); err != nil {
response.InternalServerError(req.Request, resp.ResponseWriter, err)
Expand Down
3 changes: 0 additions & 3 deletions src/server/handler_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ func (suite *VolumeTestSuite) TestCreateVolume() {
suite.Equal(volume.StorageName, retVolume.StorageName)
suite.Equal(volume.AccessMode, retVolume.AccessMode)
suite.Equal(volume.Capacity, retVolume.Capacity)
suite.NotEqual("", retVolume.MetaName)

//We use the new write but empty input which will cause the readEntity Error
httpWriter = httptest.NewRecorder()
Expand Down Expand Up @@ -147,7 +146,6 @@ func (suite *VolumeTestSuite) TestDeleteVolume() {
StorageName: namesgenerator.GetRandomName(0),
Capacity: tCapacity,
AccessMode: tAccessMode,
MetaName: namesgenerator.GetRandomName(0),
}

err := suite.session.Insert(entity.StorageCollectionName, &entity.Storage{
Expand Down Expand Up @@ -239,7 +237,6 @@ func (suite *VolumeTestSuite) TestListVolume() {
suite.Equal(tc.expectSize, len(retVolumes))
for i, v := range retVolumes {
suite.Equal(volumes[i].Name, v.Name)
suite.Equal(volumes[i].MetaName, v.MetaName)
suite.Equal(volumes[i].StorageName, v.StorageName)
suite.Equal(volumes[i].AccessMode, v.AccessMode)
}
Expand Down
30 changes: 16 additions & 14 deletions src/storageprovider/nfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const NFS_PROVISIONER_PREFIX = "nfs-provisioner-"
const NFS_STORAGECLASS_PREFIX = "nfs-storageclass-"
const (
NFSProvisionerPrefix string = "nfs-provisioner-"
NFSStorageClassPrefix string = "nfs-storageclass-"
)

type NFSStorageProvider struct {
entity.NFSStorage
entity.Storage
}

func (nfs NFSStorageProvider) ValidateBeforeCreating(sp *serviceprovider.Container, storage *entity.Storage) error {
ip := net.ParseIP(storage.NFS.IP)
ip := net.ParseIP(storage.IP)
if len(ip) == 0 {
return fmt.Errorf("Invalid IP address %s\n", storage.NFS.IP)
return fmt.Errorf("Invalid IP address %s\n", storage.IP)
}

path := storage.NFS.PATH
path := storage.PATH
if path == "" || path[0] != '/' {
return fmt.Errorf("Invalid NFS export path %s\n", path)
}
Expand Down Expand Up @@ -68,8 +70,8 @@ func getDeployment(name string, storage *entity.Storage) *appsv1.Deployment {
ImagePullPolicy: v1.PullIfNotPresent,
Env: []v1.EnvVar{
{Name: "PROVISIONER_NAME", Value: name},
{Name: "NFS_SERVER", Value: storage.NFS.IP},
{Name: "NFS_PATH", Value: storage.NFS.PATH},
{Name: "NFS_SERVER", Value: storage.IP},
{Name: "NFS_PATH", Value: storage.PATH},
},
VolumeMounts: []v1.VolumeMount{
{Name: volumeName, MountPath: "/persistentvolumes"},
Expand All @@ -81,8 +83,8 @@ func getDeployment(name string, storage *entity.Storage) *appsv1.Deployment {
Name: volumeName,
VolumeSource: v1.VolumeSource{
NFS: &v1.NFSVolumeSource{
Server: storage.NFS.IP,
Path: storage.NFS.PATH,
Server: storage.IP,
Path: storage.PATH,
},
},
},
Expand All @@ -103,8 +105,8 @@ func getStorageClass(name string, provisioner string, storage *entity.Storage) *
}

func (nfs NFSStorageProvider) CreateStorage(sp *serviceprovider.Container, storage *entity.Storage) error {
name := NFS_PROVISIONER_PREFIX + storage.ID.Hex()
storageClassName := NFS_STORAGECLASS_PREFIX + storage.ID.Hex()
name := NFSProvisionerPrefix + storage.ID.Hex()
storageClassName := NFSStorageClassPrefix + storage.ID.Hex()
//Create deployment
deployment := getDeployment(name, storage)
//Create storageClass
Expand All @@ -118,8 +120,8 @@ 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()
deployName := NFSProvisionerPrefix + storage.ID.Hex()
storageName := NFSStorageClassPrefix + storage.ID.Hex()

//If the storage is used by some volume, we can't delete it.
q := bson.M{"storageName": storage.Name}
Expand Down
58 changes: 20 additions & 38 deletions src/storageprovider/nfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ func TestStorageSuite(t *testing.T) {
func (suite *StorageTestSuite) TestGetDeployment() {
storage := &entity.Storage{
Type: entity.NFSStorageType,
NFS: &entity.NFSStorage{
IP: "1.2.3.4",
PATH: "/exports",
},
IP: "1.2.3.4",
PATH: "/exports",
}

deployment := getDeployment(bson.NewObjectId().Hex(), storage)
Expand All @@ -52,10 +50,8 @@ func (suite *StorageTestSuite) TestGetDeployment() {
func (suite *StorageTestSuite) TestGetStorageClass() {
storage := &entity.Storage{
Type: entity.NFSStorageType,
NFS: &entity.NFSStorage{
IP: "1.2.3.4",
PATH: "/exports",
},
IP: "1.2.3.4",
PATH: "/exports",
}

storageClass := getStorageClass(bson.NewObjectId().Hex(), bson.NewObjectId().Hex(), storage)
Expand All @@ -65,10 +61,8 @@ func (suite *StorageTestSuite) TestGetStorageClass() {
func (suite *StorageTestSuite) TestValidateBeforeCreating() {
storage := &entity.Storage{
Type: entity.NFSStorageType,
NFS: &entity.NFSStorage{
IP: "1.2.3.4",
PATH: "/exports",
},
IP: "1.2.3.4",
PATH: "/exports",
}

//Parameters
Expand All @@ -84,10 +78,8 @@ func (suite *StorageTestSuite) TestCreateStorage() {
storage := entity.Storage{
ID: bson.NewObjectId(),
Type: entity.NFSStorageType,
NFS: &entity.NFSStorage{
IP: "1.2.3.4",
PATH: "/exports",
},
IP: "1.2.3.4",
PATH: "/exports",
}

sp, err := GetStorageProvider(&storage)
Expand All @@ -97,7 +89,7 @@ func (suite *StorageTestSuite) TestCreateStorage() {
err = sp.CreateStorage(suite.sp, &storage)
suite.NoError(err)

deploy, err := suite.sp.KubeCtl.GetDeployment(NFS_PROVISIONER_PREFIX + storage.ID.Hex())
deploy, err := suite.sp.KubeCtl.GetDeployment(NFSProvisionerPrefix + storage.ID.Hex())
suite.NotNil(deploy)
suite.NoError(err)
}
Expand All @@ -106,10 +98,8 @@ func (suite *StorageTestSuite) TestDeleteStorage() {
storage := &entity.Storage{
ID: bson.NewObjectId(),
Type: entity.NFSStorageType,
NFS: &entity.NFSStorage{
IP: "1.2.3.4",
PATH: "/exports",
},
IP: "1.2.3.4",
PATH: "/exports",
}

sp, err := GetStorageProvider(storage)
Expand All @@ -119,14 +109,14 @@ func (suite *StorageTestSuite) TestDeleteStorage() {
err = sp.CreateStorage(suite.sp, storage)
suite.NoError(err)

deploy, err := suite.sp.KubeCtl.GetDeployment(NFS_PROVISIONER_PREFIX + storage.ID.Hex())
deploy, err := suite.sp.KubeCtl.GetDeployment(NFSProvisionerPrefix + storage.ID.Hex())
suite.NotNil(deploy)
suite.NoError(err)

err = sp.DeleteStorage(suite.sp, storage)
suite.NoError(err)

deploy, err = suite.sp.KubeCtl.GetDeployment(NFS_PROVISIONER_PREFIX + storage.ID.Hex())
deploy, err = suite.sp.KubeCtl.GetDeployment(NFSProvisionerPrefix + storage.ID.Hex())
suite.Nil(deploy)
suite.Error(err)
}
Expand All @@ -135,10 +125,8 @@ func (suite *StorageTestSuite) TestDeleteStorageFail() {
storage := &entity.Storage{
ID: bson.NewObjectId(),
Type: entity.NFSStorageType,
NFS: &entity.NFSStorage{
IP: "1.2.3.4",
PATH: "/exports",
},
IP: "1.2.3.4",
PATH: "/exports",
Name: namesgenerator.GetRandomName(0),
}

Expand Down Expand Up @@ -168,23 +156,17 @@ func (suite *StorageTestSuite) TestValidateBeforeCreatingFail() {
}{
{"invalidIP", &entity.Storage{
Type: entity.NFSStorageType,
NFS: &entity.NFSStorage{
IP: "a.b.c.d",
},
IP: "a.b.c.d",
}},
{"invalidExports-1", &entity.Storage{
Type: entity.NFSStorageType,
NFS: &entity.NFSStorage{
IP: "1.2.3.4",
PATH: "tmp",
},
IP: "1.2.3.4",
PATH: "tmp",
}},
{"invalidExports-2", &entity.Storage{
Type: entity.NFSStorageType,
NFS: &entity.NFSStorage{
IP: "1.2.3.4",
PATH: "",
},
IP: "1.2.3.4",
PATH: "",
}},
}

Expand Down
2 changes: 1 addition & 1 deletion src/storageprovider/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type StorageProvider interface {
func GetStorageProvider(storage *entity.Storage) (StorageProvider, error) {
switch storage.Type {
case "nfs":
return NFSStorageProvider{*storage.NFS}, nil
return NFSStorageProvider{*storage}, nil
case "fake":
return FakeStorageProvider{*storage.Fake}, nil
default:
Expand Down
1 change: 0 additions & 1 deletion src/storageprovider/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func TestGetStorageProvider(t *testing.T) {
&entity.Storage{
Type: tc.storageType,
Fake: &entity.FakeStorage{},
NFS: &entity.NFSStorage{},
})
assert.NoError(t, err)
a := reflect.TypeOf(provider)
Expand Down
13 changes: 8 additions & 5 deletions src/volume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func getPVCInstance(volume *entity.Volume, storageClassName string) *v1.PersistentVolumeClaim {
func getPVCInstance(volume *entity.Volume, name string, storageClassName string) *v1.PersistentVolumeClaim {
capacity, _ := resource.ParseQuantity(volume.Capacity)
return &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: volume.MetaName,
Name: name,
},
Spec: v1.PersistentVolumeClaimSpec{
AccessModes: []v1.PersistentVolumeAccessMode{volume.AccessMode},
Expand All @@ -41,16 +41,19 @@ func getStorageClassName(session *mongo.Session, storageName string) (string, er
func CreateVolume(sp *serviceprovider.Container, volume *entity.Volume) error {
session := sp.Mongo.NewSession()
defer session.Close()
//fetch the db to get the storageName
storageName, err := getStorageClassName(session, volume.StorageName)
if err != nil {
return err
}
//fetch the db to get the storageName
pvc := getPVCInstance(volume, storageName)

name := volume.GetPVCName()
pvc := getPVCInstance(volume, name, storageName)
_, err = sp.KubeCtl.CreatePVC(pvc)
return err
}

func DeleteVolume(sp *serviceprovider.Container, volume *entity.Volume) error {
return sp.KubeCtl.DeletePVC(volume.MetaName)
name := volume.GetPVCName()
return sp.KubeCtl.DeletePVC(name)
}
Loading

0 comments on commit f214af0

Please sign in to comment.