/
kamelets.go
383 lines (341 loc) · 11.6 KB
/
kamelets.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
/*
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 (
"errors"
"fmt"
"path/filepath"
"sort"
"strconv"
"strings"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
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/kamelet/repository"
"github.com/apache/camel-k/v2/pkg/platform"
"github.com/apache/camel-k/v2/pkg/util"
"github.com/apache/camel-k/v2/pkg/util/camel"
"github.com/apache/camel-k/v2/pkg/util/digest"
"github.com/apache/camel-k/v2/pkg/util/dsl"
"github.com/apache/camel-k/v2/pkg/util/kamelets"
)
const (
kameletsTraitID = "kamelets"
contentKey = "content"
KameletLocationProperty = "camel.component.kamelet.location"
kameletLabel = "camel.apache.org/kamelet"
kameletConfigurationLabel = "camel.apache.org/kamelet.configuration"
kameletMountPointAnnotation = "camel.apache.org/kamelet.mount-point"
)
type configurationKey struct {
kamelet string
configurationID string
}
func newConfigurationKey(kamelet, configurationID string) configurationKey {
return configurationKey{
kamelet: kamelet,
configurationID: configurationID,
}
}
type kameletsTrait struct {
BaseTrait
traitv1.KameletsTrait `property:",squash"`
}
func newKameletsTrait() Trait {
return &kameletsTrait{
BaseTrait: NewBaseTrait("kamelets", 450),
}
}
func (t *kameletsTrait) Configure(e *Environment) (bool, *TraitCondition, error) {
if e.Integration == nil {
return false, nil, nil
}
if !pointer.BoolDeref(t.Enabled, true) {
return false, NewIntegrationConditionUserDisabled("Kamelets"), nil
}
if !e.IntegrationInPhase(v1.IntegrationPhaseInitialization) && !e.IntegrationInRunningPhases() {
return false, nil, nil
}
if pointer.BoolDeref(t.Auto, true) {
kamelets, err := kamelets.ExtractKameletFromSources(e.Ctx, e.Client, e.CamelCatalog, e.Resources, e.Integration)
if err != nil {
return false, nil, err
}
if len(kamelets) > 0 {
sort.Strings(kamelets)
t.List = strings.Join(kamelets, ",")
}
if t.MountPoint == "" {
t.MountPoint = filepath.Join(camel.BasePath, "kamelets")
}
}
return len(t.getKameletKeys()) > 0, nil, nil
}
func (t *kameletsTrait) Apply(e *Environment) error {
if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) || e.IntegrationInRunningPhases() {
if err := t.addKamelets(e); err != nil {
return err
}
}
return nil
}
func (t *kameletsTrait) collectKamelets(e *Environment) (map[string]*v1.Kamelet, error) {
repo, err := repository.NewForPlatform(e.Ctx, e.Client, e.Platform, e.Integration.Namespace, platform.GetOperatorNamespace())
if err != nil {
return nil, err
}
kamelets := make(map[string]*v1.Kamelet)
missingKamelets := make([]string, 0)
availableKamelets := make([]string, 0)
for _, key := range t.getKameletKeys() {
kamelet, err := repo.Get(e.Ctx, key)
if err != nil {
return nil, err
}
if kamelet == nil {
missingKamelets = append(missingKamelets, key)
} else {
availableKamelets = append(availableKamelets, key)
kamelets[key] = kamelet
}
}
sort.Strings(availableKamelets)
sort.Strings(missingKamelets)
if len(missingKamelets) > 0 {
message := fmt.Sprintf("kamelets [%s] found, kamelets [%s] not found in %s repositories",
strings.Join(availableKamelets, ","),
strings.Join(missingKamelets, ","),
repo.String())
e.Integration.Status.SetCondition(
v1.IntegrationConditionKameletsAvailable,
corev1.ConditionFalse,
v1.IntegrationConditionKameletsAvailableReason,
message,
)
return nil, errors.New(message)
}
e.Integration.Status.SetCondition(
v1.IntegrationConditionKameletsAvailable,
corev1.ConditionTrue,
v1.IntegrationConditionKameletsAvailableReason,
fmt.Sprintf("kamelets [%s] found in %s repositories", strings.Join(availableKamelets, ","), repo.String()),
)
return kamelets, nil
}
func (t *kameletsTrait) addKamelets(e *Environment) error {
if len(t.getKameletKeys()) > 0 {
kamelets, err := t.collectKamelets(e)
if err != nil {
return err
}
kb := newKameletBundle()
for _, key := range t.getKameletKeys() {
kamelet := kamelets[key]
if err := t.addKameletAsSource(e, kamelet); err != nil {
return err
}
// Adding dependencies from Kamelets
util.StringSliceUniqueConcat(&e.Integration.Status.Dependencies, kamelet.Spec.Dependencies)
// Add to Kamelet bundle configmap
kb.add(kamelet)
}
bundleConfigmaps, err := kb.toConfigmaps(e.Integration.Name, e.Integration.Namespace)
if err != nil {
return err
}
// set kamelets runtime location
if e.ApplicationProperties == nil {
e.ApplicationProperties = map[string]string{}
}
for _, cm := range bundleConfigmaps {
kameletMountPoint := fmt.Sprintf("%s/%s", t.MountPoint, cm.Name)
cm.Annotations[kameletMountPointAnnotation] = kameletMountPoint
e.Resources.Add(cm)
if e.ApplicationProperties[KameletLocationProperty] == "" {
e.ApplicationProperties[KameletLocationProperty] = fmt.Sprintf("file:%s", kameletMountPoint)
} else {
e.ApplicationProperties[KameletLocationProperty] += fmt.Sprintf(",file:%s", kameletMountPoint)
}
}
e.ApplicationProperties[KameletLocationProperty] += ",classpath:/kamelets"
// resort dependencies
sort.Strings(e.Integration.Status.Dependencies)
}
return nil
}
// This func will add a Kamelet as a generated Integration source. The source included here is going to be used in order to parse the Kamelet
// for any component or capability (ie, rest) which is included in the Kamelet spec itself. However, the generated source is marked as coming `FromKamelet`.
// When mounting the sources, these generated sources won't be mounted as sources but as Kamelet instead.
func (t *kameletsTrait) addKameletAsSource(e *Environment, kamelet *v1.Kamelet) error {
sources := make([]v1.SourceSpec, 0)
if kamelet.Spec.Template != nil {
template := kamelet.Spec.Template
flowData, err := dsl.TemplateToYamlDSL(*template, kamelet.Name)
if err != nil {
return err
}
flowSource := v1.SourceSpec{
DataSpec: v1.DataSpec{
Name: fmt.Sprintf("%s.yaml", kamelet.Name),
Content: string(flowData),
},
Language: v1.LanguageYaml,
FromKamelet: true,
}
flowSource, err = integrationSourceFromKameletSource(e, kamelet, flowSource, fmt.Sprintf("%s-kamelet-%s-template", e.Integration.Name, kamelet.Name))
if err != nil {
return err
}
sources = append(sources, flowSource)
}
for idx, s := range kamelet.Spec.Sources {
intSource, err := integrationSourceFromKameletSource(e, kamelet, s, fmt.Sprintf("%s-kamelet-%s-%03d", e.Integration.Name, kamelet.Name, idx))
if err != nil {
return err
}
sources = append(sources, intSource)
}
for _, source := range sources {
replaced := false
for idx, existing := range e.Integration.Status.GeneratedSources {
if existing.Name == source.Name {
replaced = true
e.Integration.Status.GeneratedSources[idx] = source
}
}
if !replaced {
e.Integration.Status.GeneratedSources = append(e.Integration.Status.GeneratedSources, source)
}
}
return nil
}
// Deprecated: use explicit secret configuration instead.
func (t *kameletsTrait) listConfigurationSecrets(e *Environment) ([]string, error) {
listConfigurationSecrets := make([]string, 0)
for _, k := range t.getConfigurationKeys() {
options := metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", kameletLabel, k.kamelet),
}
if k.configurationID != "" {
options.LabelSelector = fmt.Sprintf("%s=%s,%s=%s", kameletLabel, k.kamelet, kameletConfigurationLabel, k.configurationID)
}
secrets, err := t.Client.CoreV1().Secrets(e.Integration.Namespace).List(e.Ctx, options)
if err != nil {
return nil, err
}
for _, item := range secrets.Items {
if item.Labels != nil && item.Labels[kameletConfigurationLabel] != k.configurationID {
continue
}
listConfigurationSecrets = append(listConfigurationSecrets, item.Name)
}
}
return listConfigurationSecrets, nil
}
func (t *kameletsTrait) getKameletKeys() []string {
answer := make([]string, 0)
for _, item := range strings.Split(t.List, ",") {
i := strings.Trim(item, " \t\"")
if strings.Contains(i, "/") {
i = strings.SplitN(i, "/", 2)[0]
}
if i != "" && v1.ValidKameletName(i) {
util.StringSliceUniqueAdd(&answer, i)
}
}
sort.Strings(answer)
return answer
}
func (t *kameletsTrait) getConfigurationKeys() []configurationKey {
answer := make([]configurationKey, 0)
for _, item := range t.getKameletKeys() {
answer = append(answer, newConfigurationKey(item, ""))
}
for _, item := range strings.Split(t.List, ",") {
i := strings.Trim(item, " \t\"")
if strings.Contains(i, "/") {
parts := strings.SplitN(i, "/", 2)
newKey := newConfigurationKey(parts[0], parts[1])
alreadyPresent := false
for _, existing := range answer {
if existing == newKey {
alreadyPresent = true
break
}
}
if !alreadyPresent {
answer = append(answer, newKey)
}
}
}
sort.Slice(answer, func(i, j int) bool {
o1 := answer[i]
o2 := answer[j]
return o1.kamelet < o2.kamelet || (o1.kamelet == o2.kamelet && o1.configurationID < o2.configurationID)
})
return answer
}
func integrationSourceFromKameletSource(e *Environment, kamelet *v1.Kamelet, source v1.SourceSpec, name string) (v1.SourceSpec, error) {
if source.Type == v1.SourceTypeTemplate {
// Kamelets must be named "<kamelet-name>.extension"
language := source.InferLanguage()
source.Name = fmt.Sprintf("%s.%s", kamelet.Name, string(language))
}
if source.DataSpec.ContentRef != "" {
return source, nil
}
// Create configmaps to avoid storing kamelet definitions in the integration CR
// Compute the input digest and store it along with the configmap
hash, err := digest.ComputeForSource(source)
if err != nil {
return v1.SourceSpec{}, err
}
cm := initializeConfigmapKameletSource(source, hash, name, e.Integration.Namespace, e.Integration.Name, kamelet.Name)
e.Resources.Add(&cm)
target := source.DeepCopy()
target.Content = ""
target.ContentRef = name
target.ContentKey = contentKey
return *target, nil
}
func initializeConfigmapKameletSource(source v1.SourceSpec, hash, name, namespace, itName, kamName string) corev1.ConfigMap {
return corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
"camel.apache.org/integration": itName,
"camel.apache.org/kamelet": kamName,
},
Annotations: map[string]string{
sourceLanguageAnnotation: string(source.Language),
sourceNameAnnotation: name,
sourceCompressionAnnotation: strconv.FormatBool(source.Compression),
"camel.apache.org/source.generated": "true",
"camel.apache.org/source.type": string(source.Type),
"camel.apache.org/source.digest": hash,
},
},
Data: map[string]string{
contentKey: source.Content,
},
}
}