You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by cl...@apache.org on 2024/03/25 17:10:36 UTC

(camel-k) branch main updated: Only enable knative trait when there is a knative endpoint (#5275)

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

claudio4j pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/main by this push:
     new 234815577 Only enable knative trait when there is a knative endpoint (#5275)
234815577 is described below

commit 23481557787487591f6c998eaa2433f17e559cad
Author: Claudio Miranda <cl...@claudius.com.br>
AuthorDate: Mon Mar 25 14:10:31 2024 -0300

    Only enable knative trait when there is a knative endpoint (#5275)
    
    https://github.com/apache/camel-k/issues/5145
---
 pkg/trait/knative.go              |   5 +
 pkg/trait/knative_service_test.go |   6 +-
 pkg/trait/knative_test.go         | 235 ++++++++++++++++++++++----------------
 3 files changed, 144 insertions(+), 102 deletions(-)

diff --git a/pkg/trait/knative.go b/pkg/trait/knative.go
index 312a1af4b..30063f98a 100644
--- a/pkg/trait/knative.go
+++ b/pkg/trait/knative.go
@@ -123,6 +123,11 @@ func (t *knativeTrait) Configure(e *Environment) (bool, *TraitCondition, error)
 			}
 			t.EventSinks = items
 		}
+		hasKnativeEndpoint := len(t.ChannelSources) > 0 || len(t.ChannelSinks) > 0 || len(t.EndpointSources) > 0 || len(t.EndpointSinks) > 0 || len(t.EventSources) > 0 || len(t.EventSinks) > 0
+		t.Enabled = &hasKnativeEndpoint
+		if !hasKnativeEndpoint {
+			return false, nil, nil
+		}
 		if t.FilterSourceChannels == nil {
 			// Filtering is no longer used by default
 			t.FilterSourceChannels = pointer.Bool(false)
diff --git a/pkg/trait/knative_service_test.go b/pkg/trait/knative_service_test.go
index 55cf37efe..a4e79f7dc 100644
--- a/pkg/trait/knative_service_test.go
+++ b/pkg/trait/knative_service_test.go
@@ -122,7 +122,7 @@ func TestKnativeService(t *testing.T) {
 
 	require.NoError(t, err)
 	assert.NotEmpty(t, environment.ExecutedTraits)
-	assert.NotNil(t, environment.GetTrait("knative"))
+	assert.NotNil(t, environment.GetTrait("knative-service"))
 	assert.Equal(t, 4, environment.Resources.Size())
 
 	s := environment.Resources.GetKnativeService(func(service *serving.Service) bool {
@@ -335,7 +335,7 @@ func TestKnativeServiceWithRest(t *testing.T) {
 
 	require.NoError(t, err)
 	assert.NotEmpty(t, environment.ExecutedTraits)
-	assert.NotNil(t, environment.GetTrait("knative"))
+	assert.NotNil(t, environment.GetTrait("knative-service"))
 
 	assert.NotNil(t, environment.Resources.GetKnativeService(func(service *serving.Service) bool {
 		return service.Name == KnativeServiceTestName
@@ -403,7 +403,7 @@ func TestKnativeServiceNotApplicable(t *testing.T) {
 
 	require.NoError(t, err)
 	assert.NotEmpty(t, environment.ExecutedTraits)
-	assert.NotNil(t, environment.GetTrait("knative"))
+	assert.Nil(t, environment.GetTrait("knative-service"))
 
 	assert.Nil(t, environment.Resources.GetKnativeService(func(service *serving.Service) bool {
 		return service.Name == KnativeServiceTestName
diff --git a/pkg/trait/knative_test.go b/pkg/trait/knative_test.go
index adc47978a..75d075620 100644
--- a/pkg/trait/knative_test.go
+++ b/pkg/trait/knative_test.go
@@ -348,6 +348,142 @@ func TestKnativePlatformHttpDependencies(t *testing.T) {
 	}
 }
 
+func TestKnativeEnabled(t *testing.T) {
+	catalog, err := camel.DefaultCatalog()
+	require.NoError(t, err)
+
+	traitCatalog := NewCatalog(nil)
+
+	environment := Environment{
+		CamelCatalog: catalog,
+		Catalog:      traitCatalog,
+		Integration: &v1.Integration{
+			ObjectMeta: metav1.ObjectMeta{
+				Name:      "test",
+				Namespace: "ns",
+			},
+			Status: v1.IntegrationStatus{
+				Phase: v1.IntegrationPhaseInitialization,
+			},
+			Spec: v1.IntegrationSpec{
+				Profile: v1.TraitProfileKnative,
+				Sources: []v1.SourceSpec{
+					{
+						DataSpec: v1.DataSpec{
+							Name:    "route.groovy",
+							Content: `from('timer:foo').to('knative:channel/channel-source-1')`,
+						},
+						Language: v1.LanguageGroovy,
+					},
+				},
+			},
+		},
+		Platform: &v1.IntegrationPlatform{
+			Spec: v1.IntegrationPlatformSpec{
+				Cluster: v1.IntegrationPlatformClusterOpenShift,
+				Build: v1.IntegrationPlatformBuildSpec{
+					PublishStrategy: v1.IntegrationPlatformBuildPublishStrategyS2I,
+					Registry:        v1.RegistrySpec{Address: "registry"},
+				},
+				Profile: v1.TraitProfileKnative,
+			},
+		},
+		EnvVars:        make([]corev1.EnvVar, 0),
+		ExecutedTraits: make([]Trait, 0),
+		Resources:      k8sutils.NewCollection(),
+	}
+	environment.Platform.ResyncStatusFullConfig()
+
+	// configure the init trait
+	init := NewInitTrait()
+	ok, condition, err := init.Configure(&environment)
+	require.NoError(t, err)
+	assert.True(t, ok)
+	assert.Nil(t, condition)
+
+	// apply the init trait
+	require.NoError(t, init.Apply(&environment))
+
+	// configure the knative trait
+	knTrait, _ := newKnativeTrait().(*knativeTrait)
+	ok, condition, err = knTrait.Configure(&environment)
+	require.NoError(t, err)
+	assert.True(t, ok)
+	assert.Nil(t, condition)
+
+	// apply the knative trait
+	require.NoError(t, knTrait.Apply(&environment))
+
+	assert.True(t, *knTrait.Enabled)
+	assert.Contains(t, environment.Integration.Status.Capabilities, v1.CapabilityKnative)
+}
+
+func TestKnativeNotEnabled(t *testing.T) {
+	catalog, err := camel.DefaultCatalog()
+	require.NoError(t, err)
+
+	traitCatalog := NewCatalog(nil)
+
+	environment := Environment{
+		CamelCatalog: catalog,
+		Catalog:      traitCatalog,
+		Integration: &v1.Integration{
+			ObjectMeta: metav1.ObjectMeta{
+				Name:      "test",
+				Namespace: "ns",
+			},
+			Status: v1.IntegrationStatus{
+				Phase: v1.IntegrationPhaseInitialization,
+			},
+			Spec: v1.IntegrationSpec{
+				Profile: v1.TraitProfileKnative,
+				Sources: []v1.SourceSpec{
+					{
+						DataSpec: v1.DataSpec{
+							Name:    "route.groovy",
+							Content: `from('timer:foo').to('log:info')`,
+						},
+						Language: v1.LanguageGroovy,
+					},
+				},
+			},
+		},
+		Platform: &v1.IntegrationPlatform{
+			Spec: v1.IntegrationPlatformSpec{
+				Cluster: v1.IntegrationPlatformClusterOpenShift,
+				Build: v1.IntegrationPlatformBuildSpec{
+					PublishStrategy: v1.IntegrationPlatformBuildPublishStrategyS2I,
+					Registry:        v1.RegistrySpec{Address: "registry"},
+				},
+				Profile: v1.TraitProfileKnative,
+			},
+		},
+		EnvVars:        make([]corev1.EnvVar, 0),
+		ExecutedTraits: make([]Trait, 0),
+		Resources:      k8sutils.NewCollection(),
+	}
+	environment.Platform.ResyncStatusFullConfig()
+
+	// configure the init trait
+	init := NewInitTrait()
+	ok, condition, err := init.Configure(&environment)
+	require.NoError(t, err)
+	assert.True(t, ok)
+	assert.Nil(t, condition)
+
+	// apply the init trait
+	require.NoError(t, init.Apply(&environment))
+
+	// configure the knative trait
+	knTrait, _ := newKnativeTrait().(*knativeTrait)
+	ok, condition, err = knTrait.Configure(&environment)
+	require.NoError(t, err)
+	assert.False(t, ok)
+	assert.Nil(t, condition)
+
+	assert.NotContains(t, environment.Integration.Status.Capabilities, v1.CapabilityKnative)
+}
+
 func NewFakeEnvironment(t *testing.T, source v1.SourceSpec) Environment {
 	t.Helper()
 
@@ -552,105 +688,6 @@ func NewFakeClient(namespace string) (client.Client, error) {
 	)
 }
 
-func SortChannelFakeClient(namespace string) (client.Client, error) {
-	channelSourceURL1, err := apis.ParseURL("http://channel-source-1.host/")
-	if err != nil {
-		return nil, err
-	}
-	channelSourceURL2, err := apis.ParseURL("http://channel-source-2.host/")
-	if err != nil {
-		return nil, err
-	}
-	channelSourceURL3, err := apis.ParseURL("http://channel-source-3.host/")
-	if err != nil {
-		return nil, err
-	}
-	channelSourceURL4, err := apis.ParseURL("http://channel-source-4.host/")
-	if err != nil {
-		return nil, err
-	}
-
-	return test.NewFakeClient(
-		// Channels unsorted on purpose
-		&messaging.Channel{
-			TypeMeta: metav1.TypeMeta{
-				Kind:       "Channel",
-				APIVersion: messaging.SchemeGroupVersion.String(),
-			},
-			ObjectMeta: metav1.ObjectMeta{
-				Namespace: namespace,
-				Name:      "channel-source-2",
-			},
-			Status: messaging.ChannelStatus{
-				ChannelableStatus: eventingduckv1.ChannelableStatus{
-					AddressStatus: duckv1.AddressStatus{
-						Address: &duckv1.Addressable{
-							URL: channelSourceURL2,
-						},
-					},
-				},
-			},
-		},
-		&messaging.Channel{
-			TypeMeta: metav1.TypeMeta{
-				Kind:       "Channel",
-				APIVersion: messaging.SchemeGroupVersion.String(),
-			},
-			ObjectMeta: metav1.ObjectMeta{
-				Namespace: namespace,
-				Name:      "channel-source-4",
-			},
-			Status: messaging.ChannelStatus{
-				ChannelableStatus: eventingduckv1.ChannelableStatus{
-					AddressStatus: duckv1.AddressStatus{
-						Address: &duckv1.Addressable{
-							URL: channelSourceURL4,
-						},
-					},
-				},
-			},
-		},
-		&messaging.Channel{
-			TypeMeta: metav1.TypeMeta{
-				Kind:       "Channel",
-				APIVersion: messaging.SchemeGroupVersion.String(),
-			},
-			ObjectMeta: metav1.ObjectMeta{
-				Namespace: namespace,
-				Name:      "channel-source-1",
-			},
-			Status: messaging.ChannelStatus{
-				ChannelableStatus: eventingduckv1.ChannelableStatus{
-					AddressStatus: duckv1.AddressStatus{
-						Address: &duckv1.Addressable{
-							URL: channelSourceURL1,
-						},
-					},
-				},
-			},
-		},
-		&messaging.Channel{
-			TypeMeta: metav1.TypeMeta{
-				Kind:       "Channel",
-				APIVersion: messaging.SchemeGroupVersion.String(),
-			},
-			ObjectMeta: metav1.ObjectMeta{
-				Namespace: namespace,
-				Name:      "channel-source-3",
-			},
-			Status: messaging.ChannelStatus{
-				ChannelableStatus: eventingduckv1.ChannelableStatus{
-					AddressStatus: duckv1.AddressStatus{
-						Address: &duckv1.Addressable{
-							URL: channelSourceURL3,
-						},
-					},
-				},
-			},
-		},
-	)
-}
-
 func TestKnativeSinkBinding(t *testing.T) {
 	source := v1.SourceSpec{
 		DataSpec: v1.DataSpec{