-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug: make canary groups account for host mappingSelector labels (#5215)
If two mappings have different host labels but share the same prefix, then they should not be placed in the same canary group. Previously, labels were not taken into account when grouping mappings into canary groups, which caused mappings sharing the same prefix to be part of the same canary group when using host labels (hostname or host field was not specified in the mapping). In order to solve this, we update the mapping group_id logic to incorporate host selector labels. This means that the mappings are grouped based on the following criteria: - HTTP method - Prefix - Headers - Query parameters - Host labels - Precedence Because this affects only how mappings are grouped, the following behavior is retained: - hostname, host, and :authority header fields take precendence over host labels for the purposes of grouping. - If hostname, host, and/or :authority header fields are specified along with host labels, then they must match before they can be associated. - A canary group can be associated with multiple Hosts based on selector labels. Fixes #4170. --------- Signed-off-by: Hamzah Qudsi <[email protected]>
- Loading branch information
Hamzah Qudsi
authored
Aug 14, 2023
1 parent
31734e5
commit eef86a4
Showing
17 changed files
with
448 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import json | ||
import os | ||
from dataclasses import dataclass | ||
from typing import List, Optional | ||
|
||
import pytest | ||
import yaml | ||
|
||
from tests.utils import compile_with_cachecheck | ||
|
||
|
||
@dataclass | ||
class MappingGroupTestOutput: | ||
group_id: str | ||
host: Optional[str] | ||
prefix: str | ||
mappings: List[str] | ||
|
||
|
||
test_cases = [ | ||
"mapping_selector", | ||
"mapping_selector_and_authority", | ||
"mapping_selector_and_hostname", | ||
"mapping_selector_and_host", | ||
"mapping_selector_to_multiple_hosts", | ||
"mapping_selector_irrelevant_labels", | ||
] | ||
|
||
|
||
@pytest.mark.compilertest | ||
@pytest.mark.parametrize("test_case", test_cases) | ||
def test_mapping_canary_group_selectors(test_case): | ||
testdata_dir = os.path.join( | ||
os.path.dirname(os.path.abspath(__file__)), "testdata", "canary_groups" | ||
) | ||
|
||
with open(os.path.join(testdata_dir, f"{test_case}_in.yaml"), "r") as f: | ||
test_yaml = f.read() | ||
|
||
r = compile_with_cachecheck(test_yaml, errors_ok=True) | ||
|
||
ir = r["ir"] | ||
|
||
errors = ir.aconf.errors | ||
assert len(errors) == 0, "Expected no errors but got %s" % ( | ||
json.dumps(errors, sort_keys=True, indent=4) | ||
) | ||
|
||
mapping_groups = [] | ||
for g in ir.groups.values(): | ||
if g.prefix.startswith("/ambassador"): | ||
continue | ||
|
||
mg = MappingGroupTestOutput( | ||
group_id=g.group_id, host=g.host, prefix=g.prefix, mappings=[m.name for m in g.mappings] | ||
) | ||
mapping_groups.append(mg) | ||
|
||
with open(os.path.join(testdata_dir, f"{test_case}_out.yaml"), "r") as f: | ||
out = yaml.safe_load(f) | ||
|
||
expected_output = [MappingGroupTestOutput(**group_yaml) for group_yaml in out["mapping_groups"]] | ||
assert sorted(mapping_groups, key=lambda g: g.group_id) == sorted( | ||
expected_output, key=lambda g: g.group_id | ||
) |
47 changes: 47 additions & 0 deletions
47
python/tests/unit/testdata/canary_groups/mapping_selector_and_authority_in.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
apiVersion: getambassador.io/v3alpha1 | ||
kind: Host | ||
metadata: | ||
name: foo-host | ||
namespace: default | ||
spec: | ||
hostname: foo.example.com | ||
mappingSelector: | ||
matchLabels: | ||
host: foo | ||
--- | ||
apiVersion: getambassador.io/v3alpha1 | ||
kind: Host | ||
metadata: | ||
name: bar-host | ||
namespace: default | ||
spec: | ||
hostname: bar.example.com | ||
mappingSelector: | ||
matchLabels: | ||
host: bar | ||
--- | ||
apiVersion: getambassador.io/v3alpha1 | ||
kind: Mapping | ||
metadata: | ||
name: star-backend-foo | ||
namespace: default | ||
labels: | ||
host: foo | ||
spec: | ||
prefix: /test/ | ||
service: star | ||
headers: | ||
":authority": foo.example.com | ||
--- | ||
apiVersion: getambassador.io/v3alpha1 | ||
kind: Mapping | ||
metadata: | ||
name: star-backend-bar | ||
namespace: default | ||
labels: | ||
host: bar | ||
spec: | ||
prefix: /test/ | ||
service: star | ||
headers: | ||
":authority": bar.example.com |
12 changes: 12 additions & 0 deletions
12
python/tests/unit/testdata/canary_groups/mapping_selector_and_authority_out.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
mapping_groups: | ||
- group_id: '1a5a176cc92a69ab669ac332644324fe7813e889' | ||
host: 'foo.example.com' | ||
prefix: /test/ | ||
mappings: | ||
- star-backend-foo | ||
|
||
- group_id: '71cf9a8de7cec920d057da6ae1a1d304dc599a05' | ||
host: 'bar.example.com' | ||
prefix: /test/ | ||
mappings: | ||
- star-backend-bar |
45 changes: 45 additions & 0 deletions
45
python/tests/unit/testdata/canary_groups/mapping_selector_and_host_in.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
apiVersion: getambassador.io/v3alpha1 | ||
kind: Host | ||
metadata: | ||
name: foo-host | ||
namespace: default | ||
spec: | ||
hostname: foo.example.com | ||
mappingSelector: | ||
matchLabels: | ||
host: foo | ||
--- | ||
apiVersion: getambassador.io/v3alpha1 | ||
kind: Host | ||
metadata: | ||
name: bar-host | ||
namespace: default | ||
spec: | ||
hostname: bar.example.com | ||
mappingSelector: | ||
matchLabels: | ||
host: bar | ||
--- | ||
apiVersion: getambassador.io/v3alpha1 | ||
kind: Mapping | ||
metadata: | ||
name: star-backend-foo | ||
namespace: default | ||
labels: | ||
host: foo | ||
spec: | ||
prefix: /test/ | ||
service: star | ||
host: foo.example.com | ||
--- | ||
apiVersion: getambassador.io/v3alpha1 | ||
kind: Mapping | ||
metadata: | ||
name: star-backend-bar | ||
namespace: default | ||
labels: | ||
host: bar | ||
spec: | ||
prefix: /test/ | ||
service: star | ||
host: bar.example.com |
12 changes: 12 additions & 0 deletions
12
python/tests/unit/testdata/canary_groups/mapping_selector_and_host_out.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
mapping_groups: | ||
- group_id: '1a5a176cc92a69ab669ac332644324fe7813e889' | ||
host: foo.example.com | ||
prefix: /test/ | ||
mappings: | ||
- star-backend-foo | ||
|
||
- group_id: '71cf9a8de7cec920d057da6ae1a1d304dc599a05' | ||
host: bar.example.com | ||
prefix: /test/ | ||
mappings: | ||
- star-backend-bar |
Oops, something went wrong.