Skip to content

Commit

Permalink
Fix the CI (#2505)
Browse files Browse the repository at this point in the history
* Fix the CI. Change the implementation of the method to make it easy to test

Signed-off-by: Israel Blancas <[email protected]>

* Fix E2E

Signed-off-by: Israel Blancas <[email protected]>

* Disable test #2508

Signed-off-by: Israel Blancas <[email protected]>

---------

Signed-off-by: Israel Blancas <[email protected]>
  • Loading branch information
iblancasa authored Mar 16, 2024
1 parent 712e04c commit 6d35c98
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 172 deletions.
3 changes: 3 additions & 0 deletions apis/v1/jaeger_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ const (
// IngressSecurityNone disables any form of security for ingress objects (default)
IngressSecurityNone IngressSecurityType = ""

// FlagDefaultIngressClass represents the default Ingress class from the cluster
FlagDefaultIngressClass = "default-ingressclass"

// IngressSecurityNoneExplicit used when the user specifically set it to 'none'
IngressSecurityNoneExplicit IngressSecurityType = "none"

Expand Down
29 changes: 29 additions & 0 deletions pkg/autodetect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/spf13/viper"
"go.opentelemetry.io/otel"
authenticationapi "k8s.io/api/authentication/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/discovery"
Expand Down Expand Up @@ -100,6 +101,7 @@ func (b *Background) autoDetectCapabilities() {
// the version of the APIs provided by the platform will not change
b.detectCronjobsVersion(ctx)
b.detectAutoscalingVersion(ctx)
b.detectDefaultIngressClass(ctx)
})
b.detectOAuthProxyImageStream(ctx)
b.detectElasticsearch(ctx, apiList)
Expand Down Expand Up @@ -193,6 +195,33 @@ func AvailableAPIs(discovery discovery.DiscoveryInterface, groups map[string]boo
return apiLists, errors
}

func (b *Background) detectDefaultIngressClass(ctx context.Context) {
if OperatorConfiguration.GetPlatform() == OpenShiftPlatform {
return
}

ingressClasses := networkingv1.IngressClassList{}
err := b.cl.List(ctx, &ingressClasses)
if err != nil {
log.Log.Info("It was not possible to get any IngressClasses from the Kubernetes cluster")
}

oldValue := viper.GetString(v1.FlagDefaultIngressClass)

for _, ingressClass := range ingressClasses.Items {
val, ok := ingressClass.Annotations["ingressclass.kubernetes.io/is-default-class"]
if ok {
if val == "true" {
if oldValue != ingressClass.Name {
log.Log.Info("New default IngressClass value found", "old", oldValue, "new", ingressClass.Name)
}
viper.Set(v1.FlagDefaultIngressClass, ingressClass.Name)
return
}
}
}
}

func (b *Background) detectPlatform(ctx context.Context, apiList []*metav1.APIResourceList) {
// detect the platform, we run this only once, as the platform can't change between runs ;)
platform := OperatorConfiguration.GetPlatform()
Expand Down
87 changes: 86 additions & 1 deletion pkg/autodetect/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
openapi_v2 "github.com/google/gnostic-models/openapiv2"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/discovery"
Expand Down Expand Up @@ -612,20 +613,104 @@ func TestAuthDelegatorBecomesUnavailable(t *testing.T) {
assert.False(t, OperatorConfiguration.IsAuthDelegatorAvailable())
}

func TestSkipDefaultIngressClassOpenShift(t *testing.T) {
// prepare
OperatorConfiguration.SetPlatform(OpenShiftPlatform)
defer viper.Reset()

dcl := &fakeDiscoveryClient{}
cl := customFakeClient()
b := WithClients(cl, dcl, cl)

// test
b.detectDefaultIngressClass(context.Background())

// verify
assert.Equal(t, "", viper.GetString(v1.FlagDefaultIngressClass))
}

func TestDetectDefaultIngressClass(t *testing.T) {
// prepare
OperatorConfiguration.SetPlatform(KubernetesPlatform)
defer viper.Reset()

dcl := &fakeDiscoveryClient{}
cl := customFakeClient()

cl.ListFunc = func(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
if listPointer, ok := list.(*networkingv1.IngressClassList); ok {
listPointer.Items = []networkingv1.IngressClass{
{
ObjectMeta: metav1.ObjectMeta{
Name: "nginx",
Annotations: map[string]string{
"ingressclass.kubernetes.io/is-default-class": "true",
},
},
},
}
}

return nil
}
b := WithClients(cl, dcl, cl)

// test
b.detectDefaultIngressClass(context.Background())

// verify
assert.Equal(t, "nginx", viper.GetString(v1.FlagDefaultIngressClass))
}

func TestDetectNoDefaultIngressClass(t *testing.T) {
// prepare
OperatorConfiguration.SetPlatform(KubernetesPlatform)
defer viper.Reset()

dcl := &fakeDiscoveryClient{}
cl := customFakeClient()

cl.ListFunc = func(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
if listPointer, ok := list.(*networkingv1.IngressClassList); ok {
listPointer.Items = []networkingv1.IngressClass{
{
ObjectMeta: metav1.ObjectMeta{
Name: "nginx",
},
},
}
}

return nil
}
b := WithClients(cl, dcl, cl)

// test
b.detectDefaultIngressClass(context.Background())

// verify
assert.Equal(t, "", viper.GetString(v1.FlagDefaultIngressClass))
}

type fakeClient struct {
client.Client
CreateFunc func(ctx context.Context, obj client.Object, opts ...client.CreateOption) error
ListFunc func(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error
}

func customFakeClient() *fakeClient {
c := fake.NewClientBuilder().Build()
return &fakeClient{Client: c, CreateFunc: c.Create}
return &fakeClient{Client: c, CreateFunc: c.Create, ListFunc: c.List}
}

func (f *fakeClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
return f.CreateFunc(ctx, obj)
}

func (f *fakeClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
return f.ListFunc(ctx, list)
}

type fakeDiscoveryClient struct {
discovery.DiscoveryInterface
ServerGroupsFunc func() (apiGroupList *metav1.APIGroupList, err error)
Expand Down
48 changes: 8 additions & 40 deletions pkg/ingress/query.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
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"

"github.com/spf13/viper"

v1 "github.com/jaegertracing/jaeger-operator/apis/v1"
"github.com/jaegertracing/jaeger-operator/pkg/service"
Expand Down Expand Up @@ -56,10 +55,7 @@ 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
}
spec.IngressClassName = getInClusterAvailableIngressClass()
}

return &networkingv1.Ingress{
Expand Down Expand Up @@ -159,38 +155,10 @@ 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
func getInClusterAvailableIngressClass() *string {
ingressClassName := viper.GetString(v1.FlagDefaultIngressClass)
if ingressClassName == "" {
return nil
}
return "", fmt.Errorf("no available ingress controller found")
return &ingressClassName
}
82 changes: 4 additions & 78 deletions pkg/ingress/query_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
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 @@ -282,8 +277,8 @@ func TestQueryIngressClass(t *testing.T) {
jaeger := v1.NewJaeger(types.NamespacedName{Name: "TestQueryIngressClass"})
jaegerNoIngressNoClass := v1.NewJaeger(types.NamespacedName{Name: "TestQueryIngressNoClass"})

inressClassName := "nginx"
jaeger.Spec.Ingress.IngressClassName = &inressClassName
ingressClassName := "nginx"
jaeger.Spec.Ingress.IngressClassName = &ingressClassName

ingress := NewQueryIngress(jaeger)
ingressNoClass := NewQueryIngress(jaegerNoIngressNoClass)
Expand All @@ -292,68 +287,8 @@ 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)
}
}
r := ingressNoClass.Get().Spec.IngressClassName
assert.Nil(t, r)
}

func TestQueryIngressTLSHosts(t *testing.T) {
Expand All @@ -378,12 +313,3 @@ 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
}
1 change: 0 additions & 1 deletion tests/assert-jobs/query/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ func main() {
logrus.Debug(resp.Data)
return false, nil
})

if err != nil {
logrus.Fatalln("Error querying the Jaeger instance: ", err)
}
Expand Down
Loading

0 comments on commit 6d35c98

Please sign in to comment.