Skip to content

Commit

Permalink
fix: lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
KevFan committed Aug 16, 2023
1 parent 70bf8cb commit 4bf4198
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 38 deletions.
2 changes: 1 addition & 1 deletion api/v1beta2/ratelimitpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
}
Expand Down
3 changes: 2 additions & 1 deletion api/v1beta2/route_selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions controllers/ratelimitpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions controllers/ratelimitpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down Expand Up @@ -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{
{
Expand Down
2 changes: 1 addition & 1 deletion controllers/ratelimitpolicy_limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (r *RateLimitPolicyReconciler) deleteLimits(ctx context.Context, rlp *kuadr
if err != nil {
return err
}

Check warning on line 27 in controllers/ratelimitpolicy_limits.go

View check run for this annotation

Codecov / codecov/patch

controllers/ratelimitpolicy_limits.go#L26-L27

Added lines #L26 - L27 were not covered by tests
var rlpRefsWithoutRLP []client.ObjectKey
rlpRefsWithoutRLP := make([]client.ObjectKey, 0)
for _, rlpRef := range rlpRefs {
if rlpRef.Name == rlp.Name && rlpRef.Namespace == rlp.Namespace {
continue
Expand Down
12 changes: 4 additions & 8 deletions controllers/ratelimitpolicy_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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),
Expand All @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions controllers/ratelimitpolicy_wasm_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -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

Check warning on line 178 in controllers/ratelimitpolicy_wasm_plugins.go

View check run for this annotation

Codecov / codecov/patch

controllers/ratelimitpolicy_wasm_plugins.go#L178

Added line #L178 was not covered by tests
Expand All @@ -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),
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/gatewayapi_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/reconcilers/targetref_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ func (r *TargetRefReconciler) FetchAcceptedGatewayHTTPRoutes(ctx context.Context
return
}

Check warning on line 108 in pkg/reconcilers/targetref_reconciler.go

View check run for this annotation

Codecov / codecov/patch

pkg/reconcilers/targetref_reconciler.go#L99-L108

Added lines #L99 - L108 were not covered by tests

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) &&
Expand Down
8 changes: 3 additions & 5 deletions pkg/rlptools/rate_limit_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -125,8 +123,8 @@ func Equal(a, b RateLimitList) bool {
sort.Strings(bCopy[idx].Variables)
}

Check warning on line 124 in pkg/rlptools/rate_limit_index.go

View check run for this annotation

Codecov / codecov/patch

pkg/rlptools/rate_limit_index.go#L118-L124

Added lines #L118 - L124 were not covered by tests

sort.Sort(RateLimitList(aCopy))
sort.Sort(RateLimitList(bCopy))
sort.Sort(aCopy)
sort.Sort(bCopy)

return reflect.DeepEqual(aCopy, bCopy)

Check warning on line 129 in pkg/rlptools/rate_limit_index.go

View check run for this annotation

Codecov / codecov/patch

pkg/rlptools/rate_limit_index.go#L126-L129

Added lines #L126 - L129 were not covered by tests
}
4 changes: 2 additions & 2 deletions pkg/rlptools/wasm/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 13 additions & 11 deletions pkg/rlptools/wasm_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func WasmRules(rlp *kuadrantv1beta2.RateLimitPolicy, route *gatewayapiv1beta1.HT
return rules
}

Check warning on line 31 in pkg/rlptools/wasm_utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/rlptools/wasm_utils.go#L30-L31

Added lines #L30 - L31 were not covered by tests

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 {
Expand All @@ -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
}
Expand All @@ -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)...)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
}

Expand All @@ -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) {

Check warning on line 254 in pkg/rlptools/wasm_utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/rlptools/wasm_utils.go#L254

Added line #L254 was not covered by tests
if structure == nil {
return nil, errors.New("cannot desestructure WASMPlugin from nil")
}
Expand All @@ -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{}

Check warning on line 264 in pkg/rlptools/wasm_utils.go

View check run for this annotation

Codecov / codecov/patch

pkg/rlptools/wasm_utils.go#L264

Added line #L264 was not covered by tests
if err := json.Unmarshal(configJSON, wasmPlugin); err != nil {
return nil, err
}
Expand Down

0 comments on commit 4bf4198

Please sign in to comment.