Skip to content

Commit

Permalink
Improve the getting of the kuadrant CR.
Browse files Browse the repository at this point in the history
Get the oldest kuadrant CR and use that as the only CR.

Signed-off-by: Jim Fitzpatrick <[email protected]>
  • Loading branch information
Boomatang committed Sep 25, 2024
1 parent f82f1fa commit 1733359
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 7 deletions.
37 changes: 30 additions & 7 deletions controllers/state_of_the_world.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,11 @@ func (r *AuthorinoCrReconciler) Reconcile(ctx context.Context, _ []controller.Re
}
return nil, false
})
if len(kobjs) == 0 {
logger.Info("no kuadrant resources found", "status", "skipping")
return
}
if len(kobjs) > 1 {
logger.Error(fmt.Errorf("multiple Kuadrant resources found"), "cannot select root Kuadrant resource", "status", "error")

kobj, err := GetOldestKuadrant(kobjs)
if err != nil {
logger.Error(err, "cannot find Kuadrant resource", "status", "error")
}
kobj := kobjs[0]

if kobj.GetDeletionTimestamp() != nil {
logger.Info("root kuadrant marked for deletion", "status", "skipping")
Expand Down Expand Up @@ -280,3 +277,29 @@ func (e *EventLogger) Log(ctx context.Context, resourceEvents []controller.Resou
}
}
}

func GetOldestKuadrant(kuadrants []*kuadrantv1beta1.Kuadrant) (*kuadrantv1beta1.Kuadrant, error) {
if len(kuadrants) == 1 {
return kuadrants[0], nil
}
if len(kuadrants) == 0 {
return nil, fmt.Errorf("empty list passed")
}
oldest := kuadrants[0]
for _, k := range kuadrants[1:] {
if k == nil || k.DeletionTimestamp != nil {
continue
}
if oldest == nil {
oldest = k
continue
}
if k.CreationTimestamp.Before(&oldest.CreationTimestamp) {
oldest = k
}
}
if oldest == nil {
return nil, fmt.Errorf("only nil pointers in list")
}
return oldest, nil
}
178 changes: 178 additions & 0 deletions controllers/state_of_the_world_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package controllers

import (
"reflect"
"testing"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

kuadrantv1beta1 "github.com/kuadrant/kuadrant-operator/api/v1beta1"
)

func TestGetOldest(t *testing.T) {
type args struct {
kuadrants []*kuadrantv1beta1.Kuadrant
}
tests := []struct {
name string
args args
want *kuadrantv1beta1.Kuadrant
wantErr bool
}{
{
name: "oldest is first",
args: args{
kuadrants: []*kuadrantv1beta1.Kuadrant{
{
ObjectMeta: metav1.ObjectMeta{
Name: "Expected",
CreationTimestamp: metav1.Time{
Time: time.Unix(1, 0),
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "UnExpected",
CreationTimestamp: metav1.Time{
Time: time.Unix(2, 0),
},
},
},
},
},
want: &kuadrantv1beta1.Kuadrant{
ObjectMeta: metav1.ObjectMeta{
Name: "Expected",
CreationTimestamp: metav1.Time{
Time: time.Unix(1, 0),
},
},
},
wantErr: false,
},
{
name: "oldest is second",
args: args{
kuadrants: []*kuadrantv1beta1.Kuadrant{
{
ObjectMeta: metav1.ObjectMeta{
Name: "UnExpected",
CreationTimestamp: metav1.Time{
Time: time.Unix(2, 0),
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "Expected",
CreationTimestamp: metav1.Time{
Time: time.Unix(1, 0),
},
},
},
},
},
want: &kuadrantv1beta1.Kuadrant{
ObjectMeta: metav1.ObjectMeta{
Name: "Expected",
CreationTimestamp: metav1.Time{
Time: time.Unix(1, 0),
},
},
},
wantErr: false,
},
{
name: "Empty list is passed",
args: args{kuadrants: []*kuadrantv1beta1.Kuadrant{}},
want: nil,
wantErr: true,
},
{
name: "List contains nil pointer",
args: args{
kuadrants: []*kuadrantv1beta1.Kuadrant{
{
ObjectMeta: metav1.ObjectMeta{
Name: "UnExpected",
CreationTimestamp: metav1.Time{
Time: time.Unix(2, 0),
},
},
},
nil,
{
ObjectMeta: metav1.ObjectMeta{
Name: "Expected",
CreationTimestamp: metav1.Time{
Time: time.Unix(1, 0),
},
},
},
},
},
want: &kuadrantv1beta1.Kuadrant{
ObjectMeta: metav1.ObjectMeta{
Name: "Expected",
CreationTimestamp: metav1.Time{
Time: time.Unix(1, 0),
},
},
},
wantErr: false,
},
{
name: "first object is nil pointer",
args: args{
kuadrants: []*kuadrantv1beta1.Kuadrant{
nil,
{
ObjectMeta: metav1.ObjectMeta{
Name: "UnExpected",
CreationTimestamp: metav1.Time{
Time: time.Unix(2, 0),
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "Expected",
CreationTimestamp: metav1.Time{
Time: time.Unix(1, 0),
},
},
},
},
},
want: &kuadrantv1beta1.Kuadrant{
ObjectMeta: metav1.ObjectMeta{
Name: "Expected",
CreationTimestamp: metav1.Time{
Time: time.Unix(1, 0),
},
},
},
wantErr: false,
},
{
name: "List contains all nil pointer",
args: args{[]*kuadrantv1beta1.Kuadrant{nil, nil, nil}},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetOldestKuadrant(tt.args.kuadrants)
if (err != nil) != tt.wantErr {
t.Errorf("GetOldestKuadrant() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetOldestKuadrant() got = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 1733359

Please sign in to comment.