3

I am setting up a GCP url map to route requests to backend services based on cookie values. Since cookies would have multiple key values, I am trying to use a regex matcher. I need to route requests to backends based on region value from cookie. A typical cookie would look like this: foo=bar;region=eu;variant=beta;

defaultService: https://www.googleapis.com/compute/v1/projects/<project_id>/global/backendServices/multi-region-1
kind: compute#urlMap
name: regex-url-map
hostRules:
- hosts:
  - '*'
  pathMatcher: path-matcher-1
pathMatchers:
- defaultService: https://www.googleapis.com/compute/v1/projects/<project_id>/global/backendServices/multi-region-1
  name: path-matcher-1
  routeRules:
    - matchRules:
        - prefixMatch: /
          headerMatches:
            - headerName: Cookie
              regexMatch: (region=us)
      priority: 0
      service: https://www.googleapis.com/compute/v1/projects/<project_id>/global/backendServices/multi-region-1
    - matchRules:
        - prefixMatch: /
          headerMatches:
            - headerName: Cookie
              regexMatch: (region=eu)
      priority: 1
      service: https://www.googleapis.com/compute/v1/projects/<project_id>/global/backendServices/multi-region-2

However, this url-map fails validation with this error:

$ gcloud compute url-maps validate --source regex-url-map.yaml
result:
  loadErrors:
  - HttpHeaderMatch has no predicates specified
  loadSucceeded: false
  testPassed: false

Please note that an exact match with cookie passes validation and matches correctly if cookie value is just something like this: region=us. The headerMatches section for exact match would look like this:

          headerMatches:
            - headerName: Cookie
              exactMatch: region=us

Any pointers on what am I doing wrong here? Thanks!

Santosh Kewat
  • 522
  • 6
  • 19
  • I'm trying to get my haad around this - can you provide more example what worked and what didn't ? – Wojtek_B Jul 15 '21 at 15:13
  • 1
    Sure, if we use `exactMatch` (as shown in the snippet at the end, instead of regex match in the url-map definition at the top), we are able to use `curl --cookie 'region=us' ` and the routing works correctly. However, since my cookies would compose of multiple key-value pairs (e.g. foo=bar;region=eu;x=y), I would like to use a regex and take routing decisions based on whether or not "region=eu" exists. That does not work, we are always routed to the default backend service. – Santosh Kewat Jul 15 '21 at 15:25
  • I have the same issue, did you find a solution? – Shawn Jan 28 '22 at 19:51
  • No, raised this bug: https://issuetracker.google.com/issues/193648377 – Santosh Kewat Jan 11 '23 at 23:27

1 Answers1

4

Your way of reasoning is correct but the feature you're trying to use is unsupported in external load balancing in GCP; it works only with internal load balancing.

Look at the last phrase from the documentation:

Note that regexMatch only applies to Loadbalancers that have their loadBalancingScheme set to INTERNAL_SELF_MANAGED.

enter image description here

I know it isn't the answer you're looking for but you can always file a new feature request on Google's IssueTracker and explain in detail what you want, how it could work etc.

You can always try to pass the region value in the http request - instead of requesting https://myhost.com all the time - also if you could add a suffix, for example: https://myhost.com/region1 it would allow the GCP load balancer rules to process it and direct the traffic to the backend you wish.

Have a look at this example what you can and can't do with forwarding rules in GCP. Another example here. And another one (mine) explaining how to use pathMatcher to direct traffic to different backend services.

Wojtek_B
  • 4,245
  • 1
  • 7
  • 21
  • Thanks @wojtek-b for this detailed explanation and suggestions. I would prefer to avoid putting region in URL to make the experience seamless across customers mapped to different regions. Internal load balancers are restricted to their regions, so it does not help me. I want users to get routed to backend servers belonging to different regions. I am going to file a feature request. – Santosh Kewat Jul 16 '21 at 09:16
  • 1
    Hello @SantoshKewat I see this answered helped you. You can react to the answer by giving it a +1 or by accepting it. This way other people will know the answer was helpful and consider it. – Mark Watney Jul 16 '21 at 10:48
  • @SantoshKewat - did you file a feature request we can vote on. We are hitting the same issue. – Bronumski Jan 10 '23 at 21:44
  • I raised a bug here: https://issuetracker.google.com/issues/193648377 – Santosh Kewat Jan 11 '23 at 23:27