Skip to content

Commit

Permalink
Set default IngressClass to Jaeger Ingress if not IngressClass was pr…
Browse files Browse the repository at this point in the history
…ovided jaegertracing#2225

Signed-off-by: Israel Blancas <[email protected]>
  • Loading branch information
iblancasa committed Jun 7, 2023
1 parent 76b16a3 commit 3b01996
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
3 changes: 3 additions & 0 deletions apis/v1/jaeger_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const (
// FlagCronJobsVersionBatchV1Beta1 represents the batch/v1beta1 version of the Kubernetes CronJob API, no longer available as of 1.25
FlagCronJobsVersionBatchV1Beta1 = "batch/v1beta1"

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

// FlagAutoscalingVersion represents the version of the Kubernetes Autoscaling API
FlagAutoscalingVersion = "autoscaling-version"

Expand Down
30 changes: 30 additions & 0 deletions pkg/autodetect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/spf13/viper"
appsv1 "k8s.io/api/apps/v1"
authenticationapi "k8s.io/api/authentication/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
Expand Down Expand Up @@ -101,6 +102,7 @@ func (b *Background) autoDetectCapabilities() {
b.detectKafka(ctx, apiList)
b.detectCronjobsVersion(ctx)
b.detectAutoscalingVersion(ctx)
b.detectDefaultIngressClass(ctx)
}

b.detectClusterRoles(ctx)
Expand Down Expand Up @@ -287,6 +289,34 @@ func (b *Background) detectClusterRoles(ctx context.Context) {
}
}

func (b *Background) detectDefaultIngressClass(ctx context.Context) {
if viper.GetString("platform") == v1.FlagPlatformOpenShift {
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")
viper.Set(v1.FlagDefaultIngressClass, "")
}

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) cleanDeployments(ctx context.Context) {
log.Log.V(-1).Info("detecting orphaned deployments.")

Expand Down
57 changes: 56 additions & 1 deletion pkg/autodetect/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -581,6 +582,55 @@ func TestAuthDelegatorBecomesUnavailable(t *testing.T) {
assert.False(t, viper.GetBool("auth-delegator-available"))
}

func TestSkipDefaultIngressClassOpenShift(t *testing.T) {
// prepare
viper.Set("platform", v1.FlagPlatformOpenShift)
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
viper.Set("platform", v1.FlagPlatformKubernetes)
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 TestCleanDeployments(t *testing.T) {
for _, tt := range []struct {
cap string // caption for the test
Expand Down Expand Up @@ -694,17 +744,22 @@ func TestCleanDeployments(t *testing.T) {
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
5 changes: 5 additions & 0 deletions pkg/ingress/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/spf13/viper"

v1 "github.com/jaegertracing/jaeger-operator/apis/v1"
"github.com/jaegertracing/jaeger-operator/pkg/service"
"github.com/jaegertracing/jaeger-operator/pkg/util"
Expand Down Expand Up @@ -50,8 +52,11 @@ func (i *QueryIngress) Get() *networkingv1.Ingress {

i.addTLSSpec(&spec)

clusterDefaultIngressClass := viper.GetString(v1.FlagDefaultIngressClass)
if i.jaeger.Spec.Ingress.IngressClassName != nil {
spec.IngressClassName = i.jaeger.Spec.Ingress.IngressClassName
} else if clusterDefaultIngressClass != "" {
spec.IngressClassName = &clusterDefaultIngressClass
}

return &networkingv1.Ingress{
Expand Down

0 comments on commit 3b01996

Please sign in to comment.