Skip to content

Commit

Permalink
refactor: sotw conventions (#909)
Browse files Browse the repository at this point in the history
* refactor: seperate event logger and topology reconciler into own files

Signed-off-by: KevFan <chfan@redhat.com>

* stow: placeholders for main workflow

Signed-off-by: KevFan <chfan@redhat.com>

* refactor: align with conventions for group kind and resource naming

Signed-off-by: KevFan <chfan@redhat.com>

* sotw: more placeholders and convention adjustments

Signed-off-by: KevFan <chfan@redhat.com>

---------

Signed-off-by: KevFan <chfan@redhat.com>
  • Loading branch information
KevFan authored Oct 4, 2024
1 parent d838ca2 commit 7e0aefb
Show file tree
Hide file tree
Showing 16 changed files with 312 additions and 144 deletions.
4 changes: 2 additions & 2 deletions api/v1alpha1/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

var (
DNSPoliciesResource = GroupVersion.WithResource("dnspolicies")
DNSPolicyKind = schema.GroupKind{Group: GroupVersion.Group, Kind: "DNSPolicy"}
DNSPolicyGroupKind = schema.GroupKind{Group: GroupVersion.Group, Kind: "DNSPolicy"}
TLSPoliciesResource = GroupVersion.WithResource("tlspolicies")
TLSPolicyKind = schema.GroupKind{Group: GroupVersion.Group, Kind: "TLSPolicy"}
TLSPolicyGroupKind = schema.GroupKind{Group: GroupVersion.Group, Kind: "TLSPolicy"}
)

var _ machinery.Policy = &DNSPolicy{}
Expand Down
28 changes: 14 additions & 14 deletions api/v1beta1/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
)

var (
AuthorinoKind = schema.GroupKind{Group: authorinov1beta1.GroupVersion.Group, Kind: "Authorino"}
KuadrantKind = schema.GroupKind{Group: GroupVersion.Group, Kind: "Kuadrant"}
LimitadorKind = schema.GroupKind{Group: limitadorv1alpha1.GroupVersion.Group, Kind: "Limitador"}
AuthorinoGroupKind = schema.GroupKind{Group: authorinov1beta1.GroupVersion.Group, Kind: "Authorino"}
KuadrantGroupKind = schema.GroupKind{Group: GroupVersion.Group, Kind: "Kuadrant"}
LimitadorGroupKind = schema.GroupKind{Group: limitadorv1alpha1.GroupVersion.Group, Kind: "Limitador"}

AuthorinoResource = authorinov1beta1.GroupVersion.WithResource("authorinos")
KuadrantResource = GroupVersion.WithResource("kuadrants")
LimitadorResource = limitadorv1alpha1.GroupVersion.WithResource("limitadors")
AuthorinosResource = authorinov1beta1.GroupVersion.WithResource("authorinos")
KuadrantsResource = GroupVersion.WithResource("kuadrants")
LimitadorsResource = limitadorv1alpha1.GroupVersion.WithResource("limitadors")
)

var _ machinery.Object = &Kuadrant{}
Expand All @@ -27,10 +27,10 @@ func (p *Kuadrant) GetLocator() string {
}

