Skip to content

Commit bb679d0

Browse files
committed
implement translator
1 parent eed6b4d commit bb679d0

18 files changed

Lines changed: 372 additions & 173 deletions

api/v2/apisixroute_types.go

Lines changed: 97 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@
1313
package v2
1414

1515
import (
16-
"encoding/json"
16+
"slices"
17+
"strings"
1718

19+
"github.com/pkg/errors"
20+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1821
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1922
"k8s.io/apimachinery/pkg/util/intstr"
23+
24+
"github.com/apache/apisix-ingress-controller/api/adc"
2025
)
2126

2227
// ApisixRouteSpec is the spec definition for ApisixRouteSpec.
@@ -119,7 +124,7 @@ type ApisixRouteHTTPMatch struct {
119124
// value:
120125
// - "127.0.0.1"
121126
// - "10.0.5.11"
122-
NginxVars []ApisixRouteHTTPMatchExpr `json:"exprs,omitempty" yaml:"exprs,omitempty"`
127+
NginxVars ApisixRouteHTTPMatchExprs `json:"exprs,omitempty" yaml:"exprs,omitempty"`
123128
// Matches based on a user-defined filtering function.
124129
// These functions can accept an input parameter `vars`
125130
// which can be used to access the Nginx variables.
@@ -220,24 +225,101 @@ type ApisixRouteHTTPMatchExpr struct {
220225
Value *string `json:"value" yaml:"value"`
221226
}
222227

223-
// ApisixRoutePluginConfig is the configuration for
224-
// any plugins.
225-
type ApisixRoutePluginConfig map[string]any
228+
type ApisixRouteHTTPMatchExprs []ApisixRouteHTTPMatchExpr
226229

227-
func (p ApisixRoutePluginConfig) DeepCopyInto(out *ApisixRoutePluginConfig) {
228-
b, _ := json.Marshal(&p)
229-
_ = json.Unmarshal(b, out)
230-
}
230+
func (exprs ApisixRouteHTTPMatchExprs) ToVars() (result adc.Vars, err error) {
231+
for _, expr := range exprs {
232+
if expr.Subject.Name == "" && expr.Subject.Scope != ScopePath {
233+
return result, errors.New("empty subject.name")
234+
}
235+
236+
// process key
237+
var (
238+
subj string
239+
this adc.StringOrSlice
240+
)
241+
switch expr.Subject.Scope {
242+
case ScopeQuery:
243+
subj = "arg_" + expr.Subject.Name
244+
case ScopeHeader:
245+
subj = "http_" + strings.ReplaceAll(strings.ToLower(expr.Subject.Name), "-", "_")
246+
case ScopeCookie:
247+
subj = "cookie_" + expr.Subject.Name
248+
case ScopePath:
249+
subj = "uri"
250+
case ScopeVariable:
251+
subj = expr.Subject.Name
252+
default:
253+
return result, errors.New("invalid http match expr: subject.scope should be one of [query, header, cookie, path, variable]")
254+
}
255+
this.SliceVal = append(this.SliceVal, adc.StringOrSlice{StrVal: subj})
256+
257+
// process operator
258+
var (
259+
op string
260+
)
261+
switch expr.Op {
262+
case OpEqual:
263+
op = "=="
264+
case OpGreaterThan:
265+
op = ">"
266+
case OpGreaterThanEqual:
267+
op = ">="
268+
case OpIn:
269+
op = "in"
270+
case OpLessThan:
271+
op = "<"
272+
case OpLessThanEqual:
273+
op = "<="
274+
case OpNotEqual:
275+
op = "~="
276+
case OpNotIn:
277+
op = "in"
278+
case OpRegexMatch:
279+
op = "~~"
280+
case OpRegexMatchCaseInsensitive:
281+
op = "~*"
282+
case OpRegexNotMatch:
283+
op = "~~"
284+
case OpRegexNotMatchCaseInsensitive:
285+
op = "~*"
286+
default:
287+
return result, errors.New("unknown operator")
288+
}
289+
if invert := slices.Contains([]string{OpNotIn, OpRegexNotMatch, OpRegexNotMatchCaseInsensitive}, op); invert {
290+
this.SliceVal = append(this.SliceVal, adc.StringOrSlice{StrVal: "!"})
291+
}
292+
this.SliceVal = append(this.SliceVal, adc.StringOrSlice{StrVal: op})
231293

232-
func (p *ApisixRoutePluginConfig) DeepCopy() *ApisixRoutePluginConfig {
233-
if p == nil {
234-
return nil
294+
// process value
295+
switch expr.Op {
296+
case OpIn, OpNotIn:
297+
if expr.Set == nil {
298+
return result, errors.New("empty set value")
299+
}
300+
var value adc.StringOrSlice
301+
for _, item := range expr.Set {
302+
value.SliceVal = append(value.SliceVal, adc.StringOrSlice{StrVal: item})
303+
}
304+
this.SliceVal = append(this.SliceVal, value)
305+
default:
306+
if expr.Value == nil {
307+
return result, errors.New("empty value")
308+
}
309+
this.SliceVal = append(this.SliceVal, adc.StringOrSlice{StrVal: *expr.Value})
310+
}
311+
312+
// append to result
313+
result = append(result, this.SliceVal)
235314
}
236-
out := new(ApisixRoutePluginConfig)
237-
p.DeepCopyInto(out)
238-
return out
315+
316+
return result, nil
239317
}
240318

319+
// ApisixRoutePluginConfig is the configuration for
320+
// any plugins.
321+
type ApisixRoutePluginConfig map[string]apiextensionsv1.JSON
322+
241323
// ApisixRouteAuthenticationKeyAuth is the keyAuth-related
242324
// configuration in ApisixRouteAuthentication.
243325
type ApisixRouteAuthenticationKeyAuth struct {

api/v2/shared_types.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
package v2
2-
3-
import (
4-
"time"
5-
6-
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
7-
)
8-
91
// Licensed under the Apache License, Version 2.0 (the "License");
102
// you may not use this file except in compliance with the License.
113
// You may obtain a copy of the License at
@@ -18,6 +10,14 @@ import (
1810
// See the License for the specific language governing permissions and
1911
// limitations under the License.
2012

13+
package v2
14+
15+
import (
16+
"time"
17+
18+
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
19+
)
20+
2121
type (
2222
// ApisixRouteConditionType is a type of condition for a route.
2323
ApisixRouteConditionType = gatewayv1.RouteConditionType
@@ -35,6 +35,8 @@ const (
3535
// DefaultUpstreamTimeout represents the default connect,
3636
// read and send timeout (in seconds) with upstreams.
3737
DefaultUpstreamTimeout = 60 * time.Second
38+
39+
DefaultWeight = 100
3840
)
3941

4042
const (

api/v2/zz_generated.deepcopy.go

Lines changed: 50 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/crd/api.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ ApisixRouteHTTPMatch represents the match condition for hitting this route.
10911091
| `methods` _string array_ | HTTP request method predicates. |
10921092
| `hosts` _string array_ | HTTP Host predicates, host can be a wildcard domain or an exact domain. For wildcard domain, only one generic level is allowed, for instance, "*.foo.com" is valid but "*.*.foo.com" is not. |
10931093
| `remoteAddrs` _string array_ | Remote address predicates, items can be valid IPv4 address or IPv6 address or CIDR. |
1094-
| `exprs` _[ApisixRouteHTTPMatchExpr](#apisixroutehttpmatchexpr) array_ | NginxVars represents generic match predicates, it uses Nginx variable systems, so any predicate like headers, querystring and etc can be leveraged here to match the route. For instance, it can be: nginxVars: - subject: "$remote_addr" op: in value: - "127.0.0.1" - "10.0.5.11" |
1094+
| `exprs` _[ApisixRouteHTTPMatchExprs](#apisixroutehttpmatchexprs)_ | NginxVars represents generic match predicates, it uses Nginx variable systems, so any predicate like headers, querystring and etc can be leveraged here to match the route. For instance, it can be: nginxVars: - subject: "$remote_addr" op: in value: - "127.0.0.1" - "10.0.5.11" |
10951095
| `filter_func` _string_ | Matches based on a user-defined filtering function. These functions can accept an input parameter `vars` which can be used to access the Nginx variables. |
10961096

10971097

@@ -1114,7 +1114,7 @@ ApisixRouteHTTPMatchExpr represents a binary route match expression .
11141114

11151115

11161116
_Appears in:_
1117-
- [ApisixRouteHTTPMatch](#apisixroutehttpmatch)
1117+
- [ApisixRouteHTTPMatchExprs](#apisixroutehttpmatchexprs)
11181118

11191119
#### ApisixRouteHTTPMatchExprSubject
11201120

@@ -1132,6 +1132,24 @@ ApisixRouteHTTPMatchExprSubject describes the route match expression subject.
11321132
_Appears in:_
11331133
- [ApisixRouteHTTPMatchExpr](#apisixroutehttpmatchexpr)
11341134

1135+
#### ApisixRouteHTTPMatchExprs
1136+
_Base type:_ `[ApisixRouteHTTPMatchExpr](#apisixroutehttpmatchexpr)`
1137+
1138+
1139+
1140+
1141+
1142+
| Field | Description |
1143+
| --- | --- |
1144+
| `subject` _[ApisixRouteHTTPMatchExprSubject](#apisixroutehttpmatchexprsubject)_ | Subject is the expression subject, it can be any string composed by literals and nginx vars. |
1145+
| `op` _string_ | Op is the operator. |
1146+
| `set` _string array_ | Set is an array type object of the expression. It should be used when the Op is "in" or "not_in"; |
1147+
| `value` _string_ | Value is the normal type object for the expression, it should be used when the Op is not "in" and "not_in". Set and Value are exclusive so only of them can be set in the same time. |
1148+
1149+
1150+
_Appears in:_
1151+
- [ApisixRouteHTTPMatch](#apisixroutehttpmatch)
1152+
11351153
#### ApisixRoutePlugin
11361154

11371155

@@ -1634,6 +1652,10 @@ _Appears in:_
16341652

16351653

16361654

1655+
1656+
1657+
1658+
16371659
#### UpstreamTimeout
16381660

16391661

internal/controller/apisixglobalrule_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ func (r *ApisixGlobalRuleReconciler) processIngressClassParameters(ctx context.C
367367
// updateStatus updates the ApisixGlobalRule status with the given condition
368368
func (r *ApisixGlobalRuleReconciler) updateStatus(globalRule *apiv2.ApisixGlobalRule, condition metav1.Condition) {
369369
r.Updater.Update(status.Update{
370-
NamespacedName: NamespacedName(globalRule),
370+
NamespacedName: utils.NamespacedName(globalRule),
371371
Resource: &apiv2.ApisixGlobalRule{},
372372
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
373373
gr, ok := obj.(*apiv2.ApisixGlobalRule)

0 commit comments

Comments
 (0)