Skip to content

Commit 497da5f

Browse files
authored
chore: Refactor Vars and StringOrSlice types into adc package (#95)
1 parent e664535 commit 497da5f

File tree

8 files changed

+79
-188
lines changed

8 files changed

+79
-188
lines changed

api/adc/types.go

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"time"
1111

1212
"github.com/incubator4/go-resty-expr/expr"
13-
14-
"github.com/api7/api7-ingress-controller/api/common"
1513
)
1614

1715
const (
@@ -115,16 +113,16 @@ type Service struct {
115113
type Route struct {
116114
Metadata `json:",inline" yaml:",inline"`
117115

118-
EnableWebsocket *bool `json:"enable_websocket,omitempty" yaml:"enable_websocket,omitempty"`
119-
FilterFunc string `json:"filter_func,omitempty" yaml:"filter_func,omitempty"`
120-
Hosts []string `json:"hosts,omitempty" yaml:"hosts,omitempty"`
121-
Methods []string `json:"methods,omitempty" yaml:"methods,omitempty"`
122-
Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"`
123-
Priority *int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
124-
RemoteAddrs []string `json:"remote_addrs,omitempty" yaml:"remote_addrs,omitempty"`
125-
Timeout *Timeout `json:"timeout,omitempty" yaml:"timeout,omitempty"`
126-
Uris []string `json:"uris" yaml:"uris"`
127-
Vars common.Vars `json:"vars,omitempty" yaml:"vars,omitempty"`
116+
EnableWebsocket *bool `json:"enable_websocket,omitempty" yaml:"enable_websocket,omitempty"`
117+
FilterFunc string `json:"filter_func,omitempty" yaml:"filter_func,omitempty"`
118+
Hosts []string `json:"hosts,omitempty" yaml:"hosts,omitempty"`
119+
Methods []string `json:"methods,omitempty" yaml:"methods,omitempty"`
120+
Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"`
121+
Priority *int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
122+
RemoteAddrs []string `json:"remote_addrs,omitempty" yaml:"remote_addrs,omitempty"`
123+
Timeout *Timeout `json:"timeout,omitempty" yaml:"timeout,omitempty"`
124+
Uris []string `json:"uris" yaml:"uris"`
125+
Vars Vars `json:"vars,omitempty" yaml:"vars,omitempty"`
128126
}
129127

130128
type Timeout struct {
@@ -502,3 +500,49 @@ type ResponseData struct {
502500
Value map[string]any `json:"value"`
503501
ErrorMsg string `json:"error_msg"`
504502
}
503+
504+
// Vars represents the route match expressions of APISIX.
505+
type Vars [][]StringOrSlice
506+
507+
// UnmarshalJSON implements json.Unmarshaler interface.
508+
// lua-cjson doesn't distinguish empty array and table,
509+
// and by default empty array will be encoded as '{}'.
510+
// We have to maintain the compatibility.
511+
func (vars *Vars) UnmarshalJSON(p []byte) error {
512+
if p[0] == '{' {
513+
if len(p) != 2 {
514+
return errors.New("unexpected non-empty object")
515+
}
516+
return nil
517+
}
518+
var data [][]StringOrSlice
519+
if err := json.Unmarshal(p, &data); err != nil {
520+
return err
521+
}
522+
*vars = data
523+
return nil
524+
}
525+
526+
// StringOrSlice represents a string or a string slice.
527+
// TODO Do not use interface{} to avoid the reflection overheads.
528+
type StringOrSlice struct {
529+
StrVal string `json:"-"`
530+
SliceVal []StringOrSlice `json:"-"`
531+
}
532+
533+
func (s *StringOrSlice) MarshalJSON() ([]byte, error) {
534+
if s.SliceVal != nil {
535+
return json.Marshal(s.SliceVal)
536+
}
537+
return json.Marshal(s.StrVal)
538+
}
539+
540+
func (s *StringOrSlice) UnmarshalJSON(p []byte) error {
541+
if len(p) == 0 {
542+
return errors.New("empty object")
543+
}
544+
if p[0] == '[' {
545+
return json.Unmarshal(p, &s.SliceVal)
546+
}
547+
return json.Unmarshal(p, &s.StrVal)
548+
}

api/common/types.go

Lines changed: 0 additions & 64 deletions
This file was deleted.

api/common/zz_generated.deepcopy.go

Lines changed: 0 additions & 70 deletions
This file was deleted.

api/v1alpha1/httproutepolicy_types.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2021
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2122
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
22-
23-
"github.com/api7/api7-ingress-controller/api/common"
2423
)
2524

2625
// HTTPRoutePolicySpec defines the desired state of HTTPRoutePolicy.
@@ -36,8 +35,8 @@ type HTTPRoutePolicySpec struct {
3635
// +kubebuilder:validation:MaxItems=16
3736
TargetRefs []gatewayv1alpha2.LocalPolicyTargetReferenceWithSectionName `json:"targetRefs"`
3837

39-
Priority *int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
40-
Vars Vars `json:"vars,omitempty" yaml:"vars,omitempty"`
38+
Priority *int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
39+
Vars []apiextensionsv1.JSON `json:"vars,omitempty" yaml:"vars,omitempty"`
4140
}
4241

4342
// +kubebuilder:object:root=true
@@ -61,10 +60,6 @@ type HTTPRoutePolicyList struct {
6160
Items []HTTPRoutePolicy `json:"items"`
6261
}
6362

64-
// Vars represents the route match expressions of APISIX.
65-
// +kubebuilder:object:generate=false
66-
type Vars = common.Vars
67-
6863
func init() {
6964
SchemeBuilder.Register(&HTTPRoutePolicy{}, &HTTPRoutePolicyList{})
7065
}

api/v1alpha1/zz_generated.deepcopy.go

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

config/crd/bases/gateway.apisix.io_httproutepolicies.yaml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,8 @@ spec:
110110
- name
111111
x-kubernetes-list-type: map
112112
vars:
113-
description: Vars represents the route match expressions of APISIX.
114113
items:
115-
items:
116-
description: |-
117-
StringOrSlice represents a string or a string slice.
118-
TODO Do not use interface{} to avoid the reflection overheads.
119-
type: object
120-
type: array
114+
x-kubernetes-preserve-unknown-fields: true
121115
type: array
122116
required:
123117
- targetRefs

internal/provider/adc/translator/httproute.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"k8s.io/apimachinery/pkg/types"
1212
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
1313

14-
"github.com/api7/api7-ingress-controller/api/common"
1514
"github.com/api7/gopkg/pkg/log"
1615

1716
adctypes "github.com/api7/api7-ingress-controller/api/adc"
@@ -356,14 +355,14 @@ func (t *Translator) translateGatewayHTTPRouteMatch(match *gatewayv1.HTTPRouteMa
356355
case gatewayv1.PathMatchPathPrefix:
357356
route.Uris = []string{*match.Path.Value + "*"}
358357
case gatewayv1.PathMatchRegularExpression:
359-
var this []common.StringOrSlice
360-
this = append(this, common.StringOrSlice{
358+
var this []adctypes.StringOrSlice
359+
this = append(this, adctypes.StringOrSlice{
361360
StrVal: "uri",
362361
})
363-
this = append(this, common.StringOrSlice{
362+
this = append(this, adctypes.StringOrSlice{
364363
StrVal: "~~",
365364
})
366-
this = append(this, common.StringOrSlice{
365+
this = append(this, adctypes.StringOrSlice{
367366
StrVal: *match.Path.Value,
368367
})
369368

@@ -378,25 +377,25 @@ func (t *Translator) translateGatewayHTTPRouteMatch(match *gatewayv1.HTTPRouteMa
378377
name := strings.ToLower(string(header.Name))
379378
name = strings.ReplaceAll(name, "-", "_")
380379

381-
var this []common.StringOrSlice
382-
this = append(this, common.StringOrSlice{
380+
var this []adctypes.StringOrSlice
381+
this = append(this, adctypes.StringOrSlice{
383382
StrVal: "http_" + name,
384383
})
385384

386385
switch *header.Type {
387386
case gatewayv1.HeaderMatchExact:
388-
this = append(this, common.StringOrSlice{
387+
this = append(this, adctypes.StringOrSlice{
389388
StrVal: "==",
390389
})
391390
case gatewayv1.HeaderMatchRegularExpression:
392-
this = append(this, common.StringOrSlice{
391+
this = append(this, adctypes.StringOrSlice{
393392
StrVal: "~~",
394393
})
395394
default:
396395
return nil, errors.New("unknown header match type " + string(*header.Type))
397396
}
398397

399-
this = append(this, common.StringOrSlice{
398+
this = append(this, adctypes.StringOrSlice{
400399
StrVal: header.Value,
401400
})
402401

@@ -406,25 +405,25 @@ func (t *Translator) translateGatewayHTTPRouteMatch(match *gatewayv1.HTTPRouteMa
406405

407406
if len(match.QueryParams) > 0 {
408407
for _, query := range match.QueryParams {
409-
var this []common.StringOrSlice
410-
this = append(this, common.StringOrSlice{
408+
var this []adctypes.StringOrSlice
409+
this = append(this, adctypes.StringOrSlice{
411410
StrVal: "arg_" + strings.ToLower(fmt.Sprintf("%v", query.Name)),
412411
})
413412

414413
switch *query.Type {
415414
case gatewayv1.QueryParamMatchExact:
416-
this = append(this, common.StringOrSlice{
415+
this = append(this, adctypes.StringOrSlice{
417416
StrVal: "==",
418417
})
419418
case gatewayv1.QueryParamMatchRegularExpression:
420-
this = append(this, common.StringOrSlice{
419+
this = append(this, adctypes.StringOrSlice{
421420
StrVal: "~~",
422421
})
423422
default:
424423
return nil, errors.New("unknown query match type " + string(*query.Type))
425424
}
426425

427-
this = append(this, common.StringOrSlice{
426+
this = append(this, adctypes.StringOrSlice{
428427
StrVal: query.Value,
429428
})
430429

0 commit comments

Comments
 (0)