From c8b3e7351482236f63f18da52a8292a27fdf890e Mon Sep 17 00:00:00 2001 From: amerlin <4397319+antoniomerlin@users.noreply.github.com> Date: Mon, 11 Mar 2024 15:59:20 +0530 Subject: [PATCH] fixes #1568 #2225 (#2466) * fixes #1568 #2225 added function getIngressClass which set default or nginx ingress class to jaeger define ingress for providing loadbalancer IP. Signed-off-by: Gaurav Singh * Added test for checking if ingressClass is not set then the default or nginx ingressClass is added to the jaeger ingress. Signed-off-by: Gaurav Singh * Added request chenges mentioned by iblancasa re: test for checking if ingressClass is not set then the default or nginx ingressClass is added to the jaeger ingress. Signed-off-by: Gaurav Singh * changed func name from getInClusterAvailableIngressClasses() to getInClusterAvailableIngressClass() and updated manifests Signed-off-by: Gaurav Singh * fixed formatting Signed-off-by: Gaurav Singh --------- Signed-off-by: Gaurav Singh Co-authored-by: Gaurav Singh --- ...jaeger-operator.clusterserviceversion.yaml | 1 + config/rbac/role.yaml | 1 + .../jaegertracing/jaeger_controller.go | 2 +- pkg/ingress/query.go | 44 +++++++++++ pkg/ingress/query_test.go | 75 +++++++++++++++++++ 5 files changed, 122 insertions(+), 1 deletion(-) diff --git a/bundle/manifests/jaeger-operator.clusterserviceversion.yaml b/bundle/manifests/jaeger-operator.clusterserviceversion.yaml index 853361b2b..f6df8b8c1 100644 --- a/bundle/manifests/jaeger-operator.clusterserviceversion.yaml +++ b/bundle/manifests/jaeger-operator.clusterserviceversion.yaml @@ -336,6 +336,7 @@ spec: - apiGroups: - networking.k8s.io resources: + - ingressclasses - ingresses verbs: - create diff --git a/config/rbac/role.yaml b/config/rbac/role.yaml index 1ebba574a..ae92ec97f 100644 --- a/config/rbac/role.yaml +++ b/config/rbac/role.yaml @@ -221,6 +221,7 @@ rules: - apiGroups: - networking.k8s.io resources: + - ingressclasses - ingresses verbs: - create diff --git a/controllers/jaegertracing/jaeger_controller.go b/controllers/jaegertracing/jaeger_controller.go index a61b29351..0071a9a77 100644 --- a/controllers/jaegertracing/jaeger_controller.go +++ b/controllers/jaegertracing/jaeger_controller.go @@ -48,7 +48,7 @@ func NewReconciler(client client.Client, clientReader client.Reader, scheme *run // +kubebuilder:rbac:groups=apps,resources=deployments;daemonsets;replicasets;statefulsets,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=extensions,resources=ingresses,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=autoscaling,resources=horizontalpodautoscalers,verbs=get;list;watch;create;update;patch;delete -// +kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses,verbs=get;list;watch;create;update;patch;delete +// +kubebuilder:rbac:groups=networking.k8s.io,resources=ingresses;ingressclasses,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=route.openshift.io,resources=routes,verbs=get;list;watch;create;update;patch;delete // +kubebuilder:rbac:groups=route.openshift.io,resources=routes/custom-host,verbs=create // +kubebuilder:rbac:groups=console.openshift.io,resources=consolelinks,verbs=get;list;watch;create;update;patch;delete diff --git a/pkg/ingress/query.go b/pkg/ingress/query.go index 503be7d47..6a23d78cb 100644 --- a/pkg/ingress/query.go +++ b/pkg/ingress/query.go @@ -1,10 +1,13 @@ package ingress import ( + "context" "fmt" networkingv1 "k8s.io/api/networking/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" v1 "github.com/jaegertracing/jaeger-operator/apis/v1" "github.com/jaegertracing/jaeger-operator/pkg/service" @@ -52,6 +55,11 @@ func (i *QueryIngress) Get() *networkingv1.Ingress { if i.jaeger.Spec.Ingress.IngressClassName != nil { spec.IngressClassName = i.jaeger.Spec.Ingress.IngressClassName + } else { + ingressClass, err := getInClusterAvailableIngressClass() + if err == nil { + spec.IngressClassName = &ingressClass + } } return &networkingv1.Ingress{ @@ -150,3 +158,39 @@ func getRule(host string, path string, pathType *networkingv1.PathType, backend } return rule } + +func getInClusterAvailableIngressClass() (string, error) { + ingressClass := "" + nginxIngressAvailable := false + config, err := rest.InClusterConfig() + if err != nil { + return "", err + } + clientSet, err := kubernetes.NewForConfig(config) + if err != nil { + return "", err + } + ingressList, err := clientSet.NetworkingV1().IngressClasses().List(context.Background(), metav1.ListOptions{}) + if err != nil { + return "", err + } + for _, ingress := range ingressList.Items { + if ingress.Name == "nginx" { + nginxIngressAvailable = true + } + for k, v := range ingress.Annotations { + if k == "ingressclass.kubernetes.io/is-default-class" { + if v == "true" { + ingressClass = ingress.Name + break + } + } + } + } + if len(ingressClass) > 0 { + return ingressClass, nil + } else if nginxIngressAvailable { + return "nginx", nil + } + return "", fmt.Errorf("no available ingress controller found") +} diff --git a/pkg/ingress/query_test.go b/pkg/ingress/query_test.go index ff0915b09..4449fb079 100644 --- a/pkg/ingress/query_test.go +++ b/pkg/ingress/query_test.go @@ -1,8 +1,13 @@ package ingress import ( + "context" "testing" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" + "github.com/stretchr/testify/assert" "k8s.io/apimachinery/pkg/types" @@ -288,6 +293,67 @@ func TestQueryIngressClass(t *testing.T) { assert.NotNil(t, dep.Spec.IngressClassName) assert.Equal(t, "nginx", *dep.Spec.IngressClassName) assert.Nil(t, ingressNoClass.Get().Spec.IngressClassName) + +} + +func TestForDefaultIngressClass(t *testing.T) { + jaegerNoIngressNoClass := v1.NewJaeger(types.NamespacedName{Name: "TestQueryIngressNoClass"}) + + // for storing all ingress controller names installed inside cluster + AvailableIngressController := make(map[string]bool) + + config, err := rest.InClusterConfig() + if err != nil { + t.Fatalf("%v", err) + } + clientSet, err := kubernetes.NewForConfig(config) + if err != nil { + t.Fatalf("%v", err) + } + ingressList, err := clientSet.NetworkingV1().IngressClasses().List(context.Background(), metav1.ListOptions{}) + if err != nil { + t.Fatalf("%v", err) + } + + // check for all available ingress controller installed in cluster + for _, ingress := range ingressList.Items { + AvailableIngressController[ingress.Name] = false + for k, v := range ingress.Annotations { + if k == "ingressclass.kubernetes.io/is-default-class" { + if v == "true" { + AvailableIngressController[ingress.Name] = true + } + } + } + } + + getIngressClass, err := getInClusterAvailableIngressClass() + if err != nil { + if len(AvailableIngressController) > 0 { + t.Fatalf("%v", err) + } else { + ingressNoClass := NewQueryIngress(jaegerNoIngressNoClass) + assert.Nil(t, ingressNoClass.Get().Spec.IngressClassName) + } + } else { + jaegerNoIngressNoClass.Spec.Ingress.IngressClassName = &getIngressClass + } + + ingressNoClass := NewQueryIngress(jaegerNoIngressNoClass) + + dep := ingressNoClass.Get() + + defaultIngressClass := getDefaultIngressController(AvailableIngressController) + if len(defaultIngressClass) > 0 { + assert.Equal(t, defaultIngressClass, *dep.Spec.IngressClassName) + } else { + if _, ok := AvailableIngressController["nginx"]; ok { + assert.Equal(t, "nginx", *dep.Spec.IngressClassName) + } else { + ingressNoClass := NewQueryIngress(jaegerNoIngressNoClass) + assert.Nil(t, ingressNoClass.Get().Spec.IngressClassName) + } + } } func TestQueryIngressTLSHosts(t *testing.T) { @@ -312,3 +378,12 @@ func TestQueryIngressTLSHosts(t *testing.T) { assert.Equal(t, "test-host-2", dep.Spec.TLS[1].Hosts[0]) assert.Equal(t, "test-host-3", dep.Spec.TLS[1].Hosts[1]) } + +func getDefaultIngressController(m map[string]bool) string { + for key, value := range m { + if value { + return key + } + } + return "" // return an empty string if no key has a true value +}