Skip to content

Commit

Permalink
Merge pull request #109 from linkernetworks/alex/validatePodEntity
Browse files Browse the repository at this point in the history
VX-186 VX-176 Validate pod entity with namespace
  • Loading branch information
John-Lin authored Jul 17, 2018
2 parents c46a248 + da64cde commit 48acd3c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
22 changes: 12 additions & 10 deletions src/entity/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,24 @@ const (
)

type Container struct {
Name string `bson:"name" json:"name"`
Image string `bson:"image" json:"image"`
Command []string `bson:"command" json:"command"`
Name string `bson:"name" json:"name" validate:"required"`
Image string `bson:"image" json:"image" validate:"required"`
Command []string `bson:"command" json:"command" validate:"-"`
}

type PodVolume struct {
Name string `bson:"name" json:"name"`
MountPath string `bson:"mountPath" json:"mountPath"`
Name string `bson:"name" json:"name" validate:"required"`
MountPath string `bson:"mountPath" json:"mountPath" validate:"required"`
}

type Pod struct {
ID bson.ObjectId `bson:"_id,omitempty" json:"id"`
Name string `bson:"name" json:"name"`
Containers []Container `bson:"containers" json:"containers"`
CreatedAt *time.Time `bson:"createdAt,omitempty" json:"createdAt,omitempty"`
Volumes []PodVolume `bson:"volumes,omitempty" json:"volumes"`
ID bson.ObjectId `bson:"_id,omitempty" json:"id" validate:"-"`
Name string `bson:"name" json:"name" validate:"required"`
Namespace string `bson:"namespace" json:"namespace" validate:"required"`
Labels map[string]string `bson:"labels,omitempty" json:"labels" validate:"-"`
Containers []Container `bson:"containers" json:"containers" validate:"required,dive,required"`
CreatedAt *time.Time `bson:"createdAt,omitempty" json:"createdAt,omitempty" validate:"-"`
Volumes []PodVolume `bson:"volumes,omitempty" json:"volumes" validate:"-"`
}

//GetCollection - get model mongo collection name.
Expand Down
14 changes: 8 additions & 6 deletions src/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func generateVolume(pod *entity.Pod, session *mongo.Session) ([]corev1.Volume, [
}

func CreatePod(sp *serviceprovider.Container, pod *entity.Pod) error {
namespace := "default"
session := sp.Mongo.NewSession()
defer session.Close()

Expand All @@ -101,18 +100,21 @@ func CreatePod(sp *serviceprovider.Container, pod *entity.Pod) error {

p := corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: pod.Name,
Name: pod.Name,
Labels: pod.Labels,
},
Spec: corev1.PodSpec{
Containers: containers,
Volumes: volumes,
},
}
_, err = sp.KubeCtl.CreatePod(&p, namespace)
if pod.Namespace == "" {
pod.Namespace = "default"
}
_, err = sp.KubeCtl.CreatePod(&p, pod.Namespace)
return err
}

