Skip to content

Commit

Permalink
Refactor SecurityPolicy controller For Kuadrants
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Cattermole <[email protected]>
  • Loading branch information
adam-cattermole committed Jul 30, 2024
1 parent 238acdc commit 888f81d
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 46 deletions.
96 changes: 52 additions & 44 deletions controllers/authpolicy_envoysecuritypolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

egv1alpha1 "github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/go-logr/logr"
kuadrantv1beta1 "github.com/kuadrant/kuadrant-operator/api/v1beta1"
kuadrantv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2"
kuadrantenvoygateway "github.com/kuadrant/kuadrant-operator/pkg/envoygateway"
"github.com/kuadrant/kuadrant-operator/pkg/kuadranttools"
Expand All @@ -15,7 +16,9 @@ import (
"github.com/kuadrant/kuadrant-operator/pkg/library/mappers"
"github.com/kuadrant/kuadrant-operator/pkg/library/reconcilers"
"github.com/kuadrant/kuadrant-operator/pkg/library/utils"
"github.com/samber/lo"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -33,55 +36,35 @@ type AuthPolicyEnvoySecurityPolicyReconciler struct {
//+kubebuilder:rbac:groups=gateway.envoyproxy.io,resources=securitypolicies,verbs=get;list;watch;create;update;patch;delete

func (r *AuthPolicyEnvoySecurityPolicyReconciler) Reconcile(eventCtx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := r.Logger().WithValues("Gateway", req.NamespacedName)
logger := r.Logger().WithValues("Kuadrant", req.NamespacedName)
logger.Info("Reconciling auth SecurityPolicy")
ctx := logr.NewContext(eventCtx, logger)

Check warning on line 41 in controllers/authpolicy_envoysecuritypolicy_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/authpolicy_envoysecuritypolicy_controller.go#L38-L41

Added lines #L38 - L41 were not covered by tests

gw := &gatewayapiv1.Gateway{}
if err := r.Client().Get(ctx, req.NamespacedName, gw); err != nil {
kObj := &kuadrantv1beta1.Kuadrant{}
if err := r.Client().Get(ctx, req.NamespacedName, kObj); err != nil {
if apierrors.IsNotFound(err) {
logger.Info("no gateway found")
logger.Info("no kuadrant object found")
return ctrl.Result{}, nil

Check warning on line 47 in controllers/authpolicy_envoysecuritypolicy_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/authpolicy_envoysecuritypolicy_controller.go#L43-L47

Added lines #L43 - L47 were not covered by tests
}
logger.Error(err, "failed to get gateway")
logger.Error(err, "failed to get kuadrant object")
return ctrl.Result{}, err

Check warning on line 50 in controllers/authpolicy_envoysecuritypolicy_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/authpolicy_envoysecuritypolicy_controller.go#L49-L50

Added lines #L49 - L50 were not covered by tests
}

if logger.V(1).Enabled() {
jsonData, err := json.MarshalIndent(gw, "", " ")
jsonData, err := json.MarshalIndent(kObj, "", " ")
if err != nil {
return ctrl.Result{}, err

Check warning on line 56 in controllers/authpolicy_envoysecuritypolicy_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/authpolicy_envoysecuritypolicy_controller.go#L53-L56

Added lines #L53 - L56 were not covered by tests
}
logger.V(1).Info(string(jsonData))

Check warning on line 58 in controllers/authpolicy_envoysecuritypolicy_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/authpolicy_envoysecuritypolicy_controller.go#L58

Added line #L58 was not covered by tests
}

if !kuadrant.IsKuadrantManaged(gw) {
return ctrl.Result{}, nil
}

topology, err := kuadranttools.TopologyFromGateway(ctx, r.Client(), gw, &kuadrantv1beta2.AuthPolicy{})
topology, err := kuadranttools.TopologyForPolicies(ctx, r.Client(), &kuadrantv1beta2.AuthPolicy{})
if err != nil {
return ctrl.Result{}, err

Check warning on line 63 in controllers/authpolicy_envoysecuritypolicy_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/authpolicy_envoysecuritypolicy_controller.go#L61-L63

Added lines #L61 - L63 were not covered by tests
}

kuadrantNamespace, err := kuadrant.GetKuadrantNamespace(gw)
if err != nil {
logger.Error(err, "failed to get kuadrant namespace")
return ctrl.Result{}, err
}

// reconcile security policies for gateways
for _, gwNode := range topology.Gateways() {
node := gwNode
err := r.reconcileSecurityPolicy(ctx, &node, kuadrantNamespace)
if err != nil {
return ctrl.Result{}, err
}
}
// reconcile security policies for routes
for _, routeNode := range topology.Routes() {
node := routeNode
err := r.reconcileSecurityPolicy(ctx, &node, kuadrantNamespace)
for _, policy := range topology.Policies() {
err := r.reconcileSecurityPolicy(ctx, policy, kObj.Namespace)
if err != nil {
return ctrl.Result{}, err

Check warning on line 69 in controllers/authpolicy_envoysecuritypolicy_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/authpolicy_envoysecuritypolicy_controller.go#L66-L69

Added lines #L66 - L69 were not covered by tests
}
Expand All @@ -90,19 +73,29 @@ func (r *AuthPolicyEnvoySecurityPolicyReconciler) Reconcile(eventCtx context.Con
return ctrl.Result{}, nil

Check warning on line 73 in controllers/authpolicy_envoysecuritypolicy_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/authpolicy_envoysecuritypolicy_controller.go#L73

Added line #L73 was not covered by tests
}

func (r *AuthPolicyEnvoySecurityPolicyReconciler) reconcileSecurityPolicy(ctx context.Context, targetable kuadrantgatewayapi.PolicyTargetNode, kuadrantNamespace string) error {
func (r *AuthPolicyEnvoySecurityPolicyReconciler) reconcileSecurityPolicy(ctx context.Context, policy kuadrantgatewayapi.PolicyNode, kuadrantNamespace string) error {
logger, _ := logr.FromContext(ctx)
logger = logger.WithName("reconcileSecurityPolicy")

Check warning on line 78 in controllers/authpolicy_envoysecuritypolicy_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/authpolicy_envoysecuritypolicy_controller.go#L76-L78

Added lines #L76 - L78 were not covered by tests

esp := envoySecurityPolicy(targetable.GetObject(), kuadrantNamespace)
if len(targetable.AttachedPolicies()) == 0 {
utils.TagObjectToDelete(esp)
targetRef := policy.TargetRef()
if policy.TargetRef() == nil {
return nil

Check warning on line 82 in controllers/authpolicy_envoysecuritypolicy_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/authpolicy_envoysecuritypolicy_controller.go#L80-L82

Added lines #L80 - L82 were not covered by tests
}

if err := r.SetOwnerReference(targetable.GetObject(), esp); err != nil {
esp := envoySecurityPolicy(targetRef.GetObject(), kuadrantNamespace)
if err := r.SetOwnerReference(policy.Policy, esp); err != nil {
return err

Check warning on line 87 in controllers/authpolicy_envoysecuritypolicy_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/authpolicy_envoysecuritypolicy_controller.go#L85-L87

Added lines #L85 - L87 were not covered by tests
}

// if gateway target and not programmed, or route target which is not accepted by any parent
// tag for deletion
if (targetRef.GetGatewayNode() != nil && meta.IsStatusConditionFalse(targetRef.GetGatewayNode().Status.Conditions, string(gatewayapiv1.GatewayConditionProgrammed))) ||
(targetRef.GetRouteNode() != nil && !lo.ContainsBy(targetRef.GetRouteNode().Status.Parents, func(p gatewayapiv1.RouteParentStatus) bool {
return meta.IsStatusConditionTrue(p.Conditions, string(gatewayapiv1.RouteConditionAccepted))
})) {
utils.TagObjectToDelete(esp)

Check warning on line 96 in controllers/authpolicy_envoysecuritypolicy_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/authpolicy_envoysecuritypolicy_controller.go#L92-L96

Added lines #L92 - L96 were not covered by tests
}

if err := r.ReconcileResource(ctx, &egv1alpha1.SecurityPolicy{}, esp, kuadrantenvoygateway.EnvoySecurityPolicyMutator); err != nil && !apierrors.IsAlreadyExists(err) {
logger.Error(err, "failed to reconcile envoy SecurityPolicy resource")
return err

Check warning on line 101 in controllers/authpolicy_envoysecuritypolicy_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/authpolicy_envoysecuritypolicy_controller.go#L99-L101

Added lines #L99 - L101 were not covered by tests
Expand Down Expand Up @@ -177,24 +170,39 @@ func (r *AuthPolicyEnvoySecurityPolicyReconciler) SetupWithManager(mgr ctrl.Mana
return nil

Check warning on line 170 in controllers/authpolicy_envoysecuritypolicy_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/authpolicy_envoysecuritypolicy_controller.go#L168-L170

Added lines #L168 - L170 were not covered by tests
}

httpRouteToParentGatewaysEventMapper := mappers.NewHTTPRouteToParentGatewaysEventMapper(
mappers.WithLogger(r.Logger().WithName("httpRouteToParentGatewaysEventMapper")),
securityPolicyToKuadrantEventMapper := mappers.NewSecurityPolicyToKuadrantEventMapper(
mappers.WithLogger(r.Logger().WithName("securityPolicyToKuadrantEventMapper")),
mappers.WithClient(r.Client()),
)
apToParentGatewaysEventMapper := mappers.NewPolicyToParentGatewaysEventMapper(
mappers.WithLogger(r.Logger().WithName("authpolicyToParentGatewaysEventMapper")),
policyToKuadrantEventMapper := mappers.NewPolicyToKuadrantEventMapper(
mappers.WithLogger(r.Logger().WithName("policyToKuadrantEventMapper")),
mappers.WithClient(r.Client()),
)
gatewayToKuadrantEventMapper := mappers.NewGatewayToKuadrantEventMapper(
mappers.WithLogger(r.Logger().WithName("gatewayToKuadrantEventMapper")),
mappers.WithClient(r.Client()),
)
httpRouteToKuadrantEventMapper := mappers.NewHTTPRouteToKuadrantEventMapper(
mappers.WithLogger(r.Logger().WithName("httpRouteToKuadrantEventMapper")),
mappers.WithClient(r.Client()),
)

return ctrl.NewControllerManagedBy(mgr).
For(&gatewayapiv1.Gateway{}).
Owns(&egv1alpha1.SecurityPolicy{}).
For(&kuadrantv1beta1.Kuadrant{}).
Watches(
&gatewayapiv1.HTTPRoute{},
handler.EnqueueRequestsFromMapFunc(httpRouteToParentGatewaysEventMapper.Map),
&egv1alpha1.SecurityPolicy{},
handler.EnqueueRequestsFromMapFunc(securityPolicyToKuadrantEventMapper.Map),
).
Watches(
&kuadrantv1beta2.AuthPolicy{},
handler.EnqueueRequestsFromMapFunc(apToParentGatewaysEventMapper.Map),
handler.EnqueueRequestsFromMapFunc(policyToKuadrantEventMapper.Map),
).
Watches(
&gatewayapiv1.Gateway{},
handler.EnqueueRequestsFromMapFunc(gatewayToKuadrantEventMapper.Map),
).
Watches(
&gatewayapiv1.HTTPRoute{},
handler.EnqueueRequestsFromMapFunc(httpRouteToKuadrantEventMapper.Map),
).
Complete(r)

Check warning on line 207 in controllers/authpolicy_envoysecuritypolicy_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/authpolicy_envoysecuritypolicy_controller.go#L173-L207

Added lines #L173 - L207 were not covered by tests
}
45 changes: 43 additions & 2 deletions pkg/kuadranttools/topology_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TopologyFromGateway(ctx context.Context, cl client.Client, gw *gatewayapiv1
client.MatchingFields{
fieldindexers.HTTPRouteGatewayParentField: client.ObjectKeyFromObject(gw).String(),
})
logger.V(1).Info("topologyIndexesFromGateway: list httproutes from gateway",
logger.V(1).Info("TopologyFromGateway: list httproutes from gateway",
"gateway", client.ObjectKeyFromObject(gw),
"#HTTPRoutes", len(routeList.Items),
"err", err)
Expand All @@ -37,7 +37,7 @@ func TopologyFromGateway(ctx context.Context, cl client.Client, gw *gatewayapiv1

// Get all the policyKind policies
policies := policyKind.List(ctx, cl, "")
logger.V(1).Info("topologyIndexesFromGateway: list policies",
logger.V(1).Info("TopologyFromGateway: list policies",
"#policies", len(policies),
"err", err)

Expand All @@ -48,3 +48,44 @@ func TopologyFromGateway(ctx context.Context, cl client.Client, gw *gatewayapiv1
kuadrantgatewayapi.WithLogger(logger),
)
}

func TopologyForPolicies(ctx context.Context, cl client.Client, policyKind kuadrantgatewayapi.Policy) (*kuadrantgatewayapi.Topology, error) {
logger, err := logr.FromContext(ctx)
if err != nil {
return nil, err

Check warning on line 55 in pkg/kuadranttools/topology_tools.go

View check run for this annotation

Codecov / codecov/patch

pkg/kuadranttools/topology_tools.go#L52-L55

Added lines #L52 - L55 were not covered by tests
}

gatewayList := &gatewayapiv1.GatewayList{}
err = cl.List(
ctx,
gatewayList)
logger.V(1).Info("TopologyForPolicies: list all gateways",
"#Gateways", len(gatewayList.Items),
"err", err)
if err != nil {
return nil, err

Check warning on line 66 in pkg/kuadranttools/topology_tools.go

View check run for this annotation

Codecov / codecov/patch

pkg/kuadranttools/topology_tools.go#L58-L66

Added lines #L58 - L66 were not covered by tests
}

routeList := &gatewayapiv1.HTTPRouteList{}
err = cl.List(
ctx,
routeList)
logger.V(1).Info("TopologyForPolicies: list all httproutes",
"#HTTPRoutes", len(routeList.Items),
"err", err)
if err != nil {
return nil, err

Check warning on line 77 in pkg/kuadranttools/topology_tools.go

View check run for this annotation

Codecov / codecov/patch

pkg/kuadranttools/topology_tools.go#L69-L77

Added lines #L69 - L77 were not covered by tests
}

policies := policyKind.List(ctx, cl, "")
logger.V(1).Info("TopologyForPolicies: list policies",
"#policies", len(policies),
"err", err)

Check warning on line 83 in pkg/kuadranttools/topology_tools.go

View check run for this annotation

Codecov / codecov/patch

pkg/kuadranttools/topology_tools.go#L80-L83

Added lines #L80 - L83 were not covered by tests

return kuadrantgatewayapi.NewTopology(
kuadrantgatewayapi.WithGateways(utils.Map(gatewayList.Items, ptr.To[gatewayapiv1.Gateway])),
kuadrantgatewayapi.WithRoutes(utils.Map(routeList.Items, ptr.To[gatewayapiv1.HTTPRoute])),
kuadrantgatewayapi.WithPolicies(policies),
kuadrantgatewayapi.WithLogger(logger),
)

Check warning on line 90 in pkg/kuadranttools/topology_tools.go

View check run for this annotation

Codecov / codecov/patch

pkg/kuadranttools/topology_tools.go#L85-L90

Added lines #L85 - L90 were not covered by tests
}
53 changes: 53 additions & 0 deletions pkg/library/mappers/gateway_to_kuadrant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package mappers

import (
"context"
"fmt"

kuadrantv1beta1 "github.com/kuadrant/kuadrant-operator/api/v1beta1"
"github.com/kuadrant/kuadrant-operator/pkg/library/kuadrant"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
)

type GatewayToKuadrantEventMapper struct {
opts MapperOptions
}

func NewGatewayToKuadrantEventMapper(o ...MapperOption) *GatewayToKuadrantEventMapper {
return &GatewayToKuadrantEventMapper{opts: Apply(o...)}

Check warning on line 19 in pkg/library/mappers/gateway_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/gateway_to_kuadrant.go#L18-L19

Added lines #L18 - L19 were not covered by tests
}

func (m *GatewayToKuadrantEventMapper) Map(ctx context.Context, obj client.Object) []reconcile.Request {
logger := m.opts.Logger.WithValues("object", client.ObjectKeyFromObject(obj))

Check warning on line 23 in pkg/library/mappers/gateway_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/gateway_to_kuadrant.go#L22-L23

Added lines #L22 - L23 were not covered by tests

gw, ok := obj.(*gatewayapiv1.Gateway)
if !ok {
logger.Error(fmt.Errorf("%T is not a *gatweayapiv1.Gateway", obj), "cannot map")
return []reconcile.Request{}

Check warning on line 28 in pkg/library/mappers/gateway_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/gateway_to_kuadrant.go#L25-L28

Added lines #L25 - L28 were not covered by tests
}

if !kuadrant.IsKuadrantManaged(gw) {
logger.V(1).Info("gateway is not kuadrant managed", "gateway", gw)
return []reconcile.Request{}

Check warning on line 33 in pkg/library/mappers/gateway_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/gateway_to_kuadrant.go#L31-L33

Added lines #L31 - L33 were not covered by tests
}

kuadrantNamespace, err := kuadrant.GetKuadrantNamespace(gw)
if err != nil {
logger.Error(err, "cannot get kuadrant namespace")
return []reconcile.Request{}

Check warning on line 39 in pkg/library/mappers/gateway_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/gateway_to_kuadrant.go#L36-L39

Added lines #L36 - L39 were not covered by tests
}
kuadrantList := &kuadrantv1beta1.KuadrantList{}
err = m.opts.Client.List(ctx, kuadrantList, &client.ListOptions{Namespace: kuadrantNamespace})
if err != nil {
logger.Error(err, "cannot list kuadrants")
return []reconcile.Request{}

Check warning on line 45 in pkg/library/mappers/gateway_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/gateway_to_kuadrant.go#L41-L45

Added lines #L41 - L45 were not covered by tests
}
if len(kuadrantList.Items) == 0 {
logger.Error(err, "kuadrant does not exist in expected namespace")
return []reconcile.Request{}

Check warning on line 49 in pkg/library/mappers/gateway_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/gateway_to_kuadrant.go#L47-L49

Added lines #L47 - L49 were not covered by tests
}

return []reconcile.Request{{NamespacedName: client.ObjectKeyFromObject(&kuadrantList.Items[0])}}

Check warning on line 52 in pkg/library/mappers/gateway_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/gateway_to_kuadrant.go#L52

Added line #L52 was not covered by tests
}
65 changes: 65 additions & 0 deletions pkg/library/mappers/httproute_to_kuadrant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package mappers

import (
"context"
"fmt"

kuadrantv1beta1 "github.com/kuadrant/kuadrant-operator/api/v1beta1"
kuadrantgatewayapi "github.com/kuadrant/kuadrant-operator/pkg/library/gatewayapi"
"github.com/kuadrant/kuadrant-operator/pkg/library/kuadrant"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
)

type HTTPRouteToKuadrantEventMapper struct {
opts MapperOptions
}

func NewHTTPRouteToKuadrantEventMapper(o ...MapperOption) *HTTPRouteToKuadrantEventMapper {
return &HTTPRouteToKuadrantEventMapper{opts: Apply(o...)}

Check warning on line 20 in pkg/library/mappers/httproute_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/httproute_to_kuadrant.go#L19-L20

Added lines #L19 - L20 were not covered by tests
}

func (m *HTTPRouteToKuadrantEventMapper) Map(ctx context.Context, obj client.Object) []reconcile.Request {
logger := m.opts.Logger.WithValues("object", client.ObjectKeyFromObject(obj))

Check warning on line 24 in pkg/library/mappers/httproute_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/httproute_to_kuadrant.go#L23-L24

Added lines #L23 - L24 were not covered by tests

httpRoute, ok := obj.(*gatewayapiv1.HTTPRoute)
if !ok {
logger.Error(fmt.Errorf("%T is not a *gatweayapiv1.HTTPRoute", obj), "cannot map")
return []reconcile.Request{}

Check warning on line 29 in pkg/library/mappers/httproute_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/httproute_to_kuadrant.go#L26-L29

Added lines #L26 - L29 were not covered by tests
}

gatewayKeys := kuadrantgatewayapi.GetRouteAcceptedGatewayParentKeys(httpRoute)

Check warning on line 32 in pkg/library/mappers/httproute_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/httproute_to_kuadrant.go#L32

Added line #L32 was not covered by tests

for _, gatewayKey := range gatewayKeys {
gateway := &gatewayapiv1.Gateway{}
err := m.opts.Client.Get(ctx, gatewayKey, gateway)
if err != nil {
logger.Info("cannot get gateway", "error", err)
continue

Check warning on line 39 in pkg/library/mappers/httproute_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/httproute_to_kuadrant.go#L34-L39

Added lines #L34 - L39 were not covered by tests
}

if !kuadrant.IsKuadrantManaged(gateway) {
logger.V(1).Info("gateway is not kuadrant managed", "gateway", gateway)
continue

Check warning on line 44 in pkg/library/mappers/httproute_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/httproute_to_kuadrant.go#L42-L44

Added lines #L42 - L44 were not covered by tests
}
kuadrantNamespace, err := kuadrant.GetKuadrantNamespace(gateway)
if err != nil {
logger.Error(err, "cannot get kuadrant namespace")
continue

Check warning on line 49 in pkg/library/mappers/httproute_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/httproute_to_kuadrant.go#L46-L49

Added lines #L46 - L49 were not covered by tests
}
kuadrantList := &kuadrantv1beta1.KuadrantList{}
err = m.opts.Client.List(ctx, kuadrantList, &client.ListOptions{Namespace: kuadrantNamespace})
if err != nil {
logger.Error(err, "cannot list kuadrants")
return []reconcile.Request{}

Check warning on line 55 in pkg/library/mappers/httproute_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/httproute_to_kuadrant.go#L51-L55

Added lines #L51 - L55 were not covered by tests
}
if len(kuadrantList.Items) == 0 {
logger.Error(err, "kuadrant does not exist in expected namespace")
return []reconcile.Request{}

Check warning on line 59 in pkg/library/mappers/httproute_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/httproute_to_kuadrant.go#L57-L59

Added lines #L57 - L59 were not covered by tests
}
return []reconcile.Request{{NamespacedName: client.ObjectKeyFromObject(&kuadrantList.Items[0])}}

Check warning on line 61 in pkg/library/mappers/httproute_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/httproute_to_kuadrant.go#L61

Added line #L61 was not covered by tests
}
logger.V(1).Info("no matching kuadrant instance found")
return []reconcile.Request{}

Check warning on line 64 in pkg/library/mappers/httproute_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/httproute_to_kuadrant.go#L63-L64

Added lines #L63 - L64 were not covered by tests
}
47 changes: 47 additions & 0 deletions pkg/library/mappers/policy_to_kuadrant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package mappers

import (
"context"
"fmt"

kuadrantv1beta1 "github.com/kuadrant/kuadrant-operator/api/v1beta1"
"github.com/kuadrant/kuadrant-operator/pkg/library/kuadrant"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

type PolicyToKuadrantEventMapper struct {
opts MapperOptions
}

func NewPolicyToKuadrantEventMapper(o ...MapperOption) *PolicyToKuadrantEventMapper {
return &PolicyToKuadrantEventMapper{opts: Apply(o...)}

Check warning on line 18 in pkg/library/mappers/policy_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/policy_to_kuadrant.go#L17-L18

Added lines #L17 - L18 were not covered by tests
}

func (m *PolicyToKuadrantEventMapper) Map(ctx context.Context, obj client.Object) []reconcile.Request {
logger := m.opts.Logger.WithValues("object", client.ObjectKeyFromObject(obj))

Check warning on line 22 in pkg/library/mappers/policy_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/policy_to_kuadrant.go#L21-L22

Added lines #L21 - L22 were not covered by tests

policy, ok := obj.(kuadrant.Policy)
if !ok {
logger.Error(fmt.Errorf("%T is not a kuadrant.Policy", obj), "cannot map")
return []reconcile.Request{}

Check warning on line 27 in pkg/library/mappers/policy_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/policy_to_kuadrant.go#L24-L27

Added lines #L24 - L27 were not covered by tests
}

kuadrantNamespace, err := kuadrant.GetKuadrantNamespaceFromPolicyTargetRef(ctx, m.opts.Client, policy)
if err != nil {
logger.Error(err, "cannot get kuadrant namespace")
return []reconcile.Request{}

Check warning on line 33 in pkg/library/mappers/policy_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/policy_to_kuadrant.go#L30-L33

Added lines #L30 - L33 were not covered by tests
}
kuadrantList := &kuadrantv1beta1.KuadrantList{}
err = m.opts.Client.List(ctx, kuadrantList, &client.ListOptions{Namespace: kuadrantNamespace})
if err != nil {
logger.Error(err, "cannot list kuadrants")
return []reconcile.Request{}

Check warning on line 39 in pkg/library/mappers/policy_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/policy_to_kuadrant.go#L35-L39

Added lines #L35 - L39 were not covered by tests
}
if len(kuadrantList.Items) == 0 {
logger.Error(err, "kuadrant does not exist in expected namespace")
return []reconcile.Request{}

Check warning on line 43 in pkg/library/mappers/policy_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/policy_to_kuadrant.go#L41-L43

Added lines #L41 - L43 were not covered by tests
}

return []reconcile.Request{{NamespacedName: client.ObjectKeyFromObject(&kuadrantList.Items[0])}}

Check warning on line 46 in pkg/library/mappers/policy_to_kuadrant.go

View check run for this annotation

Codecov / codecov/patch

pkg/library/mappers/policy_to_kuadrant.go#L46

Added line #L46 was not covered by tests
}

0 comments on commit 888f81d

Please sign in to comment.