feat: allow to specify IngressClassName (#1669)

* feat: allow to specify IngressClassName

Signed-off-by: Anatolii Bazko <abazko@redhat.com>
pull/1671/head
Anatolii Bazko 2023-04-25 08:50:07 +03:00 committed by GitHub
parent a98d18233e
commit c927cac2bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 136 additions and 25 deletions

View File

@ -224,6 +224,10 @@ type CheClusterSpecNetworking struct {
// The secret must have a `app.kubernetes.io/part-of=che.eclipse.org` label.
// +optional
TlsSecretName string `json:"tlsSecretName,omitempty"`
// IngressClassName is the name of an IngressClass cluster resource.
// If a class name is defined in both the `IngressClassName` field and the `kubernetes.io/ingress.class` annotation,
// `IngressClassName` field takes precedence.
IngressClassName string `json:"ingressClassName,omitempty"`
// Authentication settings.
// +optional
// +kubebuilder:default:={gateway: {configLabels: {app: che, component: che-gateway-config}}}

View File

@ -7706,6 +7706,12 @@ spec:
hostname:
description: The public hostname of the installed Che server.
type: string
ingressClassName:
description: IngressClassName is the name of an IngressClass
cluster resource. If a class name is defined in both the `IngressClassName`
field and the `kubernetes.io/ingress.class` annotation, `IngressClassName`
field takes precedence.
type: string
labels:
additionalProperties:
type: string

View File

@ -7496,6 +7496,12 @@ spec:
hostname:
description: The public hostname of the installed Che server.
type: string
ingressClassName:
description: IngressClassName is the name of an IngressClass cluster
resource. If a class name is defined in both the `IngressClassName`
field and the `kubernetes.io/ingress.class` annotation, `IngressClassName`
field takes precedence.
type: string
labels:
additionalProperties:
type: string

View File

@ -7515,6 +7515,12 @@ spec:
hostname:
description: The public hostname of the installed Che server.
type: string
ingressClassName:
description: IngressClassName is the name of an IngressClass cluster
resource. If a class name is defined in both the `IngressClassName`
field and the `kubernetes.io/ingress.class` annotation, `IngressClassName`
field takes precedence.
type: string
labels:
additionalProperties:
type: string

View File

@ -7510,6 +7510,12 @@ spec:
hostname:
description: The public hostname of the installed Che server.
type: string
ingressClassName:
description: IngressClassName is the name of an IngressClass cluster
resource. If a class name is defined in both the `IngressClassName`
field and the `kubernetes.io/ingress.class` annotation, `IngressClassName`
field takes precedence.
type: string
labels:
additionalProperties:
type: string

View File

@ -7515,6 +7515,12 @@ spec:
hostname:
description: The public hostname of the installed Che server.
type: string
ingressClassName:
description: IngressClassName is the name of an IngressClass cluster
resource. If a class name is defined in both the `IngressClassName`
field and the `kubernetes.io/ingress.class` annotation, `IngressClassName`
field takes precedence.
type: string
labels:
additionalProperties:
type: string

View File

@ -7510,6 +7510,12 @@ spec:
hostname:
description: The public hostname of the installed Che server.
type: string
ingressClassName:
description: IngressClassName is the name of an IngressClass cluster
resource. If a class name is defined in both the `IngressClassName`
field and the `kubernetes.io/ingress.class` annotation, `IngressClassName`
field takes precedence.
type: string
labels:
additionalProperties:
type: string

View File

@ -7510,6 +7510,12 @@ spec:
hostname:
description: The public hostname of the installed Che server.
type: string
ingressClassName:
description: IngressClassName is the name of an IngressClass cluster
resource. If a class name is defined in both the `IngressClassName`
field and the `kubernetes.io/ingress.class` annotation, `IngressClassName`
field takes precedence.
type: string
labels:
additionalProperties:
type: string

View File

@ -15,6 +15,8 @@ import (
"reflect"
"sort"
"k8s.io/utils/pointer"
"github.com/eclipse-che/che-operator/pkg/common/chetypes"
"github.com/eclipse-che/che-operator/pkg/common/constants"
"github.com/eclipse-che/che-operator/pkg/common/test"
@ -124,6 +126,13 @@ func GetIngressSpec(
}
}
ingressClassName := deployContext.CheCluster.Spec.Networking.IngressClassName
if ingressClassName == "" {
ingressClassName = annotations["kubernetes.io/ingress.class"]
}
// annotations `kubernetes.io/ingress.class` can not be set when the class field is also set
delete(annotations, "kubernetes.io/ingress.class")
ingress := &networking.Ingress{
TypeMeta: metav1.TypeMeta{
Kind: "Ingress",
@ -136,6 +145,7 @@ func GetIngressSpec(
Annotations: annotations,
},
Spec: networking.IngressSpec{
IngressClassName: pointer.String(ingressClassName),
Rules: []networking.IngressRule{
{
Host: host,

View File

@ -31,20 +31,14 @@ import (
func TestIngressSpec(t *testing.T) {
type testCase struct {
name string
ingressName string
ingressPath string
ingressComponent string
serviceName string
servicePort int
expectedIngress *networking.Ingress
cheCluster *chev2.CheCluster
name string
expectedIngress *networking.Ingress
cheCluster *chev2.CheCluster
}
testCases := []testCase{
{
name: "Test ingress",
ingressName: "test",
name: "Test case #1",
cheCluster: &chev2.CheCluster{
ObjectMeta: metav1.ObjectMeta{
Namespace: "eclipse-che",
@ -52,16 +46,13 @@ func TestIngressSpec(t *testing.T) {
},
Spec: chev2.CheClusterSpec{
Networking: chev2.CheClusterSpecNetworking{
Hostname: "test-host",
Labels: map[string]string{"type": "default"},
Annotations: map[string]string{"annotation-key": "annotation-value"},
Hostname: "test-host",
Labels: map[string]string{"type": "default"},
Annotations: map[string]string{"annotation-key": "annotation-value"},
IngressClassName: "nginx",
},
},
},
ingressPath: "",
ingressComponent: defaults.GetCheFlavor(),
serviceName: "che-host",
servicePort: 8080,
expectedIngress: &networking.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
@ -84,7 +75,73 @@ func TestIngressSpec(t *testing.T) {
APIVersion: networking.SchemeGroupVersion.String(),
},
Spec: networking.IngressSpec{
TLS: []v1.IngressTLS{{Hosts: []string{"test-host"}}},
IngressClassName: pointer.String("nginx"),
TLS: []v1.IngressTLS{{Hosts: []string{"test-host"}}},
Rules: []networking.IngressRule{
{
Host: "test-host",
IngressRuleValue: networking.IngressRuleValue{
HTTP: &networking.HTTPIngressRuleValue{
Paths: []networking.HTTPIngressPath{
{
Backend: networking.IngressBackend{
Service: &networking.IngressServiceBackend{
Name: "che-host",
Port: networking.ServiceBackendPort{
Number: 8080,
},
},
},
Path: "/",
PathType: (*networking.PathType)(pointer.StringPtr(string(networking.PathTypeImplementationSpecific))),
},
},
},
},
},
},
},
},
},
{
name: "Test case #1",
cheCluster: &chev2.CheCluster{
ObjectMeta: metav1.ObjectMeta{
Namespace: "eclipse-che",
Name: "eclipse-che",
},
Spec: chev2.CheClusterSpec{
Networking: chev2.CheClusterSpecNetworking{
Hostname: "test-host",
Labels: map[string]string{"type": "default"},
Annotations: map[string]string{"annotation-key": "annotation-value", "kubernetes.io/ingress.class": "nginx"},
},
},
},
expectedIngress: &networking.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "eclipse-che",
Labels: map[string]string{
"type": "default",
"app.kubernetes.io/component": defaults.GetCheFlavor(),
"app.kubernetes.io/instance": defaults.GetCheFlavor(),
"app.kubernetes.io/part-of": "che.eclipse.org",
"app.kubernetes.io/managed-by": defaults.GetCheFlavor() + "-operator",
"app.kubernetes.io/name": defaults.GetCheFlavor(),
},
Annotations: map[string]string{
"che.eclipse.org/managed-annotations-digest": "0000",
"annotation-key": "annotation-value",
},
},
TypeMeta: metav1.TypeMeta{
Kind: "Ingress",
APIVersion: networking.SchemeGroupVersion.String(),
},
Spec: networking.IngressSpec{
IngressClassName: pointer.String("nginx"),
TLS: []v1.IngressTLS{{Hosts: []string{"test-host"}}},
Rules: []networking.IngressRule{
{
Host: "test-host",
@ -116,15 +173,13 @@ func TestIngressSpec(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
deployContext := test.GetDeployContext(testCase.cheCluster, []runtime.Object{})
_, actualIngress := GetIngressSpec(deployContext,
testCase.ingressName,
testCase.ingressPath,
testCase.serviceName,
testCase.servicePort,
testCase.ingressComponent,
"test",
"",
"che-host",
8080,
defaults.GetCheFlavor(),
)
assert.Equal(t, testCase.expectedIngress, actualIngress)
})
}