diff --git a/inputs/prod_inner.json b/inputs/prod_inner.json index 33147c6d9..bf56a5dc6 100644 --- a/inputs/prod_inner.json +++ b/inputs/prod_inner.json @@ -135,6 +135,12 @@ } ], "exit_plugins": [ + { + "name": "delete_from_registry", + "args": { + "registries": {} + } + }, { "name": "koji_promote", "args": { diff --git a/osbs/build/build_request.py b/osbs/build/build_request.py index 566e41dd1..d92af88af 100644 --- a/osbs/build/build_request.py +++ b/osbs/build/build_request.py @@ -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') @@ -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'): @@ -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): """ diff --git a/tests/build/test_build_request.py b/tests/build/test_build_request.py index 9178d7da5..5e4d8e54b 100644 --- a/tests/build/test_build_request.py +++ b/tests/build/test_build_request.py @@ -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"] @@ -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 @@ -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", @@ -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')) @@ -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) @@ -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,