func LinkKuadrantToGatewayClasses(objs controller.Store) machinery.LinkFunc {
kuadrants := lo.Map(objs.FilterByGroupKind(KuadrantKind), controller.ObjectAs[*Kuadrant])
kuadrants := lo.Map(objs.FilterByGroupKind(KuadrantGroupKind), controller.ObjectAs[*Kuadrant])

return machinery.LinkFunc{
From: KuadrantKind,
From: KuadrantGroupKind,
To: schema.GroupKind{Group: gwapiv1.GroupVersion.Group, Kind: "GatewayClass"},
Func: func(_ machinery.Object) []machinery.Object {
parents := make([]machinery.Object, len(kuadrants))
Expand All @@ -43,11 +43,11 @@ func LinkKuadrantToGatewayClasses(objs controller.Store) machinery.LinkFunc {
}

func LinkKuadrantToLimitador(objs controller.Store) machinery.LinkFunc {
kuadrants := lo.Map(objs.FilterByGroupKind(KuadrantKind), controller.ObjectAs[machinery.Object])
kuadrants := lo.Map(objs.FilterByGroupKind(KuadrantGroupKind), controller.ObjectAs[machinery.Object])

return machinery.LinkFunc{
From: KuadrantKind,
To: LimitadorKind,
From: KuadrantGroupKind,
To: LimitadorGroupKind,
Func: func(child machinery.Object) []machinery.Object {
return lo.Filter(kuadrants, func(kuadrant machinery.Object, _ int) bool {
return kuadrant.GetNamespace() == child.GetNamespace() && child.GetName() == "limitador"
Expand All @@ -57,11 +57,11 @@ func LinkKuadrantToLimitador(objs controller.Store) machinery.LinkFunc {
}

func LinkKuadrantToAuthorino(objs controller.Store) machinery.LinkFunc {
kuadrants := lo.Map(objs.FilterByGroupKind(KuadrantKind), controller.ObjectAs[machinery.Object])
kuadrants := lo.Map(objs.FilterByGroupKind(KuadrantGroupKind), controller.ObjectAs[machinery.Object])

return machinery.LinkFunc{
From: KuadrantKind,
To: AuthorinoKind,
From: KuadrantGroupKind,
To: AuthorinoGroupKind,
Func: func(child machinery.Object) []machinery.Object {
return lo.Filter(kuadrants, func(kuadrant machinery.Object, _ int) bool {
return kuadrant.GetNamespace() == child.GetNamespace() && child.GetName() == "authorino"
Expand Down
4 changes: 2 additions & 2 deletions api/v1beta2/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

var (
AuthPoliciesResource = GroupVersion.WithResource("authpolicies")
AuthPolicyKind = schema.GroupKind{Group: GroupVersion.Group, Kind: "AuthPolicy"}
AuthPolicyGroupKind = schema.GroupKind{Group: GroupVersion.Group, Kind: "AuthPolicy"}
RateLimitPoliciesResource = GroupVersion.WithResource("ratelimitpolicies")
RateLimitPolicyKind = schema.GroupKind{Group: GroupVersion.Group, Kind: "RateLimitPolicy"}
RateLimitPolicyGroupKind = schema.GroupKind{Group: GroupVersion.Group, Kind: "RateLimitPolicy"}
)

var _ machinery.Policy = &AuthPolicy{}
Expand Down
7 changes: 7 additions & 0 deletions controllers/auth_workflow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package controllers

import "github.com/kuadrant/policy-machinery/controller"

func NewAuthWorkflow() *controller.Workflow {
return &controller.Workflow{}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@ import (
"github.com/kuadrant/kuadrant-operator/api/v1beta1"
)

type AuthorinoCrReconciler struct {
type AuthorinoReconciler struct {
Client *dynamic.DynamicClient
}

func NewAuthorinoCrReconciler(client *dynamic.DynamicClient) *AuthorinoCrReconciler {
return &AuthorinoCrReconciler{Client: client}
func NewAuthorinoReconciler(client *dynamic.DynamicClient) *AuthorinoReconciler {
return &AuthorinoReconciler{Client: client}
}

func (r *AuthorinoCrReconciler) Subscription() *controller.Subscription {
func (r *AuthorinoReconciler) Subscription() *controller.Subscription {
return &controller.Subscription{
ReconcileFunc: r.Reconcile,
Events: []controller.ResourceEventMatcher{
{Kind: ptr.To(v1beta1.KuadrantKind), EventType: ptr.To(controller.CreateEvent)},
{Kind: ptr.To(v1beta1.AuthorinoKind), EventType: ptr.To(controller.DeleteEvent)},
{Kind: ptr.To(v1beta1.KuadrantGroupKind), EventType: ptr.To(controller.CreateEvent)},
{Kind: ptr.To(v1beta1.AuthorinoGroupKind), EventType: ptr.To(controller.DeleteEvent)},
},
}
}

func (r *AuthorinoCrReconciler) Reconcile(ctx context.Context, _ []controller.ResourceEvent, topology *machinery.Topology, _ error, _ *sync.Map) error {
logger := controller.LoggerFromContext(ctx).WithName("AuthorinoCrReconciler")
func (r *AuthorinoReconciler) Reconcile(ctx context.Context, _ []controller.ResourceEvent, topology *machinery.Topology, _ error, _ *sync.Map) error {
logger := controller.LoggerFromContext(ctx).WithName("AuthorinoReconciler")
logger.Info("reconciling authorino resource", "status", "started")
defer logger.Info("reconciling authorino resource", "status", "completed")

kobjs := lo.FilterMap(topology.Objects().Roots(), func(item machinery.Object, _ int) (*v1beta1.Kuadrant, bool) {
if item.GroupVersionKind().Kind == v1beta1.KuadrantKind.Kind {
if item.GroupVersionKind().Kind == v1beta1.KuadrantGroupKind.Kind {
return item.(*v1beta1.Kuadrant), true
}
return nil, false
Expand All @@ -58,7 +58,7 @@ func (r *AuthorinoCrReconciler) Reconcile(ctx context.Context, _ []controller.Re
}

aobjs := lo.FilterMap(topology.Objects().Objects().Children(kobj), func(item machinery.Object, _ int) (machinery.Object, bool) {
if item.GroupVersionKind().Kind == v1beta1.AuthorinoKind.Kind {
if item.GroupVersionKind().Kind == v1beta1.AuthorinoGroupKind.Kind {
return item, true
}
return nil, false
Expand Down Expand Up @@ -109,7 +109,7 @@ func (r *AuthorinoCrReconciler) Reconcile(ctx context.Context, _ []controller.Re
logger.Error(err, "failed to destruct authorino", "status", "error")
}
logger.Info("creating authorino resource", "status", "processing")
_, err = r.Client.Resource(v1beta1.AuthorinoResource).Namespace(authorino.Namespace).Create(ctx, unstructuredAuthorino, metav1.CreateOptions{})
_, err = r.Client.Resource(v1beta1.AuthorinosResource).Namespace(authorino.Namespace).Create(ctx, unstructuredAuthorino, metav1.CreateOptions{})
if err != nil {
if errors.IsAlreadyExists(err) {
logger.Info("already created authorino resource", "status", "acceptable")
Expand Down
7 changes: 7 additions & 0 deletions controllers/dns_workflow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package controllers

import "github.com/kuadrant/policy-machinery/controller"

func NewDNSWorkflow() *controller.Workflow {
return &controller.Workflow{}
}
18 changes: 18 additions & 0 deletions controllers/envoy_gateway_extensions_janitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package controllers

import (
"github.com/kuadrant/policy-machinery/controller"
"k8s.io/client-go/dynamic"
)

type EnvoyGatewayJanitor struct {
Client *dynamic.DynamicClient
}

func NewEnvoyGatewayJanitor(client *dynamic.DynamicClient) *EnvoyGatewayJanitor {
return &EnvoyGatewayJanitor{Client: client}
}

func (r *EnvoyGatewayJanitor) Subscription() *controller.Subscription {
return &controller.Subscription{}
}
42 changes: 42 additions & 0 deletions controllers/event_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package controllers

import (
"context"
"sync"

"github.com/google/go-cmp/cmp"
"github.com/kuadrant/policy-machinery/controller"
"github.com/kuadrant/policy-machinery/machinery"
)

type EventLogger struct{}

func NewEventLogger() *EventLogger {
return &EventLogger{}
}

func (e *EventLogger) Log(ctx context.Context, resourceEvents []controller.ResourceEvent, _ *machinery.Topology, err error, _ *sync.Map) error {
logger := controller.LoggerFromContext(ctx).WithName("event logger")
for _, event := range resourceEvents {
// log the event
obj := event.OldObject
if obj == nil {
obj = event.NewObject
}
values := []any{
"type", event.EventType.String(),
"kind", obj.GetObjectKind().GroupVersionKind().Kind,
"namespace", obj.GetNamespace(),
"name", obj.GetName(),
}
if event.EventType == controller.UpdateEvent && logger.V(1).Enabled() {
values = append(values, "diff", cmp.Diff(event.OldObject, event.NewObject))
}
logger.Info("new event", values...)
if err != nil {
logger.Error(err, "error passed to reconcile")
}
}

return nil
}
18 changes: 18 additions & 0 deletions controllers/gateway_policy_discoverability_reconciler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package controllers

import (
"github.com/kuadrant/policy-machinery/controller"
"k8s.io/client-go/dynamic"
)

type GatewayPolicyDiscoverabilityReconciler struct {
Client *dynamic.DynamicClient
}

func NewGatewayPolicyDiscoverabilityReconciler(client *dynamic.DynamicClient) *GatewayPolicyDiscoverabilityReconciler {
return &GatewayPolicyDiscoverabilityReconciler{Client: client}
}

func (r *GatewayPolicyDiscoverabilityReconciler) Subscription() *controller.Subscription {
return &controller.Subscription{}
}
18 changes: 18 additions & 0 deletions controllers/httproute_policy_discoverability_reconciler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package controllers

import (
"github.com/kuadrant/policy-machinery/controller"
"k8s.io/client-go/dynamic"
)

type HTTPRoutePolicyDiscoverabilityReconciler struct {
Client *dynamic.DynamicClient
}

func NewHTTPRoutePolicyDiscoverabilityReconciler(client *dynamic.DynamicClient) *HTTPRoutePolicyDiscoverabilityReconciler {
return &HTTPRoutePolicyDiscoverabilityReconciler{Client: client}
}

func (r *HTTPRoutePolicyDiscoverabilityReconciler) Subscription() *controller.Subscription {
return &controller.Subscription{}
}
18 changes: 18 additions & 0 deletions controllers/istio_extensions_janitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package controllers

import (
"github.com/kuadrant/policy-machinery/controller"
"k8s.io/client-go/dynamic"
)

type IstioExtensionsJanitor struct {
Client *dynamic.DynamicClient
}

func NewIstioExtensionsJanitor(client *dynamic.DynamicClient) *IstioExtensionsJanitor {
return &IstioExtensionsJanitor{Client: client}
}

func (r *IstioExtensionsJanitor) Subscription() *controller.Subscription {
return &controller.Subscription{}
}
18 changes: 18 additions & 0 deletions controllers/limitador_reconciler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package controllers

import (
"github.com/kuadrant/policy-machinery/controller"
"k8s.io/client-go/dynamic"
)

type LimitadorReconciler struct {
Client *dynamic.DynamicClient
}

func NewLimitadorReconciler(client *dynamic.DynamicClient) *LimitadorReconciler {
return &LimitadorReconciler{Client: client}
}

func (r *LimitadorReconciler) Subscription() *controller.Subscription {
return &controller.Subscription{}
}
7 changes: 7 additions & 0 deletions controllers/ratelimit_workflow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package controllers

import "github.com/kuadrant/policy-machinery/controller"

func NewRateLimitWorkflow() *controller.Workflow {
return &controller.Workflow{}
}
Loading

0 comments on commit 7e0aefb

Please sign in to comment.