Skip to content

Commit

Permalink
Add the nodeaffinity
Browse files Browse the repository at this point in the history
  • Loading branch information
hwchiu committed Jul 17, 2018
1 parent b46f633 commit 33f6fb1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
27 changes: 25 additions & 2 deletions src/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ func CreatePod(sp *serviceprovider.Container, pod *entity.Pod) error {
return err
}

nodesNames, initContainers, err := generateNetwork(pod, session)
if err != nil {
return err
}

volumes = append(volumes, corev1.Volume{
Name: "grpc-sock",
VolumeSource: corev1.VolumeSource{
Expand All @@ -202,6 +207,7 @@ func CreatePod(sp *serviceprovider.Container, pod *entity.Pod) error {
},
},
})

var containers []corev1.Container
for _, container := range pod.Containers {
containers = append(containers, corev1.Container{
Expand All @@ -218,8 +224,25 @@ func CreatePod(sp *serviceprovider.Container, pod *entity.Pod) error {
Labels: pod.Labels,
},
Spec: corev1.PodSpec{
Containers: containers,
Volumes: volumes,
InitContainers: initContainers,
Containers: containers,
Volumes: volumes,
Affinity: &corev1.Affinity{
NodeAffinity: &corev1.NodeAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
NodeSelectorTerms: []corev1.NodeSelectorTerm{
{
MatchExpressions: []corev1.NodeSelectorRequirement{
{
Key: "kubernetes.io/hostname",
Values: nodesNames,
},
},
},
},
},
},
},
},
}
if pod.Namespace == "" {
Expand Down
27 changes: 26 additions & 1 deletion src/pod/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (suite *PodTestSuite) TestCreatePod() {
suite.NoError(err)
}

func (suite *PodTestSuite) TestCreatePodFail() {
func (suite *PodTestSuite) TestCreatePodFailWithoutVolume() {
containers := []entity.Container{
{
Name: namesgenerator.GetRandomName(0),
Expand All @@ -205,6 +205,31 @@ func (suite *PodTestSuite) TestCreatePodFail() {
suite.Error(err)
}

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

podName := namesgenerator.GetRandomName(0)
pod := &entity.Pod{
ID: bson.NewObjectId(),
Name: podName,
Containers: containers,
Networks: []entity.PodNetwork{
{
Name: namesgenerator.GetRandomName(0),
},
},
}

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

func (suite *PodTestSuite) TestGenerateNodeLabels() {
networks := []entity.Network{
{
Expand Down
1 change: 1 addition & 0 deletions src/server/handler_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (suite *PodTestSuite) TestCreatePod() {
Name: tName,
Namespace: namespace,
Containers: containers,
Volumes: []entity.PodVolume{},
}

bodyBytes, err := json.MarshalIndent(pod, "", " ")
Expand Down

0 comments on commit 33f6fb1

Please sign in to comment.