forked from redhat-developer/odo
/
templates.go
290 lines (272 loc) · 8.43 KB
/
templates.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package occlient
import (
"fmt"
appsv1 "github.com/openshift/api/apps/v1"
buildv1 "github.com/openshift/api/build/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// CommonImageMeta has all the most common image data that is passed around within Odo
type CommonImageMeta struct {
Name string
Tag string
Namespace string
Ports []corev1.ContainerPort
}
func generateSupervisordDeploymentConfig(commonObjectMeta metav1.ObjectMeta, builderImage string, commonImageMeta CommonImageMeta, envVar []corev1.EnvVar) appsv1.DeploymentConfig {
// Generates and deploys a DeploymentConfig with an InitContainer to copy over the SupervisorD binary.
return appsv1.DeploymentConfig{
ObjectMeta: commonObjectMeta,
Spec: appsv1.DeploymentConfigSpec{
Replicas: 1,
Selector: map[string]string{
"deploymentconfig": commonObjectMeta.Name,
},
Template: &corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"deploymentconfig": commonObjectMeta.Name,
},
// https://github.com/redhat-developer/odo/pull/622#issuecomment-413410736
Annotations: map[string]string{
"alpha.image.policy.openshift.io/resolve-names": "*",
},
},
Spec: corev1.PodSpec{
// The application container
Containers: []corev1.Container{
{
Image: builderImage,
Name: commonObjectMeta.Name,
Ports: commonImageMeta.Ports,
// Run the actual supervisord binary that has been mounted into the container
Command: []string{
"/var/lib/supervisord/bin/supervisord",
},
// Using the appropriate configuration file that contains the "run" script for the component.
// either from: /usr/libexec/s2i/assemble or /opt/app-root/src/.s2i/bin/assemble
Args: []string{
"-c",
"/var/lib/supervisord/conf/supervisor.conf",
},
VolumeMounts: []corev1.VolumeMount{
{
Name: supervisordVolumeName,
MountPath: "/var/lib/supervisord",
},
},
Env: envVar,
},
},
// Create a volume that will be shared betwen InitContainer and the applicationContainer
// in order to pass over the SupervisorD binary
Volumes: []corev1.Volume{
{
Name: supervisordVolumeName,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
},
},
},
// We provide triggers to create an ImageStream so that the application container will use the
// correct and approriate image that's located internally within the OpenShift commonObjectMeta.Namespace
Triggers: []appsv1.DeploymentTriggerPolicy{
{
Type: "ConfigChange",
},
{
Type: "ImageChange",
ImageChangeParams: &appsv1.DeploymentTriggerImageChangeParams{
Automatic: true,
ContainerNames: []string{
commonObjectMeta.Name,
"copy-files-to-volume",
},
From: corev1.ObjectReference{
Kind: "ImageStreamTag",
Name: fmt.Sprintf("%s:%s", commonImageMeta.Name, commonImageMeta.Tag),
Namespace: commonImageMeta.Namespace,
},
},
},
},
},
}
}
func generateGitDeploymentConfig(commonObjectMeta metav1.ObjectMeta, image string, containerPorts []corev1.ContainerPort, envVars []corev1.EnvVar) appsv1.DeploymentConfig {
return appsv1.DeploymentConfig{
ObjectMeta: commonObjectMeta,
Spec: appsv1.DeploymentConfigSpec{
Replicas: 1,
Selector: map[string]string{
"deploymentconfig": commonObjectMeta.Name,
},
Template: &corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"deploymentconfig": commonObjectMeta.Name,
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Image: image,
Name: commonObjectMeta.Name,
Ports: containerPorts,
Env: envVars,
},
},
},
},
Triggers: []appsv1.DeploymentTriggerPolicy{
{
Type: "ConfigChange",
},
{
Type: "ImageChange",
ImageChangeParams: &appsv1.DeploymentTriggerImageChangeParams{
Automatic: true,
ContainerNames: []string{
commonObjectMeta.Name,
},
From: corev1.ObjectReference{
Kind: "ImageStreamTag",
Name: image,
},
},
},
},
},
}
}
// generateBuildConfig creates a BuildConfig for Git URL's being passed into Odo
func generateBuildConfig(commonObjectMeta metav1.ObjectMeta, gitURL string, imageName string, imageNamespace string) buildv1.BuildConfig {
buildSource := buildv1.BuildSource{
Git: &buildv1.GitBuildSource{
URI: gitURL,
},
Type: buildv1.BuildSourceGit,
}
return buildv1.BuildConfig{
ObjectMeta: commonObjectMeta,
Spec: buildv1.BuildConfigSpec{
CommonSpec: buildv1.CommonSpec{
Output: buildv1.BuildOutput{
To: &corev1.ObjectReference{
Kind: "ImageStreamTag",
Name: commonObjectMeta.Name + ":latest",
},
},
Source: buildSource,
Strategy: buildv1.BuildStrategy{
SourceStrategy: &buildv1.SourceBuildStrategy{
From: corev1.ObjectReference{
Kind: "ImageStreamTag",
Name: imageName,
Namespace: imageNamespace,
},
},
},
},
},
}
}
//
// Below is related to SUPERVISORD
//
// AddBootstrapInitContainer adds the bootstrap init container to the deployment config
// dc is the deployment config to be updated
// dcName is the name of the deployment config
func addBootstrapVolumeCopyInitContainer(dc *appsv1.DeploymentConfig, dcName string) {
dc.Spec.Template.Spec.InitContainers = append(dc.Spec.Template.Spec.InitContainers,
corev1.Container{
Name: "copy-files-to-volume",
// Using custom image from bootstrapperImage variable for the initial initContainer
Image: dc.Spec.Template.Spec.Containers[0].Image,
Command: []string{
"sh",
"-c"},
// Script required to copy over file information from /opt/app-root
// Source https://github.com/jupyter-on-openshift/jupyter-notebooks/blob/master/minimal-notebook/setup-volume.sh
Args: []string{`
SRC=/opt/app-root
DEST=/mnt/app-root
if [ -f $DEST/.delete-volume ]; then
rm -rf $DEST
fi
if [ -d $DEST ]; then
if [ -f $DEST/.sync-volume ]; then
if ! [[ "$JUPYTER_SYNC_VOLUME" =~ ^(false|no|n|0)$ ]]; then
JUPYTER_SYNC_VOLUME=yes
fi
fi
if [[ "$JUPYTER_SYNC_VOLUME" =~ ^(true|yes|y|1)$ ]]; then
rsync -ar --ignore-existing $SRC/. $DEST
fi
exit
fi
if [ -d $DEST.setup-volume ]; then
rm -rf $DEST.setup-volume
fi
mkdir -p $DEST.setup-volume
tar -C $SRC -cf - . | tar -C $DEST.setup-volume -xvf -
mv $DEST.setup-volume $DEST
`},
VolumeMounts: []corev1.VolumeMount{
{
Name: getAppRootVolumeName(dcName),
MountPath: "/mnt",
},
},
})
}
// addBootstrapSupervisordInitContainer creates an init container that will copy over
// supervisord to the application image during the start-up procress.
func addBootstrapSupervisordInitContainer(dc *appsv1.DeploymentConfig, dcName string) {
dc.Spec.Template.Spec.InitContainers = append(dc.Spec.Template.Spec.InitContainers,
corev1.Container{
Name: "copy-supervisord",
Image: bootstrapperImage,
VolumeMounts: []corev1.VolumeMount{
{
Name: supervisordVolumeName,
MountPath: "/var/lib/supervisord",
},
},
Command: []string{
"/usr/bin/cp",
},
Args: []string{
"-r",
"/opt/supervisord",
"/var/lib/",
},
})
}
// addBootstrapVolume adds the bootstrap volume to the deployment config
// dc is the deployment config to be updated
// dcName is the name of the deployment config
func addBootstrapVolume(dc *appsv1.DeploymentConfig, dcName string) {
dc.Spec.Template.Spec.Volumes = append(dc.Spec.Template.Spec.Volumes, corev1.Volume{
Name: getAppRootVolumeName(dcName),
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: getAppRootVolumeName(dcName),
},
},
})
}
// addBootstrapVolumeMount mounts the bootstrap volume to the deployment config
// dc is the deployment config to be updated
// dcName is the name of the deployment config
func addBootstrapVolumeMount(dc *appsv1.DeploymentConfig, dcName string) {
for i := range dc.Spec.Template.Spec.Containers {
dc.Spec.Template.Spec.Containers[i].VolumeMounts = append(dc.Spec.Template.Spec.Containers[i].VolumeMounts, corev1.VolumeMount{
Name: getAppRootVolumeName(dcName),
MountPath: "/opt/app-root",
SubPath: "app-root",
})
}
}