Skip to content

Commit

Permalink
build_request: configure delete_from_registry
Browse files Browse the repository at this point in the history
  • Loading branch information
mmilata committed Aug 18, 2016
1 parent d902045 commit 40ac6b8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
6 changes: 6 additions & 0 deletions inputs/prod_inner.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@
}
],
"exit_plugins": [
{
"name": "delete_from_registry",
"args": {
"registries": {}
}
},
{
"name": "koji_promote",
"args": {
Expand Down
27 changes: 24 additions & 3 deletions osbs/build/build_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,9 @@ def adjust_for_registry_api_versions(self):

if 'v2' not in versions:
# Remove v2-only plugins
logger.info("removing v2-only plugin: pulp_sync")
logger.info("removing v2-only plugins: pulp_sync, delete_from_registry")
self.dj.remove_plugin('postbuild_plugins', 'pulp_sync')
self.dj.remove_plugin('exit_plugins', 'delete_from_registry')

# remove extra tag_and_push config
self.remove_tag_and_push_registries(tag_and_push_registries, 'v2')
Expand Down Expand Up @@ -558,7 +559,8 @@ def render_pulp_push(self):

def render_pulp_sync(self):
"""
If a pulp registry is specified, use the pulp plugin
If a pulp registry is specified, use the pulp plugin as well as the
delete_from_registry to delete the image after sync
"""
if not self.dj.dock_json_has_plugin_conf('postbuild_plugins',
'pulp_sync'):
Expand Down Expand Up @@ -598,11 +600,30 @@ def render_pulp_sync(self):
if self.spec.pulp_secret.value is None:
raise OsbsValidationException("Pulp registry specified "
"but no auth config")

source_registry = self.spec.source_registry_uri.value
perform_delete = (source_registry is None or
source_registry.docker_uri != registry.docker_uri)
if perform_delete:
delete_registries = {docker_registry: {}}
if registry_secret:
delete_registries[docker_registry]['secret'] = \
os.path.join(SECRETS_PATH, registry_secret)
# tag_and_push configured the registry secret, no neet to set it again

self.dj.dock_json_set_arg('exit_plugins', 'delete_from_registry',
'registries', delete_registries)
else:
logger.info("removing delete_from_registry from request, "
"source and target registry are identical")
self.dj.remove_plugin("exit_plugins", "delete_from_registry")
else:
# If no pulp registry is specified, don't run the pulp plugin
logger.info("removing pulp_sync from request, "
logger.info("removing pulp_sync+delete_from_registry from request, "
"requires pulp_registry and a v2 registry")
self.dj.remove_plugin("postbuild_plugins", "pulp_sync")
self.dj.remove_plugin("exit_plugins", "delete_from_registry")


def render_import_image(self, use_auth=None):
"""
Expand Down
33 changes: 32 additions & 1 deletion tests/build/test_build_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ def test_render_prod_request_with_repo(self, architecture, build_image, build_im
get_plugin(plugins, "postbuild_plugins", "import_image")
with pytest.raises(NoSuchPluginException):
get_plugin(plugins, "exit_plugins", "sendmail")
with pytest.raises(NoSuchPluginException):
get_plugin(plugins, 'exit_plugins', 'delete_from_registry')
assert 'sourceSecret' not in build_json["spec"]["source"]
assert plugin_value_get(plugins, "prebuild_plugins", "add_yum_repo_by_url",
"args", "repourls") == ["http://example.com/my.repo"]
Expand Down Expand Up @@ -396,6 +398,8 @@ def test_render_prod_request(self, proxy):
get_plugin(plugins, "postbuild_plugins", "import_image")
with pytest.raises(NoSuchPluginException):
get_plugin(plugins, "exit_plugins", "sendmail")
with pytest.raises(NoSuchPluginException):
get_plugin(plugins, 'exit_plugins', 'delete_from_registry')
assert get_plugin(plugins, "exit_plugins", "koji_promote")
assert plugin_value_get(plugins, "exit_plugins", "koji_promote", "args",
"target") == koji_target
Expand Down Expand Up @@ -478,6 +482,8 @@ def test_render_prod_without_koji_request(self):
get_plugin(plugins, "exit_plugins", "koji_promote")
with pytest.raises(NoSuchPluginException):
get_plugin(plugins, "exit_plugins", "sendmail")
with pytest.raises(NoSuchPluginException):
get_plugin(plugins, 'exit_plugins', 'delete_from_registry')
assert 'sourceSecret' not in build_json["spec"]["source"]

labels = plugin_value_get(plugins, "prebuild_plugins", "add_labels_in_dockerfile",
Expand Down Expand Up @@ -548,11 +554,14 @@ def test_render_prod_with_secret_request(self):
get_plugin(plugins, "postbuild_plugins", "import_image")
with pytest.raises(NoSuchPluginException):
get_plugin(plugins, "exit_plugins", "sendmail")
with pytest.raises(NoSuchPluginException):
get_plugin(plugins, 'exit_plugins', 'delete_from_registry')
assert plugin_value_get(plugins, "postbuild_plugins", "tag_and_push", "args",
"registries") == {}

@pytest.mark.parametrize('registry_secrets', [None, ['registry-secret']])
def test_render_pulp_sync(self, registry_secrets):
@pytest.mark.parametrize('source_registry', [None, 'registry.example.com', 'localhost'])
def test_render_pulp_sync(self, registry_secrets, source_registry):
build_request = BuildRequest(INPUTS_PATH)
# OpenShift Origin >= 1.0.6 is required for v2
build_request.set_openshift_required_version(parse_version('1.0.6'))
Expand Down Expand Up @@ -580,6 +589,9 @@ def test_render_pulp_sync(self, registry_secrets):
'pulp_registry': pulp_env,
'pulp_secret': pulp_secret,
}
if source_registry:
kwargs['source_registry_uri'] = source_registry

build_request.set_params(**kwargs)
build_json = build_request.render()
plugins = get_plugins_from_build_json(build_json)
Expand All @@ -592,6 +604,25 @@ def test_render_pulp_sync(self, registry_secrets):
'pulp_sync', 'args',
'docker_registry') == registry_uri

if source_registry and source_registry in kwargs['registry_uri']:
with pytest.raises(NoSuchPluginException):
get_plugin(plugins, 'exit_plugins', 'delete_from_registry')
else:
assert get_plugin(plugins, 'exit_plugins', 'delete_from_registry')
assert 'https://registry.example.com' in plugin_value_get(plugins, 'exit_plugins',
'delete_from_registry',
'args', 'registries')

if registry_secrets:
assert plugin_value_get(plugins, 'exit_plugins',
'delete_from_registry', 'args',
'registries', 'https://registry.example.com', 'secret')
else:
assert plugin_value_get(plugins, 'exit_plugins',
'delete_from_registry', 'args',
'registries', 'https://registry.example.com') == {}


assert 'sourceSecret' not in build_json['spec']['source']
if registry_secrets:
mount_path = get_secret_mountpath_by_name(build_json,
Expand Down

0 comments on commit 40ac6b8

Please sign in to comment.