Skip to content

Commit d13f546

Browse files
committed
fix(trait): merge integrationplatform trait
Closes #6006
1 parent 2d942f4 commit d13f546

File tree

2 files changed

+79
-11
lines changed

2 files changed

+79
-11
lines changed

pkg/trait/util.go

+6-11
Original file line numberDiff line numberDiff line change
@@ -382,27 +382,22 @@ func extractAsArray(value string) []string {
382382
return []string{value}
383383
}
384384

385+
// NewSpecTraitsOptionsForIntegrationAndPlatform will merge traits giving priority to Integration configuration over Platform configuration.
385386
func NewSpecTraitsOptionsForIntegrationAndPlatform(c client.Client, i *v1.Integration, pl *v1.IntegrationPlatform) (Options, error) {
386-
var options Options
387-
var err error
387+
var mergedTraits v1.Traits
388388
if pl != nil {
389-
options, err = ToTraitMap(pl.Status.Traits)
390-
if err != nil {
389+
mergedTraits = pl.Status.Traits
390+
if err := mergedTraits.Merge(i.Spec.Traits); err != nil {
391391
return nil, err
392392
}
393393
} else {
394-
options = Options{}
394+
mergedTraits = i.Spec.Traits
395395
}
396-
397-
m1, err := ToTraitMap(i.Spec.Traits)
396+
options, err := ToTraitMap(mergedTraits)
398397
if err != nil {
399398
return nil, err
400399
}
401400

402-
for k, v := range m1 {
403-
options[k] = v
404-
}
405-
406401
// Deprecated: to remove when we remove support for traits annotations.
407402
// IMPORTANT: when we remove this we'll need to remove the client.Client from the func,
408403
// which will bring to more cascade removal. It had to be introduced to support the deprecated feature

pkg/trait/util_test.go

+73
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,76 @@ func TestIntegrationAndPipeSameTraits(t *testing.T) {
265265
require.NoError(t, err)
266266
assert.True(t, result)
267267
}
268+
269+
func TestMergePlatformTraits(t *testing.T) {
270+
integration := &v1.Integration{
271+
Spec: v1.IntegrationSpec{
272+
Traits: v1.Traits{
273+
Camel: &traitv1.CamelTrait{
274+
Properties: []string{"hello=world"},
275+
},
276+
},
277+
},
278+
}
279+
platform := &v1.IntegrationPlatform{
280+
Status: v1.IntegrationPlatformStatus{
281+
IntegrationPlatformSpec: v1.IntegrationPlatformSpec{
282+
Traits: v1.Traits{
283+
Camel: &traitv1.CamelTrait{
284+
RuntimeVersion: "1.2.3",
285+
},
286+
},
287+
},
288+
},
289+
}
290+
291+
expectedOptions := Options{
292+
"camel": {
293+
"properties": []any{"hello=world"},
294+
"runtimeVersion": "1.2.3",
295+
},
296+
}
297+
298+
c, err := internal.NewFakeClient()
299+
require.NoError(t, err)
300+
mergedOptions, err := NewSpecTraitsOptionsForIntegrationAndPlatform(c, integration, platform)
301+
require.NoError(t, err)
302+
assert.Equal(t, expectedOptions, mergedOptions)
303+
}
304+
305+
func TestMergePlatformTraitsIntegrationPriority(t *testing.T) {
306+
integration := &v1.Integration{
307+
Spec: v1.IntegrationSpec{
308+
Traits: v1.Traits{
309+
Camel: &traitv1.CamelTrait{
310+
Properties: []string{"hello=world"},
311+
RuntimeVersion: "0.0.0",
312+
},
313+
},
314+
},
315+
}
316+
platform := &v1.IntegrationPlatform{
317+
Status: v1.IntegrationPlatformStatus{
318+
IntegrationPlatformSpec: v1.IntegrationPlatformSpec{
319+
Traits: v1.Traits{
320+
Camel: &traitv1.CamelTrait{
321+
RuntimeVersion: "1.2.3",
322+
},
323+
},
324+
},
325+
},
326+
}
327+
328+
expectedOptions := Options{
329+
"camel": {
330+
"properties": []any{"hello=world"},
331+
"runtimeVersion": "0.0.0",
332+
},
333+
}
334+
335+
c, err := internal.NewFakeClient()
336+
require.NoError(t, err)
337+
mergedOptions, err := NewSpecTraitsOptionsForIntegrationAndPlatform(c, integration, platform)
338+
require.NoError(t, err)
339+
assert.Equal(t, expectedOptions, mergedOptions)
340+
}

0 commit comments

Comments
 (0)