Skip to content

Commit 2b5f4fb

Browse files
authored
chore: Add CRD HTTPRoutePolicy (#89)
1 parent 8876cd2 commit 2b5f4fb

File tree

12 files changed

+842
-81
lines changed

12 files changed

+842
-81
lines changed

PROJECT

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,12 @@ resources:
2626
kind: GatewayProxy
2727
path: github.com/api7/api7-ingress-controller/api/v1alpha1
2828
version: v1alpha1
29+
- api:
30+
crdVersion: v1
31+
namespaced: true
32+
domain: github.com
33+
group: gateway.apisix.io
34+
kind: HTTPRoutePolicy
35+
path: github.com/api7/api7-ingress-controller/api/v1alpha1
36+
version: v1alpha1
2937
version: "3"

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.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Copyright 2024.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
22+
23+
"github.com/api7/api7-ingress-controller/api/common"
24+
)
25+
26+
// HTTPRoutePolicySpec defines the desired state of HTTPRoutePolicy.
27+
type HTTPRoutePolicySpec struct {
28+
// TargetRef identifies an API object (enum: HTTPRoute, Ingress) to apply HTTPRoutePolicy to.
29+
//
30+
// target references.
31+
// +listType=map
32+
// +listMapKey=group
33+
// +listMapKey=kind
34+
// +listMapKey=name
35+
// +kubebuilder:validation:MinItems=1
36+
// +kubebuilder:validation:MaxItems=16
37+
TargetRefs []gatewayv1alpha2.LocalPolicyTargetReferenceWithSectionName `json:"targetRefs"`
38+
39+
Priority *int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
40+
Vars Vars `json:"vars,omitempty" yaml:"vars,omitempty"`
41+
}
42+
43+
// +kubebuilder:object:root=true
44+
// +kubebuilder:subresource:status
45+
46+
// HTTPRoutePolicy is the Schema for the httproutepolicies API.
47+
type HTTPRoutePolicy struct {
48+
metav1.TypeMeta `json:",inline"`
49+
metav1.ObjectMeta `json:"metadata,omitempty"`
50+
51+
Spec HTTPRoutePolicySpec `json:"spec,omitempty"`
52+
Status gatewayv1alpha2.PolicyStatus `json:"status,omitempty"`
53+
}
54+
55+
// +kubebuilder:object:root=true
56+
57+
// HTTPRoutePolicyList contains a list of HTTPRoutePolicy.
58+
type HTTPRoutePolicyList struct {
59+
metav1.TypeMeta `json:",inline"`
60+
metav1.ListMeta `json:"metadata,omitempty"`
61+
Items []HTTPRoutePolicy `json:"items"`
62+
}
63+
64+
// Vars represents the route match expressions of APISIX.
65+
// +kubebuilder:object:generate=false
66+
type Vars = common.Vars
67+
68+
func init() {
69+
SchemeBuilder.Register(&HTTPRoutePolicy{}, &HTTPRoutePolicyList{})
70+
}

0 commit comments

Comments
 (0)