Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#5635) Add TLS configuration in the ingress trait #5650

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
},
}
}
squakez marked this conversation as resolved.
Show resolved Hide resolved

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
Loading