Skip to content

Commit 08ab630

Browse files
authored
Bug 1572133 - Update apb remove --all -l to delete all APB images (#276)
1 parent 20e382c commit 08ab630

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

src/apb/engine.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,18 @@ def build_apb(project, dockerfile=None, tag=None):
893893
return tag
894894

895895

896+
def get_registry_images():
897+
try:
898+
openshift_config.load_kube_config()
899+
oapi = openshift_client.OapiApi()
900+
image_list = oapi.list_image(_preload_content=False)
901+
image_list = json.loads(image_list.data)
902+
except Exception as e:
903+
print("Exception retrieving list of images: %s" % e)
904+
raise Exception("Unable to retrieve images in local registry")
905+
return image_list
906+
907+
896908
def get_registry(kwargs):
897909
namespace = kwargs['reg_namespace']
898910
service = kwargs['reg_svc_name']
@@ -914,10 +926,8 @@ def delete_old_images(image_name):
914926
# Let's ignore the registry prefix for now because sometimes our tag doesn't match the registry
915927
registry, image_name = image_name.split('/', 1)
916928
try:
917-
openshift_config.load_kube_config()
918929
oapi = openshift_client.OapiApi()
919-
image_list = oapi.list_image(_preload_content=False)
920-
image_list = json.loads(image_list.data)
930+
image_list = get_registry_images()
921931
for image in image_list['items']:
922932
image_fqn, image_sha = image['dockerImageReference'].split("@")
923933
if image_name in image_fqn:
@@ -1150,25 +1160,37 @@ def cmdrun_push(**kwargs):
11501160

11511161

11521162
def cmdrun_remove(**kwargs):
1153-
if kwargs["all"]:
1163+
images = []
1164+
if kwargs["all"] and not kwargs["local"]:
11541165
route = "/v2/apb"
11551166
old_route = "/apb/spec"
11561167
elif kwargs["id"] is not None:
11571168
route = "/v2/apb/" + kwargs["id"]
11581169
old_route = "/apb/spec/" + kwargs["id"]
11591170
elif kwargs["local"] is True:
1160-
print("Attempting to delete associated registry image.")
1161-
project = kwargs['base_path']
1162-
spec = get_spec(project, 'dict')
1163-
kwargs['reg_namespace'] = "default"
1164-
kwargs['reg_svc_name'] = "docker-registry"
1165-
kwargs['reg_route'] = None
1166-
kwargs['namespace'] = "openshift"
1167-
1168-
registry = get_registry(kwargs)
1169-
tag = registry + "/" + kwargs['namespace'] + "/" + spec['name']
1170-
print("Image: [%s]" % tag)
1171-
delete_old_images(tag)
1171+
if kwargs["all"]:
1172+
print("Attempting to remove all registry images ending in: *-apb")
1173+
image_list = get_registry_images()
1174+
for image in image_list['items']:
1175+
image_fqn, image_sha = image['dockerImageReference'].split("@")
1176+
if "-apb" in image_fqn:
1177+
images.append(image_fqn)
1178+
else:
1179+
print("Attempting to delete associated registry image.")
1180+
project = kwargs['base_path']
1181+
spec = get_spec(project, 'dict')
1182+
kwargs['reg_namespace'] = "default"
1183+
kwargs['reg_svc_name'] = "docker-registry"
1184+
kwargs['reg_route'] = None
1185+
kwargs['namespace'] = "openshift"
1186+
1187+
registry = get_registry(kwargs)
1188+
tag = registry + "/" + kwargs['namespace'] + "/" + spec['name']
1189+
images.append(tag)
1190+
1191+
for image in images:
1192+
delete_old_images(image)
1193+
11721194
bootstrap(
11731195
kwargs["broker"],
11741196
kwargs.get("basic_auth_username"),

0 commit comments

Comments
 (0)