Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed Oct 6, 2023
1 parent fd02034 commit 310a675
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 13 deletions.
82 changes: 69 additions & 13 deletions controllers/rate_limiting_wasmplugin_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,37 @@ func (r *RateLimitingWASMPluginReconciler) reconcileRateLimitingWASMPlugin(ctx c
return nil
}

func (r *RateLimitingWASMPluginReconciler) desiredRateLimitingWASMPlugin(ctx context.Context, gw *gatewayapiv1beta1.Gateway) (*istioclientgoextensionv1alpha1.WasmPlugin, error) {
func (r *RateLimitingWASMPluginReconciler) gatewayAPITopologyFromGateway(ctx context.Context, gw *gatewayapiv1beta1.Gateway) (*common.KuadrantTopology, error) {
logger, err := logr.FromContext(ctx)
if err != nil {
return nil, err
}

routeList := &gatewayapiv1beta1.HTTPRouteList{}
// Get all the routes having the gateway as parent
err = r.Client().List(ctx, routeList, client.MatchingFields{common.HTTPRouteParents: client.ObjectKeyFromObject(gw).String()})
logger.V(1).Info("gatewayAPITopologyFromGateway: list httproutes from gateway", "err", err)
if err != nil {
return nil, err
}

rlpList := &kuadrantv1beta2.RateLimitPolicyList{}
// Get all the rate limit policies
// TODO(eastizle): Add index field??
err = r.Client().List(ctx, rlpList)
logger.V(1).Info("gatewayAPITopologyFromGateway: list rate limit policies", "err", err)
if err != nil {
return nil, err
}

return common.NewKuadrantTopology(
[]*gatewayapiv1beta1.Gateway{gw},
common.Map(routeList.Items, func(r gatewayapiv1beta1.HTTPRoute) *gatewayapiv1beta1.HTTPRoute { return &r }),
common.Map(rlpList.Items, func(p kuadrantv1beta2.RateLimitPolicy) common.KuadrantPolicy { return &p }),
), nil
}

func (r *RateLimitingWASMPluginReconciler) desiredRateLimitingWASMPlugin(ctx context.Context, gw *gatewayapiv1beta1.Gateway) (*istioclientgoextensionv1alpha1.WasmPlugin, error) {
wasmPlugin := &istioclientgoextensionv1alpha1.WasmPlugin{
TypeMeta: metav1.TypeMeta{
Kind: "WasmPlugin",
Expand All @@ -121,21 +146,12 @@ func (r *RateLimitingWASMPluginReconciler) desiredRateLimitingWASMPlugin(ctx con
},
}

gateway := common.GatewayWrapper{Gateway: gw, PolicyRefsConfig: &common.KuadrantRateLimitPolicyRefsConfig{}}
rlpRefs := gateway.PolicyRefs()
logger.V(1).Info("desiredRateLimitingClusterEnvoyFilter", "rlpRefs", rlpRefs)

if len(rlpRefs) < 1 {
common.TagObjectToDelete(wasmPlugin)
return wasmPlugin, nil
}

pluginConfig, err := r.wasmPluginConfig(ctx, gateway, rlpRefs)
pluginConfig, err := r.wasmPluginConfig(ctx, gw)
if err != nil {
return nil, err
}

if pluginConfig == nil {
if pluginConfig == nil || len(pluginConfig.RateLimitPolicies) == 0 {
common.TagObjectToDelete(wasmPlugin)
return wasmPlugin, nil
}
Expand All @@ -155,7 +171,47 @@ func (r *RateLimitingWASMPluginReconciler) desiredRateLimitingWASMPlugin(ctx con
return wasmPlugin, nil
}

func (r *RateLimitingWASMPluginReconciler) wasmPluginConfig(ctx context.Context, gw common.GatewayWrapper, rlpRefs []client.ObjectKey) (*wasm.Plugin, error) {
func (r *RateLimitingWASMPluginReconciler) wasmPluginConfig(ctx context.Context, gw *gatewayapiv1beta1.Gateway) (*wasm.Plugin, error) {
logger, err := logr.FromContext(ctx)
if err != nil {
return nil, err
}

wasmPlugin := &wasm.Plugin{
FailureMode: wasm.FailureModeDeny,
RateLimitPolicies: make([]wasm.RateLimitPolicy, 0),
}

gatewayAPITopology, err := r.gatewayAPITopologyFromGateway(ctx, gw)
if err != nil {
return nil, err
}

rateLimitPolicies := gatewayAPITopology.GatewayPolicies()

logger.V(1).Info("wasmPluginConfig", "#RLPS", len(rateLimitPolicies))

// TODO(eastizle): Sort RLPs by name for consistent comparison with existing objects??

for _, rlp := range rateLimitPolicies {
route := gatewayAPITopology.GetPolicyHTTPRoute(rlp)

// For gateway targeted policies, if no httproutes attached to the gateway, skip wasm config
// test wasm config when no http routes attached to the gateway
//logger.V(1).Info("no httproutes attached to the targeted gateway, skipping wasm config for the gateway rlp", "ratelimitpolicy", gwRLPKey)

if route == nil {
// policy targeting a gateway
// All the routes not having a RLP targeting it will be targeted by the
// Build imaginary route with all the routes not having a RLP targeting it
}
}

return wasmPlugin, nil
}

// SHOULD BE DELETED
func (r *RateLimitingWASMPluginReconciler) wasmPluginConfigTOBEDELETED(ctx context.Context, gw common.GatewayWrapper, rlpRefs []client.ObjectKey) (*wasm.Plugin, error) {
logger, _ := logr.FromContext(ctx)
logger = logger.WithName("wasmPluginConfig").WithValues("gateway", gw.Key())

Expand Down
23 changes: 23 additions & 0 deletions pkg/common/kuadrant_topology.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package common

import (
gatewayapiv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)

type KuadrantTopology struct {
}

func NewKuadrantTopology(gateways []*gatewayapiv1beta1.Gateway, routes []*gatewayapiv1beta1.HTTPRoute, policy []KuadrantPolicy) *KuadrantTopology {
// TODO
return &KuadrantTopology{}
}

func (k *KuadrantTopology) GatewayPolicies() []KuadrantPolicy {
// TODO
return nil
}

func (k *KuadrantTopology) GetPolicyHTTPRoute(policy KuadrantPolicy) *gatewayapiv1beta1.HTTPRoute {
// TODO
return nil
}

0 comments on commit 310a675

Please sign in to comment.