Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed Nov 20, 2023
1 parent e688387 commit 9742c17
Show file tree
Hide file tree
Showing 11 changed files with 282 additions and 157 deletions.
1 change: 0 additions & 1 deletion api/external/maistra/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions api/external/maistra/v2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/v1beta2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

126 changes: 69 additions & 57 deletions controllers/rate_limiting_wasmplugin_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ package controllers
import (
"context"
"encoding/json"
"fmt"
"sort"

"github.com/go-logr/logr"
istioextensionsv1alpha1 "istio.io/api/extensions/v1alpha1"
istiov1beta1 "istio.io/api/type/v1beta1"
istioclientgoextensionv1alpha1 "istio.io/client-go/pkg/apis/extensions/v1alpha1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/handler"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"

kuadrantv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2"
Expand All @@ -46,8 +45,9 @@ type RateLimitingWASMPluginReconciler struct {
}

//+kubebuilder:rbac:groups=extensions.istio.io,resources=wasmplugins,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways,verbs=get
//+kubebuilder:rbac:groups=kuadrant.io,resources=ratelimitpolicies,verbs=get
//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=gateways,verbs=get;list;watch;update;patch
//+kubebuilder:rbac:groups=gateway.networking.k8s.io,resources=httproutes,verbs=get;list;watch;update;patch
//+kubebuilder:rbac:groups=kuadrant.io,resources=ratelimitpolicies,verbs=get;list;watch;update;patch

// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
Expand All @@ -74,58 +74,18 @@ func (r *RateLimitingWASMPluginReconciler) Reconcile(eventCtx context.Context, r
logger.V(1).Info(string(jsonData))
}

err := r.reconcileRateLimitingWASMPlugin(ctx, gw)

if err != nil {
return ctrl.Result{}, err
}

logger.Info("Rate limiting WASMPlugin reconciled successfully")
return ctrl.Result{}, nil
}

func (r *RateLimitingWASMPluginReconciler) reconcileRateLimitingWASMPlugin(ctx context.Context, gw *gatewayapiv1.Gateway) error {
desired, err := r.desiredRateLimitingWASMPlugin(ctx, gw)
if err != nil {
return err
return ctrl.Result{}, err
}

err = r.ReconcileResource(ctx, &istioclientgoextensionv1alpha1.WasmPlugin{}, desired, rlptools.WASMPluginMutator)
if err != nil {
return err
}

return nil
}

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

routeList := &gatewayapiv1.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 ctrl.Result{}, err
}

return common.NewKuadrantTopology(
[]*gatewayapiv1.Gateway{gw},
common.Map(routeList.Items, func(r gatewayapiv1.HTTPRoute) *gatewayapiv1.HTTPRoute { return &r }),
common.Map(rlpList.Items, func(p kuadrantv1beta2.RateLimitPolicy) common.KuadrantPolicy { return &p }),
), nil
logger.Info("Rate limiting WASMPlugin reconciled successfully")
return ctrl.Result{}, nil
}

