forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_rc.go
212 lines (195 loc) · 5.86 KB
/
worker_rc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package server
import (
"fmt"
"strings"
client "github.com/pachyderm/pachyderm/src/client"
"github.com/pachyderm/pachyderm/src/client/pps"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
)
// Parameters used when creating the kubernetes replication controller in charge
// of a job or pipeline's workers
type workerOptions struct {
rcName string // Name of the replication controller managing workers
userImage string // The user's pipeline/job image
labels map[string]string // k8s labels attached to the RC and workers
parallelism int32 // Number of replicas the RC maintains
workerEnv []api.EnvVar // Environment vars set in the user container
volumes []api.Volume // Volumes that we expose to the user container
volumeMounts []api.VolumeMount // Paths where we mount each volume in 'volumes'
// Secrets that we mount in the worker container (e.g. for reading/writing to
// s3)
imagePullSecrets []api.LocalObjectReference
}
// PipelineRcName generates the name of the k8s replication controller that
// manages a pipeline's workers
func PipelineRcName(name string, version uint64) string {
// k8s won't allow RC names that contain upper-case letters
// or underscores
// TODO: deal with name collision
name = strings.Replace(name, "_", "-", -1)
return fmt.Sprintf("pipeline-%s-v%d", strings.ToLower(name), version)
}
// JobRcName generates the name of the k8s replication controller that manages
// an orphan job's workers
func JobRcName(id string) string {
// k8s won't allow RC names that contain upper-case letters
// or underscores
// TODO: deal with name collision
id = strings.Replace(id, "_", "-", -1)
return fmt.Sprintf("job-%s", strings.ToLower(id))
}
func (a *apiServer) workerPodSpec(options *workerOptions) api.PodSpec {
pullPolicy := a.workerImagePullPolicy
if pullPolicy == "" {
pullPolicy = "IfNotPresent"
}
return api.PodSpec{
InitContainers: []api.Container{
{
Name: "init",
Image: a.workerImage,
Command: []string{"/pach/worker.sh"},
ImagePullPolicy: api.PullPolicy(pullPolicy),
Env: options.workerEnv,
VolumeMounts: options.volumeMounts,
},
},
Containers: []api.Container{
{
Name: "user",
Image: options.userImage,
Command: []string{"/pach-bin/guest.sh"},
SecurityContext: &api.SecurityContext{
Privileged: &trueVal, // god is this dumb
},
ImagePullPolicy: api.PullPolicy(pullPolicy),
Env: options.workerEnv,
VolumeMounts: options.volumeMounts,
},
},
RestartPolicy: "Always",
Volumes: options.volumes,
ImagePullSecrets: options.imagePullSecrets,
}
}
func (a *apiServer) getWorkerOptions(rcName string, parallelism int32, transform *pps.Transform) *workerOptions {
labels := labels(rcName)
userImage := transform.Image
if userImage == "" {
userImage = DefaultUserImage
}
var workerEnv []api.EnvVar
for name, value := range transform.Env {
workerEnv = append(
workerEnv,
api.EnvVar{
Name: name,
Value: value,
},
)
}
// We use Kubernetes' "Downward API" so the workers know their IP
// addresses, which they will then post on etcd so the job managers
// can discover the workers.
workerEnv = append(workerEnv, api.EnvVar{
Name: client.PPSWorkerIPEnv,
ValueFrom: &api.EnvVarSource{
FieldRef: &api.ObjectFieldSelector{
APIVersion: "v1",
FieldPath: "status.podIP",
},
},
})
workerEnv = append(workerEnv, api.EnvVar{
Name: client.PPSPodNameEnv,
ValueFrom: &api.EnvVarSource{
FieldRef: &api.ObjectFieldSelector{
APIVersion: "v1",
FieldPath: "metadata.name",
},
},
})
// Set the etcd prefix env
workerEnv = append(workerEnv, api.EnvVar{
Name: client.PPSEtcdPrefixEnv,
Value: a.etcdPrefix,
})
var volumes []api.Volume
var volumeMounts []api.VolumeMount
for _, secret := range transform.Secrets {
volumes = append(volumes, api.Volume{
Name: secret.Name,
VolumeSource: api.VolumeSource{
Secret: &api.SecretVolumeSource{
SecretName: secret.Name,
},
},
})
volumeMounts = append(volumeMounts, api.VolumeMount{
Name: secret.Name,
MountPath: secret.MountPath,
})
}
volumes = append(volumes, api.Volume{
Name: "pach-bin",
VolumeSource: api.VolumeSource{
EmptyDir: &api.EmptyDirVolumeSource{},
},
})
volumeMounts = append(volumeMounts, api.VolumeMount{
Name: "pach-bin",
MountPath: "/pach-bin",
})
volumes = append(volumes, api.Volume{
Name: client.PPSHostPathVolume,
VolumeSource: api.VolumeSource{
HostPath: &api.HostPathVolumeSource{
Path: client.PPSHostPath,
},
},
})
volumeMounts = append(volumeMounts, api.VolumeMount{
Name: client.PPSHostPathVolume,
MountPath: client.PPSHostPath,
})
var imagePullSecrets []api.LocalObjectReference
for _, secret := range transform.ImagePullSecrets {
imagePullSecrets = append(imagePullSecrets, api.LocalObjectReference{Name: secret})
}
return &workerOptions{
rcName: rcName,
labels: labels,
parallelism: int32(parallelism),
userImage: userImage,
workerEnv: workerEnv,
volumes: volumes,
volumeMounts: volumeMounts,
imagePullSecrets: imagePullSecrets,
}
}
func (a *apiServer) createWorkerRc(options *workerOptions) error {
rc := &api.ReplicationController{
TypeMeta: unversioned.TypeMeta{
Kind: "ReplicationController",
APIVersion: "v1",
},
ObjectMeta: api.ObjectMeta{
Name: options.rcName,
Labels: options.labels,
},
Spec: api.ReplicationControllerSpec{
Selector: options.labels,
Replicas: options.parallelism,
Template: &api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Name: options.rcName,
Labels: options.labels,
},
Spec: a.workerPodSpec(options),
},
},
}
_, err := a.kubeClient.ReplicationControllers(a.namespace).Create(rc)
return err
}