|
13 | 13 | package v2 |
14 | 14 |
|
15 | 15 | import ( |
| 16 | + "strings" |
| 17 | + |
| 18 | + "github.com/pkg/errors" |
16 | 19 | apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
17 | 20 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
18 | 21 | "k8s.io/apimachinery/pkg/util/intstr" |
| 22 | + |
| 23 | + "github.com/apache/apisix-ingress-controller/api/adc" |
19 | 24 | ) |
20 | 25 |
|
21 | 26 | // ApisixRouteSpec is the spec definition for ApisixRouteSpec. |
@@ -66,6 +71,7 @@ type ApisixRouteHTTP struct { |
66 | 71 | // Upstreams refer to ApisixUpstream CRD |
67 | 72 | Upstreams []ApisixRouteUpstreamReference `json:"upstreams,omitempty" yaml:"upstreams,omitempty"` |
68 | 73 |
|
| 74 | + // +kubebuilder:validation:Optional |
69 | 75 | Websocket bool `json:"websocket" yaml:"websocket"` |
70 | 76 | PluginConfigName string `json:"plugin_config_name,omitempty" yaml:"plugin_config_name,omitempty"` |
71 | 77 | // By default, PluginConfigNamespace will be the same as the namespace of ApisixRoute |
@@ -118,7 +124,7 @@ type ApisixRouteHTTPMatch struct { |
118 | 124 | // value: |
119 | 125 | // - "127.0.0.1" |
120 | 126 | // - "10.0.5.11" |
121 | | - NginxVars []ApisixRouteHTTPMatchExpr `json:"exprs,omitempty" yaml:"exprs,omitempty"` |
| 127 | + NginxVars ApisixRouteHTTPMatchExprs `json:"exprs,omitempty" yaml:"exprs,omitempty"` |
122 | 128 | // Matches based on a user-defined filtering function. |
123 | 129 | // These functions can accept an input parameter `vars` |
124 | 130 | // which can be used to access the Nginx variables. |
@@ -153,6 +159,7 @@ type ApisixRouteHTTPBackend struct { |
153 | 159 | // default is endpoints. |
154 | 160 | ResolveGranularity string `json:"resolveGranularity,omitempty" yaml:"resolveGranularity,omitempty"` |
155 | 161 | // Weight of this backend. |
| 162 | + // +kubebuilder:validation:Optional |
156 | 163 | Weight *int `json:"weight" yaml:"weight"` |
157 | 164 | // Subset specifies a subset for the target Service. The subset should be pre-defined |
158 | 165 | // in ApisixUpstream about this service. |
@@ -211,14 +218,107 @@ type ApisixRouteHTTPMatchExpr struct { |
211 | 218 | Op string `json:"op" yaml:"op"` |
212 | 219 | // Set is an array type object of the expression. |
213 | 220 | // It should be used when the Op is "in" or "not_in"; |
| 221 | + // +kubebuilder:validation:Optional |
214 | 222 | Set []string `json:"set" yaml:"set"` |
215 | 223 | // Value is the normal type object for the expression, |
216 | 224 | // it should be used when the Op is not "in" and "not_in". |
217 | 225 | // Set and Value are exclusive so only of them can be set |
218 | 226 | // in the same time. |
| 227 | + // +kubebuilder:validation:Optional |
219 | 228 | Value *string `json:"value" yaml:"value"` |
220 | 229 | } |
221 | 230 |
|
| 231 | +type ApisixRouteHTTPMatchExprs []ApisixRouteHTTPMatchExpr |
| 232 | + |
| 233 | +func (exprs ApisixRouteHTTPMatchExprs) ToVars() (result adc.Vars, err error) { |
| 234 | + for _, expr := range exprs { |
| 235 | + if expr.Subject.Name == "" && expr.Subject.Scope != ScopePath { |
| 236 | + return result, errors.New("empty subject.name") |
| 237 | + } |
| 238 | + |
| 239 | + // process key |
| 240 | + var ( |
| 241 | + subj string |
| 242 | + this adc.StringOrSlice |
| 243 | + ) |
| 244 | + switch expr.Subject.Scope { |
| 245 | + case ScopeQuery: |
| 246 | + subj = "arg_" + expr.Subject.Name |
| 247 | + case ScopeHeader: |
| 248 | + subj = "http_" + strings.ReplaceAll(strings.ToLower(expr.Subject.Name), "-", "_") |
| 249 | + case ScopeCookie: |
| 250 | + subj = "cookie_" + expr.Subject.Name |
| 251 | + case ScopePath: |
| 252 | + subj = "uri" |
| 253 | + case ScopeVariable: |
| 254 | + subj = expr.Subject.Name |
| 255 | + default: |
| 256 | + return result, errors.New("invalid http match expr: subject.scope should be one of [query, header, cookie, path, variable]") |
| 257 | + } |
| 258 | + this.SliceVal = append(this.SliceVal, adc.StringOrSlice{StrVal: subj}) |
| 259 | + |
| 260 | + // process operator |
| 261 | + var ( |
| 262 | + op string |
| 263 | + ) |
| 264 | + switch expr.Op { |
| 265 | + case OpEqual: |
| 266 | + op = "==" |
| 267 | + case OpGreaterThan: |
| 268 | + op = ">" |
| 269 | + case OpGreaterThanEqual: |
| 270 | + op = ">=" |
| 271 | + case OpIn: |
| 272 | + op = "in" |
| 273 | + case OpLessThan: |
| 274 | + op = "<" |
| 275 | + case OpLessThanEqual: |
| 276 | + op = "<=" |
| 277 | + case OpNotEqual: |
| 278 | + op = "~=" |
| 279 | + case OpNotIn: |
| 280 | + op = "in" |
| 281 | + case OpRegexMatch: |
| 282 | + op = "~~" |
| 283 | + case OpRegexMatchCaseInsensitive: |
| 284 | + op = "~*" |
| 285 | + case OpRegexNotMatch: |
| 286 | + op = "~~" |
| 287 | + case OpRegexNotMatchCaseInsensitive: |
| 288 | + op = "~*" |
| 289 | + default: |
| 290 | + return result, errors.New("unknown operator") |
| 291 | + } |
| 292 | + if expr.Op == OpNotIn || expr.Op == OpRegexNotMatch || expr.Op == OpRegexNotMatchCaseInsensitive { |
| 293 | + this.SliceVal = append(this.SliceVal, adc.StringOrSlice{StrVal: "!"}) |
| 294 | + } |
| 295 | + this.SliceVal = append(this.SliceVal, adc.StringOrSlice{StrVal: op}) |
| 296 | + |
| 297 | + // process value |
| 298 | + switch expr.Op { |
| 299 | + case OpIn, OpNotIn: |
| 300 | + if expr.Set == nil { |
| 301 | + return result, errors.New("empty set value") |
| 302 | + } |
| 303 | + var value adc.StringOrSlice |
| 304 | + for _, item := range expr.Set { |
| 305 | + value.SliceVal = append(value.SliceVal, adc.StringOrSlice{StrVal: item}) |
| 306 | + } |
| 307 | + this.SliceVal = append(this.SliceVal, value) |
| 308 | + default: |
| 309 | + if expr.Value == nil { |
| 310 | + return result, errors.New("empty value") |
| 311 | + } |
| 312 | + this.SliceVal = append(this.SliceVal, adc.StringOrSlice{StrVal: *expr.Value}) |
| 313 | + } |
| 314 | + |
| 315 | + // append to result |
| 316 | + result = append(result, this.SliceVal) |
| 317 | + } |
| 318 | + |
| 319 | + return result, nil |
| 320 | +} |
| 321 | + |
222 | 322 | // ApisixRoutePluginConfig is the configuration for |
223 | 323 | // any plugins. |
224 | 324 | type ApisixRoutePluginConfig map[string]apiextensionsv1.JSON |
|
0 commit comments