This is an automated email from the ASF dual-hosted git repository. tsato pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 5288f5952f123e26bb5bc9f839fc7e552903d972 Author: Antonin Stefanutti <anto...@stefanutti.fr> AuthorDate: Wed Sep 7 16:14:27 2022 +0200 feat: Ingress trait host is optional --- pkg/trait/ingress.go | 55 ++++++++++++++++------------------------------- pkg/trait/ingress_test.go | 34 +++-------------------------- 2 files changed, 21 insertions(+), 68 deletions(-) diff --git a/pkg/trait/ingress.go b/pkg/trait/ingress.go index 0c46ef4ac..f674c1ac7 100644 --- a/pkg/trait/ingress.go +++ b/pkg/trait/ingress.go @@ -22,7 +22,7 @@ import ( "fmt" corev1 "k8s.io/api/core/v1" - networking "k8s.io/api/networking/v1" + networkingv1 "k8s.io/api/networking/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/utils/pointer" @@ -50,51 +50,32 @@ func (t *ingressTrait) IsAllowedInProfile(profile v1.TraitProfile) bool { } func (t *ingressTrait) Configure(e *Environment) (bool, error) { - if e.Integration == nil || !pointer.BoolDeref(t.Enabled, true) { - if e.Integration != nil { - e.Integration.Status.SetCondition( - v1.IntegrationConditionExposureAvailable, - corev1.ConditionFalse, - v1.IntegrationConditionIngressNotAvailableReason, - "explicitly disabled", - ) - } - + if !e.IntegrationInRunningPhases() { return false, nil } - if !e.IntegrationInRunningPhases() { + if !pointer.BoolDeref(t.Enabled, true) { + e.Integration.Status.SetCondition( + v1.IntegrationConditionExposureAvailable, + corev1.ConditionFalse, + v1.IntegrationConditionIngressNotAvailableReason, + "explicitly disabled", + ) return false, nil } if pointer.BoolDeref(t.Auto, true) { - hasService := e.Resources.GetUserServiceForIntegration(e.Integration) != nil - hasHost := t.Host != "" - enabled := hasService && hasHost - - if !enabled { + if e.Resources.GetUserServiceForIntegration(e.Integration) == nil { e.Integration.Status.SetCondition( v1.IntegrationConditionExposureAvailable, corev1.ConditionFalse, v1.IntegrationConditionIngressNotAvailableReason, - "no host or service defined", + "no service defined", ) - return false, nil } } - if t.Host == "" { - e.Integration.Status.SetCondition( - v1.IntegrationConditionExposureAvailable, - corev1.ConditionFalse, - v1.IntegrationConditionIngressNotAvailableReason, - "no host defined", - ) - - return false, errors.New("cannot Apply ingress trait: no host defined") - } - return true, nil } @@ -104,25 +85,25 @@ func (t *ingressTrait) Apply(e *Environment) error { return errors.New("cannot Apply ingress trait: no target service") } - ingress := networking.Ingress{ + ingress := networkingv1.Ingress{ TypeMeta: metav1.TypeMeta{ Kind: "Ingress", - APIVersion: networking.SchemeGroupVersion.String(), + APIVersion: networkingv1.SchemeGroupVersion.String(), }, ObjectMeta: metav1.ObjectMeta{ Name: service.Name, Namespace: service.Namespace, }, - Spec: networking.IngressSpec{ - DefaultBackend: &networking.IngressBackend{ - Service: &networking.IngressServiceBackend{ + Spec: networkingv1.IngressSpec{ + DefaultBackend: &networkingv1.IngressBackend{ + Service: &networkingv1.IngressServiceBackend{ Name: service.Name, - Port: networking.ServiceBackendPort{ + Port: networkingv1.ServiceBackendPort{ Name: "http", }, }, }, - Rules: []networking.IngressRule{ + Rules: []networkingv1.IngressRule{ { Host: t.Host, }, diff --git a/pkg/trait/ingress_test.go b/pkg/trait/ingress_test.go index 240346489..1090a0c1f 100644 --- a/pkg/trait/ingress_test.go +++ b/pkg/trait/ingress_test.go @@ -23,7 +23,7 @@ import ( "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" - networking "k8s.io/api/networking/v1" + networkingv1 "k8s.io/api/networking/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/utils/pointer" @@ -76,21 +76,7 @@ func TestConfigureAutoIngressTraitWithoutUserServiceDoesNotSucceed(t *testing.T) assert.Nil(t, err) conditions := environment.Integration.Status.Conditions assert.Len(t, conditions, 1) - assert.Equal(t, "no host or service defined", conditions[0].Message) -} - -func TestConfigureAutoIngressTraitWithEmptyHostDoesNotSucceed(t *testing.T) { - ingressTrait, environment := createNominalIngressTest() - ingressTrait.Auto = nil - ingressTrait.Host = "" - - configured, err := ingressTrait.Configure(environment) - - assert.False(t, configured) - assert.Nil(t, err) - conditions := environment.Integration.Status.Conditions - assert.Len(t, conditions, 1) - assert.Equal(t, "no host or service defined", conditions[0].Message) + assert.Equal(t, "no service defined", conditions[0].Message) } func TestConfigureAutoIngressTraitWithUserServiceDoesSucceed(t *testing.T) { @@ -104,20 +90,6 @@ func TestConfigureAutoIngressTraitWithUserServiceDoesSucceed(t *testing.T) { assert.Len(t, environment.Integration.Status.Conditions, 0) } -func TestConfigureIngressTraitWithoutHostDoesNotSucceed(t *testing.T) { - ingressTrait, environment := createNominalIngressTest() - ingressTrait.Host = "" - - configured, err := ingressTrait.Configure(environment) - - assert.False(t, configured) - assert.NotNil(t, err) - assert.Equal(t, "cannot Apply ingress trait: no host defined", err.Error()) - conditions := environment.Integration.Status.Conditions - assert.Len(t, conditions, 1) - assert.Equal(t, "no host defined", conditions[0].Message) -} - func TestApplyIngressTraitWithoutUserServiceDoesNotSucceed(t *testing.T) { ingressTrait, environment := createNominalIngressTest() environment.Resources = kubernetes.NewCollection() @@ -139,7 +111,7 @@ func TestApplyIngressTraitDoesSucceed(t *testing.T) { assert.Len(t, environment.Resources.Items(), 2) environment.Resources.Visit(func(resource runtime.Object) { - if ingress, ok := resource.(*networking.Ingress); ok { + if ingress, ok := resource.(*networkingv1.Ingress); ok { assert.Equal(t, "service-name", ingress.Name) assert.Equal(t, "namespace", ingress.Namespace) assert.Equal(t, "service-name", ingress.Spec.DefaultBackend.Service.Name)