forked from redhat-developer/odo
/
component.go
502 lines (428 loc) · 17.8 KB
/
component.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package component
import (
"bufio"
"fmt"
"io"
"net/url"
"strings"
"github.com/fatih/color"
"github.com/pkg/errors"
applabels "github.com/redhat-developer/odo/pkg/application/labels"
componentlabels "github.com/redhat-developer/odo/pkg/component/labels"
"github.com/redhat-developer/odo/pkg/config"
"github.com/redhat-developer/odo/pkg/occlient"
"github.com/redhat-developer/odo/pkg/storage"
urlpkg "github.com/redhat-developer/odo/pkg/url"
"github.com/redhat-developer/odo/pkg/util"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
)
// componentSourceURLAnnotation is an source url from which component was build
// it can be also file://
const componentSourceURLAnnotation = "app.kubernetes.io/url"
const componentSourceTypeAnnotation = "app.kubernetes.io/component-source-type"
// ComponentInfo holds all important information about one component
type ComponentInfo struct {
Name string
Type string
}
// validateSourceType check if given sourceType is supported
func validateSourceType(sourceType string) bool {
validSourceTypes := []string{
"git",
"local",
"binary",
}
for _, valid := range validSourceTypes {
if valid == sourceType {
return true
}
}
return false
}
func CreateFromGit(client *occlient.Client, name string, ctype string, url string, applicationName string) error {
labels := componentlabels.GetLabels(name, applicationName, true)
// save component type as label
labels[componentlabels.ComponentTypeLabel] = ctype
// save source path as annotation
annotations := map[string]string{componentSourceURLAnnotation: url}
annotations[componentSourceTypeAnnotation] = "git"
// Namespace the component
namespacedOpenShiftObject, err := util.NamespaceOpenShiftObject(name, applicationName)
if err != nil {
return errors.Wrapf(err, "unable to create namespaced name")
}
err = client.NewAppS2I(namespacedOpenShiftObject, ctype, url, labels, annotations)
if err != nil {
return errors.Wrapf(err, "unable to create git component %s", namespacedOpenShiftObject)
}
return nil
}
// CreateFromPath create new component with source or binary from the given local path
// sourceType indicates the source type of the component and can be either local or binary
func CreateFromPath(client *occlient.Client, name string, ctype string, path string, applicationName string, sourceType string) error {
labels := componentlabels.GetLabels(name, applicationName, true)
// save component type as label
labels[componentlabels.ComponentTypeLabel] = ctype
// save source path as annotation
sourceURL := url.URL{Scheme: "file", Path: path}
annotations := map[string]string{componentSourceURLAnnotation: sourceURL.String()}
annotations[componentSourceTypeAnnotation] = sourceType
// Namespace the component
namespacedOpenShiftObject, err := util.NamespaceOpenShiftObject(name, applicationName)
if err != nil {
return errors.Wrapf(err, "unable to create namespaced name")
}
err = client.BootstrapSupervisoredS2I(namespacedOpenShiftObject, ctype, labels, annotations)
if err != nil {
return err
}
return nil
}
// Delete whole component
func Delete(client *occlient.Client, name string, applicationName string, projectName string) error {
cfg, err := config.New()
if err != nil {
return errors.Wrapf(err, "unable to create new configuration to delete %s", name)
}
labels := componentlabels.GetLabels(name, applicationName, false)
err = client.Delete(labels)
if err != nil {
return errors.Wrapf(err, "error deleting component %s", name)
}
// Get a list of all active components
components, err := List(client, applicationName, projectName)
if err != nil {
return errors.Wrapf(err, "unable to retrieve list of components")
}
// We will *only* set a new component if either len(components) is zero, or the
// current component matches the one being deleted.
if current := cfg.GetActiveComponent(applicationName, projectName); current == name || len(components) == 0 {
// If there's more than one component, set it to the first one..
if len(components) > 0 {
err = cfg.SetActiveComponent(components[0].Name, applicationName, projectName)
if err != nil {
return errors.Wrapf(err, "unable to set current component to '%s'", name)
}
} else {
// Unset to blank
err = cfg.UnsetActiveComponent(applicationName, projectName)
if err != nil {
return errors.Wrapf(err, "error unsetting current component while deleting %s", name)
}
}
}
return nil
}
func SetCurrent(client *occlient.Client, name string, applicationName string, projectName string) error {
cfg, err := config.New()
if err != nil {
return errors.Wrapf(err, "unable to set current component %s", name)
}
err = cfg.SetActiveComponent(name, applicationName, projectName)
if err != nil {
return errors.Wrapf(err, "unable to set current component %s", name)
}
return nil
}
// GetCurrent component in active application
// returns "" if there is no active component
func GetCurrent(client *occlient.Client, applicationName string, projectName string) (string, error) {
cfg, err := config.New()
if err != nil {
return "", errors.Wrap(err, "unable to get config")
}
currentComponent := cfg.GetActiveComponent(applicationName, projectName)
return currentComponent, nil
}
// PushLocal push local code to the cluster and trigger build there.
// asFile indicates if it is a binary component or not
func PushLocal(client *occlient.Client, componentName string, applicationName string, path string, asFile bool, out io.Writer) error {
const targetPath = "/opt/app-root/src"
if !asFile {
// We need to make sure that there is a '/' at the end, otherwise rsync will sync files to wrong directory
path = fmt.Sprintf("%s/", path)
}
// Find DeploymentConfig for component
componentLabels := componentlabels.GetLabels(componentName, applicationName, false)
componentSelector := util.ConvertLabelsToSelector(componentLabels)
dc, err := client.GetOneDeploymentConfigFromSelector(componentSelector)
if err != nil {
return errors.Wrap(err, "unable to get deployment for component")
}
// Find Pod for component
podSelector := fmt.Sprintf("deploymentconfig=%s", dc.Name)
// Wait for Pod to be in running state otherwise we can't sync data to it.
pod, err := client.WaitAndGetPod(podSelector)
if err != nil {
return errors.Wrapf(err, "error while waiting for pod %s", podSelector)
}
var syncOutput string
if !asFile {
syncOutput, err = client.RsyncPath(path, pod.Name, targetPath)
} else {
syncOutput, err = client.CopyFile(path, pod.Name, targetPath)
}
if err != nil {
return errors.Wrap(err, "unable push files to pod")
}
fmt.Fprintf(out, syncOutput)
fmt.Fprintf(out, "Please wait, building component....\n")
// use pipes to write output from ExecCMDInContainer in yellow to 'out' io.Writer
pipeReader, pipeWriter := io.Pipe()
go func() {
yellowFprintln := color.New(color.FgYellow).FprintlnFunc()
scanner := bufio.NewScanner(pipeReader)
for scanner.Scan() {
line := scanner.Text()
yellowFprintln(out, line)
}
}()
err = client.ExecCMDInContainer(pod.Name,
[]string{"/opt/app-root/bin/assemble-and-restart.sh"},
pipeWriter, pipeWriter, nil, false)
if err != nil {
return errors.Wrap(err, "unable to execute assemble script")
}
return nil
}
// Build component from BuildConfig.
// If 'streamLogs' is true than it streams build logs on stdout, set 'wait' to true if you want to return error if build fails.
// If 'wait' is true than it waits for build to successfully complete.
// If 'wait' is false than this function won't return error even if build failed.
func Build(client *occlient.Client, componentName string, applicationName string, streamLogs bool, wait bool, stdout io.Writer) error {
// Namespace the component
namespacedOpenShiftObject, err := util.NamespaceOpenShiftObject(componentName, applicationName)
if err != nil {
return errors.Wrapf(err, "unable to create namespaced name")
}
buildName, err := client.StartBuild(namespacedOpenShiftObject)
if err != nil {
return errors.Wrapf(err, "unable to rebuild %s", componentName)
}
if streamLogs {
if err := client.FollowBuildLog(buildName, stdout); err != nil {
return errors.Wrapf(err, "unable to follow logs for %s", buildName)
}
}
if wait {
if err := client.WaitForBuildToFinish(buildName); err != nil {
return errors.Wrapf(err, "unable to wait for build %s", buildName)
}
}
return nil
}
// GetComponentType returns type of component in given application and project
func GetComponentType(client *occlient.Client, componentName string, applicationName string, projectName string) (string, error) {
// filter according to component and application name
selector := fmt.Sprintf("%s=%s,%s=%s", componentlabels.ComponentLabel, componentName, applabels.ApplicationLabel, applicationName)
ctypes, err := client.GetLabelValues(projectName, componentlabels.ComponentTypeLabel, selector)
if err != nil {
return "", errors.Wrap(err, "unable to get type of %s component")
}
if len(ctypes) < 1 {
// no type returned
return "", errors.Wrap(err, "unable to find type of %s component")
}
// check if all types are the same
// it should be as we are secting only exactly one component, and it doesn't make sense
// to have one component labeled with different component type labels
for _, ctype := range ctypes {
if ctypes[0] != ctype {
return "", errors.Wrap(err, "data mismatch: %s component has objects with different types")
}
}
return ctypes[0], nil
}
// List lists components in active application
func List(client *occlient.Client, applicationName string, projectName string) ([]ComponentInfo, error) {
applicationSelector := fmt.Sprintf("%s=%s", applabels.ApplicationLabel, applicationName)
componentNames, err := client.GetLabelValues(projectName, componentlabels.ComponentLabel, applicationSelector)
if err != nil {
return nil, errors.Wrapf(err, "unable to list components")
}
components := []ComponentInfo{}
for _, name := range componentNames {
ctype, err := GetComponentType(client, name, applicationName, projectName)
if err != nil {
return nil, errors.Wrapf(err, "unable to list components")
}
components = append(components, ComponentInfo{Name: name, Type: ctype})
}
return components, nil
}
// GetComponentSource what source type given component uses
// The first returned string is component source type ("git" or "local" or "binary")
// The second returned string is a source (url to git repository or local path or path to binary)
func GetComponentSource(client *occlient.Client, componentName string, applicationName string, projectName string) (string, string, error) {
// Namespace the application
namespacedOpenShiftObject, err := util.NamespaceOpenShiftObject(componentName, applicationName)
if err != nil {
return "", "", errors.Wrapf(err, "unable to create namespaced name")
}
bc, err := client.GetBuildConfig(namespacedOpenShiftObject, projectName)
if err != nil {
return "", "", errors.Wrapf(err, "unable to get source path for component %s", namespacedOpenShiftObject)
}
sourcePath := bc.ObjectMeta.Annotations[componentSourceURLAnnotation]
sourceType := bc.ObjectMeta.Annotations[componentSourceTypeAnnotation]
if !validateSourceType(sourceType) {
return "", "", fmt.Errorf("unsupported component source type %s", sourceType)
}
log.Debugf("Source for component %s is %s (%s)", componentName, sourcePath, sourceType)
return sourceType, sourcePath, nil
}
// Update updates the requested component
// Component name is the name component to be updated
// to indicates what type of source type the component source is changing to e.g from git to local or local to binary
// source indicates path of the source directory or binary or the git URL
func Update(client *occlient.Client, componentName string, applicationName string, to string, source string) error {
// Namespace the application
namespacedOpenShiftObject, err := util.NamespaceOpenShiftObject(componentName, applicationName)
if err != nil {
return errors.Wrapf(err, "unable to create namespaced name")
}
projectName := client.GetCurrentProjectName()
// save source path as annotation
var annotations map[string]string
if to == "git" {
annotations = map[string]string{componentSourceURLAnnotation: source}
annotations[componentSourceTypeAnnotation] = to
err = client.UpdateBuildConfig(namespacedOpenShiftObject, projectName, source, annotations)
} else if to == "local" {
sourceURL := url.URL{Scheme: "file", Path: source}
annotations = map[string]string{componentSourceURLAnnotation: sourceURL.String()}
annotations[componentSourceTypeAnnotation] = to
err = client.UpdateBuildConfig(namespacedOpenShiftObject, projectName, "", annotations)
} else if to == "binary" {
sourceURL := url.URL{Scheme: "file", Path: source}
annotations = map[string]string{componentSourceURLAnnotation: sourceURL.String()}
annotations[componentSourceTypeAnnotation] = to
err = client.UpdateBuildConfig(namespacedOpenShiftObject, projectName, "", annotations)
}
if err != nil {
return errors.Wrap(err, "unable to update the component")
}
return nil
}
// Checks whether a component with the given name exists in the current application or not
// componentName is the component name to perform check for
// The first returned parameter is a bool indicating if a component with the given name already exists or not
// The second returned parameter is the error that might occurs while execution
func Exists(client *occlient.Client, componentName, applicationName, projectName string) (bool, error) {
componentList, err := List(client, applicationName, projectName)
if err != nil {
return false, errors.Wrap(err, "unable to get the component list")
}
for _, component := range componentList {
if component.Name == componentName {
return true, nil
}
}
return false, nil
}
// LinkInfo contains the information about the link being created between
// components
type LinkInfo struct {
SourceComponent string
TargetComponent string
Envs []string
}
// Link injects connection information of the target component into the source
// component as environment variables
func Link(client *occlient.Client, sourceComponent, targetComponent, applicationName string) (*LinkInfo, error) {
// Get Service associated with the target component
serviceLabels := componentlabels.GetLabels(targetComponent, applicationName, false)
serviceSelector := util.ConvertLabelsToSelector(serviceLabels)
targetService, err := client.GetOneServiceFromSelector(serviceSelector)
if err != nil {
return nil, errors.Wrapf(err, "unable to get service associated with the component %v", targetComponent)
}
// Generate environment variables to inject
linkHostKey := fmt.Sprintf("COMPONENT_%v_HOST", strings.ToUpper(targetComponent))
linkHostValue := targetService.Name
linkPortKey := fmt.Sprintf("COMPONENT_%v_PORT", strings.ToUpper(targetComponent))
linkPort, err := getPortFromService(targetService)
if err != nil {
return nil, errors.Wrapf(err, "unable to get port from Service %v", targetService.Name)
}
linkPortValue := fmt.Sprint(linkPort)
// Inject environment variable to source component
dcLabels := componentlabels.GetLabels(sourceComponent, applicationName, false)
dcSelector := util.ConvertLabelsToSelector(dcLabels)
sourceDC, err := client.GetOneDeploymentConfigFromSelector(dcSelector)
if err != nil {
return nil, errors.Wrapf(err, "unable to get Deployment Config for component %v", sourceComponent)
}
if err := client.AddEnvironmentVariablesToDeploymentConfig(
[]corev1.EnvVar{
{
Name: linkHostKey,
Value: linkHostValue,
},
{
Name: linkPortKey,
Value: linkPortValue,
},
}, sourceDC); err != nil {
return nil, errors.Wrapf(err, "unable to add environment variables to Deployment Config %v", sourceDC.Name)
}
return &LinkInfo{
SourceComponent: sourceComponent,
TargetComponent: targetComponent,
Envs: []string{
fmt.Sprintf("%v=%v", linkHostKey, linkHostValue),
fmt.Sprintf("%v=%v", linkPortKey, linkPortValue),
},
}, nil
}
// getPortFromService returns the first port listed in the service
func getPortFromService(service *corev1.Service) (int32, error) {
numServicePorts := len(service.Spec.Ports)
if numServicePorts != 1 {
return 0, fmt.Errorf("expected exactly one port in the service %v, but got %v", service.Name, numServicePorts)
}
return service.Spec.Ports[0].Port, nil
}
// Get Component Description
func GetComponentDesc(client *occlient.Client, currentComponent string, currentApplication string, currentProject string) (componentType string, path string, componentURL string, appStore []storage.StorageInfo, err error) {
// Component Type
componentType, err = GetComponentType(client, currentComponent, currentApplication, currentProject)
if err != nil {
return "", "", "", nil, errors.Wrap(err, "unable to get source path")
}
// Source
_, path, err = GetComponentSource(client, currentComponent, currentApplication, currentProject)
if err != nil {
return "", "", "", nil, errors.Wrap(err, "unable to get source path")
}
// URL
urlList, err := urlpkg.List(client, currentComponent, currentApplication)
if len(urlList) != 0 {
componentURL = urlList[0].URL
}
if err != nil {
return "", "", "", nil, errors.Wrap(err, "unable to get url list")
}
//Storage
appStore, err = storage.List(client, currentComponent, currentApplication)
if err != nil {
return "", "", "", nil, errors.Wrap(err, "unable to get storage list")
}
return componentType, path, componentURL, appStore, nil
}
// Get Component logs
// follow the DeploymentConfig logs if follow is set to true
func GetLogs(client *occlient.Client, componentName string, applicationName string, follow bool, stdout io.Writer) error {
// Namespace the component
namespacedOpenShiftObject, err := util.NamespaceOpenShiftObject(componentName, applicationName)
if err != nil {
return errors.Wrapf(err, "unable to create namespaced name")
}
// Retrieve the logs
err = client.DisplayDeploymentConfigLog(namespacedOpenShiftObject, follow, stdout)
if err != nil {
return err
}
return nil
}