Skip to content

Commit

Permalink
refactor: GetKuadrantFromTopology using Policy Machinery's sort func …
Browse files Browse the repository at this point in the history
…for controller objects

Signed-off-by: Guilherme Cassolato <guicassolato@gmail.com>
  • Loading branch information
guicassolato committed Oct 2, 2024
1 parent 5a2e79f commit bfe0026
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 30 deletions.
4 changes: 2 additions & 2 deletions controllers/ratelimit_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ type effectiveRateLimitPolicyReconciler struct {
func (r *effectiveRateLimitPolicyReconciler) Reconcile(ctx context.Context, _ []controller.ResourceEvent, topology *machinery.Topology, _ error, state *sync.Map) error {
logger := controller.LoggerFromContext(ctx).WithName("effectiveRateLimitPolicyReconciler")

kuadrant, err := GetKuadrantFromTopology(topology) // assumes single kuadrant instance per cluster
kuadrant, err := GetKuadrantFromTopology(topology)
if err != nil {
logger.Error(err, "failed to get kuadrant from topology")
return nil
Expand Down Expand Up @@ -157,7 +157,7 @@ type limitadorLimitsReconciler struct {
func (r *limitadorLimitsReconciler) Reconcile(ctx context.Context, _ []controller.ResourceEvent, topology *machinery.Topology, _ error, state *sync.Map) error {
logger := controller.LoggerFromContext(ctx).WithName("effectiveRateLimitPolicyReconciler").WithName("reconcilerLimitador")

kuadrant, err := GetKuadrantFromTopology(topology) // assumes single kuadrant instance per cluster
kuadrant, err := GetKuadrantFromTopology(topology)
if err != nil {
logger.Error(err, "failed to get kuadrant from topology")
return nil
Expand Down
33 changes: 6 additions & 27 deletions controllers/state_of_the_world.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/utils/env"
"k8s.io/utils/ptr"
ctrlruntime "sigs.k8s.io/controller-runtime"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"

Expand Down Expand Up @@ -276,35 +275,15 @@ func (e *EventLogger) Log(ctx context.Context, resourceEvents []controller.Resou

var MissingKuadrantError = fmt.Errorf("missing kuadrant object in topology")

func GetKuadrantFromTopology(topology *machinery.Topology) (machinery.Object, error) {
kuadrants := lo.FilterMap(topology.Objects().Roots(), func(root machinery.Object, _ int) (SortableObject, bool) {
o, isSortable := root.(SortableObject)
func GetKuadrantFromTopology(topology *machinery.Topology) (*kuadrantv1beta1.Kuadrant, error) {
kuadrants := lo.FilterMap(topology.Objects().Roots(), func(root machinery.Object, _ int) (controller.Object, bool) {
o, isSortable := root.(controller.Object)
return o, isSortable && root.GroupVersionKind().GroupKind() == kuadrantv1beta1.KuadrantKind
})
if len(kuadrants) == 0 {
return nil, MissingKuadrantError
}
sort.Sort(ObjectByCreationTimestamp(kuadrants))
return kuadrants[0], nil
}

// TODO: move to github.com/kuadrant/policy-machinery

type SortableObject interface {
machinery.Object
GetCreationTimestamp() metav1.Time
}

type ObjectByCreationTimestamp []SortableObject

func (a ObjectByCreationTimestamp) Len() int { return len(a) }
func (a ObjectByCreationTimestamp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ObjectByCreationTimestamp) Less(i, j int) bool {
p1Time := ptr.To(a[i].GetCreationTimestamp())
p2Time := ptr.To(a[j].GetCreationTimestamp())
if !p1Time.Equal(p2Time) {
return p1Time.Before(p2Time)
}
// The object appearing first in alphabetical order by "{namespace}/{name}".
return fmt.Sprintf("%s/%s", a[i].GetNamespace(), a[i].GetName()) < fmt.Sprintf("%s/%s", a[j].GetNamespace(), a[j].GetName())
sort.Sort(controller.ObjectsByCreationTimestamp(kuadrants))
kuadrant, _ := kuadrants[0].(*kuadrantv1beta1.Kuadrant)
return kuadrant, nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/kuadrant/authorino-operator v0.11.1
github.com/kuadrant/dns-operator v0.0.0-20240926100317-2e2497411ab3
github.com/kuadrant/limitador-operator v0.9.0
github.com/kuadrant/policy-machinery v0.2.0
github.com/kuadrant/policy-machinery v0.4.0
github.com/martinlindhe/base36 v1.1.1
github.com/onsi/ginkgo/v2 v2.17.2
github.com/onsi/gomega v1.33.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ github.com/kuadrant/limitador-operator v0.9.0 h1:hTQ6CFPayf/sL7cIzwWjCoU8uTn6fzW
github.com/kuadrant/limitador-operator v0.9.0/go.mod h1:DQOlg9qFOcnWPrwO529JRCMLLOEXJQxkmOes952S/Hw=
github.com/kuadrant/policy-machinery v0.2.0 h1:6kACb+bdEwHXz2tvTs6dlLgvxFgFrowvGTZKMI9p0Qo=
github.com/kuadrant/policy-machinery v0.2.0/go.mod h1:ZV4xS0CCxPgu/Xg6gz+YUaS9zqEXKOiAj33bZ67B6Lo=
github.com/kuadrant/policy-machinery v0.4.0 h1:YgmlpG3U4YOrhP5oje+nMJ4GYD+UYYfoDevJ2Tjl6DA=
github.com/kuadrant/policy-machinery v0.4.0/go.mod h1:ZV4xS0CCxPgu/Xg6gz+YUaS9zqEXKOiAj33bZ67B6Lo=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw=
Expand Down

0 comments on commit bfe0026

Please sign in to comment.