Skip to content

Commit

Permalink
Refactor structure
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Fitzpatrick <[email protected]>
  • Loading branch information
Boomatang committed Sep 25, 2024
1 parent 1733359 commit 02ed548
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 104 deletions.
116 changes: 116 additions & 0 deletions controllers/authorino_task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package controllers

import (
"context"
v1beta2 "github.com/kuadrant/authorino-operator/api/v1beta1"
"github.com/kuadrant/kuadrant-operator/api/v1beta1"
"github.com/kuadrant/policy-machinery/controller"
"github.com/kuadrant/policy-machinery/machinery"
"github.com/samber/lo"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/dynamic"
"k8s.io/utils/ptr"
)

type AuthorinoCrReconciler struct {
Client *dynamic.DynamicClient
}

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

func (r *AuthorinoCrReconciler) 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)},
},
}
}

func (r *AuthorinoCrReconciler) Reconcile(ctx context.Context, _ []controller.ResourceEvent, topology *machinery.Topology, _ error) {
logger := controller.LoggerFromContext(ctx).WithName("AuthorinoCrReconciler")
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 {
return item.(*v1beta1.Kuadrant), true
}
return nil, false
})

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

if kobj.GetDeletionTimestamp() != nil {
logger.Info("root kuadrant marked for deletion", "status", "skipping")
return
}

aobjs := lo.FilterMap(topology.Objects().Objects().Items(), func(item machinery.Object, _ int) (machinery.Object, bool) {
if item.GroupVersionKind().Kind == v1beta1.AuthorinoKind.Kind {
return item, true
}
return nil, false
})

if len(aobjs) > 0 {
logger.Info("authorino resource already exists, no need to create", "status", "skipping")
return
}

authorino := &v1beta2.Authorino{
TypeMeta: v1.TypeMeta{
Kind: "Authorino",
APIVersion: "operator.authorino.kuadrant.io/v1beta1",
},
ObjectMeta: v1.ObjectMeta{
Name: "authorino",
Namespace: kobj.Namespace,
OwnerReferences: []v1.OwnerReference{
{
APIVersion: kobj.GroupVersionKind().GroupVersion().String(),
Kind: kobj.GroupVersionKind().Kind,
Name: kobj.Name,
UID: kobj.UID,
BlockOwnerDeletion: ptr.To(true),
Controller: ptr.To(true),
},
},
},
Spec: v1beta2.AuthorinoSpec{
ClusterWide: true,
SupersedingHostSubsets: true,
Listener: v1beta2.Listener{
Tls: v1beta2.Tls{
Enabled: ptr.To(false),
},
},
OIDCServer: v1beta2.OIDCServer{
Tls: v1beta2.Tls{
Enabled: ptr.To(false),
},
},
},
}

unstructuredAuthorino, err := controller.Destruct(authorino)
if err != nil {
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, v1.CreateOptions{})
if err != nil {
if errors.IsAlreadyExists(err) {
logger.Info("already created authorino resource", "status", "acceptable")
} else {
logger.Error(err, "failed to create authorino resource", "status", "error")
}
}
}
104 changes: 0 additions & 104 deletions controllers/state_of_the_world.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ import (
authorinov1beta1 "github.com/kuadrant/authorino-operator/api/v1beta1"
"github.com/kuadrant/policy-machinery/controller"
"github.com/kuadrant/policy-machinery/machinery"
"github.com/samber/lo"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"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 @@ -85,108 +83,6 @@ func buildReconciler(client *dynamic.DynamicClient) controller.ReconcileFunc {
return reconciler.Run
}

type AuthorinoCrReconciler struct {
Client *dynamic.DynamicClient
}

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

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

func (r *AuthorinoCrReconciler) Reconcile(ctx context.Context, _ []controller.ResourceEvent, topology *machinery.Topology, _ error) {
logger := controller.LoggerFromContext(ctx).WithName("AuthorinoCrReconciler")
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) (*kuadrantv1beta1.Kuadrant, bool) {
if item.GroupVersionKind().Kind == kuadrantv1beta1.KuadrantKind.Kind {
return item.(*kuadrantv1beta1.Kuadrant), true
}
return nil, false
})

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

if kobj.GetDeletionTimestamp() != nil {
logger.Info("root kuadrant marked for deletion", "status", "skipping")
return
}

aobjs := lo.FilterMap(topology.Objects().Objects().Items(), func(item machinery.Object, _ int) (machinery.Object, bool) {
if item.GroupVersionKind().Kind == kuadrantv1beta1.AuthorinoKind.Kind {
return item, true
}
return nil, false
})

if len(aobjs) > 0 {
logger.Info("authorino resource already exists, no need to create", "status", "skipping")
return
}

authorino := &authorinov1beta1.Authorino{
TypeMeta: metav1.TypeMeta{
Kind: "Authorino",
APIVersion: "operator.authorino.kuadrant.io/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "authorino",
Namespace: kobj.Namespace,
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: kobj.GroupVersionKind().GroupVersion().String(),
Kind: kobj.GroupVersionKind().Kind,
Name: kobj.Name,
UID: kobj.UID,
BlockOwnerDeletion: ptr.To(true),
Controller: ptr.To(true),
},
},
},
Spec: authorinov1beta1.AuthorinoSpec{
ClusterWide: true,
SupersedingHostSubsets: true,
Listener: authorinov1beta1.Listener{
Tls: authorinov1beta1.Tls{
Enabled: ptr.To(false),
},
},
OIDCServer: authorinov1beta1.OIDCServer{
Tls: authorinov1beta1.Tls{
Enabled: ptr.To(false),
},
},
},
}

unstructuredAuthorino, err := controller.Destruct(authorino)
if err != nil {
logger.Error(err, "failed to destruct authorino", "status", "error")
}
logger.Info("creating authorino resource", "status", "processing")
_, err = r.Client.Resource(kuadrantv1beta1.AuthorinoResource).Namespace(authorino.Namespace).Create(ctx, unstructuredAuthorino, metav1.CreateOptions{})
if err != nil {
if errors.IsAlreadyExists(err) {
logger.Info("already created authorino resource", "status", "acceptable")
} else {
logger.Error(err, "failed to create authorino resource", "status", "error")
}
}
}

type TopologyFileReconciler struct {
Client *dynamic.DynamicClient
Namespace string
Expand Down

0 comments on commit 02ed548

Please sign in to comment.