Skip to content

Commit

Permalink
Added hosts into environment.
Browse files Browse the repository at this point in the history
Signed-off-by: Klaus Ma <klaus1982.cn@gmail.com>
  • Loading branch information
k82cn committed Dec 15, 2019
1 parent 4db0bf2 commit 5425dfb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
2 changes: 2 additions & 0 deletions pkg/controllers/job/plugins/svc/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package svc
const (
// ConfigMapTaskHostFmt key in config map
ConfigMapTaskHostFmt = "%s.host"
EnvTaskHostFmt = "VC_%s_HOSTS"
EnvTaskNumFmt = "VC_%s_NUM"

// ConfigMapMountPath mount path
ConfigMapMountPath = "/etc/volcano"
Expand Down
42 changes: 36 additions & 6 deletions pkg/controllers/job/plugins/svc/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package svc
import (
"flag"
"fmt"
"strconv"
"strings"

"k8s.io/klog"
Expand All @@ -43,6 +44,9 @@ type servicePlugin struct {

// flag parse args
publishNotReadyAddresses bool

// the host info of each task
hostEnv map[string]string
}

// New creates service plugin
Expand Down Expand Up @@ -88,6 +92,19 @@ func (sp *servicePlugin) OnPodCreate(pod *v1.Pod, job *batch.Job) error {
pod.Spec.Subdomain = job.Name
}

var hostEnv []v1.EnvVar
for k, v := range sp.hostEnv {
hostEnv = append(hostEnv, v1.EnvVar{Name: k, Value: v})
}

for i := range pod.Spec.Containers {
pod.Spec.Containers[i].Env = append(pod.Spec.Containers[i].Env, hostEnv...)
}

for i := range pod.Spec.InitContainers {
pod.Spec.InitContainers[i].Env = append(pod.Spec.InitContainers[i].Env, hostEnv...)
}

sp.mountConfigmap(pod, job)

return nil
Expand All @@ -98,9 +115,13 @@ func (sp *servicePlugin) OnJobAdd(job *batch.Job) error {
return nil
}

data := generateHost(job)
hostFile, hostEnv := generateHosts(job)

if err := helpers.CreateConfigMapIfNotExist(job, sp.Clientset.KubeClients, data, sp.cmName(job)); err != nil {
// Add hosts as environment when creating pods.
sp.hostEnv = hostEnv

// Create ConfigMap of hosts for Pods to mount.
if err := helpers.CreateConfigMapIfNotExist(job, sp.Clientset.KubeClients, hostFile, sp.cmName(job)); err != nil {
return err
}

Expand Down Expand Up @@ -258,8 +279,9 @@ func (sp *servicePlugin) cmName(job *batch.Job) string {
return fmt.Sprintf("%s-%s", job.Name, sp.Name())
}

func generateHost(job *batch.Job) map[string]string {
data := make(map[string]string, len(job.Spec.Tasks))
func generateHosts(job *batch.Job) (hostFile map[string]string, hostEnv map[string]string) {
hostFile = make(map[string]string, len(job.Spec.Tasks))
hostEnv = make(map[string]string, len(job.Spec.Tasks))

for _, ts := range job.Spec.Tasks {
hosts := make([]string, 0, ts.Replicas)
Expand All @@ -280,8 +302,16 @@ func generateHost(job *batch.Job) map[string]string {
}

key := fmt.Sprintf(ConfigMapTaskHostFmt, ts.Name)
data[key] = strings.Join(hosts, "\n")
hostFile[key] = strings.Join(hosts, "\n")

// TODO(k82cn): The splitter and the prefix of env should be configurable.
// export hosts as environment
key = fmt.Sprintf(EnvTaskHostFmt, strings.ToUpper(ts.Name))
hostFile[key] = strings.Join(hosts, ",")
// export host number as environment.
key = fmt.Sprintf(EnvTaskNumFmt, strings.ToUpper(ts.Name))
hostFile[key] = strconv.Itoa(len(hosts))
}

return data
return
}

0 comments on commit 5425dfb

Please sign in to comment.