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

[Task] Support the environments for Pod. #248

Merged
merged 3 commits into from
Aug 16, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ For each Pod, we have fileds need to handle.
- Always,OnFailure,Never
9. networkType: the string options for network type, support "host", "custom" and "cluster".
10. nodeAffinity: the string array to indicate whchi nodes I want my Pod can run in.

2. envVars: the environment variables for containers and it's map (string to stirng) form.

Example:

Expand All @@ -641,6 +641,8 @@ Request Data:
```json
{
"name": "awesome",
"labels": {},
"envVars":{},
"containers": [{
"name": "busybox",
"image": "busybox",
Expand Down
3 changes: 2 additions & 1 deletion src/entity/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ type Pod struct {
ID bson.ObjectId `bson:"_id,omitempty" json:"id" validate:"-"`
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"`
Labels map[string]string `bson:"labels,omitempty" json:"labels" validate:"required,dive,keys,printascii,endkeys,required,printascii"`
EnvVars map[string]string `bson:"envVars,omitempty" json:"envVars" validate:"required,dive,keys,printascii,endkeys,required,printascii"`
Containers []Container `bson:"containers" json:"containers" validate:"required,dive,required"`
Volumes []PodVolume `bson:"volumes,omitempty" json:"volumes" validate:"required,dive,required"`
Networks []PodNetwork `bson:"networks,omitempty" json:"networks" validate:"required,dive,required"`
Expand Down
14 changes: 14 additions & 0 deletions src/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,18 @@ func generateAffinity(nodeNames []string) *corev1.Affinity {
}
}

func generateEnvVars(pod *entity.Pod) []corev1.EnvVar {
envVars := []corev1.EnvVar{}

for k, v := range pod.EnvVars {
envVars = append(envVars, corev1.EnvVar{
Name: k,
Value: v,
})
}
return envVars
}

// CreatePod will Create Pod
func CreatePod(sp *serviceprovider.Container, pod *entity.Pod) error {
session := sp.Mongo.NewSession()
Expand Down Expand Up @@ -263,13 +275,15 @@ func CreatePod(sp *serviceprovider.Container, pod *entity.Pod) error {

var containers []corev1.Container
securityContext := generateContainerSecurity(pod)
envVars := generateEnvVars(pod)
for _, container := range pod.Containers {
containers = append(containers, corev1.Container{
Name: container.Name,
Image: container.Image,
Command: container.Command,
VolumeMounts: volumeMounts,
SecurityContext: securityContext,
Env: envVars,
})
}

Expand Down
3 changes: 3 additions & 0 deletions src/pod/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ func (suite *PodTestSuite) TestCreatePod() {
Name: podName,
Containers: containers,
NetworkType: entity.PodHostNetwork,
EnvVars: map[string]string{
"MY_IP": "1.2.3.4",
},
}

err := CreatePod(suite.sp, pod)
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 @@ -64,6 +64,7 @@ func (suite *PodTestSuite) TestCreatePod() {
Name: tName,
Namespace: namespace,
Labels: map[string]string{},
EnvVars: map[string]string{},
Containers: containers,
Volumes: []entity.PodVolume{},
Networks: []entity.PodNetwork{},
Expand Down
1 change: 1 addition & 0 deletions tests/01-multiple-interface/pod.info
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@PODNAME@",
"labels":{},
"envVars":{},
"namespace":"default",
"containers":[
{
Expand Down
3 changes: 3 additions & 0 deletions tests/02-hoet-network/pod.info
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "@PODNAME@",
"labels":{},
"envVars":{
"myip":"1.2.3.4"
},
"namespace":"default",
"containers":[
{
Expand Down
5 changes: 5 additions & 0 deletions tests/02-hoet-network/script.bats
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ load init
[ $status = 0 ]
[ "$output" = "true" ]
}

@test "Check Pod Env" {
kubectl get pods ${podName} -o jsonpath='{.spec.containers[0].env}' | grep "myip"
[ $? = 0 ]
}
@test "Delete Pod" {
run bash -c 'http http://127.0.0.1:7890/v1/pods/ 2>/dev/null | jq -r ".[0].id"'
run http DELETE http://127.0.0.1:7890/v1/pods/${output} 2>/dev/null
Expand Down
1 change: 1 addition & 0 deletions tests/03-storage-pod/pod.info
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@PODNAME@",
"labels":{},
"envVars":{},
"namespace":"default",
"containers":[
{
Expand Down