#!/usr/bin/env python import yaml import sys registry_config = 'registries.yaml' if len(sys.argv) != 4: print("Usage: generate-sync-config.py ") sys.exit(1) image_config = sys.argv[1] target_registry = sys.argv[2] output_file = sys.argv[3] images = {} with open(image_config, 'r') as f: images = yaml.safe_load(f) registries = {} with open(registry_config, 'r') as f: registries = yaml.safe_load(f) exist = False target_registry_info = {} for info in registries: if target_registry == info['registry']: exist = True target_registry_info = info break if not exist: print(f"Error: Target registry '{target_registry}' not found in registries.yaml") sys.exit(1) if not target_registry_info.get('provider'): print(f"Error: registry has no provider config") sys.exit(1) cloud_provider = target_registry_info['provider'] registry_type = '' if cloud_provider == 'aws': registry_type = 'ECR' elif cloud_provider == 'gcp': registry_type = 'GCR' elif cloud_provider == 'azure': registry_type = 'ACR' elif cloud_provider == 'aliyun': registry_type = 'ALI_CR' else: print(f"Error: unknown cloud provider '{cloud_provider}'") data = { 'repositorySets': { 'commonRepositories': [], }, 'targetRegistries': [ { 'name': target_registry, 'type': registry_type, 'includeRepositorySets': ['commonRepositories'], }, ], 'defaults': { 'ratelimit': { 'min': 100, 'retry': '15m', }, 'parallel': 5, }, } for image in images: if cloud_provider not in image['providers']: continue if target_registry_info.get('byoc') and not image.get('sync_customer'): # if it's byoc account, skip images that are not marked for customer sync continue name = image.get('localName') if not name: print(f"Skip image that has no localName '{image}'") continue source = image.get('source') if not source: print(f"Skip image that has no source '{image}'") continue tags = image.get('tags') if not tags: # for production, we must explicitly specify tags print(f"Skip image that has no tags '{image}'") continue repo_entry = { 'name': name, 'source': source, 'tags': { 'allow': tags, }, } data['repositorySets']['commonRepositories'].append(repo_entry) with open(output_file, 'w') as output: yaml.dump(data, output)