forked from redhat-developer/odo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.go
428 lines (368 loc) · 15 KB
/
adapter.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package component
import (
"fmt"
"reflect"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/fatih/color"
"github.com/pkg/errors"
"k8s.io/klog"
"github.com/openshift/odo/pkg/component"
"github.com/openshift/odo/pkg/config"
"github.com/openshift/odo/pkg/devfile/adapters/common"
adaptersCommon "github.com/openshift/odo/pkg/devfile/adapters/common"
"github.com/openshift/odo/pkg/devfile/adapters/kubernetes/storage"
"github.com/openshift/odo/pkg/devfile/adapters/kubernetes/utils"
versionsCommon "github.com/openshift/odo/pkg/devfile/parser/data/common"
"github.com/openshift/odo/pkg/exec"
"github.com/openshift/odo/pkg/kclient"
"github.com/openshift/odo/pkg/log"
odoutil "github.com/openshift/odo/pkg/odo/util"
"github.com/openshift/odo/pkg/sync"
)
// New instantiantes a component adapter
func New(adapterContext common.AdapterContext, client kclient.Client) Adapter {
return Adapter{
Client: client,
AdapterContext: adapterContext,
}
}
// Adapter is a component adapter implementation for Kubernetes
type Adapter struct {
Client kclient.Client
common.AdapterContext
devfileInitCmd string
devfileBuildCmd string
devfileRunCmd string
}
// Push updates the component if a matching component exists or creates one if it doesn't exist
// Once the component has started, it will sync the source code to it.
func (a Adapter) Push(parameters common.PushParameters) (err error) {
componentExists := utils.ComponentExists(a.Client, a.ComponentName)
a.devfileInitCmd = parameters.DevfileInitCmd
a.devfileBuildCmd = parameters.DevfileBuildCmd
a.devfileRunCmd = parameters.DevfileRunCmd
podChanged := false
var podName string
// If the component already exists, retrieve the pod's name before it's potentially updated
if componentExists {
pod, err := a.waitAndGetComponentPod(true)
if err != nil {
return errors.Wrapf(err, "unable to get pod for component %s", a.ComponentName)
}
podName = pod.GetName()
}
// Validate the devfile build and run commands
log.Info("\nValidation")
s := log.Spinner("Validating the devfile")
pushDevfileCommands, err := common.ValidateAndGetPushDevfileCommands(a.Devfile.Data, a.devfileInitCmd, a.devfileBuildCmd, a.devfileRunCmd)
if err != nil {
s.End(false)
return errors.Wrap(err, "failed to validate devfile build and run commands")
}
s.End(true)
log.Infof("\nCreating Kubernetes resources for component %s", a.ComponentName)
err = a.createOrUpdateComponent(componentExists)
if err != nil {
return errors.Wrap(err, "unable to create or update component")
}
_, err = a.Client.WaitForDeploymentRollout(a.ComponentName)
if err != nil {
return errors.Wrap(err, "error while waiting for deployment rollout")
}
// Wait for Pod to be in running state otherwise we can't sync data or exec commands to it.
pod, err := a.waitAndGetComponentPod(true)
if err != nil {
return errors.Wrapf(err, "unable to get pod for component %s", a.ComponentName)
}
err = component.ApplyConfig(nil, &a.Client, config.LocalConfigInfo{}, parameters.EnvSpecificInfo, color.Output, componentExists)
if err != nil {
odoutil.LogErrorAndExit(err, "Failed to update config to component deployed.")
}
// Compare the name of the pod with the one before the rollout. If they differ, it means there's a new pod and a force push is required
if componentExists && podName != pod.GetName() {
podChanged = true
}
// Find at least one pod with the source volume mounted, error out if none can be found
containerName, err := getFirstContainerWithSourceVolume(pod.Spec.Containers)
if err != nil {
return errors.Wrapf(err, "error while retrieving container from pod %s with a mounted project volume", podName)
}
log.Infof("\nSyncing to component %s", a.ComponentName)
// Get a sync adapter. Check if project files have changed and sync accordingly
syncAdapter := sync.New(a.AdapterContext, &a.Client)
compInfo := common.ComponentInfo{
ContainerName: containerName,
PodName: pod.GetName(),
}
syncParams := adaptersCommon.SyncParameters{
PushParams: parameters,
CompInfo: compInfo,
ComponentExists: componentExists,
PodChanged: podChanged,
}
execRequired, err := syncAdapter.SyncFiles(syncParams)
if err != nil {
return errors.Wrapf(err, "Failed to sync to component with name %s", a.ComponentName)
}
if execRequired {
log.Infof("\nExecuting devfile commands for component %s", a.ComponentName)
err = a.execDevfile(pushDevfileCommands, componentExists, parameters.Show, pod.GetName(), pod.Spec.Containers)
if err != nil {
return err
}
}
return nil
}
// DoesComponentExist returns true if a component with the specified name exists, false otherwise
func (a Adapter) DoesComponentExist(cmpName string) bool {
return utils.ComponentExists(a.Client, cmpName)
}
func (a Adapter) createOrUpdateComponent(componentExists bool) (err error) {
componentName := a.ComponentName
labels := map[string]string{
"component": componentName,
}
containers, err := utils.GetContainers(a.Devfile)
if err != nil {
return err
}
if len(containers) == 0 {
return fmt.Errorf("No valid components found in the devfile")
}
containers, err = utils.UpdateContainersWithSupervisord(a.Devfile, containers, a.devfileRunCmd)
if err != nil {
return err
}
objectMeta := kclient.CreateObjectMeta(componentName, a.Client.Namespace, labels, nil)
podTemplateSpec := kclient.GeneratePodTemplateSpec(objectMeta, containers)
kclient.AddBootstrapSupervisordInitContainer(podTemplateSpec)
componentAliasToVolumes := adaptersCommon.GetVolumes(a.Devfile)
var uniqueStorages []common.Storage
volumeNameToPVCName := make(map[string]string)
processedVolumes := make(map[string]bool)
// Get a list of all the unique volume names and generate their PVC names
for _, volumes := range componentAliasToVolumes {
for _, vol := range volumes {
if _, ok := processedVolumes[*vol.Name]; !ok {
processedVolumes[*vol.Name] = true
// Generate the PVC Names
klog.V(3).Infof("Generating PVC name for %v", *vol.Name)
generatedPVCName, err := storage.GeneratePVCNameFromDevfileVol(*vol.Name, componentName)
if err != nil {
return err
}
// Check if we have an existing PVC with the labels, overwrite the generated name with the existing name if present
existingPVCName, err := storage.GetExistingPVC(&a.Client, *vol.Name, componentName)
if err != nil {
return err
}
if len(existingPVCName) > 0 {
klog.V(3).Infof("Found an existing PVC for %v, PVC %v will be re-used", *vol.Name, existingPVCName)
generatedPVCName = existingPVCName
}
pvc := common.Storage{
Name: generatedPVCName,
Volume: vol,
}
uniqueStorages = append(uniqueStorages, pvc)
volumeNameToPVCName[*vol.Name] = generatedPVCName
}
}
}
// Add PVC and Volume Mounts to the podTemplateSpec
err = kclient.AddPVCAndVolumeMount(podTemplateSpec, volumeNameToPVCName, componentAliasToVolumes)
if err != nil {
return err
}
deploymentSpec := kclient.GenerateDeploymentSpec(*podTemplateSpec)
var containerPorts []corev1.ContainerPort
for _, c := range deploymentSpec.Template.Spec.Containers {
if len(containerPorts) == 0 {
containerPorts = c.Ports
} else {
containerPorts = append(containerPorts, c.Ports...)
}
}
serviceSpec := kclient.GenerateServiceSpec(objectMeta.Name, containerPorts)
klog.V(3).Infof("Creating deployment %v", deploymentSpec.Template.GetName())
klog.V(3).Infof("The component name is %v", componentName)
if utils.ComponentExists(a.Client, componentName) {
// If the component already exists, get the resource version of the deploy before updating
klog.V(3).Info("The component already exists, attempting to update it")
deployment, err := a.Client.UpdateDeployment(*deploymentSpec)
if err != nil {
return err
}
klog.V(3).Infof("Successfully updated component %v", componentName)
oldSvc, err := a.Client.KubeClient.CoreV1().Services(a.Client.Namespace).Get(componentName, metav1.GetOptions{})
objectMetaTemp := objectMeta
ownerReference := kclient.GenerateOwnerReference(deployment)
objectMetaTemp.OwnerReferences = append(objectMeta.OwnerReferences, ownerReference)
if err != nil {
// no old service was found, create a new one
if len(serviceSpec.Ports) > 0 {
_, err = a.Client.CreateService(objectMetaTemp, *serviceSpec)
if err != nil {
return err
}
klog.V(3).Infof("Successfully created Service for component %s", componentName)
}
} else {
if len(serviceSpec.Ports) > 0 {
serviceSpec.ClusterIP = oldSvc.Spec.ClusterIP
objectMetaTemp.ResourceVersion = oldSvc.GetResourceVersion()
_, err = a.Client.UpdateService(objectMetaTemp, *serviceSpec)
if err != nil {
return err
}
klog.V(3).Infof("Successfully update Service for component %s", componentName)
} else {
err = a.Client.KubeClient.CoreV1().Services(a.Client.Namespace).Delete(componentName, &metav1.DeleteOptions{})
if err != nil {
return err
}
}
}
} else {
deployment, err := a.Client.CreateDeployment(*deploymentSpec)
if err != nil {
return err
}
klog.V(3).Infof("Successfully created component %v", componentName)
ownerReference := kclient.GenerateOwnerReference(deployment)
objectMetaTemp := objectMeta
objectMetaTemp.OwnerReferences = append(objectMeta.OwnerReferences, ownerReference)
if len(serviceSpec.Ports) > 0 {
_, err = a.Client.CreateService(objectMetaTemp, *serviceSpec)
if err != nil {
return err
}
klog.V(3).Infof("Successfully created Service for component %s", componentName)
}
}
// Get the storage adapter and create the volumes if it does not exist
stoAdapter := storage.New(a.AdapterContext, a.Client)
err = stoAdapter.Create(uniqueStorages)
if err != nil {
return err
}
return nil
}
func (a Adapter) waitAndGetComponentPod(hideSpinner bool) (*corev1.Pod, error) {
podSelector := fmt.Sprintf("component=%s", a.ComponentName)
watchOptions := metav1.ListOptions{
LabelSelector: podSelector,
}
// Wait for Pod to be in running state otherwise we can't sync data to it.
pod, err := a.Client.WaitAndGetPod(watchOptions, corev1.PodRunning, "Waiting for component to start", hideSpinner)
if err != nil {
return nil, errors.Wrapf(err, "error while waiting for pod %s", podSelector)
}
return pod, nil
}
// Executes all the commands from the devfile in order: init and build - which are both optional, and a compulsary run.
// Init only runs once when the component is created.
func (a Adapter) execDevfile(pushDevfileCommands []versionsCommon.DevfileCommand, componentExists, show bool, podName string, containers []corev1.Container) (err error) {
// If nothing has been passed, then the devfile is missing the required run command
if len(pushDevfileCommands) == 0 {
return errors.New(fmt.Sprint("error executing devfile commands - there should be at least 1 command"))
}
commandOrder := []common.CommandNames{}
// Only add runinit to the expected commands if the component doesn't already exist
// This would be the case when first running the container
if !componentExists {
commandOrder = append(commandOrder, common.CommandNames{DefaultName: string(common.DefaultDevfileInitCommand), AdapterName: a.devfileInitCmd})
}
commandOrder = append(
commandOrder,
common.CommandNames{DefaultName: string(common.DefaultDevfileBuildCommand), AdapterName: a.devfileBuildCmd},
common.CommandNames{DefaultName: string(common.DefaultDevfileRunCommand), AdapterName: a.devfileRunCmd},
)
// Loop through each of the expected commands in the devfile
for i, currentCommand := range commandOrder {
// Loop through each of the command given from the devfile
for _, command := range pushDevfileCommands {
// If the current command from the devfile is the currently expected command from the devfile
if command.Name == currentCommand.DefaultName || command.Name == currentCommand.AdapterName {
// If the current command is not the last command in the slice
// it is not expected to be the run command
if i < len(commandOrder)-1 {
// Any exec command such as "Init" and "Build"
for _, action := range command.Actions {
compInfo := common.ComponentInfo{
ContainerName: *action.Component,
PodName: podName,
}
err = exec.ExecuteDevfileBuildAction(&a.Client, action, command.Name, compInfo, show)
if err != nil {
return err
}
}
// If the current command is the last command in the slice
// it is expected to be the run command
} else {
// Last command is "Run"
klog.V(4).Infof("Executing devfile command %v", command.Name)
for _, action := range command.Actions {
// Check if the devfile run component containers have supervisord as the entrypoint.
// Start the supervisord if the odo component does not exist
if !componentExists {
err = a.InitRunContainerSupervisord(*action.Component, podName, containers)
if err != nil {
return
}
}
compInfo := common.ComponentInfo{
ContainerName: *action.Component,
PodName: podName,
}
if componentExists && !common.IsRestartRequired(command) {
klog.V(4).Infof("restart:false, Not restarting DevRun Command")
err = exec.ExecuteDevfileRunActionWithoutRestart(&a.Client, action, command.Name, compInfo, show)
return
}
err = exec.ExecuteDevfileRunAction(&a.Client, action, command.Name, compInfo, show)
}
}
}
}
}
return
}
// InitRunContainerSupervisord initializes the supervisord in the container if
// the container has entrypoint that is not supervisord
func (a Adapter) InitRunContainerSupervisord(containerName, podName string, containers []corev1.Container) (err error) {
for _, container := range containers {
if container.Name == containerName && !reflect.DeepEqual(container.Command, []string{common.SupervisordBinaryPath}) {
command := []string{common.SupervisordBinaryPath, "-c", common.SupervisordConfFile, "-d"}
compInfo := common.ComponentInfo{
ContainerName: containerName,
PodName: podName,
}
err = exec.ExecuteCommand(&a.Client, compInfo, command, true)
}
}
return
}
// getFirstContainerWithSourceVolume returns the first container that set mountSources: true
// Because the source volume is shared across all components that need it, we only need to sync once,
// so we only need to find one container. If no container was found, that means there's no
// container to sync to, so return an error
func getFirstContainerWithSourceVolume(containers []corev1.Container) (string, error) {
for _, c := range containers {
for _, vol := range c.VolumeMounts {
if vol.Name == kclient.OdoSourceVolume {
return c.Name, nil
}
}
}
return "", fmt.Errorf("In order to sync files, odo requires at least one component in a devfile to set 'mountSources: true'")
}
// Delete deletes the component
func (a Adapter) Delete(labels map[string]string) error {
if !utils.ComponentExists(a.Client, a.ComponentName) {
return errors.Errorf("the component %s doesn't exist on the cluster", a.ComponentName)
}
return a.Client.DeleteDeployment(labels)
}