Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 56 additions & 12 deletions api/adc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"time"

"github.com/incubator4/go-resty-expr/expr"

"github.com/api7/api7-ingress-controller/api/common"
)

const (
Expand Down Expand Up @@ -115,16 +113,16 @@ type Service struct {
type Route struct {
Metadata `json:",inline" yaml:",inline"`

EnableWebsocket *bool `json:"enable_websocket,omitempty" yaml:"enable_websocket,omitempty"`
FilterFunc string `json:"filter_func,omitempty" yaml:"filter_func,omitempty"`
Hosts []string `json:"hosts,omitempty" yaml:"hosts,omitempty"`
Methods []string `json:"methods,omitempty" yaml:"methods,omitempty"`
Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"`
Priority *int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
RemoteAddrs []string `json:"remote_addrs,omitempty" yaml:"remote_addrs,omitempty"`
Timeout *Timeout `json:"timeout,omitempty" yaml:"timeout,omitempty"`
Uris []string `json:"uris" yaml:"uris"`
Vars common.Vars `json:"vars,omitempty" yaml:"vars,omitempty"`
EnableWebsocket *bool `json:"enable_websocket,omitempty" yaml:"enable_websocket,omitempty"`
FilterFunc string `json:"filter_func,omitempty" yaml:"filter_func,omitempty"`
Hosts []string `json:"hosts,omitempty" yaml:"hosts,omitempty"`
Methods []string `json:"methods,omitempty" yaml:"methods,omitempty"`
Plugins Plugins `json:"plugins,omitempty" yaml:"plugins,omitempty"`
Priority *int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
RemoteAddrs []string `json:"remote_addrs,omitempty" yaml:"remote_addrs,omitempty"`
Timeout *Timeout `json:"timeout,omitempty" yaml:"timeout,omitempty"`
Uris []string `json:"uris" yaml:"uris"`
Vars Vars `json:"vars,omitempty" yaml:"vars,omitempty"`
}

type Timeout struct {
Expand Down Expand Up @@ -502,3 +500,49 @@ type ResponseData struct {
Value map[string]any `json:"value"`
ErrorMsg string `json:"error_msg"`
}

// Vars represents the route match expressions of APISIX.
type Vars [][]StringOrSlice

// UnmarshalJSON implements json.Unmarshaler interface.
// lua-cjson doesn't distinguish empty array and table,
// and by default empty array will be encoded as '{}'.
// We have to maintain the compatibility.
func (vars *Vars) UnmarshalJSON(p []byte) error {
if p[0] == '{' {
if len(p) != 2 {
return errors.New("unexpected non-empty object")
}
return nil
}
var data [][]StringOrSlice
if err := json.Unmarshal(p, &data); err != nil {
return err
}
*vars = data
return nil
}

// StringOrSlice represents a string or a string slice.
// TODO Do not use interface{} to avoid the reflection overheads.
type StringOrSlice struct {
StrVal string `json:"-"`
SliceVal []StringOrSlice `json:"-"`
}

func (s *StringOrSlice) MarshalJSON() ([]byte, error) {
if s.SliceVal != nil {
return json.Marshal(s.SliceVal)
}
return json.Marshal(s.StrVal)
}

func (s *StringOrSlice) UnmarshalJSON(p []byte) error {
if len(p) == 0 {
return errors.New("empty object")
}
if p[0] == '[' {
return json.Unmarshal(p, &s.SliceVal)
}
return json.Unmarshal(p, &s.StrVal)
}
64 changes: 0 additions & 64 deletions api/common/types.go

This file was deleted.

70 changes: 0 additions & 70 deletions api/common/zz_generated.deepcopy.go

This file was deleted.

11 changes: 3 additions & 8 deletions api/v1alpha1/httproutepolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ limitations under the License.
package v1alpha1

import (
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"

"github.com/api7/api7-ingress-controller/api/common"
)

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

Priority *int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
Vars Vars `json:"vars,omitempty" yaml:"vars,omitempty"`
Priority *int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
Vars []apiextensionsv1.JSON `json:"vars,omitempty" yaml:"vars,omitempty"`
}

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

// Vars represents the route match expressions of APISIX.
// +kubebuilder:object:generate=false
type Vars = common.Vars

func init() {
SchemeBuilder.Register(&HTTPRoutePolicy{}, &HTTPRoutePolicyList{})
}
11 changes: 2 additions & 9 deletions api/v1alpha1/zz_generated.deepcopy.go

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

8 changes: 1 addition & 7 deletions config/crd/bases/gateway.apisix.io_httproutepolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,8 @@ spec:
- name
x-kubernetes-list-type: map
vars:
description: Vars represents the route match expressions of APISIX.
items:
items:
description: |-
StringOrSlice represents a string or a string slice.
TODO Do not use interface{} to avoid the reflection overheads.
type: object
type: array
x-kubernetes-preserve-unknown-fields: true
type: array
required:
- targetRefs
Expand Down
29 changes: 14 additions & 15 deletions internal/provider/adc/translator/httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"k8s.io/apimachinery/pkg/types"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"

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

adctypes "github.com/api7/api7-ingress-controller/api/adc"
Expand Down Expand Up @@ -356,14 +355,14 @@ func (t *Translator) translateGatewayHTTPRouteMatch(match *gatewayv1.HTTPRouteMa
case gatewayv1.PathMatchPathPrefix:
route.Uris = []string{*match.Path.Value + "*"}
case gatewayv1.PathMatchRegularExpression:
var this []common.StringOrSlice
this = append(this, common.StringOrSlice{
var this []adctypes.StringOrSlice
this = append(this, adctypes.StringOrSlice{
StrVal: "uri",
})
this = append(this, common.StringOrSlice{
this = append(this, adctypes.StringOrSlice{
StrVal: "~~",
})
this = append(this, common.StringOrSlice{
this = append(this, adctypes.StringOrSlice{
StrVal: *match.Path.Value,
})

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

var this []common.StringOrSlice
this = append(this, common.StringOrSlice{
var this []adctypes.StringOrSlice
this = append(this, adctypes.StringOrSlice{
StrVal: "http_" + name,
})

switch *header.Type {
case gatewayv1.HeaderMatchExact:
this = append(this, common.StringOrSlice{
this = append(this, adctypes.StringOrSlice{
StrVal: "==",
})
case gatewayv1.HeaderMatchRegularExpression:
this = append(this, common.StringOrSlice{
this = append(this, adctypes.StringOrSlice{
StrVal: "~~",
})
default:
return nil, errors.New("unknown header match type " + string(*header.Type))
}

this = append(this, common.StringOrSlice{
this = append(this, adctypes.StringOrSlice{
StrVal: header.Value,
})

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

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

switch *query.Type {
case gatewayv1.QueryParamMatchExact:
this = append(this, common.StringOrSlice{
this = append(this, adctypes.StringOrSlice{
StrVal: "==",
})
case gatewayv1.QueryParamMatchRegularExpression:
this = append(this, common.StringOrSlice{
this = append(this, adctypes.StringOrSlice{
StrVal: "~~",
})
default:
return nil, errors.New("unknown query match type " + string(*query.Type))
}

this = append(this, common.StringOrSlice{
this = append(this, adctypes.StringOrSlice{
StrVal: query.Value,
})

Expand Down
Loading
Loading