func DeletePod(sp *serviceprovider.Container, podName string) error {
namespace := "default"
return sp.KubeCtl.DeletePod(podName, namespace)
func DeletePod(sp *serviceprovider.Container, pod *entity.Pod) error {
return sp.KubeCtl.DeletePod(pod.Name, pod.Namespace)
}
2 changes: 1 addition & 1 deletion src/pod/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (suite *PodTestSuite) TestCreatePod() {
err := CreatePod(suite.sp, pod)
suite.NoError(err)

err = DeletePod(suite.sp, podName)
err = DeletePod(suite.sp, pod)
suite.NoError(err)
}

Expand Down
7 changes: 6 additions & 1 deletion src/server/handler_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func createPodHandler(ctx *web.Context) {
return
}

if err := sp.Validator.Struct(p); err != nil {
response.BadRequest(req.Request, resp.ResponseWriter, err)
return
}

session := sp.Mongo.NewSession()
session.C(entity.PodCollectionName).EnsureIndex(mgo.Index{
Key: []string{"name"},
Expand Down Expand Up @@ -73,7 +78,7 @@ func deletePodHandler(ctx *web.Context) {
return
}

if err := pod.DeletePod(sp, p.Name); err != nil {
if err := pod.DeletePod(sp, &p); err != nil {
response.InternalServerError(req.Request, resp.ResponseWriter, err)
return
}
Expand Down
13 changes: 11 additions & 2 deletions src/server/handler_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestPodSuite(t *testing.T) {
}

func (suite *PodTestSuite) TestCreatePod() {
namespace := "default"
containers := []entity.Container{
{
Name: namesgenerator.GetRandomName(0),
Expand All @@ -62,6 +63,7 @@ func (suite *PodTestSuite) TestCreatePod() {
tName := namesgenerator.GetRandomName(0)
pod := entity.Pod{
Name: tName,
Namespace: namespace,
Containers: containers,
}

Expand Down Expand Up @@ -99,11 +101,12 @@ func (suite *PodTestSuite) TestCreatePod() {
suite.wc.Dispatch(httpWriter, httpRequest)
assertResponseCode(suite.T(), http.StatusInternalServerError, httpWriter)

err = p.DeletePod(suite.sp, tName)
err = p.DeletePod(suite.sp, &retPod)
suite.NoError(err)
}

func (suite *PodTestSuite) TestCreatePodFail() {
namespace := "default"
containers := []entity.Container{
{
Name: namesgenerator.GetRandomName(0),
Expand All @@ -114,6 +117,7 @@ func (suite *PodTestSuite) TestCreatePodFail() {
tName := namesgenerator.GetRandomName(0)
pod := entity.Pod{
Name: tName,
Namespace: namespace,
Containers: containers,
Volumes: []entity.PodVolume{
{Name: namesgenerator.GetRandomName(0)},
Expand All @@ -134,6 +138,7 @@ func (suite *PodTestSuite) TestCreatePodFail() {
}

func (suite *PodTestSuite) TestDeletePod() {
namespace := "default"
containers := []entity.Container{
{
Name: namesgenerator.GetRandomName(0),
Expand All @@ -145,6 +150,7 @@ func (suite *PodTestSuite) TestDeletePod() {
pod := entity.Pod{
ID: bson.NewObjectId(),
Name: tName,
Namespace: namespace,
Containers: containers,
}

Expand Down Expand Up @@ -183,6 +189,7 @@ func (suite *PodTestSuite) TestDeletePodWithInvalidID() {

//For Get/List, we only return mongo document
func (suite *PodTestSuite) TestGetPod() {
namespace := "default"
containers := []entity.Container{
{
Name: namesgenerator.GetRandomName(0),
Expand All @@ -194,6 +201,7 @@ func (suite *PodTestSuite) TestGetPod() {
pod := entity.Pod{
ID: bson.NewObjectId(),
Name: tName,
Namespace: namespace,
Containers: containers,
}

Expand All @@ -216,7 +224,6 @@ func (suite *PodTestSuite) TestGetPod() {
}

func (suite *PodTestSuite) TestGetPodWithInvalidID() {

//Get data with non-exits ID
httpRequest, err := http.NewRequest("GET", "http://localhost:7890/v1/pods/"+bson.NewObjectId().Hex(), nil)
suite.NoError(err)
Expand All @@ -227,6 +234,7 @@ func (suite *PodTestSuite) TestGetPodWithInvalidID() {
}

func (suite *PodTestSuite) TestListPod() {
namespace := "default"
pods := []entity.Pod{}
count := 3
for i := 0; i < count; i++ {
Expand All @@ -240,6 +248,7 @@ func (suite *PodTestSuite) TestListPod() {
pods = append(pods, entity.Pod{
ID: bson.NewObjectId(),
Name: namesgenerator.GetRandomName(0),
Namespace: namespace,
Containers: containers,
})
}
Expand Down

0 comments on commit 48acd3c

Please sign in to comment.