This is an automated email from the ASF dual-hosted git repository.

pcongiusti pushed a commit to branch release-2.3.x
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit 830a5bdaf2696a2554b83ef18072c65bc1790ddf
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Mon May 20 10:36:48 2024 +0200

    fix(traits): knative for synthetic kits
    
    Ref #5519
---
 pkg/trait/knative.go      | 11 +++++++
 pkg/trait/knative_test.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)

diff --git a/pkg/trait/knative.go b/pkg/trait/knative.go
index a6a032a9d..e879535ce 100644
--- a/pkg/trait/knative.go
+++ b/pkg/trait/knative.go
@@ -74,6 +74,12 @@ func (t *knativeTrait) Configure(e *Environment) (bool, 
*TraitCondition, error)
                return false, NewIntegrationConditionUserDisabled("Knative"), 
nil
        }
        if e.CamelCatalog == nil {
+               if t.isForcefullyEnabled() {
+                       // Likely a sourceless Integration. Here we must verify 
the user has forcefully enabled the feature in order to turn it on
+                       // as we don't have the possibility to scan the 
Integration source to verify if there is any endpoint suitable with
+                       // Knative
+                       return true, nil, nil
+               }
                return false, 
NewIntegrationConditionPlatformDisabledCatalogMissing(), nil
        }
        if !e.IntegrationInPhase(v1.IntegrationPhaseInitialization) && 
!e.IntegrationInRunningPhases() {
@@ -144,6 +150,11 @@ func (t *knativeTrait) Configure(e *Environment) (bool, 
*TraitCondition, error)
        return true, nil, nil
 }
 
+// This is true only when the user set the enabled flag on and the auto flag 
off
+func (t *knativeTrait) isForcefullyEnabled() bool {
+       return pointer.BoolDeref(t.Enabled, false) && 
!pointer.BoolDeref(t.Auto, true)
+}
+
 func filterMetaItems(catalog *camel.RuntimeCatalog, sources []v1.SourceSpec, 
cst knativeapi.CamelServiceType, uriType string) ([]string, error) {
        items := make([]string, 0)
        if err := metadata.Each(catalog, sources, func(_ int, meta 
metadata.IntegrationMetadata) bool {
diff --git a/pkg/trait/knative_test.go b/pkg/trait/knative_test.go
index 454331ecf..dbbec2a45 100644
--- a/pkg/trait/knative_test.go
+++ b/pkg/trait/knative_test.go
@@ -752,6 +752,60 @@ func NewFakeEnvironment(t *testing.T, source 
v1.SourceSpec) Environment {
        return environment
 }
 
+func NewFakeEnvironmentForSyntheticKit(t *testing.T) Environment {
+       t.Helper()
+       client, _ := NewFakeClient("ns")
+       traitCatalog := NewCatalog(nil)
+
+       environment := Environment{
+               Catalog: traitCatalog,
+               Client:  client,
+               Integration: &v1.Integration{
+                       ObjectMeta: metav1.ObjectMeta{
+                               Name:      "test",
+                               Namespace: "ns",
+                       },
+                       Status: v1.IntegrationStatus{
+                               Phase: v1.IntegrationPhaseDeploying,
+                       },
+                       Spec: v1.IntegrationSpec{
+                               Profile: v1.TraitProfileKnative,
+                               Traits: v1.Traits{
+                                       Knative: &traitv1.KnativeTrait{
+                                               Trait: traitv1.Trait{
+                                                       Enabled: 
pointer.Bool(true),
+                                               },
+                                       },
+                               },
+                       },
+               },
+               IntegrationKit: &v1.IntegrationKit{
+                       Status: v1.IntegrationKitStatus{
+                               Phase: v1.IntegrationKitPhaseReady,
+                       },
+               },
+               Platform: &v1.IntegrationPlatform{
+                       Spec: v1.IntegrationPlatformSpec{
+                               Cluster: v1.IntegrationPlatformClusterOpenShift,
+                               Build: v1.IntegrationPlatformBuildSpec{
+                                       PublishStrategy: 
v1.IntegrationPlatformBuildPublishStrategyS2I,
+                                       Registry:        
v1.RegistrySpec{Address: "registry"},
+                               },
+                               Profile: v1.TraitProfileKnative,
+                       },
+                       Status: v1.IntegrationPlatformStatus{
+                               Phase: v1.IntegrationPlatformPhaseReady,
+                       },
+               },
+               EnvVars:        make([]corev1.EnvVar, 0),
+               ExecutedTraits: make([]Trait, 0),
+               Resources:      k8sutils.NewCollection(),
+       }
+       environment.Platform.ResyncStatusFullConfig()
+
+       return environment
+}
+
 func NewFakeClient(namespace string) (client.Client, error) {
        channelSourceURL, err := apis.ParseURL("http://channel-source-1.host/";)
        if err != nil {
@@ -955,3 +1009,23 @@ func fromCamelProperties(appProps map[string]string) 
(*knativeapi.CamelEnvironme
 
        return &env, nil
 }
+
+func TestKnativeSyntheticKitDefault(t *testing.T) {
+       e := NewFakeEnvironmentForSyntheticKit(t)
+       knTrait, _ := newKnativeTrait().(*knativeTrait)
+       ok, condition, err := knTrait.Configure(&e)
+       require.NoError(t, err)
+       assert.False(t, ok)
+       assert.NotNil(t, condition)
+}
+
+func TestKnativeSyntheticKitEnabled(t *testing.T) {
+       e := NewFakeEnvironmentForSyntheticKit(t)
+       knTrait, _ := newKnativeTrait().(*knativeTrait)
+       knTrait.Enabled = pointer.Bool(true)
+       knTrait.Auto = pointer.Bool(false)
+       ok, condition, err := knTrait.Configure(&e)
+       require.NoError(t, err)
+       assert.True(t, ok)
+       assert.Nil(t, condition)
+}

Reply via email to