Skip to content

Commit

Permalink
Merge pull request #141 from linkernetworks/johnlin/podname-valitation
Browse files Browse the repository at this point in the history
VX-199 pod k8s name validatation
  • Loading branch information
Hung-Wei Chiu authored Jul 20, 2018
2 parents c0a0698 + 1e9a321 commit 2a60bc0
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/entity/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const (
)

type Container struct {
Name string `bson:"name" json:"name" validate:"required"`
Name string `bson:"name" json:"name" validate:"required,k8sname"`
Image string `bson:"image" json:"image" validate:"required"`
Command []string `bson:"command" json:"command" validate:"required,dive,required"`
}
Expand All @@ -32,7 +32,7 @@ type PodVolume struct {

type Pod struct {
ID bson.ObjectId `bson:"_id,omitempty" json:"id" validate:"-"`
Name string `bson:"name" json:"name" validate:"required"`
Name string `bson:"name" json:"name" validate:"required,k8sname"`
Namespace string `bson:"namespace" json:"namespace" validate:"required"`
Labels map[string]string `bson:"labels,omitempty" json:"labels" validate:"required,dive,keys,alphanum,endkeys,required,alphanum"`
Containers []Container `bson:"containers" json:"containers" validate:"required,dive,required"`
Expand Down
18 changes: 0 additions & 18 deletions src/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pod

import (
"fmt"
"regexp"
"strconv"

"github.com/linkernetworks/mongo"
Expand All @@ -18,27 +17,10 @@ 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
26 changes: 4 additions & 22 deletions src/pod/pod_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package pod

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

"github.com/linkernetworks/vortex/src/config"
"github.com/linkernetworks/vortex/src/entity"
"github.com/linkernetworks/vortex/src/serviceprovider"
"github.com/moby/moby/pkg/namesgenerator"
"github.com/stretchr/testify/suite"
"gopkg.in/mgo.v2/bson"
"math/rand"
"testing"
"time"
)

func init() {
Expand Down Expand Up @@ -70,25 +71,6 @@ 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

0 comments on commit 2a60bc0

Please sign in to comment.