Skip to content

Commit

Permalink
fixes #1568 #2225 (#2466)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>

* 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 <[email protected]>

* changed func name from getInClusterAvailableIngressClasses() to
getInClusterAvailableIngressClass() and updated manifests

Signed-off-by: Gaurav Singh <[email protected]>

* fixed formatting

Signed-off-by: Gaurav Singh <[email protected]>

---------

Signed-off-by: Gaurav Singh <[email protected]>
Co-authored-by: Gaurav Singh <[email protected]>
  • Loading branch information
antoniomerlin and Gaurav Singh authored Mar 11, 2024
1 parent 2583d2a commit c8b3e73
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ spec:
- apiGroups:
- networking.k8s.io
resources:
- ingressclasses
- ingresses
verbs:
- create
Expand Down
1 change: 1 addition & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ rules:
- apiGroups:
- networking.k8s.io
resources:
- ingressclasses
- ingresses
verbs:
- create
Expand Down
2 changes: 1 addition & 1 deletion controllers/jaegertracing/jaeger_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions pkg/ingress/query.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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")
}
75 changes: 75 additions & 0 deletions pkg/ingress/query_test.go
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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) {
Expand All @@ -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
}

0 comments on commit c8b3e73

Please sign in to comment.