forked from redhat-developer/odo
/
create.go
608 lines (503 loc) · 20.5 KB
/
create.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
package component
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/openshift/odo/pkg/catalog"
"github.com/openshift/odo/pkg/component"
"github.com/openshift/odo/pkg/config"
"github.com/openshift/odo/pkg/log"
appCmd "github.com/openshift/odo/pkg/odo/cli/application"
catalogutil "github.com/openshift/odo/pkg/odo/cli/catalog/util"
"github.com/openshift/odo/pkg/odo/cli/component/ui"
projectCmd "github.com/openshift/odo/pkg/odo/cli/project"
commonui "github.com/openshift/odo/pkg/odo/cli/ui"
"github.com/openshift/odo/pkg/odo/genericclioptions"
odoutil "github.com/openshift/odo/pkg/odo/util"
"github.com/openshift/odo/pkg/odo/util/completion"
"github.com/openshift/odo/pkg/util"
corev1 "k8s.io/api/core/v1"
ktemplates "k8s.io/kubernetes/pkg/kubectl/cmd/templates"
)
// CreateOptions encapsulates create options
type CreateOptions struct {
componentSettings config.ComponentSettings
componentBinary string
componentGit string
componentGitRef string
componentContext string
componentPorts []string
componentEnvVars []string
memoryMax string
memoryMin string
memory string
cpuMax string
cpuMin string
cpu string
wait bool
interactive bool
now bool
*CommonPushOptions
}
// CreateRecommendedCommandName is the recommended watch command name
const CreateRecommendedCommandName = "create"
// LocalDirectoryDefaultLocation is the default location of where --local files should always be..
// since the application will always be in the same directory as `.odo`, we will always set this as: ./
const LocalDirectoryDefaultLocation = "./"
var createLongDesc = ktemplates.LongDesc(`Create a configuration describing a component to be deployed on OpenShift.
If a component name is not provided, it'll be auto-generated.
By default, builder images will be used from the current namespace. You can explicitly supply a namespace by using: odo create namespace/name:version
If version is not specified by default, latest will be chosen as the version.
A full list of component types that can be deployed is available using: 'odo catalog list'`)
var createExample = ktemplates.Examples(` # Create new Node.js component with the source in current directory.
%[1]s nodejs
# A specific image version may also be specified
%[1]s nodejs:latest
# Create new Node.js component named 'frontend' with the source in './frontend' directory
%[1]s nodejs frontend --context ./frontend
# Create a new Node.js component of version 6 from the 'openshift' namespace
%[1]s openshift/nodejs:6 --context /nodejs-ex
# Create new Wildfly component with binary named sample.war in './downloads' directory
%[1]s wildfly wildfly --binary ./downloads/sample.war
# Create new Node.js component with source from remote git repository
%[1]s nodejs --git https://github.com/openshift/nodejs-ex.git
# Create new Node.js git component while specifying a branch, tag or commit ref
%[1]s nodejs --git https://github.com/openshift/nodejs-ex.git --ref master
# Create new Node.js git component while specifying a tag
%[1]s nodejs --git https://github.com/openshift/nodejs-ex.git --ref v1.0.1
# Create new Node.js component with the source in current directory and ports 8080-tcp,8100-tcp and 9100-udp exposed
%[1]s nodejs --port 8080,8100/tcp,9100/udp
# Create new Node.js component with the source in current directory and env variables key=value and key1=value1 exposed
%[1]s nodejs --env key=value,key1=value1
# For more examples, visit: https://github.com/openshift/odo/blob/master/docs/examples.adoc
%[1]s python --git https://github.com/openshift/django-ex.git
# Passing memory limits
%[1]s nodejs --memory 150Mi
%[1]s nodejs --min-memory 150Mi --max-memory 300 Mi
# Passing cpu limits
%[1]s nodejs --cpu 2
%[1]s nodejs --min-cpu 200m --max-cpu 2
`)
// NewCreateOptions returns new instance of CreateOptions
func NewCreateOptions() *CreateOptions {
return &CreateOptions{
CommonPushOptions: NewCommonPushOptions(),
}
}
func (co *CreateOptions) setComponentSourceAttributes() (err error) {
// Set the correct application context
co.componentSettings.Application = &(co.Context.Application)
// By default we set the source as LOCAL (if --local, --binary or --git isn't passed)
componentSourceType := config.LOCAL
// If --local, --binary or --git is passed, let's set the correct source type.
if len(co.componentBinary) != 0 {
componentSourceType = config.BINARY
} else if len(co.componentGit) != 0 {
componentSourceType = config.GIT
}
co.componentSettings.SourceType = &componentSourceType
// Here we set the correct source path for each type
switch componentSourceType {
// --binary
case config.BINARY:
cPath, err := filepath.EvalSymlinks(co.componentBinary)
if err != nil {
return err
}
// we need to store the SourceLocation relative to the componentContext
relativePathToSource, err := filepath.Rel(co.componentContext, cPath)
if err != nil {
return err
}
co.componentSettings.SourceLocation = &relativePathToSource
// --git
case config.GIT:
co.componentSettings.SourceLocation = &(co.componentGit)
componentSourceType = config.GIT
co.componentSettings.SourceType = &componentSourceType
// --local / default
case config.LOCAL:
directory := LocalDirectoryDefaultLocation
co.componentSettings.SourceLocation = &directory
// Error out by default if no type of sources were passed..
default:
return fmt.Errorf("The source can be either --binary or --local or --git")
}
// Set the Git reference if passed
if len(co.componentGitRef) != 0 {
co.componentSettings.Ref = &(co.componentGitRef)
}
// Error out if reference is passed but no --git flag passed
if len(co.componentGit) == 0 && len(co.componentGitRef) != 0 {
return fmt.Errorf("The --ref flag is only valid for --git flag")
}
return
}
func (co *CreateOptions) setComponentName(args []string) (err error) {
componentImageName, componentType, _, _ := util.ParseComponentImageName(args[0])
co.componentSettings.Type = &componentImageName
if len(args) == 2 {
co.componentSettings.Name = &args[1]
return
}
if co.componentSettings.SourceType == nil {
return errors.Wrap(err, "component type is mandatory parameter to generate a default component name")
}
componentName, err := createDefaultComponentName(
co.Context,
componentType,
*(co.componentSettings.SourceType),
co.componentContext,
)
if err != nil {
return err
}
co.componentSettings.Name = &componentName
return
}
func getSourceLocation(componentContext string, currentDirectory string) (string, error) {
// After getting the path relative to the current directory, we set the SourceLocation
sourceLocation, err := filepath.Rel(currentDirectory, componentContext)
if err != nil {
return "", errors.Wrapf(err, "unable to get a path relative to the current directory")
}
// If the paths are the same (currentDirectory vs co.componentSettings.SourceLocation)
// then we use the default location
if sourceLocation == "." {
return LocalDirectoryDefaultLocation, nil
}
return sourceLocation, nil
}
func createDefaultComponentName(context *genericclioptions.Context, componentType string, sourceType config.SrcType, sourcePath string) (string, error) {
// Retrieve the componentName, if the componentName isn't specified, we will use the default image name
var err error
finalSourcePath := sourcePath
// we only get absolute path for local source type
if sourceType == config.LOCAL {
if sourcePath == "" {
wd, err := os.Getwd()
if err != nil {
return "", err
}
finalSourcePath = wd
} else {
finalSourcePath, err = filepath.Abs(sourcePath)
if err != nil {
return "", err
}
}
}
componentName, err := component.GetDefaultComponentName(
finalSourcePath,
sourceType,
componentType,
component.ComponentList{},
)
if err != nil {
return "", nil
}
return componentName, nil
}
func (co *CreateOptions) setResourceLimits() error {
ensureAndLogProperResourceUsage(co.memory, co.memoryMin, co.memoryMax, "memory")
ensureAndLogProperResourceUsage(co.cpu, co.cpuMin, co.cpuMax, "cpu")
memoryQuantity, err := util.FetchResourceQuantity(corev1.ResourceMemory, co.memoryMin, co.memoryMax, co.memory)
if err != nil {
return err
}
if memoryQuantity != nil {
minMemory := memoryQuantity.MinQty.String()
maxMemory := memoryQuantity.MaxQty.String()
co.componentSettings.MinMemory = &minMemory
co.componentSettings.MaxMemory = &maxMemory
}
cpuQuantity, err := util.FetchResourceQuantity(corev1.ResourceCPU, co.cpuMin, co.cpuMax, co.cpu)
if err != nil {
return err
}
if cpuQuantity != nil {
minCPU := cpuQuantity.MinQty.String()
maxCPU := cpuQuantity.MaxQty.String()
co.componentSettings.MinCPU = &minCPU
co.componentSettings.MaxCPU = &maxCPU
}
return nil
}
// Complete completes create args
func (co *CreateOptions) Complete(name string, cmd *cobra.Command, args []string) (err error) {
if len(args) == 0 || !cmd.HasFlags() {
co.interactive = true
}
co.localConfigInfo, err = config.NewLocalConfigInfo(co.componentContext)
if err != nil {
return errors.Wrap(err, "failed intiating local config")
}
// check to see if config file exists or not, if it does that
// means we shouldn't allow the user to override the current component
if co.localConfigInfo.ConfigFileExists() {
return errors.New("this directory already contains a component")
}
co.componentSettings = co.localConfigInfo.GetComponentSettings()
co.Context = genericclioptions.NewContextCreatingAppIfNeeded(cmd)
// Below code is for INTERACTIVE mode
if co.interactive {
client := co.Client
catalogList, err := catalog.ListComponents(client)
if err != nil {
return err
}
componentTypeCandidates := catalogutil.FilterHiddenComponents(catalogList.Items)
selectedComponentType := ui.SelectComponentType(componentTypeCandidates)
selectedImageTag := ui.SelectImageTag(componentTypeCandidates, selectedComponentType)
componentType := selectedComponentType + ":" + selectedImageTag
co.componentSettings.Type = &componentType
// Ask for the type of source if not provided
selectedSourceType := ui.SelectSourceType([]config.SrcType{config.LOCAL, config.GIT, config.BINARY})
co.componentSettings.SourceType = &selectedSourceType
selectedSourcePath := LocalDirectoryDefaultLocation
// Get the current directory
currentDirectory, err := os.Getwd()
if err != nil {
return err
}
if selectedSourceType == config.BINARY {
// We ask for the source of the component context
co.componentContext = ui.EnterInputTypePath("context", currentDirectory, currentDirectory)
glog.V(4).Infof("Context: %s", co.componentContext)
// If it's a binary, we have to ask where the actual binary in relation
// to the context
selectedSourcePath = ui.EnterInputTypePath("binary", currentDirectory)
// Get the correct source location
sourceLocation, err := getSourceLocation(selectedSourcePath, co.componentContext)
if err != nil {
return errors.Wrapf(err, "unable to get source location")
}
co.componentSettings.SourceLocation = &sourceLocation
} else if selectedSourceType == config.GIT {
// For git, we ask for the Git URL and set that as the source location
cmpSrcLOC, selectedGitRef := ui.EnterGitInfo()
co.componentSettings.SourceLocation = &cmpSrcLOC
co.componentSettings.Ref = &selectedGitRef
} else if selectedSourceType == config.LOCAL {
// We ask for the source of the component, in this case the "path"!
co.componentContext = ui.EnterInputTypePath("path", currentDirectory, currentDirectory)
// Get the correct source location
if co.componentContext == "" {
co.componentContext = LocalDirectoryDefaultLocation
}
co.componentSettings.SourceLocation = &co.componentContext
}
defaultComponentName, err := createDefaultComponentName(co.Context, selectedComponentType, selectedSourceType, selectedSourcePath)
if err != nil {
return err
}
componentName := ui.EnterComponentName(defaultComponentName, co.Context)
appName := ui.EnterOpenshiftName(co.Context.Application, "Which application do you want the commponent to be associated with", co.Context)
co.componentSettings.Application = &appName
projectName := ui.EnterOpenshiftName(co.Context.Project, "Which project go you want the component to be created in", co.Context)
co.componentSettings.Project = &projectName
co.componentSettings.Name = &componentName
if commonui.Proceed("Do you wish to set advanced options") {
ports := ui.EnterPorts()
if len(ports) > 0 {
co.componentSettings.Ports = &ports
}
co.componentEnvVars = ui.EnterEnvVars()
if commonui.Proceed("Do you wish to set resource limits") {
memMax := ui.EnterMemory("maximum", "512Mi")
memMin := ui.EnterMemory("minimum", memMax)
cpuMax := ui.EnterCPU("maximum", "1")
cpuMin := ui.EnterCPU("minimum", cpuMax)
memoryQuantity, err := util.FetchResourceQuantity(corev1.ResourceMemory, memMin, memMax, "")
if err != nil {
return err
}
if memoryQuantity != nil {
co.componentSettings.MinMemory = &memMin
co.componentSettings.MaxMemory = &memMax
}
cpuQuantity, err := util.FetchResourceQuantity(corev1.ResourceCPU, cpuMin, cpuMax, "")
if err != nil {
return err
}
if cpuQuantity != nil {
co.componentSettings.MinCPU = &cpuMin
co.componentSettings.MaxCPU = &cpuMax
}
}
}
// Above code is for INTERACTIVE mode
} else {
// Else if NOT using interactive / UI
err = co.setComponentSourceAttributes()
if err != nil {
return err
}
err = co.setComponentName(args)
if err != nil {
return err
}
err = co.setResourceLimits()
if err != nil {
return err
}
var portList []string
if len(co.componentPorts) > 0 {
portList = co.componentPorts
} else {
portList, err = co.Client.GetPortsFromBuilderImage(*co.componentSettings.Type)
if err != nil {
return err
}
}
co.componentSettings.Ports = &(portList)
}
co.componentSettings.Project = &(co.Context.Project)
envs, err := config.NewEnvVarListFromSlice(co.componentEnvVars)
if err != nil {
return
}
co.componentSettings.Envs = envs
co.ignores = []string{}
if co.now {
co.ResolveSrcAndConfigFlags()
err = co.ResolveProject(co.Context.Project)
if err != nil {
return err
}
}
return
}
// Validate validates the create parameters
func (co *CreateOptions) Validate() (err error) {
s := log.Spinner("Validating component")
defer s.End(false)
if err := component.ValidateComponentCreateRequest(co.Context.Client, co.componentSettings, false, false); err != nil {
return err
}
s.End(true)
return nil
}
// Run has the logic to perform the required actions as part of command
func (co *CreateOptions) Run() (err error) {
err = co.localConfigInfo.SetComponentSettings(co.componentSettings)
if err != nil {
return errors.Wrapf(err, "failed to persist the component settings to config file")
}
if co.now {
co.Context, co.localConfigInfo, err = genericclioptions.UpdatedContext(co.Context)
if err != nil {
return errors.Wrap(err, "unable to retrieve updated local config")
}
err = co.SetSourceInfo()
if err != nil {
return errors.Wrap(err, "unable to set source information")
}
err = co.Push()
if err != nil {
return errors.Wrapf(err, "failed to push the changes")
}
} else {
log.Infof("Please use `odo push` command to create the component with source deployed")
}
return
}
func (co *CreateOptions) createCmpIfNotExistsAndApplyCmpConfig(stdout io.Writer) error {
cmpName := co.localConfigInfo.GetName()
appName := co.localConfigInfo.GetApplication()
cmpType := co.localConfigInfo.GetType()
isCmpExists, err := component.Exists(co.Context.Client, cmpName, appName)
if err != nil {
return errors.Wrapf(err, "failed to check if component %s exists or not", cmpName)
}
if !isCmpExists {
log.Successf("Creating %s component with name %s", cmpType, cmpName)
// Classic case of component creation
if err = component.CreateComponent(co.Context.Client, *co.localConfigInfo, co.componentContext, stdout); err != nil {
log.Errorf(
"Failed to create component with name %s. Please use `odo config view` to view settings used to create component. Error: %+v",
cmpName,
err,
)
os.Exit(1)
}
log.Successf("Successfully created component %s", cmpName)
}
// Apply config
err = component.ApplyConfig(co.Context.Client, *co.localConfigInfo, stdout, isCmpExists)
if err != nil {
odoutil.LogErrorAndExit(err, "Failed to update config to component deployed")
}
log.Successf("Successfully updated component with name: %v", cmpName)
return nil
}
// The general cpu/memory is used as a fallback when it's set and both min-cpu/memory max-cpu/memory are not set
// when the only thing specified is the min or max value, we exit the application
func ensureAndLogProperResourceUsage(resource, resourceMin, resourceMax, resourceName string) {
if strings.HasPrefix(resourceMin, "-") {
log.Errorf("min-%s cannot be negative", resource)
os.Exit(1)
}
if strings.HasPrefix(resourceMax, "-") {
log.Errorf("max-%s cannot be negative", resource)
os.Exit(1)
}
if strings.HasPrefix(resource, "-") {
log.Errorf("%s cannot be negative", resource)
os.Exit(1)
}
if resourceMin != "" && resourceMax != "" && resource != "" {
log.Infof("`--%s` will be ignored as `--min-%s` and `--max-%s` has been passed\n", resourceName, resourceName, resourceName)
}
if (resourceMin == "") != (resourceMax == "") && resource != "" {
log.Infof("Using `--%s` %s for min and max limits.\n", resourceName, resource)
}
if (resourceMin == "") != (resourceMax == "") && resource == "" {
log.Errorf("`--min-%s` should accompany `--max-%s` or pass `--%s` to use same value for both min and max or try not passing any of them\n", resourceName, resourceName, resourceName)
os.Exit(1)
}
}
// NewCmdCreate implements the create odo command
func NewCmdCreate(name, fullName string) *cobra.Command {
co := NewCreateOptions()
var componentCreateCmd = &cobra.Command{
Use: fmt.Sprintf("%s <component_type> [component_name] [flags]", name),
Short: "Create a new component",
Long: createLongDesc,
Example: fmt.Sprintf(createExample, fullName),
Args: cobra.RangeArgs(0, 2),
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(co, cmd, args)
},
}
componentCreateCmd.Flags().StringVarP(&co.componentBinary, "binary", "b", "", "Use a binary as the source file for the component")
componentCreateCmd.Flags().StringVarP(&co.componentGit, "git", "g", "", "Use a git repository as the source file for the component")
componentCreateCmd.Flags().StringVarP(&co.componentGitRef, "ref", "r", "", "Use a specific ref e.g. commit, branch or tag of the git repository")
genericclioptions.AddContextFlag(componentCreateCmd, &co.componentContext)
componentCreateCmd.Flags().StringVar(&co.memory, "memory", "", "Amount of memory to be allocated to the component. ex. 100Mi")
componentCreateCmd.Flags().StringVar(&co.memoryMin, "min-memory", "", "Limit minimum amount of memory to be allocated to the component. ex. 100Mi")
componentCreateCmd.Flags().StringVar(&co.memoryMax, "max-memory", "", "Limit maximum amount of memory to be allocated to the component. ex. 100Mi")
componentCreateCmd.Flags().StringVar(&co.cpu, "cpu", "", "Amount of cpu to be allocated to the component. ex. 100m or 0.1")
componentCreateCmd.Flags().StringVar(&co.cpuMin, "min-cpu", "", "Limit minimum amount of cpu to be allocated to the component. ex. 100m")
componentCreateCmd.Flags().StringVar(&co.cpuMax, "max-cpu", "", "Limit maximum amount of cpu to be allocated to the component. ex. 1")
componentCreateCmd.Flags().StringSliceVarP(&co.componentPorts, "port", "p", []string{}, "Ports to be used when the component is created (ex. 8080,8100/tcp,9100/udp)")
componentCreateCmd.Flags().StringSliceVar(&co.componentEnvVars, "env", []string{}, "Environmental variables for the component. For example --env VariableName=Value")
// Add a defined annotation in order to appear in the help menu
componentCreateCmd.Annotations = map[string]string{"command": "component"}
componentCreateCmd.SetUsageTemplate(odoutil.CmdUsageTemplate)
// Adding `--now` flag
genericclioptions.AddNowFlag(componentCreateCmd, &co.now)
//Adding `--project` flag
projectCmd.AddProjectFlag(componentCreateCmd)
//Adding `--application` flag
appCmd.AddApplicationFlag(componentCreateCmd)
completion.RegisterCommandHandler(componentCreateCmd, completion.CreateCompletionHandler)
completion.RegisterCommandFlagHandler(componentCreateCmd, "context", completion.FileCompletionHandler)
completion.RegisterCommandFlagHandler(componentCreateCmd, "binary", completion.FileCompletionHandler)
return componentCreateCmd
}