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 Pod when we delete the volume #87

Merged
merged 4 commits into from
Jul 13, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 10 additions & 0 deletions src/kubernetes/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,13 @@ func (kc *KubeCtl) DeletePod(name string) error {
options := metav1.DeleteOptions{}
return kc.Clientset.CoreV1().Pods(kc.Namespace).Delete(name, &options)
}

func (kc *KubeCtl) DoesPodCompleted(pod *corev1.Pod) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about IsPodCompleted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good

switch pod.Status.Phase {
case corev1.PodRunning, corev1.PodPending:
return false
default:
return true
}
return true
}
39 changes: 39 additions & 0 deletions src/kubernetes/pods_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package kubernetes

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

"github.com/moby/moby/pkg/namesgenerator"
"github.com/stretchr/testify/suite"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
fakeclientset "k8s.io/client-go/kubernetes/fake"
)

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

type KubeCtlPodTestSuite struct {
suite.Suite
kubectl *KubeCtl
Expand Down Expand Up @@ -77,6 +84,38 @@ func (suite *KubeCtlPodTestSuite) TestCreateDeletePod() {
suite.NoError(err)
}

func (suite *KubeCtlPodTestSuite) TestDoesPodCompleted() {
pods := []corev1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Name: namesgenerator.GetRandomName(0),
},
Status: corev1.PodStatus{
Phase: corev1.PodPending,
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: namesgenerator.GetRandomName(0),
},
Status: corev1.PodStatus{
Phase: corev1.PodSucceeded,
},
},
}

for _, pod := range pods {
_, err := suite.kubectl.CreatePod(&pod)
suite.NoError(err)
}

run := suite.kubectl.DoesPodCompleted(&pods[0])
suite.False(run)

run = suite.kubectl.DoesPodCompleted(&pods[1])
suite.True(run)
}

func (suite *KubeCtlPodTestSuite) TearDownSuite() {}

func TestKubePodTestSuite(t *testing.T) {
Expand Down
33 changes: 31 additions & 2 deletions src/volume/volume.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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 @@ -54,6 +57,32 @@ func CreateVolume(sp *serviceprovider.Container, volume *entity.Volume) error {
}

func DeleteVolume(sp *serviceprovider.Container, volume *entity.Volume) error {
name := volume.GetPVCName()
return sp.KubeCtl.DeletePVC(name)
//Check the pod
session := sp.Mongo.NewSession()
defer session.Close()

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vscode都會建議 error message 不要大寫開頭,我們要統一嗎?
https://github.com/golang/go/wiki/CodeReviewComments#error-strings

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, 好唷

}

usedPod := []string{}
for _, pod := range pods {
//Check the pod's status, report error if at least one pod is running.
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())
}
68 changes: 68 additions & 0 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 @@ -107,3 +110,68 @@ func (suite *VolumeTestSuite) TestCreateVolumeFail() {
err := CreateVolume(suite.sp, volume)
suite.Error(err)
}

func (suite *VolumeTestSuite) TestDeleteVolumeFail() {
volume := &entity.Volume{
ID: bson.NewObjectId(),
Name: namesgenerator.GetRandomName(0),
}

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

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,
},
},
},
}

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

//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)
}