Skip to content

Commit

Permalink
update check pod parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyunchen committed Jul 11, 2018
1 parent 4ee5ab0 commit 8d36b66
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
29 changes: 18 additions & 11 deletions src/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,27 @@ import (

const VolumeNamePrefix = "volume-"

func checkNameValidation(name string) bool {
re := regexp.MustCompile(`[a-z0-9]([-a-z0-9]*[a-z0-9])`)
return re.MatchString(name)
}

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

//Check pod name validation
if !checkNameValidation(pod.Name) {
return fmt.Errorf("Pod Name: %s is invalid value", pod.Name)
}

//Check container name validation
for _, container := range pod.Containers {
if !checkNameValidation(container.Name) {
return fmt.Errorf("Container Name: %s is invalid value", container.Name)
}
}

//Check the volume
for _, v := range pod.Volumes {
count, err := session.Count(entity.VolumeCollectionName, bson.M{"name": v.Name})
Expand Down Expand Up @@ -61,11 +79,6 @@ func generateVolume(pod *entity.Pod, session *mongo.Session) ([]corev1.Volume, [
return volumes, volumeMounts, nil
}

func checkName(name string) bool {
re := regexp.MustCompile(`[a-z0-9]([-a-z0-9]*[a-z0-9])`)
return re.MatchString(name)
}

func CreatePod(sp *serviceprovider.Container, pod *entity.Pod) error {

session := sp.Mongo.NewSession()
Expand All @@ -78,9 +91,6 @@ func CreatePod(sp *serviceprovider.Container, pod *entity.Pod) error {

var containers []corev1.Container
for _, container := range pod.Containers {
if !checkName(container.Name) {
return fmt.Errorf("Container Name: %s is invalid value", container.Name)
}
containers = append(containers, corev1.Container{
Name: container.Name,
Image: container.Image,
Expand All @@ -89,9 +99,6 @@ func CreatePod(sp *serviceprovider.Container, pod *entity.Pod) error {
})
}

if !checkName(pod.Name) {
return fmt.Errorf("Pod Name: %s is invalid value", pod.Name)
}
p := corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: pod.Name,
Expand Down
54 changes: 24 additions & 30 deletions src/pod/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func TestPodSuite(t *testing.T) {
func (suite *PodTestSuite) TestCheckPodParameter() {
volumeName := namesgenerator.GetRandomName(0)
pod := &entity.Pod{
ID: bson.NewObjectId(),
ID: bson.NewObjectId(),
Name: namesgenerator.GetRandomName(0),
Volumes: []entity.PodVolume{
{Name: volumeName},
},
Expand All @@ -62,6 +63,25 @@ func (suite *PodTestSuite) TestCheckPodParameterFail() {
caseName string
pod *entity.Pod
}{
{
"InvalidPodName", &entity.Pod{
ID: bson.NewObjectId(),
Name: "~!@#$%^&*()",
},
},
{
"InvalidContainerName", &entity.Pod{
ID: bson.NewObjectId(),
Name: namesgenerator.GetRandomName(0),
Containers: []entity.Container{
{
Name: "~!@#$%^&*()",
Image: "busybox",
Command: []string{"sleep", "3600"},
},
},
},
},
{
"InvalidVolume", &entity.Pod{
ID: bson.NewObjectId(),
Expand Down Expand Up @@ -147,42 +167,16 @@ func (suite *PodTestSuite) TestCreatePod() {
}

func (suite *PodTestSuite) TestCreatePodFail() {
podName := "~!@#$%^&*()"
pod := &entity.Pod{
ID: bson.NewObjectId(),
Name: podName,
}

err := CreatePod(suite.sp, pod)
suite.Error(err)

containers := []entity.Container{
{
Name: "~!@#$%^&*()",
Image: "busybox",
Command: []string{"sleep", "3600"},
},
}
podName = namesgenerator.GetRandomName(0)
pod = &entity.Pod{
ID: bson.NewObjectId(),
Name: podName,
Containers: containers,
}

err = CreatePod(suite.sp, pod)
suite.Error(err)

containers = []entity.Container{
{
Name: namesgenerator.GetRandomName(0),
Image: "busybox",
Command: []string{"sleep", "3600"},
},
}

podName = namesgenerator.GetRandomName(0)
pod = &entity.Pod{
podName := namesgenerator.GetRandomName(0)
pod := &entity.Pod{
ID: bson.NewObjectId(),
Name: podName,
Containers: containers,
Expand All @@ -191,6 +185,6 @@ func (suite *PodTestSuite) TestCreatePodFail() {
},
}

err = CreatePod(suite.sp, pod)
err := CreatePod(suite.sp, pod)
suite.Error(err)
}
2 changes: 1 addition & 1 deletion src/server/handler_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func createPodHandler(ctx *web.Context) {
}

if err := pod.CreatePod(sp, &p); err != nil {
response.BadRequest(req.Request, resp.ResponseWriter, err)
response.InternalServerError(req.Request, resp.ResponseWriter, err)
return
}
if err := session.Insert(entity.PodCollectionName, &p); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion src/server/handler_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (suite *PodTestSuite) TestCreatePod() {
httpRequest.Header.Add("Content-Type", "application/json")
httpWriter = httptest.NewRecorder()
suite.wc.Dispatch(httpWriter, httpRequest)
assertResponseCode(suite.T(), http.StatusBadRequest, httpWriter)
assertResponseCode(suite.T(), http.StatusInternalServerError, httpWriter)

err = p.DeletePod(suite.sp, tName)
suite.NoError(err)
Expand Down

0 comments on commit 8d36b66

Please sign in to comment.