Skip to content

Commit 171a402

Browse files
committed
f:
1 parent 36d1045 commit 171a402

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
"encoding/json"
21+
"errors"
22+
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
25+
)
26+
27+
// HTTPRoutePolicySpec defines the desired state of HTTPRoutePolicy.
28+
type HTTPRoutePolicySpec struct {
29+
// TargetRef identifies an API object (enum: HTTPRoute, Ingress) to apply HTTPRoutePolicy to.
30+
//
31+
// target references.
32+
// +listType=map
33+
// +listMapKey=group
34+
// +listMapKey=kind
35+
// +listMapKey=name
36+
// +kubebuilder:validation:MinItems=1
37+
// +kubebuilder:validation:MaxItems=16
38+
TargetRefs []gatewayv1alpha2.LocalPolicyTargetReferenceWithSectionName `json:"targetRefs"`
39+
40+
Policy HTTPRoutePolicySpecPolicy `json:"policy"`
41+
}
42+
43+
// +kubebuilder:object:root=true
44+
45+
// HTTPRoutePolicy is the Schema for the httproutepolicies API.
46+
type HTTPRoutePolicy struct {
47+
metav1.TypeMeta `json:",inline"`
48+
metav1.ObjectMeta `json:"metadata,omitempty"`
49+
50+
Spec HTTPRoutePolicySpec `json:"spec,omitempty"`
51+
}
52+
53+
// +kubebuilder:object:root=true
54+
55+
// HTTPRoutePolicyList contains a list of HTTPRoutePolicy.
56+
type HTTPRoutePolicyList struct {
57+
metav1.TypeMeta `json:",inline"`
58+
metav1.ListMeta `json:"metadata,omitempty"`
59+
Items []HTTPRoutePolicy `json:"items"`
60+
}
61+
62+
type HTTPRoutePolicySpecPolicy struct {
63+
Priority *int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
64+
Vars Vars `json:"vars,omitempty" yaml:"vars,omitempty"`
65+
}
66+
67+
// 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+
}
122+
123+
func init() {
124+
SchemeBuilder.Register(&HTTPRoutePolicy{}, &HTTPRoutePolicyList{})
125+
}

0 commit comments

Comments
 (0)