Skip to content

Commit 00a8c6f

Browse files
committed
Add HTTPRoutePolicy CRD and related configurations
1 parent 171a402 commit 00a8c6f

File tree

11 files changed

+666
-196
lines changed

11 files changed

+666
-196
lines changed

api/adc/types.go

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

1212
"github.com/incubator4/go-resty-expr/expr"
13+
14+
"github.com/api7/api7-ingress-controller/api/common"
1315
)
1416

1517
const (
@@ -113,16 +115,16 @@ type Service struct {
113115
type Route struct {
114116
Metadata `json:",inline" yaml:",inline"`
115117

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"`
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"`
126128
}
127129

128130
type Timeout struct {
@@ -325,62 +327,6 @@ func (n *UpstreamNodes) UnmarshalJSON(p []byte) error {
325327
return nil
326328
}
327329

328-
// Vars represents the route match expressions of APISIX.
329-
type Vars [][]StringOrSlice
330-
331-
// UnmarshalJSON implements json.Unmarshaler interface.
332-
// lua-cjson doesn't distinguish empty array and table,
333-
// and by default empty array will be encoded as '{}'.
334-
// We have to maintain the compatibility.
335-
func (vars *Vars) UnmarshalJSON(p []byte) error {
336-
if p[0] == '{' {
337-
if len(p) != 2 {
338-
return errors.New("unexpected non-empty object")
339-
}
340-
return nil
341-
}
342-
var data [][]StringOrSlice
343-
if err := json.Unmarshal(p, &data); err != nil {
344-
return err
345-
}
346-
*vars = data
347-
return nil
348-
}
349-
350-
// StringOrSlice represents a string or a string slice.
351-
// TODO Do not use interface{} to avoid the reflection overheads.
352-
type StringOrSlice struct {
353-
StrVal string `json:"-"`
354-
SliceVal []string `json:"-"`
355-
}
356-
357-
func (s *StringOrSlice) MarshalJSON() ([]byte, error) {
358-
var (
359-
p []byte
360-
err error
361-
)
362-
if s.SliceVal != nil {
363-
p, err = json.Marshal(s.SliceVal)
364-
} else {
365-
p, err = json.Marshal(s.StrVal)
366-
}
367-
return p, err
368-
}
369-
370-
func (s *StringOrSlice) UnmarshalJSON(p []byte) error {
371-
var err error
372-
373-
if len(p) == 0 {
374-
return errors.New("empty object")
375-
}
376-
if p[0] == '[' {
377-
err = json.Unmarshal(p, &s.SliceVal)
378-
} else {
379-
err = json.Unmarshal(p, &s.StrVal)
380-
}
381-
return err
382-
}
383-
384330
// ComposeRouteName uses namespace, name and rule name to compose
385331
// the route name.
386332
func ComposeRouteName(namespace, name string, rule string) string {

api/common/types.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// +kubebuilder:object:generate=true
2+
3+
package common
4+
5+
import (
6+
"encoding/json"
7+
"errors"
8+
)
9+
10+
// Vars represents the route match expressions of APISIX.
11+
type Vars [][]StringOrSlice
12+
13+
// UnmarshalJSON implements json.Unmarshaler interface.
14+
// lua-cjson doesn't distinguish empty array and table,
15+
// and by default empty array will be encoded as '{}'.
16+
// We have to maintain the compatibility.
17+
func (vars *Vars) UnmarshalJSON(p []byte) error {
18+
if p[0] == '{' {
19+
if len(p) != 2 {
20+
return errors.New("unexpected non-empty object")
21+
}
22+
return nil
23+
}
24+
var data [][]StringOrSlice
25+
if err := json.Unmarshal(p, &data); err != nil {
26+
return err
27+
}
28+
*vars = data
29+
return nil
30+
}
31+
32+
// StringOrSlice represents a string or a string slice.
33+
// TODO Do not use interface{} to avoid the reflection overheads.
34+
type StringOrSlice struct {
35+
StrVal string `json:"-"`
36+
SliceVal []string `json:"-"`
37+
}
38+
39+
func (s *StringOrSlice) MarshalJSON() ([]byte, error) {
40+
var (
41+
p []byte
42+
err error
43+
)
44+
if s.SliceVal != nil {
45+
p, err = json.Marshal(s.SliceVal)
46+
} else {
47+
p, err = json.Marshal(s.StrVal)
48+
}
49+
return p, err
50+
}
51+
52+
func (s *StringOrSlice) UnmarshalJSON(p []byte) error {
53+
var err error
54+
55+
if len(p) == 0 {
56+
return errors.New("empty object")
57+
}
58+
if p[0] == '[' {
59+
err = json.Unmarshal(p, &s.SliceVal)
60+
} else {
61+
err = json.Unmarshal(p, &s.StrVal)
62+
}
63+
return err
64+
}

api/common/zz_generated.deepcopy.go

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

api/v1alpha1/httproutepolicy_types.go

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

1919
import (
20-
"encoding/json"
21-
"errors"
22-
2320
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2421
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
22+
23+
"github.com/api7/api7-ingress-controller/api/common"
2524
)
2625

2726
// HTTPRoutePolicySpec defines the desired state of HTTPRoutePolicy.
@@ -47,7 +46,8 @@ type HTTPRoutePolicy struct {
4746
metav1.TypeMeta `json:",inline"`
4847
metav1.ObjectMeta `json:"metadata,omitempty"`
4948

50-
Spec HTTPRoutePolicySpec `json:"spec,omitempty"`
49+
Spec HTTPRoutePolicySpec `json:"spec,omitempty"`
50+
Status gatewayv1alpha2.PolicyStatus `json:"status,omitempty"`
5151
}
5252

5353
// +kubebuilder:object:root=true
@@ -65,60 +65,8 @@ type HTTPRoutePolicySpecPolicy struct {
6565
}
6666

6767
// Vars represents the route match expressions of APISIX.
68-
type Vars [][]StringOrSlice
69-
70-
// UnmarshalJSON implements json.Unmarshaler interface.
71-
// lua-cjson doesn't distinguish empty array and table,
72-
// and by default empty array will be encoded as '{}'.
73-
// We have to maintain the compatibility.
74-
func (vars *Vars) UnmarshalJSON(p []byte) error {
75-
if p[0] == '{' {
76-
if len(p) != 2 {
77-
return errors.New("unexpected non-empty object")
78-
}
79-
return nil
80-
}
81-
var data [][]StringOrSlice
82-
if err := json.Unmarshal(p, &data); err != nil {
83-
return err
84-
}
85-
*vars = data
86-
return nil
87-
}
88-
89-
// StringOrSlice represents a string or a string slice.
90-
// TODO Do not use interface{} to avoid the reflection overheads.
91-
type StringOrSlice struct {
92-
StrVal string `json:"-"`
93-
SliceVal []string `json:"-"`
94-
}
95-
96-
func (s *StringOrSlice) MarshalJSON() ([]byte, error) {
97-
var (
98-
p []byte
99-
err error
100-
)
101-
if s.SliceVal != nil {
102-
p, err = json.Marshal(s.SliceVal)
103-
} else {
104-
p, err = json.Marshal(s.StrVal)
105-
}
106-
return p, err
107-
}
108-
109-
func (s *StringOrSlice) UnmarshalJSON(p []byte) error {
110-
var err error
111-
112-
if len(p) == 0 {
113-
return errors.New("empty object")
114-
}
115-
if p[0] == '[' {
116-
err = json.Unmarshal(p, &s.SliceVal)
117-
} else {
118-
err = json.Unmarshal(p, &s.StrVal)
119-
}
120-
return err
121-
}
68+
// +kubebuilder:object:generate=false
69+
type Vars = common.Vars
12270

12371
func init() {
12472
SchemeBuilder.Register(&HTTPRoutePolicy{}, &HTTPRoutePolicyList{})

0 commit comments

Comments
 (0)