diff --git a/api/v1beta2/ratelimitpolicy_types.go b/api/v1beta2/ratelimitpolicy_types.go index f70c694e1..9d6dfc538 100644 --- a/api/v1beta2/ratelimitpolicy_types.go +++ b/api/v1beta2/ratelimitpolicy_types.go @@ -106,7 +106,7 @@ type Limit struct { } func (l Limit) CountersAsStringList() []string { - var ret []string + ret := make([]string, 0) for idx := range l.Counters { ret = append(ret, string(l.Counters[idx])) } diff --git a/api/v1beta2/route_selectors.go b/api/v1beta2/route_selectors.go index f896a2cdd..cec7936d8 100644 --- a/api/v1beta2/route_selectors.go +++ b/api/v1beta2/route_selectors.go @@ -37,7 +37,8 @@ func (s *RouteSelector) SelectRules(route *gatewayapiv1beta1.HTTPRoute) (rules [ if len(s.Matches) == 0 { return route.Spec.Rules } - for _, routeSelectorMatch := range s.Matches { + for idx := range s.Matches { + routeSelectorMatch := s.Matches[idx] for idx, rule := range route.Spec.Rules { rs := common.HTTPRouteRuleSelector{HTTPRouteMatch: &routeSelectorMatch} if rs.Selects(rule) { diff --git a/controllers/ratelimitpolicy_controller.go b/controllers/ratelimitpolicy_controller.go index b4223e32d..8c94a400a 100644 --- a/controllers/ratelimitpolicy_controller.go +++ b/controllers/ratelimitpolicy_controller.go @@ -95,7 +95,7 @@ func (r *RateLimitPolicyReconciler) Reconcile(eventCtx context.Context, req ctrl if delResErr == nil { delResErr = err } - return r.reconcileStatus(ctx, rlp, nil, delResErr) + return r.reconcileStatus(ctx, rlp, delResErr) } return ctrl.Result{}, err } @@ -132,7 +132,7 @@ func (r *RateLimitPolicyReconciler) Reconcile(eventCtx context.Context, req ctrl specErr := r.reconcileResources(ctx, rlp, targetNetworkObject) // reconcile ratelimitpolicy status - statusResult, statusErr := r.reconcileStatus(ctx, rlp, targetNetworkObject, specErr) + statusResult, statusErr := r.reconcileStatus(ctx, rlp, specErr) if specErr != nil { return ctrl.Result{}, specErr diff --git a/controllers/ratelimitpolicy_controller_test.go b/controllers/ratelimitpolicy_controller_test.go index ef2c7eff9..7c5b155c1 100644 --- a/controllers/ratelimitpolicy_controller_test.go +++ b/controllers/ratelimitpolicy_controller_test.go @@ -222,7 +222,7 @@ var _ = Describe("RateLimitPolicy controller", func() { Expect(err).ToNot(HaveOccurred()) existingWASMConfig, err := rlptools.WASMPluginFromStruct(existingWasmPlugin.Spec.PluginConfig) Expect(err).ToNot(HaveOccurred()) - Expect(existingWASMConfig).To(Equal(&wasm.WASMPlugin{ + Expect(existingWASMConfig).To(Equal(&wasm.Plugin{ FailureMode: wasm.FailureModeDeny, RateLimitPolicies: []wasm.RateLimitPolicy{ { @@ -568,7 +568,7 @@ var _ = Describe("RateLimitPolicy controller", func() { Expect(err).ToNot(HaveOccurred()) existingWASMConfig, err := rlptools.WASMPluginFromStruct(existingWasmPlugin.Spec.PluginConfig) Expect(err).ToNot(HaveOccurred()) - Expect(existingWASMConfig).To(Equal(&wasm.WASMPlugin{ + Expect(existingWASMConfig).To(Equal(&wasm.Plugin{ FailureMode: wasm.FailureModeDeny, RateLimitPolicies: []wasm.RateLimitPolicy{ { diff --git a/controllers/ratelimitpolicy_limits.go b/controllers/ratelimitpolicy_limits.go index 9a5acdf3d..03fc9a73e 100644 --- a/controllers/ratelimitpolicy_limits.go +++ b/controllers/ratelimitpolicy_limits.go @@ -25,7 +25,7 @@ func (r *RateLimitPolicyReconciler) deleteLimits(ctx context.Context, rlp *kuadr if err != nil { return err } - var rlpRefsWithoutRLP []client.ObjectKey + rlpRefsWithoutRLP := make([]client.ObjectKey, 0) for _, rlpRef := range rlpRefs { if rlpRef.Name == rlp.Name && rlpRef.Namespace == rlp.Namespace { continue diff --git a/controllers/ratelimitpolicy_status.go b/controllers/ratelimitpolicy_status.go index ab10a9adf..f57717c76 100644 --- a/controllers/ratelimitpolicy_status.go +++ b/controllers/ratelimitpolicy_status.go @@ -9,7 +9,6 @@ import ( meta "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/reconcile" kuadrantv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2" @@ -20,12 +19,9 @@ const ( RLPAvailableConditionType string = "Available" ) -func (r *RateLimitPolicyReconciler) reconcileStatus(ctx context.Context, rlp *kuadrantv1beta2.RateLimitPolicy, targetNetworkObject client.Object, specErr error) (ctrl.Result, error) { +func (r *RateLimitPolicyReconciler) reconcileStatus(ctx context.Context, rlp *kuadrantv1beta2.RateLimitPolicy, specErr error) (ctrl.Result, error) { logger, _ := logr.FromContext(ctx) - newStatus, err := r.calculateStatus(ctx, rlp, targetNetworkObject, specErr) - if err != nil { - return reconcile.Result{}, err - } + newStatus := r.calculateStatus(ctx, rlp, specErr) equalStatus := rlp.Status.Equals(newStatus, logger) logger.V(1).Info("Status", "status is different", !equalStatus) @@ -59,7 +55,7 @@ func (r *RateLimitPolicyReconciler) reconcileStatus(ctx context.Context, rlp *ku return ctrl.Result{}, nil } -func (r *RateLimitPolicyReconciler) calculateStatus(ctx context.Context, rlp *kuadrantv1beta2.RateLimitPolicy, targetNetworkObject client.Object, specErr error) (*kuadrantv1beta2.RateLimitPolicyStatus, error) { +func (r *RateLimitPolicyReconciler) calculateStatus(_ context.Context, rlp *kuadrantv1beta2.RateLimitPolicy, specErr error) *kuadrantv1beta2.RateLimitPolicyStatus { newStatus := &kuadrantv1beta2.RateLimitPolicyStatus{ // Copy initial conditions. Otherwise, status will always be updated Conditions: common.CopyConditions(rlp.Status.Conditions), @@ -70,7 +66,7 @@ func (r *RateLimitPolicyReconciler) calculateStatus(ctx context.Context, rlp *ku meta.SetStatusCondition(&newStatus.Conditions, *availableCond) - return newStatus, nil + return newStatus } func (r *RateLimitPolicyReconciler) availableCondition(specErr error) *metav1.Condition { diff --git a/controllers/ratelimitpolicy_wasm_plugins.go b/controllers/ratelimitpolicy_wasm_plugins.go index 111abab47..78cb994f3 100644 --- a/controllers/ratelimitpolicy_wasm_plugins.go +++ b/controllers/ratelimitpolicy_wasm_plugins.go @@ -120,7 +120,7 @@ func (r *RateLimitPolicyReconciler) gatewayWASMPlugin(ctx context.Context, gw co } // returns nil when there is no rate limit policy to apply -func (r *RateLimitPolicyReconciler) wasmPluginConfig(ctx context.Context, gw common.GatewayWrapper, rlpRefs []client.ObjectKey) (*wasm.WASMPlugin, error) { +func (r *RateLimitPolicyReconciler) wasmPluginConfig(ctx context.Context, gw common.GatewayWrapper, rlpRefs []client.ObjectKey) (*wasm.Plugin, error) { logger, _ := logr.FromContext(ctx) logger = logger.WithName("wasmPluginConfig").WithValues("gateway", gw.Key()) @@ -170,7 +170,9 @@ func (r *RateLimitPolicyReconciler) wasmPluginConfig(ctx context.Context, gw com // that do not have a rlp of its own, so we can generate wasm rules for those cases if gwRLPKey != "" { rules := make([]gatewayapiv1beta1.HTTPRouteRule, 0) - for _, route := range r.FetchAcceptedGatewayHTTPRoutes(ctx, rlps[gwRLPKey].rlp.TargetKey()) { + routes := r.FetchAcceptedGatewayHTTPRoutes(ctx, rlps[gwRLPKey].rlp.TargetKey()) + for idx := range routes { + route := routes[idx] // skip routes that have a rlp of its own if _, found := routeKeys[client.ObjectKeyFromObject(&route).String()]; found { continue @@ -190,7 +192,7 @@ func (r *RateLimitPolicyReconciler) wasmPluginConfig(ctx context.Context, gw com } } - wasmPlugin := &wasm.WASMPlugin{ + wasmPlugin := &wasm.Plugin{ FailureMode: wasm.FailureModeDeny, RateLimitPolicies: make([]wasm.RateLimitPolicy, 0), } diff --git a/pkg/common/gatewayapi_utils.go b/pkg/common/gatewayapi_utils.go index 3d1cefd20..ccb743336 100644 --- a/pkg/common/gatewayapi_utils.go +++ b/pkg/common/gatewayapi_utils.go @@ -167,7 +167,7 @@ func HTTPPathMatchToString(path *gatewayapiv1beta1.HTTPPathMatch) string { if path.Type != nil { switch *path.Type { case gatewayapiv1beta1.PathMatchExact: - return fmt.Sprintf("%s", *path.Value) + return *path.Value case gatewayapiv1beta1.PathMatchRegularExpression: return fmt.Sprintf("~/%s/", *path.Value) } diff --git a/pkg/reconcilers/targetref_reconciler.go b/pkg/reconcilers/targetref_reconciler.go index 274303a62..a7fdcbde6 100644 --- a/pkg/reconcilers/targetref_reconciler.go +++ b/pkg/reconcilers/targetref_reconciler.go @@ -107,7 +107,8 @@ func (r *TargetRefReconciler) FetchAcceptedGatewayHTTPRoutes(ctx context.Context return } - for _, route := range routeList.Items { + for idx := range routeList.Items { + route := routeList.Items[idx] routeParentStatus, found := common.Find(route.Status.RouteStatus.Parents, func(p gatewayapiv1beta1.RouteParentStatus) bool { return *p.ParentRef.Kind == ("Gateway") && ((p.ParentRef.Namespace == nil && route.GetNamespace() == gwKey.Namespace) || string(*p.ParentRef.Namespace) == gwKey.Namespace) && diff --git a/pkg/rlptools/rate_limit_index.go b/pkg/rlptools/rate_limit_index.go index b475a8260..6c41816e4 100644 --- a/pkg/rlptools/rate_limit_index.go +++ b/pkg/rlptools/rate_limit_index.go @@ -34,9 +34,7 @@ func (l *RateLimitIndex) Set(key RateLimitIndexKey, rateLimits RateLimitList) { func (l *RateLimitIndex) ToRateLimits() RateLimitList { limitadorRateLimits := make(RateLimitList, 0) for rlSet := l.Front(); rlSet != nil; rlSet = rlSet.Next() { - for _, rl := range rlSet.Value { - limitadorRateLimits = append(limitadorRateLimits, rl) - } + limitadorRateLimits = append(limitadorRateLimits, rlSet.Value...) } return limitadorRateLimits } @@ -125,8 +123,8 @@ func Equal(a, b RateLimitList) bool { sort.Strings(bCopy[idx].Variables) } - sort.Sort(RateLimitList(aCopy)) - sort.Sort(RateLimitList(bCopy)) + sort.Sort(aCopy) + sort.Sort(bCopy) return reflect.DeepEqual(aCopy, bCopy) } diff --git a/pkg/rlptools/wasm/types.go b/pkg/rlptools/wasm/types.go index 9355734e2..b53a34745 100644 --- a/pkg/rlptools/wasm/types.go +++ b/pkg/rlptools/wasm/types.go @@ -97,12 +97,12 @@ const ( FailureModeAllow FailureModeType = "allow" ) -type WASMPlugin struct { +type Plugin struct { FailureMode FailureModeType `json:"failureMode"` RateLimitPolicies []RateLimitPolicy `json:"rateLimitPolicies"` } -func (w *WASMPlugin) ToStruct() (*_struct.Struct, error) { +func (w *Plugin) ToStruct() (*_struct.Struct, error) { wasmPluginJSON, err := json.Marshal(w) if err != nil { return nil, err diff --git a/pkg/rlptools/wasm_utils.go b/pkg/rlptools/wasm_utils.go index f231cf560..d1e275645 100644 --- a/pkg/rlptools/wasm_utils.go +++ b/pkg/rlptools/wasm_utils.go @@ -30,8 +30,9 @@ func WasmRules(rlp *kuadrantv1beta2.RateLimitPolicy, route *gatewayapiv1beta1.HT return rules } - for limitName, limit := range rlp.Spec.Limits { + for limitName := range rlp.Spec.Limits { // 1 RLP limit <---> 1 WASM rule + limit := rlp.Spec.Limits[limitName] limitIdentifier := LimitNameToLimitadorIdentifier(limitName) rule, err := ruleFromLimit(limitIdentifier, &limit, route) if err == nil { @@ -45,12 +46,13 @@ func WasmRules(rlp *kuadrantv1beta2.RateLimitPolicy, route *gatewayapiv1beta1.HT func ruleFromLimit(limitIdentifier string, limit *kuadrantv1beta2.Limit, route *gatewayapiv1beta1.HTTPRoute) (wasm.Rule, error) { rule := wasm.Rule{} - if conditions, err := conditionsFromLimit(limit, route); err != nil { + conditions, err := conditionsFromLimit(limit, route) + if err != nil { return rule, err - } else { - rule.Conditions = conditions } + rule.Conditions = conditions + if data := dataFromLimt(limitIdentifier, limit); data != nil { rule.Data = data } @@ -67,7 +69,8 @@ func conditionsFromLimit(limit *kuadrantv1beta2.Limit, route *gatewayapiv1beta1. if len(limit.RouteSelectors) > 0 { // build conditions from the rules selected by the route selectors - for _, routeSelector := range limit.RouteSelectors { + for idx := range limit.RouteSelectors { + routeSelector := limit.RouteSelectors[idx] hostnamesForConditions := hostnamesForConditions(route, &routeSelector) for _, rule := range routeSelector.SelectRules(route) { routeConditions = append(routeConditions, conditionsFromRule(rule, hostnamesForConditions)...) @@ -181,10 +184,9 @@ func patternExpresionsFromMatch(match gatewayapiv1beta1.HTTPRouteMatch) []wasm.P } func patternExpresionFromPathMatch(pathMatch gatewayapiv1beta1.HTTPPathMatch) wasm.PatternExpression { - var ( - operator wasm.PatternOperator = wasm.PatternOperator(kuadrantv1beta2.StartsWithOperator) // default value - value string = "/" // default value + operator = wasm.PatternOperator(kuadrantv1beta2.StartsWithOperator) // default value + value = "/" // default value ) if pathMatch.Value != nil { @@ -222,7 +224,7 @@ func patternExpresionFromHostname(hostname gatewayapiv1beta1.Hostname) wasm.Patt return wasm.PatternExpression{ Selector: "request.host", Operator: wasm.PatternOperator(operator), - Value: string(value), + Value: value, } } @@ -249,7 +251,7 @@ func dataFromLimt(limitIdentifier string, limit *kuadrantv1beta2.Limit) (data [] return data } -func WASMPluginFromStruct(structure *_struct.Struct) (*wasm.WASMPlugin, error) { +func WASMPluginFromStruct(structure *_struct.Struct) (*wasm.Plugin, error) { if structure == nil { return nil, errors.New("cannot desestructure WASMPlugin from nil") } @@ -259,7 +261,7 @@ func WASMPluginFromStruct(structure *_struct.Struct) (*wasm.WASMPlugin, error) { return nil, err } // Deserialize struct into PluginConfig struct - wasmPlugin := &wasm.WASMPlugin{} + wasmPlugin := &wasm.Plugin{} if err := json.Unmarshal(configJSON, wasmPlugin); err != nil { return nil, err }