Skip to content

Commit 212ceb2

Browse files
committed
fix(platform): load profile in Integration namespace
Closes #6864
1 parent aaa13ec commit 212ceb2

13 files changed

Lines changed: 217 additions & 32 deletions

File tree

‎pkg/apis/camel/v1/common_types.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ const (
3636
PlatformSelectorAnnotation = "camel.apache.org/platform.id"
3737
// IntegrationProfileAnnotation integration profile id annotation label.
3838
IntegrationProfileAnnotation = "camel.apache.org/integration-profile.id"
39+
// IntegrationProfileNamespaceAnnotation integration profile namespace annotation label.
40+
// NOTE: this is an internal annotation only valid for IntegrationKits and Builds.
41+
IntegrationProfileNamespaceAnnotation = "camel.apache.org/integration-profile.namespace"
3942
// IntegrationDontRunAfterBuildAnnotation -- .
4043
IntegrationDontRunAfterBuildAnnotation = "camel.apache.org/dont-run-after-build"
4144
// IntegrationDontRunAfterBuildAnnotationTrueValue -- .

‎pkg/apis/camel/v1/common_types_support.go‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,6 @@ func GetOperatorIDAnnotation(obj metav1.Object) string {
171171
return GetAnnotation(OperatorIDAnnotation, obj)
172172
}
173173

174-
// GetIntegrationProfileAnnotation to safely get the integration profile annotation value.
175-
func GetIntegrationProfileAnnotation(obj metav1.Object) string {
176-
return GetAnnotation(IntegrationProfileAnnotation, obj)
177-
}
178-
179174
// GetAnnotation safely get the annotation value.
180175
func GetAnnotation(name string, obj metav1.Object) string {
181176
if obj == nil || obj.GetAnnotations() == nil {

‎pkg/controller/integration/monitor.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ func isIntegrationKitResetRequired(integration *v1.Integration, kit *v1.Integrat
301301
return true
302302
}
303303

304-
if v1.GetIntegrationProfileAnnotation(integration) != "" &&
305-
v1.GetIntegrationProfileAnnotation(integration) != v1.GetIntegrationProfileAnnotation(kit) {
304+
if kubernetes.GetIntegrationProfileResource(integration) != "" &&
305+
kubernetes.GetIntegrationProfileResource(integration) != kubernetes.GetIntegrationProfileResource(kit) {
306306
// Integration profile for the integration has changed. Reset integration kit
307307
// so new profile can be applied
308308
return true

‎pkg/controller/integrationkit/build.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ func (action *buildAction) createBuild(ctx context.Context, kit *v1.IntegrationK
120120
if v, ok := kit.Annotations[v1.IntegrationProfileAnnotation]; ok {
121121
annotations[v1.IntegrationProfileAnnotation] = v
122122
}
123+
if v, ok := kit.Annotations[v1.IntegrationProfileNamespaceAnnotation]; ok {
124+
annotations[v1.IntegrationProfileNamespaceAnnotation] = v
125+
}
123126

124127
operatorID := defaults.OperatorID()
125128
if operatorID != "" {

‎pkg/controller/pipe/monitor.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
3232
"github.com/apache/camel-k/v2/pkg/trait"
33+
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
3334
)
3435

3536
// NewMonitorAction returns an action that monitors the Pipe after it's fully initialized.
@@ -69,8 +70,8 @@ func (action *monitorAction) Handle(ctx context.Context, pipe *v1.Pipe) (*v1.Pip
6970
operatorIDChanged := v1.GetOperatorIDAnnotation(pipe) != "" &&
7071
(v1.GetOperatorIDAnnotation(pipe) != v1.GetOperatorIDAnnotation(&it))
7172

72-
integrationProfileChanged := v1.GetIntegrationProfileAnnotation(pipe) != "" &&
73-
(v1.GetIntegrationProfileAnnotation(pipe) != v1.GetIntegrationProfileAnnotation(&it))
73+
integrationProfileChanged := kubernetes.GetIntegrationProfileResource(pipe) != "" &&
74+
(kubernetes.GetIntegrationProfileResource(pipe) != kubernetes.GetIntegrationProfileResource(&it))
7475

7576
sameTraits, err := trait.IntegrationAndPipeSameTraits(action.client, &it, pipe)
7677
if err != nil {

‎pkg/platform/operator.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
camelv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
2626
"github.com/apache/camel-k/v2/pkg/util/defaults"
27+
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
2728
coordination "k8s.io/api/coordination/v1"
2829
k8serrors "k8s.io/apimachinery/pkg/api/errors"
2930
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -222,7 +223,7 @@ func (f FilteringFuncs[T]) Update(e event.TypedUpdateEvent[T]) bool {
222223
// Always force reconciliation when the object becomes managed by the current operator
223224
return true
224225
}
225-
if camelv1.GetIntegrationProfileAnnotation(e.ObjectOld) != camelv1.GetIntegrationProfileAnnotation(e.ObjectNew) {
226+
if kubernetes.GetIntegrationProfileResource(e.ObjectOld) != kubernetes.GetIntegrationProfileResource(e.ObjectNew) {
226227
// Always force reconciliation when the object gets attached to a new integration profile
227228
return true
228229
}

‎pkg/platform/profile.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func ApplyIntegrationProfile(ctx context.Context, c k8sclient.Reader, o k8sclien
4040
// findIntegrationProfile finds profile from given resource annotations
4141
// and resolves the profile in given resource namespace.
4242
func findIntegrationProfile(ctx context.Context, c k8sclient.Reader, o k8sclient.Object) (*v1.IntegrationProfile, error) {
43-
if profileName := v1.GetIntegrationProfileAnnotation(o); profileName != "" {
44-
return kubernetes.GetIntegrationProfile(ctx, c, profileName, o.GetNamespace())
43+
if profileResource := kubernetes.GetIntegrationProfileResource(o); profileResource != "" {
44+
return kubernetes.GetIntegrationProfile(ctx, c, profileResource)
4545
}
4646

4747
return nil, nil

‎pkg/platform/profile_test.go‎

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"github.com/apache/camel-k/v2/pkg/internal"
3030
)
3131

32-
func TestFindIntegrationProfile(t *testing.T) {
32+
func TestFindOrApplyIntegrationProfile(t *testing.T) {
3333
profile := v1.IntegrationProfile{
3434
ObjectMeta: metav1.ObjectMeta{
3535
Name: "custom",
@@ -41,6 +41,9 @@ func TestFindIntegrationProfile(t *testing.T) {
4141
require.NoError(t, err)
4242

4343
integration := v1.Integration{
44+
TypeMeta: metav1.TypeMeta{
45+
Kind: v1.IntegrationKind,
46+
},
4447
ObjectMeta: metav1.ObjectMeta{
4548
Name: "test",
4649
Namespace: "ns",
@@ -56,4 +59,78 @@ func TestFindIntegrationProfile(t *testing.T) {
5659
found, err := findIntegrationProfile(context.TODO(), c, &integration)
5760
require.NoError(t, err)
5861
assert.NotNil(t, found)
62+
63+
found, err = ApplyIntegrationProfile(context.TODO(), c, &integration)
64+
require.NoError(t, err)
65+
assert.NotNil(t, found)
66+
}
67+
68+
func TestFindOrApplyIntegrationMissing(t *testing.T) {
69+
profile := v1.IntegrationProfile{
70+
ObjectMeta: metav1.ObjectMeta{
71+
Name: "custom",
72+
Namespace: "ns",
73+
},
74+
}
75+
76+
c, err := internal.NewFakeClient(&profile)
77+
require.NoError(t, err)
78+
79+
integration := v1.Integration{
80+
TypeMeta: metav1.TypeMeta{
81+
Kind: v1.IntegrationKind,
82+
},
83+
ObjectMeta: metav1.ObjectMeta{
84+
Name: "test",
85+
Namespace: "ns",
86+
Annotations: map[string]string{
87+
v1.IntegrationProfileAnnotation: "missing",
88+
},
89+
},
90+
Status: v1.IntegrationStatus{
91+
Phase: v1.IntegrationPhaseRunning,
92+
},
93+
}
94+
95+
found, err := findIntegrationProfile(context.TODO(), c, &integration)
96+
require.Error(t, err)
97+
assert.Nil(t, found)
98+
assert.Contains(t, err.Error(), "\"missing\" not found")
99+
100+
found, err = ApplyIntegrationProfile(context.TODO(), c, &integration)
101+
require.NoError(t, err)
102+
assert.Nil(t, found)
103+
}
104+
105+
func TestFindOrApplyIntegrationProfileError(t *testing.T) {
106+
profile := v1.IntegrationProfile{
107+
ObjectMeta: metav1.ObjectMeta{
108+
Name: "custom",
109+
Namespace: "ns",
110+
},
111+
}
112+
113+
c, err := internal.NewFakeClient(&profile)
114+
require.NoError(t, err)
115+
116+
integrationKit := v1.IntegrationKit{
117+
TypeMeta: metav1.TypeMeta{
118+
Kind: v1.IntegrationKitKind,
119+
},
120+
ObjectMeta: metav1.ObjectMeta{
121+
Name: "test",
122+
Namespace: "ns",
123+
Annotations: map[string]string{
124+
v1.IntegrationProfileAnnotation: "custom",
125+
},
126+
},
127+
}
128+
129+
_, err = findIntegrationProfile(context.TODO(), c, &integrationKit)
130+
require.Error(t, err)
131+
assert.Equal(t, "wrong format for IntegrationProfile, expected profile-namespace/profile-id", err.Error())
132+
133+
_, err = ApplyIntegrationProfile(context.TODO(), c, &integrationKit)
134+
require.Error(t, err)
135+
assert.Equal(t, "wrong format for IntegrationProfile, expected profile-namespace/profile-id", err.Error())
59136
}

‎pkg/trait/quarkus.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ func (t *quarkusTrait) newIntegrationKit(e *Environment, packageType quarkusPack
304304

305305
if v, ok := integration.Annotations[v1.IntegrationProfileAnnotation]; ok {
306306
v1.SetAnnotation(&kit.ObjectMeta, v1.IntegrationProfileAnnotation, v)
307+
v1.SetAnnotation(&kit.ObjectMeta, v1.IntegrationProfileNamespaceAnnotation, integration.Namespace)
307308
}
308309
operatorID := defaults.OperatorID()
309310
if operatorID != "" {

‎pkg/trait/quarkus_test.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,19 @@ func TestFailNewIntegrationKit(t *testing.T) {
289289
require.Error(t, err)
290290
assert.Equal(t, "cannot create a Kit outside Integration or Catalog namespaces", err.Error())
291291
}
292+
293+
func TestNewIntegrationKitWithProfile(t *testing.T) {
294+
quarkusTrait, environment := createNominalQuarkusTest()
295+
environment.Integration.Namespace = "it-ns"
296+
environment.Integration.Annotations = map[string]string{
297+
v1.IntegrationProfileAnnotation: "my-profile",
298+
}
299+
environment.Platform.CatalogNamespace = "op-ns"
300+
301+
kit, err := quarkusTrait.newIntegrationKit(environment, fastJarPackageType)
302+
require.NoError(t, err)
303+
require.NotNil(t, kit)
304+
require.NotEmpty(t, kit.Annotations)
305+
assert.Equal(t, "my-profile", kit.Annotations[v1.IntegrationProfileAnnotation])
306+
assert.Equal(t, "it-ns", kit.Annotations[v1.IntegrationProfileNamespaceAnnotation])
307+
}

0 commit comments

Comments
 (0)