Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Add Regex support to OMPS
Browse files Browse the repository at this point in the history
The current pattern for registry string replacement only matches on whole
strings.  This pattern is very broad and lacks the precision to handle corner
cases like strings that only end or begin with '/'.  Regexes provide the
precision needed to expand pattern matching to handle more precise string replacement.

Make regexp backwards compatible

Change strings to bools
  • Loading branch information
rthallisey authored and ralphbean committed Oct 9, 2019
1 parent f5b2244 commit becf6c6
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 7 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,23 @@ organization:
},
]
```
all specified `old` registries will be replaced by `new` in all manifests yaml
files for that organization. Replacement happen during pushing manifests into
application registry.
All specified `old` registries will be replaced by `new` in all manifests yaml
files for that organization.

You can pattern match and replace registry strings with the regexp field instead
of matching whole strings. Both `old` and `new` will be evalutated as regexes
when `regexp` is set to `True`. If `regexp` is missing it defaults to `False`.
Here's an example:
```
"replace_registry": [
{
"old": "quay.io$",
"new": "example.com",
"regexp": True,
},
]
```
Replacements occur when pushing manifests into the application registry.

### Greenwave integration

Expand Down
1 change: 1 addition & 0 deletions example.test.env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private_org:
replace_registry:
- old: registry.stage.redhat.io
new: registry.redhat.io
regexp: false
# Config of the instance tested to check if Greenwave policies are met for the
# NVR
greenwave:
Expand Down
10 changes: 8 additions & 2 deletions omps/quay.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,15 @@ def replace_registries(self, text):
return text

for conf in self._replace_registry_conf:
old = conf['old']
new = conf['new']
if old in text:
old = conf['old']
regexp = conf.get('regexp', False)

if regexp:
if re.search(old, text, flags=re.MULTILINE):
self.logger.info("Replacing registries with regexp '%s'", regexp)
text = re.sub(old, new, text, flags=re.MULTILINE)
elif old in text:
self.logger.info("Replacing registry '%s' with '%s'", old, new)
text = text.replace(old, new)

Expand Down
2 changes: 1 addition & 1 deletion tests/api/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _yield_yaml_files(dir_path):
old = 'quay.io'
new = 'example.com'
qo = QuayOrganization('testorg', 'random token', replace_registry_conf=[
{'old': old, 'new': new}
{'old': old, 'new': new, 'regexp': False}
])

should_be_replaced = set()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def koji_registries(replace_conf, path):
the manifest at 'path'
Args:
replace_conf: List of {'old': ..., 'new': ...} dictionaries.
replace_conf: List of {'old': ..., 'new': ..., 'regexp': ...} dictionaries.
path: Path of the manifest.
Returns:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_quay.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,19 @@ def test_replace_registries_unconfigured(self):
assert res == text
assert id(res) == id(text)

@pytest.mark.parametrize('text,expected', [
(
'Registry reg_old will be replaced using a regexp: reg_old',
'Registry reg_old will be replaced using a regexp: reg_new',
)
])
def test_regexp_replace_registries(self, text, expected):
"""Test if registries are replaced properly with regexp"""
replace_conf = [{'old': 'reg_old$', 'new': 'reg_new', 'regexp': True}]
org = 'testorg'
qo = QuayOrganization(org, TOKEN, replace_registry_conf=replace_conf)
assert qo.replace_registries(text) == expected


class TestOrgManager:
"""Tets for OrgManager class"""
Expand Down
29 changes: 29 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,29 @@ class ConfClass(DefaultConfig):
assert conf.organizations == expected


def test_organziation_regexp():
"""Organziation with regexp Test"""
expected = {
'myorg': {
'public': False,
'oauth_token': 'token',
'replace_registry': [
{
'old': 'quay.io/$',
'new': 'example.com',
'regexp': True,
},
]
}
}

class ConfClass(DefaultConfig):
ORGANIZATIONS = expected

conf = Config(ConfClass)
assert conf.organizations == expected


@pytest.mark.parametrize('conf', [
{
'organization': None
Expand Down Expand Up @@ -161,6 +184,12 @@ class ConfClass(DefaultConfig):
{'old': 'expected new too'}
]
}
}, {
'organization': {
'replace_registry': [
{'regexp': False}
]
}
}
])
Expand Down

0 comments on commit becf6c6

Please sign in to comment.