Skip to content

Commit

Permalink
Check the Pod when remove the volume
Browse files Browse the repository at this point in the history
Former-commit-id: 0514ae9595052656a783d01e07321e0218529b8b [formerly 0514ae9595052656a783d01e07321e0218529b8b [formerly 13c5ec8]]
Former-commit-id: fee680ec6bf83f86c37058807ab49398eff6ef12
Former-commit-id: 0ac7116
  • Loading branch information
hwchiu committed Jul 12, 2018
1 parent 19061e3 commit 744f565
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 11 deletions.
17 changes: 15 additions & 2 deletions src/volume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package volume

import (
"fmt"
"strings"

"github.com/linkernetworks/mongo"
"github.com/linkernetworks/vortex/src/entity"
"github.com/linkernetworks/vortex/src/serviceprovider"
Expand Down Expand Up @@ -60,15 +62,26 @@ func DeleteVolume(sp *serviceprovider.Container, volume *entity.Volume) error {
defer session.Close()

pods := []entity.Pod{}
fmt.Println("Volume.Name", volume.Name)
err := session.FindAll(entity.PodCollectionName, bson.M{"volumes.name": volume.Name}, &pods)
if err != nil {
return fmt.Errorf("Load the database fail:%v", err)
}

usedPod := []string{}
for _, pod := range pods {
//Check the pod's status, report error if at least one pod is running.
fmt.Println(pod)
currentPod, err := sp.KubeCtl.GetPod(pod.Name)
if err != nil {
continue
}

if !sp.KubeCtl.DoesPodCompleted(currentPod) {
usedPod = append(usedPod, pod.Name)
}
}
if len(usedPod) != 0 {
podNames := strings.Join(usedPod, ",")
return fmt.Errorf("Delete the volume [%s] fail, since the followings pods still ust it: %s", volume.Name, podNames)
}

return sp.KubeCtl.DeletePVC(volume.GetPVCName())
Expand Down
60 changes: 51 additions & 9 deletions src/volume/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
"github.com/moby/moby/pkg/namesgenerator"
"github.com/stretchr/testify/suite"
"gopkg.in/mgo.v2/bson"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func init() {
Expand Down Expand Up @@ -117,19 +120,58 @@ func (suite *VolumeTestSuite) TestDeleteVolumeFail() {
session := suite.sp.Mongo.NewSession()
defer session.Close()

pod := entity.Pod{
ID: bson.NewObjectId(),
Name: namesgenerator.GetRandomName(0),
Volumes: []entity.PodVolume{
{
Name: volume.Name,
pods := []entity.Pod{
{
ID: bson.NewObjectId(),
Name: namesgenerator.GetRandomName(0),
Volumes: []entity.PodVolume{
{
Name: volume.Name,
},
},
},
{
ID: bson.NewObjectId(),
Name: namesgenerator.GetRandomName(0),
Volumes: []entity.PodVolume{
{
Name: volume.Name,
},
},
},
}

session.Insert(entity.PodCollectionName, pod)
defer session.Remove(entity.PodCollectionName, "name", pod.Name)
for _, pod := range pods {
session.Insert(entity.PodCollectionName, pod)
defer session.Remove(entity.PodCollectionName, "name", pod.Name)
}

DeleteVolume(suite.sp, volume)
//Create the pod via kubectl
suite.sp.KubeCtl.CreatePod(&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: pods[0].Name,
},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
},
})
suite.sp.KubeCtl.CreatePod(&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: pods[1].Name,
},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
},
})
suite.sp.KubeCtl.CreatePod(&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: namesgenerator.GetRandomName(0),
},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
},
})

err := DeleteVolume(suite.sp, volume)
suite.Error(err)
}

0 comments on commit 744f565

Please sign in to comment.