Skip to content

Commit

Permalink
add tls to ingress trait
Browse files Browse the repository at this point in the history
  • Loading branch information
romain-pfund committed Jun 17, 2024
1 parent af4b627 commit 59ca396
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
4 changes: 4 additions & 0 deletions pkg/apis/camel/v1/trait/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ type IngressTrait struct {
PathType *networkingv1.PathType `property:"path-type" json:"pathType,omitempty"`
// To automatically add an ingress whenever the integration uses an HTTP endpoint consumer.
Auto *bool `property:"auto" json:"auto,omitempty"`
// To configure tls hosts
TLSHosts []string `property:"tls-hosts" json:"tlsHosts,omitempty"`
// To configure tls secret name
TLSSecretName string `property:"tls-secret-name" json:"tlsSecretName,omitempty"`
}
21 changes: 16 additions & 5 deletions pkg/trait/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ package trait
import (
"errors"
"fmt"

corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
"strings"

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
Expand All @@ -44,10 +44,12 @@ func newIngressTrait() Trait {
return &ingressTrait{
BaseTrait: NewBaseTrait(ingressTraitID, ingressTraitOrder),
IngressTrait: traitv1.IngressTrait{
Annotations: map[string]string{},
Host: "",
Path: "/",
PathType: ptrFrom(networkingv1.PathTypePrefix),
Annotations: map[string]string{},
Host: "",
Path: "/",
PathType: ptrFrom(networkingv1.PathTypePrefix),
TLSHosts: []string{},
TLSSecretName: "",
},
}
}
Expand Down Expand Up @@ -126,6 +128,15 @@ func (t *ingressTrait) Apply(e *Environment) error {
},
}

if len(t.TLSHosts) > 0 || len(strings.TrimSpace(t.TLSSecretName)) > 0 {
ingress.Spec.TLS = []networkingv1.IngressTLS{
{
Hosts: t.TLSHosts,
SecretName: t.TLSSecretName,
},
}
}

e.Resources.Add(&ingress)

message := fmt.Sprintf("%s(%s) -> %s(%s)", ingress.Name, t.Host, service.Name, "http")
Expand Down
35 changes: 35 additions & 0 deletions pkg/trait/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func TestApplyIngressTraitDoesSucceed(t *testing.T) {
assert.Equal(t, "service-name", ingress.Spec.Rules[0].HTTP.Paths[0].Backend.Service.Name)
assert.Equal(t, "/", ingress.Spec.Rules[0].HTTP.Paths[0].Path)
assert.NotNil(t, *ingress.Spec.Rules[0].HTTP.Paths[0].PathType)
assert.Nil(t, ingress.Spec.TLS)
assert.Equal(t, networkingv1.PathTypePrefix, *ingress.Spec.Rules[0].HTTP.Paths[0].PathType)
}
})
Expand All @@ -127,6 +128,40 @@ func TestApplyIngressTraitDoesSucceed(t *testing.T) {
assert.Equal(t, "service-name(hostname) -> service-name(http)", conditions[0].Message)
}

func TestConfigureTLSIngressTraitWDoesSucceed(t *testing.T) {
ingressTrait, environment := createNominalIngressTest()
ingressTrait.TLSHosts = []string{"host1.com", "host2.com"}
ingressTrait.TLSSecretName = "nginxWildcard"

err := ingressTrait.Apply(environment)

require.NoError(t, err)
assert.Len(t, environment.Integration.Status.Conditions, 1)

assert.Len(t, environment.Resources.Items(), 2)
environment.Resources.Visit(func(resource runtime.Object) {
if ingress, ok := resource.(*networkingv1.Ingress); ok {
assert.Equal(t, "service-name", ingress.Name)
assert.Equal(t, "namespace", ingress.Namespace)
assert.Len(t, ingress.Spec.Rules, 1)
assert.Equal(t, "hostname", ingress.Spec.Rules[0].Host)
assert.Len(t, ingress.Spec.Rules[0].HTTP.Paths, 1)
assert.Equal(t, "service-name", ingress.Spec.Rules[0].HTTP.Paths[0].Backend.Service.Name)
assert.Equal(t, "/", ingress.Spec.Rules[0].HTTP.Paths[0].Path)
assert.NotNil(t, *ingress.Spec.Rules[0].HTTP.Paths[0].PathType)
assert.Equal(t, networkingv1.PathTypePrefix, *ingress.Spec.Rules[0].HTTP.Paths[0].PathType)
assert.Equal(t, "host1.com", ingress.Spec.TLS[0].Hosts[0])
assert.Equal(t, "host2.com", ingress.Spec.TLS[0].Hosts[1])
assert.Equal(t, "nginxWildcard", ingress.Spec.TLS[0].SecretName)
}
})

conditions := environment.Integration.Status.Conditions
assert.Len(t, conditions, 1)
assert.Equal(t, "service-name(hostname) -> service-name(http)", conditions[0].Message)

}

func createNominalIngressTest() (*ingressTrait, *Environment) {
trait, _ := newIngressTrait().(*ingressTrait)
trait.Enabled = pointer.Bool(true)
Expand Down

0 comments on commit 59ca396

Please sign in to comment.