-
Notifications
You must be signed in to change notification settings - Fork 30
/
mirror-operator-catalogue.py
executable file
·788 lines (663 loc) · 27.5 KB
/
mirror-operator-catalogue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
#!/usr/bin/env python3
import os
import sys
import re
import tarfile
import yaml
import subprocess
import argparse
import urllib.request
from jinja2 import Template
from pathlib import Path
import upgradepath
import sqlite3
import json
import shutil
from natsort import natsorted
def is_number(string):
try:
float(string)
return True
except ValueError:
return False
parser = argparse.ArgumentParser(
description='Mirror individual operators to an offline registry')
parser.add_argument(
"--authfile",
default=None,
help="Pull secret with credentials")
parser.add_argument(
"--registry-olm",
metavar="REGISTRY",
required=True,
help="Registry to copy the operator images")
parser.add_argument(
"--registry-catalog",
metavar="REGISTRY",
required=True,
help="Registry to copy the catalog image")
parser.add_argument(
"--catalog-version",
default="1.0.0",
help="Tag for the catalog image")
parser.add_argument(
"--ocp-version",
default="4.8",
help="OpenShift Y Stream. Only use X.Y version do not use Z. Default 4.8")
parser.add_argument(
"--operator-channel",
default="4.8",
help="Operator Channel. Default 4.8")
parser.add_argument(
"--operator-image-name",
default="redhat-operators",
help="Operator Image short Name. Default redhat-operators")
parser.add_argument(
"--operator-catalog-image-url",
default="registry.redhat.io/redhat/redhat-operator-index",
help="Operator Index Image URL without version. Default registry.redhat.io/redhat/redhat-operator-index")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
"--operator-list",
nargs="*",
metavar="OPERATOR",
help="List of operators to mirror, space delimeted")
group.add_argument(
"--operator-file",
metavar="FILE",
help="Specify a file containing the operators to mirror")
group.add_argument(
"--operator-yaml-file",
metavar="FILE",
help="Specify a YAML file containing operator list to mirror")
parser.add_argument(
"--icsp-scope",
default="namespace",
help="Scope of registry mirrors in imagecontentsourcepolicy file. Allowed values: namespace, registry. Defaults to: namespace")
parser.add_argument(
"--output",
default="publish",
help="Directory to create YAML files, must be relative to script path")
parser.add_argument(
"--mirror-images",
default="True",
help="Boolean: Mirror related images. Default is True")
parser.add_argument(
"--add-tags-to-images-mirrored-by-digest",
default="False",
help="Boolean: add tags to images mirrored by digest. Default is False")
parser.add_argument(
"--delete-publish",
default="True",
help="Boolean: Delete publish directory. Default is True")
parser.add_argument(
"--run-dir",
default="",
help="Run directory for script, must be an absolute path, only handy if running script in a container")
parser.add_argument(
"--opm-path",
default="",
help="Full path of the opm binary if you want to use your own instead of the tool downloading it for you")
parser.add_argument(
"--oc-cli-path",
default="oc",
help="Full path of oc cli")
parser.add_argument(
"--custom-operator-catalog-image-url",
default="",
help="DO NOT USE THIS - This argument is depricated and will no longer work")
parser.add_argument(
"--custom-operator-catalog-image-and-tag",
default="",
help="custom operator catalog image name including the tag")
parser.add_argument(
"--custom-operator-catalog-name",
default="custom-redhat-operators",
help="custom operator catalog name")
try:
args = parser.parse_args()
except Exception as exc:
print("An exception occurred while parsing arguements list")
print(exc)
sys.exit(1)
# Global Variables
if args.run_dir != "":
script_root_dir = args.run_dir
else:
script_root_dir = os.path.dirname(os.path.realpath(__file__))
publish_root_dir = os.path.join(script_root_dir, args.output)
run_root_dir = os.path.join(script_root_dir, "run")
mirror_images = args.mirror_images
add_tags_to_images_mirrored_by_digest = args.add_tags_to_images_mirrored_by_digest
delete_publish = args.delete_publish
operator_image_list = []
operator_data_list = {}
operator_known_bad_image_list_file = os.path.join(
script_root_dir, "known-bad-images")
quay_rh_base_url = "https://quay.io/cnr/api/v1/packages/"
redhat_operators_image_name = args.operator_image_name
redhat_operators_packages_url = "https://quay.io/cnr/api/v1/packages?namespace=" + args.operator_image_name
image_content_source_policy_template_file = os.path.join(
script_root_dir, "image-content-source-template")
catalog_source_template_file = os.path.join(
script_root_dir, "catalog-source-template")
ocp_version = args.ocp_version
operator_channel = args.operator_channel
operator_index_version = ":v" + operator_channel if is_number(operator_channel) else ":" + operator_channel
redhat_operators_catalog_image_url = args.operator_catalog_image_url + operator_index_version
custom_redhat_operators_image_name = "custom-" + args.operator_image_name
custom_redhat_operators_display_name = re.sub(r'^redhat-', 'red hat-', args.operator_image_name)
custom_redhat_operators_display_name = re.sub('-', ' ', custom_redhat_operators_display_name).title()
if args.custom_operator_catalog_image_url:
print("--custom-operator-catalog-image-url is no longer supported. \n")
print("Use --custom-operator-catalog-image-and-tag instead")
exit(1)
elif args.custom_operator_catalog_image_and_tag:
custom_redhat_operators_catalog_image_url = args.registry_catalog + "/" + args.custom_operator_catalog_image_and_tag
elif args.custom_operator_catalog_name:
custom_redhat_operators_catalog_image_url = args.registry_catalog + "/" + args.custom_operator_catalog_name + ":" + args.catalog_version
else:
custom_redhat_operators_catalog_image_url = args.registry_catalog + "/custom-" + args.operator_catalog_image_url.split('/')[2] + ":" + args.catalog_version
oc_cli_path = args.oc_cli_path
image_content_source_policy_output_file = os.path.join(
publish_root_dir, custom_redhat_operators_image_name + '--icsp.yaml')
catalog_source_output_file = os.path.join(
publish_root_dir, custom_redhat_operators_image_name + '--catalogsource.yaml')
mapping_file=os.path.join(
publish_root_dir, custom_redhat_operators_image_name + '--mapping.txt')
image_manifest_file = os.path.join(
publish_root_dir, custom_redhat_operators_image_name + '--image_manifest.txt')
mirror_summary_file = os.path.join(
publish_root_dir, custom_redhat_operators_image_name + '--mirror_log.txt')
def main():
run_temp = os.path.join(run_root_dir, "temp")
mirror_summary_path = Path(mirror_summary_file)
# Create publish, run and temp paths
if delete_publish.lower() == "true":
print("Will delete the publish dir...")
delete_publish_bool = True
else:
print("--delete-publish=false Skipping deleting the publish dir")
delete_publish_bool = False
RecreatePath(publish_root_dir, delete_publish_bool)
RecreatePath(run_root_dir)
RecreatePath(run_temp)
print("Starting Catalog Build and Mirror...")
print("Getting opm CLI...")
if args.opm_path != "":
opm_cli_path = args.opm_path
else:
opm_cli_path = GetOpmCli(run_temp)
print("Getting the list of operators for custom catalogue..")
operators = GetWhiteListedOperators()
# # NEED TO BE LOGGED IN TO REGISTRY.REDHAT.IO WITHOUT AUTHFILE ARGUMENT
print("Pruning OLM catalogue...")
if int(operator_channel.split('.')[0]) > 3 and int(operator_channel.split('.')[1]) > 10:
operators = PruneFileBasedCatalog(opm_cli_path, operators, run_temp)
print("Writing summary data..")
CreateSummaryFileForFileBasedatalog(operators, mirror_summary_path)
else:
PruneSqliteBasedCatalog(opm_cli_path, operators, run_temp)
print("Extracting custom catalogue database...")
db_path = ExtractIndexDb()
print("Create upgrade matrix for selected operators...")
for operator in operators:
operator.upgrade_path = upgradepath.GetShortestUpgradePath(operator.name, operator.start_version, db_path)
print("Getting list of images to be mirrored...")
GetImageListToMirror(operators, db_path)
print("Writing summary data..")
CreateSummaryFile(operators, mirror_summary_path)
images = getImages(operators)
if mirror_images.lower() == "true":
print("Mirroring related images to offline registry...")
MirrorImagesToLocalRegistry(images)
else:
print("--mirror-images=false Skipping image mirroring")
print("Creating Image Content Source Policy YAML...")
CreateImageContentSourcePolicyFile(images)
print("Creating Mapping File...")
CreateMappingFile(images)
print("Creating Image manifest file...")
CreateManifestFile(images)
print("Creating Catalog Source YAML...")
CreateCatalogSourceYaml(custom_redhat_operators_catalog_image_url, custom_redhat_operators_image_name, custom_redhat_operators_display_name)
print("Catalogue creation and image mirroring complete")
print("See Publish folder for the image content source policy and catalog source yaml files to apply to your cluster")
cmd_args = "sudo rm -rf {}".format(run_root_dir)
subprocess.run(cmd_args, shell=True, check=True)
def CreateSummaryFileForFileBasedatalog(operators, mirror_summary_path):
with open(mirror_summary_path, "w") as f:
for operator in operators:
f.write(operator.name + '\n')
f.write("============================================================\n \n")
for bundle in operator.operator_bundles:
f.write(bundle.name + '\n')
f.write("Image List \n")
f.write("---------------------------------------- \n")
for relatedImage in bundle.relatedImages:
f.write(relatedImage["image"] + "\n")
f.write("---------------------------------------- \n \n")
f.write("============================================================\n \n \n")
def GetOcCli(run_temp):
base_url = "https://mirror.openshift.com/pub/openshift-v4/clients/ocp/"
archive_name = "openshift-client-linux.tar.gz"
ocp_bin_channel = "fast"
ocp_bin_release_url= base_url + ocp_bin_channel + "-" + ocp_version + "/" + archive_name
print(ocp_bin_release_url)
archive_file_path = os.path.join(run_temp, archive_name)
print("Downloading oc Cli...")
urllib.request.urlretrieve(ocp_bin_release_url, archive_file_path)
print("Extracting oc Cli...")
tf = tarfile.open(archive_file_path)
tf.extractall(run_root_dir)
return os.path.join(run_root_dir, "oc")
def GetOpmCli(run_temp):
base_url = "https://mirror.openshift.com/pub/openshift-v4/clients/ocp/"
archive_name = "opm-linux.tar.gz"
channel = "fast"
opm_bin_release_url = base_url + channel + "-" + ocp_version + "/" + archive_name
print(opm_bin_release_url)
archive_file_path = os.path.join(run_temp, archive_name)
print("Downloading opm Cli...")
urllib.request.urlretrieve(opm_bin_release_url, archive_file_path)
print("Extracting oc Cli...")
tf = tarfile.open(archive_file_path)
tf.extractall(run_root_dir)
return os.path.join(run_root_dir, "opm")
def GetWhiteListedOperators():
try:
operators = []
operator_list = []
if args.operator_file:
with open(args.operator_file) as f:
operators = f.read().splitlines()
for operator in operators:
operator_list.append(OperatorSpec(operator, ""))
elif args.operator_yaml_file:
with open(args.operator_yaml_file) as f:
data = yaml.safe_load(f)
for operator in data["operators"]:
operator_list.append(OperatorSpec(GetFieldValue(operator, "name"), GetFieldValue(operator, "start_version")))
elif args.operator_list:
operators = args.operator_list
for operator in operators:
operator_list.append(OperatorSpec(operator, ""))
return operator_list
except Exception as exc:
print("An exception occurred while reading operator list file")
print(exc)
sys.exit(1)
def CreateSummaryFile(operators, mirror_summary_path):
with open(mirror_summary_path, "w") as f:
for operator in operators:
f.write(operator.name + '\n')
f.write("Upgrade Path: ")
upgrade_path = operator.start_version + " -> "
for version in operator.upgrade_path:
upgrade_path += version + " -> "
upgrade_path = upgrade_path[:-4]
f.write(upgrade_path)
f.write("\n")
f.write("============================================================\n \n")
for bundle in operator.operator_bundles:
f.write("[Version: " + bundle.version + "]\n")
f.write("Image List \n")
f.write("---------------------------------------- \n")
for image in bundle.relatedImages:
f.write(image + "\n")
f.write("---------------------------------------- \n \n")
f.write("============================================================\n \n \n")
# Returns an empty string if field does not exist
def GetFieldValue(data, field):
if field in data:
return data[field]
else:
return ""
# Read data from rendered index
def readJsonFile(cdata):
objects = []
with open(cdata) as f:
braceCount = 0
jsonStr = ''
for jsonObj in f:
braceCount += jsonObj.count('{')
braceCount -= jsonObj.count('}')
jsonStr += jsonObj
if braceCount == 0:
objects.append(json.loads(jsonStr))
jsonStr = ''
return objects
# Create a custom catalog with selected operators from newer file based catalog
def PruneFileBasedCatalog(opm_cli_path, operators, run_temp):
script_root_dir = os.path.dirname(os.path.realpath(__file__))
prune_path = os.path.join(run_temp, "pruned-catalog")
configs_path = os.path.join(prune_path, "configs")
cdata = os.path.join(configs_path, "data.out")
pdata = os.path.join(configs_path, "index.json")
render_command = f"{opm_cli_path} render {redhat_operators_catalog_image_url}"
if args.authfile:
# Copy to correct folder for opm
HOME = os.getenv('HOME')
docker_cfg = os.path.join(HOME, ".docker", "config.json")
shutil.copyfile(args.authfile, docker_cfg)
else:
print("You must pass an auth file with the '--authfile' option")
exit(1)
#os.chdir(run_temp)
if not os.path.exists(configs_path):
print(f"Creating config path ('{configs_path}')")
os.makedirs(configs_path, exist_ok=True )
if not os.path.exists(cdata):
print(f"Running: '{render_command}'")
data = subprocess.run(render_command, shell=True, check=True, capture_output=True)
with open(cdata, 'a') as out:
out.write(data.stdout.decode('utf-8').strip())
objects = readJsonFile(cdata)
allowed = []
for operator in operators:
allowed.append(operator.name)
operators = []
for obj in objects:
tmp = []
version = ''
if obj['schema'] == 'olm.package' and obj['name'] in allowed:
operator = OperatorSpec(obj['name'], "")
operator.defaultChannel = obj['defaultChannel']
operator.icon = obj['icon']
operators.append(operator)
elif obj['schema'] == 'olm.channel' and obj['package'] in allowed and obj['name'] == operator.defaultChannel:
operator = next(operator for operator in operators if operator.name == obj['package'])
channel = OperatorChannel(obj['name'])
channel.package = obj['package']
for ent in obj['entries']:
tmp.append(ent['name'])
version = natsorted(tmp)[-1]
entry = next((ent for ent in obj['entries'] if ent['name'] == version), None)
channel.entries = [entry]
operator.operator_channels.append(channel)
elif obj['schema'] == 'olm.bundle' and obj['package'] in allowed:
operator = next(operator for operator in operators if operator.name == obj['package'])
for ent in operator.operator_channels[0].entries:
if obj['name'] == ent['name']:
bundle = OperatorBundle(obj['name'], version)
bundle.package = obj['package']
bundle.image = obj['image']
for relatedImage in obj['relatedImages']:
bundle.relatedImages.append(relatedImage)
for property in obj['properties']:
bundle.properties.append(property)
operator.operator_bundles.append(bundle)
# GetFileBasedImageListToMirror(operators)
os.remove(cdata)
print(f"writing index.json")
with open(pdata, 'a') as index:
for operator in operators:
package = { "schema": "olm.package", "name": operator.name, "defaultChannel": operator.defaultChannel, "icon": operator.icon }
index.write(json.dumps(package, indent=2))
for c in operator.operator_channels:
chan = {"schema": "olm.channel","name": c.name, "package": c.package,"entries": c.entries}
index.write(json.dumps(chan, indent=2))
for b in operator.operator_bundles:
bund ={"schema": "olm.bundle","name":b.name, "package": b.package, "image": b.image, "properties": b.properties, "relatedImages": b.relatedImages}
index.write(json.dumps(bund, indent=2))
dockerfile_cmd = f"{opm_cli_path} generate dockerfile {configs_path}"
print(f"Running '{dockerfile_cmd}'")
subprocess.run(dockerfile_cmd, shell=True, check=True, capture_output=True)
os.chdir(prune_path)
build_cmd = f"podman build -t {custom_redhat_operators_catalog_image_url} -f configs.Dockerfile"
print(f"Running '{build_cmd}'")
try:
build_data = subprocess.run(build_cmd, shell=True, check=True, capture_output=True)
except subprocess.CalledProcessError:
print(build_data.stderr.decode('utf-8').strip())
except:
print("Something went wrong building, bailing...")
print(build_data.stdout.decode('utf-8').strip())
push_cmd = f"podman push {custom_redhat_operators_catalog_image_url} --tls-verify=false --authfile {args.authfile}"
print(f"Pushing custom catalogue {custom_redhat_operators_catalog_image_url} to registry...")
print(f"Running '{push_cmd}'")
try:
push_data = subprocess.run(push_cmd, shell=True, check=True, capture_output=True)
except subprocess.CalledProcessError as e:
print("Something went wrong pushing (auth?), bailing...")
exit(1)
print(push_data.stdout.decode('utf-8').strip())
os.chdir(script_root_dir)
return operators
# Create a custom catalogue with selected operators from older sqlite3 based catalog
def PruneSqliteBasedCatalog(opm_cli_path, operators, run_temp):
if args.authfile:
# Copy to correct folder for opm
HOME = os.getenv('HOME')
docker_cfg = os.path.join(HOME, ".docker", "config.json")
shutil.copyfile(args.authfile, docker_cfg)
else:
print("You must pass an auth file with the '--authfile' option")
exit(1)
operator_list = GetListOfCommaDelimitedOperatorList(operators)
cmd = f"{opm_cli_path} index prune -f {redhat_operators_catalog_image_url}"
cmd += f" -p {operator_list}" # local-storage-operator,cluster-logging,kubevirt-hyperconverged "
cmd += f" -t {custom_redhat_operators_catalog_image_url}"
print(f"Running: {cmd}")
os.chdir(run_temp)
subprocess.run(cmd, shell=True, check=True)
generate_command = f"{opm_cli_path} generate dockerfile pruned-catalog/configs"
os.chdir(script_root_dir)
push_cmd = f"podman push {custom_redhat_operators_catalog_image_url} --tls-verify=false --authfile {args.authfile}"
print(f"Pushing custom catalogue {custom_redhat_operators_catalog_image_url} to registry...")
print(f"Running '{push_cmd}'")
subprocess.run(push_cmd, shell=True, check=True)
print("Finished push")
def GetImageListToMirror(operators, db_path):
con = sqlite3.connect(db_path)
cur = con.cursor()
for operator in operators:
for version in operator.upgrade_path:
# Get Operator bundle name
cmd = "select default_channel from package where name like '%" + operator.name + "%';"
result = cur.execute(cmd).fetchall()
if len(result) == 1:
channel = result[0][0]
cmd = "select operatorbundle_name from channel_entry where package_name like '" + operator.name + "' and channel_name like '" + channel + "' and operatorbundle_name like '%" + version + "%';"
result = cur.execute(cmd).fetchall()
if len(result) > 0:
bundle_name = result[0][0]
bundle = OperatorBundle(bundle_name, version)
# Get related images for the operator bundle
cmd = "select image from related_image where operatorbundle_name like '%" + bundle_name + "%';"
result = cur.execute(cmd).fetchall()
if len(result) > 0:
for image in result:
bundle.relatedImages.append(image[0])
# Get bundle images for operator bundle
cmd = "select bundlepath from operatorbundle where (name like '%" + operator.name + "%' or bundlepath like '%" + operator.name + "%') and version='" + version + "';"
result = cur.execute(cmd).fetchall()
if len(result) > 0:
for image in result:
bundle.relatedImages.append(image[0])
operator.operator_bundles.append(bundle)
def ExtractIndexDb():
cmd = oc_cli_path + " image extract " + custom_redhat_operators_catalog_image_url
cmd += " -a " + args.authfile + " --path /database/index.db:" + run_root_dir + " --confirm --insecure"
subprocess.run(cmd, shell=True, check=True)
return os.path.join(run_root_dir, "index.db")
# Get a non duplicate list of images
def getImages(operators):
image_list = []
for operator in operators:
for bundle in operator.operator_bundles:
for image in bundle.relatedImages:
if type(image) is dict:
if image['image'] not in image_list:
image_list.append(image['image'])
else:
if image not in image_list:
image_list.append(image)
return image_list
def MirrorImagesToLocalRegistry(images):
print("Copying image list to offline registry...")
failed_image_list = []
image_count = len(images)
cur_image_count = 1
for image in images:
PrintBreakLine()
print(
"Mirroring image " +
str(cur_image_count) +
" of " +
str(image_count))
if isBadImage(image) == False:
destUrl = GenerateDestUrl(image)
max_retries = 5
retries = 0
success = False
while retries < max_retries and success == False:
if (retries > 0 ):
print("RETRY ATTEMPT: " + str(retries))
try:
print("Image: " + image)
CopyImageToDestinationRegistry(image, destUrl, args.authfile)
success = True
except subprocess.CalledProcessError as e:
print("ERROR Copying image: " + image)
print("TO")
print(destUrl)
if (e.output is not None):
print("exception:" + e.output)
print("ERROR copying image!")
retries+=1
if not success:
failed_image_list.append(image)
else:
print("Known bad image: {}\n{}".format(image, "ignoring..."))
cur_image_count = cur_image_count + 1
PrintBreakLine()
print("Finished mirroring related images.")
if len(failed_image_list) > 0:
print("Failed to copy the following images:")
PrintBreakLine()
for image in failed_image_list:
print(image)
PrintBreakLine()
# Create Image Content Source Policy Yaml to apply to OCP cluster
def CreateImageContentSourcePolicyFile(images):
with open(image_content_source_policy_template_file) as f:
icpt = yaml.safe_load(f)
repoList = GetRepoListToMirror(images)
for key in repoList:
icpt['spec']['repositoryDigestMirrors'].append(
{'mirrors': [repoList[key]], 'source': key})
with open(image_content_source_policy_output_file, "w") as f:
yaml.dump(icpt, f, default_flow_style=False)
# Get a List of repos to mirror
def GetRepoListToMirror(images):
reg = r"^(.*\/){2}"
if args.icsp_scope == "registry":
reg = r"^(.*?\/){1}"
sourceList = []
mirrorList = {}
for image in images:
source = re.match(reg, image)
if source is None:
sourceRepo = image[:image.find("@")]
else:
sourceRepo = source.group()[:-1]
sourceList.append(
sourceRepo) if sourceRepo not in sourceList else sourceList
for source in sourceList:
mirrorList[source] = GenerateDestUrl(source)
return mirrorList
def CreateMappingFile(images):
repoList = GetSourceToMirrorMapping(images)
with open(mapping_file, "w") as f:
for key in repoList:
f.write(key + "=" + repoList[key])
f.write('\n')
def CreateManifestFile(images):
with open(image_manifest_file, "w") as f:
for image in images:
f.write(image)
f.write("\n")
def isBadImage(image):
with open(operator_known_bad_image_list_file, 'r') as f:
for bad_image in (l.rstrip('\n') for l in f):
if bad_image == image:
return True
return False
def GenerateDestUrl(image_url):
print(f"{image_url}")
res = image_url.find("/")
if res != -1:
GenDestUrl = args.registry_olm + image_url[res:]
else:
GenDestUrl = args.registry_olm
if add_tags_to_images_mirrored_by_digest.lower() == "true":
GenDestUrl = re.sub(r'@sha256:', ':', GenDestUrl)
return GenDestUrl
def CopyImageToDestinationRegistry(
sourceImageUrl, destinationImageUrl, authfile=None):
if args.authfile:
cmd_args = "skopeo copy --dest-tls-verify=false --authfile {} -a docker://{} docker://{}".format(
authfile, sourceImageUrl, destinationImageUrl)
else:
cmd_args = "skopeo copy --dest-tls-verify=false -a docker://{} docker://{}".format(
sourceImageUrl, destinationImageUrl)
subprocess.run(cmd_args, shell=True, check=True)
# Get a Mapping of source to mirror images
def GetSourceToMirrorMapping(images):
reg = r"^(.*@){1}"
mapping = {}
for image in images:
source = re.match(reg, image)
if source is None:
sourceRepo = image
else:
sourceRepo = source.group()[:-1]
mapping[image] = GenerateDestUrl(sourceRepo)
return mapping
def CreateCatalogSourceYaml(image_url, image_name, display_name):
with open(catalog_source_template_file, 'r') as f:
templateFile = Template(f.read())
content = templateFile.render(CatalogSourceImage=image_url, CatalogSourceName=image_name, CatalogSourceDisplayName=display_name)
with open(catalog_source_output_file, "w") as f:
f.write(content)
def GetListOfCommaDelimitedOperatorList(operators):
operator_list = ""
for item in operators:
operator_list += item.name + ","
operator_list = operator_list[:-1]
return operator_list
def RecreatePath(item_path, delete_if_exists = True):
path = Path(item_path)
if path.exists() and delete_if_exists:
cmd_args = "sudo rm -rf {}".format(item_path)
print("Running: " + str(cmd_args))
subprocess.run(cmd_args, shell=True, check=True)
if not path.exists():
os.makedirs(item_path, exist_ok=True)
def PrintBreakLine():
print("----------------------------------------------")
class OperatorSpec:
def __init__(self, name, start_version):
self.name = name
self.start_version = start_version
self.upgrade_path = ""
self.operator_bundles = []
self.operator_channels = []
self.defaultChannel = ""
self.icon = {}
class OperatorChannel:
def __init__(self, name):
self.name = name
self.package = ""
self.entries = []
class OperatorBundle:
def __init__(self, name, version):
self.name = name
self.package = ""
self.image = ""
self.version = version
self.properties = []
self.relatedImages = []
if __name__ == "__main__":
main()