-
Notifications
You must be signed in to change notification settings - Fork 350
/
builder.go
561 lines (500 loc) · 18.6 KB
/
builder.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
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package trait
import (
"fmt"
"regexp"
"sort"
"strings"
corev1 "k8s.io/api/core/v1"
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
"github.com/apache/camel-k/v2/pkg/builder"
"github.com/apache/camel-k/v2/pkg/util/jib"
mvn "github.com/apache/camel-k/v2/pkg/util/maven"
"github.com/apache/camel-k/v2/pkg/util/property"
)
const (
builderTraitID = "builder"
)
var commandsRegexp = regexp.MustCompile(`"[^"]+"|[\w/-]+`)
type builderTrait struct {
BasePlatformTrait
traitv1.BuilderTrait `property:",squash"`
}
func newBuilderTrait() Trait {
return &builderTrait{
BasePlatformTrait: NewBasePlatformTrait("builder", 600),
}
}
// InfluencesKit overrides base class method.
func (t *builderTrait) InfluencesKit() bool {
return true
}
// InfluencesBuild overrides base class method.
func (t *builderTrait) InfluencesBuild(this, prev map[string]interface{}) bool {
return true
}
func (t *builderTrait) Configure(e *Environment) (bool, *TraitCondition, error) {
if e.IntegrationKit == nil {
return false, nil, nil
}
condition := t.adaptDeprecatedFields()
if e.IntegrationKitInPhase(v1.IntegrationKitPhaseBuildSubmitted) {
if trait := e.Catalog.GetTrait(quarkusTraitID); trait != nil {
quarkus, ok := trait.(*quarkusTrait)
isNativeIntegration := quarkus.isNativeIntegration(e)
isNativeKit, err := quarkus.isNativeKit(e)
if err != nil {
return false, condition, err
}
if ok && (isNativeIntegration || isNativeKit) {
// TODO expect maven repository in local repo (need to change builder pod accordingly!)
command := builder.QuarkusRuntimeSupport(e.CamelCatalog.GetCamelQuarkusVersion()).BuildCommands()
nativeBuilderImage := quarkus.NativeBuilderImage
if nativeBuilderImage == "" {
// default from the catalog
nativeBuilderImage = e.CamelCatalog.GetQuarkusToolingImage()
}
// it should be performed as the last custom task
t.Tasks = append(t.Tasks, fmt.Sprintf(`quarkus-native;%s;/bin/bash -c "%s"`, nativeBuilderImage, command))
// Force the build to run in a separate Pod and strictly sequential
m := "This is a Quarkus native build: setting build configuration with build Pod strategy and native container sensible resources (if not specified by the user). Make sure your cluster can handle it."
t.L.Info(m)
condition = newOrAppend(condition, m)
t.Strategy = string(v1.BuildStrategyPod)
t.OrderStrategy = string(v1.BuildOrderStrategySequential)
if !existsTaskRequest(t.TasksRequestCPU, "quarkus-native") {
t.TasksRequestCPU = append(t.TasksRequestCPU, "quarkus-native:1000m")
}
if !existsTaskRequest(t.TasksRequestMemory, "quarkus-native") {
t.TasksRequestMemory = append(t.TasksRequestMemory, "quarkus-native:4Gi")
}
}
}
return true, condition, nil
}
return false, condition, nil
}
func existsTaskRequest(tasks []string, taskName string) bool {
for _, task := range tasks {
ts := strings.Split(task, ":")
if len(ts) == 2 && ts[0] == taskName {
return true
}
}
return false
}
func (t *builderTrait) adaptDeprecatedFields() *TraitCondition {
var condition *TraitCondition
if t.RequestCPU != "" {
m := "The request-cpu parameter is deprecated and may be removed in future releases. Make sure to use tasks-request-cpu parameter instead."
t.L.Info(m)
condition = newOrAppend(condition, m)
t.TasksRequestCPU = append(t.TasksRequestCPU, fmt.Sprintf("builder:%s", t.RequestCPU))
}
if t.LimitCPU != "" {
m := "The limit-cpu parameter is deprecated and may be removed in future releases. Make sure to use tasks-limit-cpu parameter instead."
t.L.Info(m)
condition = newOrAppend(condition, m)
t.TasksLimitCPU = append(t.TasksLimitCPU, fmt.Sprintf("builder:%s", t.LimitCPU))
}
if t.RequestMemory != "" {
m := "The request-memory parameter is deprecated and may be removed in future releases. Make sure to use tasks-request-memory parameter instead."
t.L.Info(m)
condition = newOrAppend(condition, m)
t.TasksRequestMemory = append(t.TasksRequestMemory, fmt.Sprintf("builder:%s", t.RequestMemory))
}
if t.LimitMemory != "" {
m := "The limit-memory parameter is deprecated and may be removed in future releases. Make sure to use tasks-limit-memory parameter instead."
t.L.Info(m)
if condition == nil {
condition = NewIntegrationCondition(v1.IntegrationConditionTraitInfo, corev1.ConditionTrue, traitConfigurationReason, "")
}
condition = newOrAppend(condition, m)
t.TasksLimitMemory = append(t.TasksLimitMemory, fmt.Sprintf("builder:%s", t.LimitMemory))
}
return condition
}
func newOrAppend(condition *TraitCondition, message string) *TraitCondition {
if condition == nil {
condition = NewIntegrationCondition(v1.IntegrationConditionTraitInfo, corev1.ConditionTrue, traitConfigurationReason, message)
} else {
condition.message += "; " + message
}
return condition
}
func (t *builderTrait) Apply(e *Environment) error {
// local pipeline tasks
var pipelineTasks []v1.Task
// task configuration resources
tasksConf, err := t.parseTasksConf()
if err != nil {
return err
}
// Building task
builderTask, err := t.builderTask(e, taskConfOrDefault(tasksConf, "builder"))
if err != nil {
e.IntegrationKit.Status.Phase = v1.IntegrationKitPhaseError
e.IntegrationKit.Status.SetCondition("IntegrationKitPropertiesFormatValid", corev1.ConditionFalse,
"IntegrationKitPropertiesFormatValid", fmt.Sprintf("One or more properties where not formatted as expected: %s", err.Error()))
if err := e.Client.Status().Update(e.Ctx, e.IntegrationKit); err != nil {
return err
}
return nil
}
pipelineTasks = append(pipelineTasks, v1.Task{Builder: builderTask})
// Custom tasks
if t.Tasks != nil {
realBuildStrategy := builderTask.Configuration.Strategy
if realBuildStrategy == "" {
realBuildStrategy = e.Platform.Status.Build.BuildConfiguration.Strategy
}
if len(t.Tasks) > 0 && realBuildStrategy != v1.BuildStrategyPod {
e.IntegrationKit.Status.Phase = v1.IntegrationKitPhaseError
e.IntegrationKit.Status.SetCondition("IntegrationKitTasksValid",
corev1.ConditionFalse,
"IntegrationKitTasksValid",
fmt.Sprintf("Pipeline tasks unavailable when using `%s` platform build strategy: use `%s` instead.",
realBuildStrategy,
v1.BuildStrategyPod),
)
if err := e.Client.Status().Update(e.Ctx, e.IntegrationKit); err != nil {
return err
}
return nil
}
customTasks, err := t.customTasks(tasksConf)
if err != nil {
return err
}
pipelineTasks = append(pipelineTasks, customTasks...)
}
// Packaging task
// It's the same builder configuration, but with different steps and conf
packageTask := builderTask.DeepCopy()
packageTask.Name = "package"
packageTask.Configuration = *taskConfOrDefault(tasksConf, "package")
packageTask.Steps = make([]string, 0)
pipelineTasks = append(pipelineTasks, v1.Task{Package: packageTask})
// Publishing task
switch e.Platform.Status.Build.PublishStrategy {
case v1.IntegrationPlatformBuildPublishStrategySpectrum:
pipelineTasks = append(pipelineTasks, v1.Task{Spectrum: &v1.SpectrumTask{
BaseTask: v1.BaseTask{
Name: "spectrum",
Configuration: *taskConfOrDefault(tasksConf, "spectrum"),
},
PublishTask: v1.PublishTask{
BaseImage: t.getBaseImage(e),
Image: getImageName(e),
Registry: e.Platform.Status.Build.Registry,
},
}})
case v1.IntegrationPlatformBuildPublishStrategyJib:
pipelineTasks = append(pipelineTasks, v1.Task{Jib: &v1.JibTask{
BaseTask: v1.BaseTask{
Name: "jib",
Configuration: *taskConfOrDefault(tasksConf, "jib"),
},
PublishTask: v1.PublishTask{
BaseImage: t.getBaseImage(e),
Image: getImageName(e),
Registry: e.Platform.Status.Build.Registry,
},
}})
case v1.IntegrationPlatformBuildPublishStrategyS2I:
pipelineTasks = append(pipelineTasks, v1.Task{S2i: &v1.S2iTask{
BaseTask: v1.BaseTask{
Name: "s2i",
Configuration: *taskConfOrDefault(tasksConf, "s2i"),
},
Tag: e.IntegrationKit.ResourceVersion,
}})
case v1.IntegrationPlatformBuildPublishStrategyBuildah:
t.L.Infof("Warning: Buildah publishing strategy is deprecated and may be removed in future releases. Use any alternative publishing strategy.")
var platform string
var found bool
if platform, found = e.Platform.Status.Build.PublishStrategyOptions[builder.BuildahPlatform]; !found {
platform = ""
t.L.Infof("Attribute platform for buildah not found, default from host will be used!")
} else {
t.L.Infof("User defined %s platform, will be used from buildah!", platform)
}
var executorImage string
if image, found := e.Platform.Status.Build.PublishStrategyOptions[builder.BuildahImage]; found {
executorImage = image
t.L.Infof("User defined executor image %s will be used for buildah", image)
}
pipelineTasks = append(pipelineTasks, v1.Task{Buildah: &v1.BuildahTask{
Platform: platform,
BaseTask: v1.BaseTask{
Name: "buildah",
Configuration: *taskConfOrDefault(tasksConf, "buildah"),
},
PublishTask: v1.PublishTask{
Image: getImageName(e),
Registry: e.Platform.Status.Build.Registry,
},
Verbose: t.Verbose,
ExecutorImage: executorImage,
}})
//nolint: staticcheck,nolintlint
case v1.IntegrationPlatformBuildPublishStrategyKaniko:
t.L.Infof("Warning: Kaniko publishing strategy is deprecated and may be removed in future releases. Use any alternative publishing strategy.")
persistentVolumeClaim := e.Platform.Status.Build.PublishStrategyOptions[builder.KanikoPVCName]
cacheEnabled := e.Platform.Status.Build.IsOptionEnabled(builder.KanikoBuildCacheEnabled)
var executorImage string
if image, found := e.Platform.Status.Build.PublishStrategyOptions[builder.KanikoExecutorImage]; found {
executorImage = image
t.L.Infof("User defined executor image %s will be used for kaniko", image)
}
pipelineTasks = append(pipelineTasks, v1.Task{Kaniko: &v1.KanikoTask{
BaseTask: v1.BaseTask{
Name: "kaniko",
Configuration: *taskConfOrDefault(tasksConf, "kaniko"),
},
PublishTask: v1.PublishTask{
Image: getImageName(e),
Registry: e.Platform.Status.Build.Registry,
},
Cache: v1.KanikoTaskCache{
Enabled: &cacheEnabled,
PersistentVolumeClaim: persistentVolumeClaim,
},
Verbose: t.Verbose,
ExecutorImage: executorImage,
}})
}
// add local pipeline tasks to env pipeline
e.Pipeline = append(e.Pipeline, pipelineTasks...)
return nil
}
func (t *builderTrait) builderTask(e *Environment, taskConf *v1.BuildConfiguration) (*v1.BuilderTask, error) {
maven := v1.MavenBuildSpec{
MavenSpec: e.Platform.Status.Build.Maven,
}
// Add Maven repositories defined in the IntegrationKit
for _, repo := range e.IntegrationKit.Spec.Repositories {
maven.Repositories = append(maven.Repositories, mvn.NewRepository(repo))
}
if t.Strategy != "" {
t.L.Infof("User defined build strategy %s", t.Strategy)
found := false
for _, s := range v1.BuildStrategies {
if string(s) == t.Strategy {
found = true
taskConf.Strategy = s
break
}
}
if !found {
var strategies []string
for _, s := range v1.BuildStrategies {
strategies = append(strategies, string(s))
}
return nil, fmt.Errorf("unknown build strategy: %s. One of [%s] is expected", t.Strategy, strings.Join(strategies, ", "))
}
}
if t.OrderStrategy != "" {
t.L.Infof("User defined build order strategy %s", t.OrderStrategy)
found := false
for _, s := range v1.BuildOrderStrategies {
if string(s) == t.OrderStrategy {
found = true
taskConf.OrderStrategy = s
break
}
}
if !found {
var strategies []string
for _, s := range v1.BuildOrderStrategies {
strategies = append(strategies, string(s))
}
return nil, fmt.Errorf("unknown build order strategy: %s. One of [%s] is expected", t.OrderStrategy, strings.Join(strategies, ", "))
}
}
task := &v1.BuilderTask{
BaseTask: v1.BaseTask{
Name: "builder",
Configuration: *taskConf,
},
BaseImage: t.getBaseImage(e),
Runtime: e.CamelCatalog.Runtime,
Dependencies: e.IntegrationKit.Spec.Dependencies,
Maven: maven,
}
if task.Maven.Properties == nil {
task.Maven.Properties = make(map[string]string)
}
// User provided Maven properties
if t.Properties != nil {
for _, v := range t.Properties {
key, value := property.SplitPropertyFileEntry(v)
if len(key) == 0 || len(value) == 0 {
return nil, fmt.Errorf("maven property must have key=value format, it was %v", v)
}
task.Maven.Properties[key] = value
}
}
if e.Platform.Status.Build.PublishStrategy == v1.IntegrationPlatformBuildPublishStrategyJib {
profile, err := jib.JibMavenProfile(e.CamelCatalog.GetJibMavenPluginVersion(), e.CamelCatalog.GetJibLayerFilterExtensionMavenVersion())
if err != nil {
return nil, fmt.Errorf("error generating default maven jib profile: %w. ", err)
}
if err := jib.CreateProfileConfigmap(e.Ctx, e.Client, e.IntegrationKit, profile); err != nil {
return nil, fmt.Errorf("could not create default maven jib profile configmap: %w. ", err)
}
t.MavenProfiles = append(t.MavenProfiles, "configmap:"+e.IntegrationKit.Name+"-publish-jib-profile/profile.xml")
}
// User provides a maven profile
if t.MavenProfiles != nil {
mavenProfiles := make([]v1.ValueSource, 0)
for _, v := range t.MavenProfiles {
if v != "" {
mavenProfile, err := v1.DecodeValueSource(v, "profile.xml",
"illegal profile definition, syntax: configmap|secret:resource-name[/profile path]")
if err != nil {
return nil, fmt.Errorf("invalid maven profile: %s: %w. ", v, err)
}
mavenProfiles = append(mavenProfiles, mavenProfile)
}
}
task.Maven.Profiles = mavenProfiles
}
steps := make([]builder.Step, 0)
steps = append(steps, builder.Project.CommonSteps...)
// sort steps by phase
sort.SliceStable(steps, func(i, j int) bool {
return steps[i].Phase() < steps[j].Phase()
})
task.Steps = builder.StepIDsFor(steps...)
return task, nil
}
func getImageName(e *Environment) string {
organization := e.Platform.Status.Build.Registry.Organization
if organization == "" {
organization = e.Platform.Namespace
}
return e.Platform.Status.Build.Registry.Address + "/" + organization + "/camel-k-" + e.IntegrationKit.Name + ":" + e.IntegrationKit.ResourceVersion
}
func (t *builderTrait) getBaseImage(e *Environment) string {
baseImage := t.BaseImage
if baseImage == "" {
baseImage = e.Platform.Status.Build.BaseImage
}
return baseImage
}
func (t *builderTrait) customTasks(tasksConf map[string]*v1.BuildConfiguration) ([]v1.Task, error) {
customTasks := make([]v1.Task, len(t.Tasks))
for i, t := range t.Tasks {
splitted := strings.Split(t, ";")
if len(splitted) < 3 {
return nil, fmt.Errorf(`provide a custom task with at least 3 arguments, ie "my-task-name;my-image;echo 'hello', was %v"`, t)
}
var containerCommand string
if len(splitted) > 3 {
// recompose in case of usage of separator char in the script
containerCommand = strings.Join(splitted[2:], ";")
} else {
containerCommand = splitted[2]
}
containerCommands := splitContainerCommand(containerCommand)
customTasks[i] = v1.Task{
Custom: &v1.UserTask{
BaseTask: v1.BaseTask{
Name: splitted[0],
Configuration: *taskConfOrDefault(tasksConf, splitted[0]),
},
ContainerImage: splitted[1],
ContainerCommands: containerCommands,
},
}
}
return customTasks, nil
}
func taskConfOrDefault(tasksConf map[string]*v1.BuildConfiguration, taskName string) *v1.BuildConfiguration {
if tasksConf == nil || tasksConf[taskName] == nil {
return &v1.BuildConfiguration{}
}
return tasksConf[taskName]
}
func (t *builderTrait) parseTasksConf() (map[string]*v1.BuildConfiguration, error) {
tasksConf := make(map[string]*v1.BuildConfiguration)
for _, t := range t.TasksRequestCPU {
splits := strings.Split(t, ":")
if len(splits) != 2 {
return nil, fmt.Errorf("could not parse %s, expected format <task-name>:<task-resource>", t)
}
taskName := splits[0]
taskResource := splits[1]
if tasksConf[taskName] == nil {
tasksConf[taskName] = &v1.BuildConfiguration{}
}
tasksConf[taskName].RequestCPU = taskResource
}
for _, t := range t.TasksRequestMemory {
splits := strings.Split(t, ":")
if len(splits) != 2 {
return nil, fmt.Errorf("could not parse %s, expected format <task-name>:<task-resource>", t)
}
taskName := splits[0]
taskResource := splits[1]
if tasksConf[taskName] == nil {
tasksConf[taskName] = &v1.BuildConfiguration{}
}
tasksConf[taskName].RequestMemory = taskResource
}
for _, t := range t.TasksLimitCPU {
splits := strings.Split(t, ":")
if len(splits) != 2 {
return nil, fmt.Errorf("could not parse %s, expected format <task-name>:<task-resource>", t)
}
taskName := splits[0]
taskResource := splits[1]
if tasksConf[taskName] == nil {
tasksConf[taskName] = &v1.BuildConfiguration{}
}
tasksConf[taskName].LimitCPU = taskResource
}
for _, t := range t.TasksLimitMemory {
splits := strings.Split(t, ":")
if len(splits) != 2 {
return nil, fmt.Errorf("could not parse %s, expected format <task-name>:<task-resource>", t)
}
taskName := splits[0]
taskResource := splits[1]
if tasksConf[taskName] == nil {
tasksConf[taskName] = &v1.BuildConfiguration{}
}
tasksConf[taskName].LimitMemory = taskResource
}
return tasksConf, nil
}
// we may get a command in the following format `/bin/bash -c "ls && echo 'hello'`
// which should provide a string with {"/bin/bash", "-c", "ls && echo 'hello'"}.
// if however we have a command which is not quoted, then we leave it the way it is.
func splitContainerCommand(command string) []string {
if !strings.Contains(command, "\"") {
// No quotes, then, splits all commands found
return strings.Split(command, " ")
}
matches := commandsRegexp.FindAllString(command, -1)
removeQuotes := make([]string, 0, len(matches))
for _, m := range matches {
removeQuotes = append(removeQuotes, strings.ReplaceAll(m, "\"", ""))
}
return removeQuotes
}