|
13 | 13 | package v2 |
14 | 14 |
|
15 | 15 | import ( |
16 | | - "encoding/json" |
| 16 | + "slices" |
| 17 | + "strings" |
17 | 18 |
|
| 19 | + "github.com/pkg/errors" |
| 20 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
18 | 21 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
19 | 22 | "k8s.io/apimachinery/pkg/util/intstr" |
| 23 | + |
| 24 | + "github.com/apache/apisix-ingress-controller/api/adc" |
20 | 25 | ) |
21 | 26 |
|
22 | 27 | // ApisixRouteSpec is the spec definition for ApisixRouteSpec. |
@@ -119,7 +124,7 @@ type ApisixRouteHTTPMatch struct { |
119 | 124 | // value: |
120 | 125 | // - "127.0.0.1" |
121 | 126 | // - "10.0.5.11" |
122 | | - NginxVars []ApisixRouteHTTPMatchExpr `json:"exprs,omitempty" yaml:"exprs,omitempty"` |
| 127 | + NginxVars ApisixRouteHTTPMatchExprs `json:"exprs,omitempty" yaml:"exprs,omitempty"` |
123 | 128 | // Matches based on a user-defined filtering function. |
124 | 129 | // These functions can accept an input parameter `vars` |
125 | 130 | // which can be used to access the Nginx variables. |
@@ -220,24 +225,101 @@ type ApisixRouteHTTPMatchExpr struct { |
220 | 225 | Value *string `json:"value" yaml:"value"` |
221 | 226 | } |
222 | 227 |
|
223 | | -// ApisixRoutePluginConfig is the configuration for |
224 | | -// any plugins. |
225 | | -type ApisixRoutePluginConfig map[string]any |
| 228 | +type ApisixRouteHTTPMatchExprs []ApisixRouteHTTPMatchExpr |
226 | 229 |
|
227 | | -func (p ApisixRoutePluginConfig) DeepCopyInto(out *ApisixRoutePluginConfig) { |
228 | | - b, _ := json.Marshal(&p) |
229 | | - _ = json.Unmarshal(b, out) |
230 | | -} |
| 230 | +func (exprs ApisixRouteHTTPMatchExprs) ToVars() (result adc.Vars, err error) { |
| 231 | + for _, expr := range exprs { |
| 232 | + if expr.Subject.Name == "" && expr.Subject.Scope != ScopePath { |
| 233 | + return result, errors.New("empty subject.name") |
| 234 | + } |
| 235 | + |
| 236 | + // process key |
| 237 | + var ( |
| 238 | + subj string |
| 239 | + this adc.StringOrSlice |
| 240 | + ) |
| 241 | + switch expr.Subject.Scope { |
| 242 | + case ScopeQuery: |
| 243 | + subj = "arg_" + expr.Subject.Name |
| 244 | + case ScopeHeader: |
| 245 | + subj = "http_" + strings.ReplaceAll(strings.ToLower(expr.Subject.Name), "-", "_") |
| 246 | + case ScopeCookie: |
| 247 | + subj = "cookie_" + expr.Subject.Name |
| 248 | + case ScopePath: |
| 249 | + subj = "uri" |
| 250 | + case ScopeVariable: |
| 251 | + subj = expr.Subject.Name |
| 252 | + default: |
| 253 | + return result, errors.New("invalid http match expr: subject.scope should be one of [query, header, cookie, path, variable]") |
| 254 | + } |
| 255 | + this.SliceVal = append(this.SliceVal, adc.StringOrSlice{StrVal: subj}) |
| 256 | + |
| 257 | + // process operator |
| 258 | + var ( |
| 259 | + op string |
| 260 | + ) |
| 261 | + switch expr.Op { |
| 262 | + case OpEqual: |
| 263 | + op = "==" |
| 264 | + case OpGreaterThan: |
| 265 | + op = ">" |
| 266 | + case OpGreaterThanEqual: |
| 267 | + op = ">=" |
| 268 | + case OpIn: |
| 269 | + op = "in" |
| 270 | + case OpLessThan: |
| 271 | + op = "<" |
| 272 | + case OpLessThanEqual: |
| 273 | + op = "<=" |
| 274 | + case OpNotEqual: |
| 275 | + op = "~=" |
| 276 | + case OpNotIn: |
| 277 | + op = "in" |
| 278 | + case OpRegexMatch: |
| 279 | + op = "~~" |
| 280 | + case OpRegexMatchCaseInsensitive: |
| 281 | + op = "~*" |
| 282 | + case OpRegexNotMatch: |
| 283 | + op = "~~" |
| 284 | + case OpRegexNotMatchCaseInsensitive: |
| 285 | + op = "~*" |
| 286 | + default: |
| 287 | + return result, errors.New("unknown operator") |
| 288 | + } |
| 289 | + if invert := slices.Contains([]string{OpNotIn, OpRegexNotMatch, OpRegexNotMatchCaseInsensitive}, op); invert { |
| 290 | + this.SliceVal = append(this.SliceVal, adc.StringOrSlice{StrVal: "!"}) |
| 291 | + } |
| 292 | + this.SliceVal = append(this.SliceVal, adc.StringOrSlice{StrVal: op}) |
231 | 293 |
|
232 | | -func (p *ApisixRoutePluginConfig) DeepCopy() *ApisixRoutePluginConfig { |
233 | | - if p == nil { |
234 | | - return nil |
| 294 | + // process value |
| 295 | + switch expr.Op { |
| 296 | + case OpIn, OpNotIn: |
| 297 | + if expr.Set == nil { |
| 298 | + return result, errors.New("empty set value") |
| 299 | + } |
| 300 | + var value adc.StringOrSlice |
| 301 | + for _, item := range expr.Set { |
| 302 | + value.SliceVal = append(value.SliceVal, adc.StringOrSlice{StrVal: item}) |
| 303 | + } |
| 304 | + this.SliceVal = append(this.SliceVal, value) |
| 305 | + default: |
| 306 | + if expr.Value == nil { |
| 307 | + return result, errors.New("empty value") |
| 308 | + } |
| 309 | + this.SliceVal = append(this.SliceVal, adc.StringOrSlice{StrVal: *expr.Value}) |
| 310 | + } |
| 311 | + |
| 312 | + // append to result |
| 313 | + result = append(result, this.SliceVal) |
235 | 314 | } |
236 | | - out := new(ApisixRoutePluginConfig) |
237 | | - p.DeepCopyInto(out) |
238 | | - return out |
| 315 | + |
| 316 | + return result, nil |
239 | 317 | } |
240 | 318 |
|
| 319 | +// ApisixRoutePluginConfig is the configuration for |
| 320 | +// any plugins. |
| 321 | +type ApisixRoutePluginConfig map[string]apiextensionsv1.JSON |
| 322 | + |
241 | 323 | // ApisixRouteAuthenticationKeyAuth is the keyAuth-related |
242 | 324 | // configuration in ApisixRouteAuthentication. |
243 | 325 | type ApisixRouteAuthenticationKeyAuth struct { |
|
0 commit comments