func (r *RateLimitingWASMPluginReconciler) desiredRateLimitingWASMPlugin(ctx context.Context, gw *gatewayapiv1.Gateway) (*istioclientgoextensionv1alpha1.WasmPlugin, error) {
Expand All @@ -134,12 +94,13 @@ func (r *RateLimitingWASMPluginReconciler) desiredRateLimitingWASMPlugin(ctx con
Kind: "WasmPlugin",
APIVersion: "extensions.istio.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("kuadrant-%s", gw.Name),
Namespace: gw.Namespace,
},
ObjectMeta: common.RateLimitingWASMPluginName(gw),
Spec: istioextensionsv1alpha1.WasmPlugin{
Selector: common.IstioWorkloadSelectorFromGateway(ctx, r.Client(), gw),
TargetRef: &istiov1beta1.PolicyTargetReference{
Group: "gateway.networking.k8s.io",
Kind: "Gateway",
Name: gw.Name,
},
Url: rlptools.WASMFilterImageURL,
PluginConfig: nil,
// Insert plugin before Istio stats filters and after Istio authorization filters.
Expand Down Expand Up @@ -212,6 +173,36 @@ func (r *RateLimitingWASMPluginReconciler) wasmPluginConfig(ctx context.Context,
return wasmPlugin, nil
}

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

routeList := &gatewayapiv1.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(
[]*gatewayapiv1.Gateway{gw},
common.Map(routeList.Items, func(r gatewayapiv1.HTTPRoute) *gatewayapiv1.HTTPRoute { return &r }),
common.Map(rlpList.Items, func(p kuadrantv1beta2.RateLimitPolicy) common.KuadrantPolicy { return &p }),
), nil
}

func (r *RateLimitingWASMPluginReconciler) WASMRateLimitPolicy(t *common.KuadrantTopology, rlp *kuadrantv1beta2.RateLimitPolicy, gw *gatewayapiv1.Gateway) *wasm.RateLimitPolicy {
gwHostnamesTmp := common.TargetHostnames(gw)
gwHostnames := common.Map(gwHostnamesTmp, func(str string) gatewayapiv1.Hostname { return gatewayapiv1.Hostname(str) })
Expand Down Expand Up @@ -274,11 +265,32 @@ func (r *RateLimitingWASMPluginReconciler) RouteFromRLP(t *common.KuadrantTopolo

// SetupWithManager sets up the controller with the Manager.
func (r *RateLimitingWASMPluginReconciler) SetupWithManager(mgr ctrl.Manager) error {
httpRouteToParentGatewaysEventMapper := &common.HTTPRouteToParentGatewaysEventMapper{
Logger: r.Logger().WithName("httpRouteToParentGatewaysEventMapper"),
}

rlpToParentGatewaysEventMapper := &common.KuadrantPolicyToParentGatewaysEventMapper{
Logger: r.Logger().WithName("ratelimitpolicyToParentGatewaysEventMapper"),
Client: r.Client(),
}

return ctrl.NewControllerManagedBy(mgr).
// Rate limiting WASMPlugin controller only cares about
// the annotation having references to RLP's
// kuadrant.io/ratelimitpolicies
For(&gatewayapiv1.Gateway{}, builder.WithPredicates(predicate.AnnotationChangedPredicate{})).
// Gateway API Gateway
// Gateway API HTTPRoutes
// Kuadrant RateLimitPolicies

// The type of object being *reconciled* is the Gateway.
// TODO(eguzki): consider having the WasmPlugin as the type of object being *reconciled*
For(&gatewayapiv1.Gateway{}).
Owns(&istioclientgoextensionv1alpha1.WasmPlugin{}).
Watches(
&gatewayapiv1.HTTPRoute{},
handler.EnqueueRequestsFromMapFunc(httpRouteToParentGatewaysEventMapper.Map),
).
Watches(
&kuadrantv1beta2.RateLimitPolicy{},
handler.EnqueueRequestsFromMapFunc(rlpToParentGatewaysEventMapper.Map),
).
Complete(r)
}
32 changes: 16 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ require (
golang.org/x/sync v0.4.0
google.golang.org/protobuf v1.31.0
gotest.tools v2.2.0+incompatible
istio.io/api v0.0.0-20230712174848-a2b2de508c88
istio.io/client-go v1.17.4-0.20230712175648-f1263a806483
istio.io/api v1.20.0
istio.io/client-go v1.20.0
istio.io/istio v0.0.0-20230719200611-681b4f65a752
k8s.io/api v0.28.3
k8s.io/apiextensions-apiserver v0.28.3
Expand All @@ -31,14 +31,14 @@ require (
)

require (
cloud.google.com/go v0.110.2 // indirect
cloud.google.com/go/compute v1.20.1 // indirect
cloud.google.com/go v0.110.8 // indirect
cloud.google.com/go/compute v1.23.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/logging v1.7.0 // indirect
cloud.google.com/go/longrunning v0.4.1 // indirect
cloud.google.com/go/logging v1.8.1 // indirect
cloud.google.com/go/longrunning v0.5.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.7.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.7.0 // indirect
Expand All @@ -53,10 +53,10 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20221212185716-aee1124e3a93 // indirect
github.com/google/s2a-go v0.1.4 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.11.0 // indirect
github.com/google/pprof v0.0.0-20230926050212-f7f687d19a98 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.1 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand Down Expand Up @@ -87,12 +87,12 @@ require (
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.14.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/api v0.126.0 // indirect
google.golang.org/api v0.147.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/genproto v0.0.0-20231002182017-d307bd883b97 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231012201019-e917dd12ba7a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231009173412-8bfb1ae86b6c // indirect
google.golang.org/grpc v1.58.3 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
Loading

0 comments on commit 9742c17

Please sign in to comment.