|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Updates operator version in all necessary files for a new release.""" |
| 3 | + |
| 4 | +import argparse |
| 5 | +import re |
| 6 | +import sys |
| 7 | +from datetime import datetime |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import yaml |
| 11 | + |
| 12 | + |
| 13 | +def detect_release_type(version): |
| 14 | + """Detect if version is RC or final.""" |
| 15 | + return 'rc' if re.search(r'[-\.]rc[\.\-]\d+$', version) else 'final' |
| 16 | + |
| 17 | + |
| 18 | +def get_previous_version_from_csv(csv_file): |
| 19 | + """Get current version from CSV file.""" |
| 20 | + try: |
| 21 | + with open(csv_file, 'r') as f: |
| 22 | + csv_data = yaml.safe_load(f) |
| 23 | + current_version = csv_data.get('spec', {}).get('version', '') |
| 24 | + if current_version: |
| 25 | + return current_version |
| 26 | + except Exception as e: |
| 27 | + print(f"Error reading CSV: {e}") |
| 28 | + sys.exit(1) |
| 29 | + |
| 30 | + print("Cannot find version in CSV file") |
| 31 | + sys.exit(1) |
| 32 | + |
| 33 | + |
| 34 | +def validate_version_format(version, release_type): |
| 35 | + """Validate version format.""" |
| 36 | + if release_type == 'rc': |
| 37 | + if not re.match(r'^\d+\.\d+\.\d+-\d+[-\.]rc\.\d+$', version): |
| 38 | + print(f"RC version must match format: X.Y.Z-N-rc.M (got: {version})") |
| 39 | + sys.exit(1) |
| 40 | + else: |
| 41 | + if not re.match(r'^\d+\.\d+\.\d+-\d+$', version): |
| 42 | + print(f"Final version must match format: X.Y.Z-N (got: {version})") |
| 43 | + sys.exit(1) |
| 44 | + if 'rc' in version.lower(): |
| 45 | + print("Final version should not contain 'rc'") |
| 46 | + sys.exit(1) |
| 47 | + |
| 48 | + |
| 49 | +def update_csv(csv_file, version, previous_version, operator_name): |
| 50 | + """Update ClusterServiceVersion file.""" |
| 51 | + with open(csv_file, 'r') as f: |
| 52 | + csv_data = yaml.safe_load(f) |
| 53 | + |
| 54 | + csv_data['spec']['version'] = version |
| 55 | + csv_data['metadata']['name'] = f"{operator_name}.v{version}" |
| 56 | + csv_data['metadata']['annotations']['createdAt'] = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ') |
| 57 | + |
| 58 | + operator_image = f"registry.connect.redhat.com/sumologic/{operator_name}:{version}" |
| 59 | + csv_data['metadata']['annotations']['containerImage'] = operator_image |
| 60 | + |
| 61 | + for deployment in csv_data['spec']['install']['spec']['deployments']: |
| 62 | + if deployment['name'] == operator_name: |
| 63 | + for container in deployment['spec']['template']['spec']['containers']: |
| 64 | + if container['name'] == 'manager': |
| 65 | + container['image'] = operator_image |
| 66 | + |
| 67 | + if 'relatedImages' in csv_data['spec']: |
| 68 | + for img in csv_data['spec']['relatedImages']: |
| 69 | + if img.get('name') == operator_name: |
| 70 | + img['image'] = operator_image |
| 71 | + break |
| 72 | + |
| 73 | + with open(csv_file, 'w') as f: |
| 74 | + yaml.dump(csv_data, f, default_flow_style=False, sort_keys=False) |
| 75 | + |
| 76 | + |
| 77 | +def update_makefile(makefile_path, version, operator_name): |
| 78 | + """Update IMG in Makefile.""" |
| 79 | + if not Path(makefile_path).exists(): |
| 80 | + return |
| 81 | + |
| 82 | + with open(makefile_path, 'r') as f: |
| 83 | + content = f.read() |
| 84 | + |
| 85 | + img_value = f"registry.connect.redhat.com/sumologic/{operator_name}:{version}" |
| 86 | + content = re.sub(r'^IMG\s*\?=\s*.*$', f'IMG ?= {img_value}', content, flags=re.MULTILINE) |
| 87 | + |
| 88 | + with open(makefile_path, 'w') as f: |
| 89 | + f.write(content) |
| 90 | + |
| 91 | + |
| 92 | +def update_kustomization(kustomize_file, version): |
| 93 | + """Update config/manager/kustomization.yaml.""" |
| 94 | + if not Path(kustomize_file).exists(): |
| 95 | + return |
| 96 | + |
| 97 | + with open(kustomize_file, 'r') as f: |
| 98 | + kustomize_data = yaml.safe_load(f) |
| 99 | + |
| 100 | + if 'images' in kustomize_data: |
| 101 | + for image in kustomize_data['images']: |
| 102 | + if image.get('name') == 'controller': |
| 103 | + image['newTag'] = version |
| 104 | + break |
| 105 | + |
| 106 | + with open(kustomize_file, 'w') as f: |
| 107 | + yaml.dump(kustomize_data, f, default_flow_style=False, sort_keys=False) |
| 108 | + |
| 109 | + |
| 110 | +def update_deploy_test_script(script_file, version, operator_name): |
| 111 | + """Update tests/deploy_helm_operator.sh.""" |
| 112 | + if not Path(script_file).exists(): |
| 113 | + return |
| 114 | + |
| 115 | + with open(script_file, 'r') as f: |
| 116 | + content = f.read() |
| 117 | + |
| 118 | + new_image = f"registry.connect.redhat.com/sumologic/{operator_name}:{version}" |
| 119 | + pattern = r'registry\.connect\.redhat\.com/sumologic/' + re.escape(operator_name) + r':[\d\.]+-[\d\.]+(?:[-\.]rc\.[\d]+)?' |
| 120 | + |
| 121 | + content = re.sub( |
| 122 | + r'readonly IMG="\$\{IMG:=' + pattern + r'\}"', |
| 123 | + f'readonly IMG="${{IMG:={new_image}}}"', |
| 124 | + content |
| 125 | + ) |
| 126 | + |
| 127 | + content = re.sub( |
| 128 | + r'sed -i\.bak "s#' + pattern + r'#', |
| 129 | + f'sed -i.bak "s#{new_image}#', |
| 130 | + content |
| 131 | + ) |
| 132 | + |
| 133 | + with open(script_file, 'w') as f: |
| 134 | + f.write(content) |
| 135 | + |
| 136 | +def update_bundle_yaml(bundle_file, version, operator_name): |
| 137 | + """Update bundle.yaml file.""" |
| 138 | + if not Path(bundle_file).exists(): |
| 139 | + return |
| 140 | + |
| 141 | + with open(bundle_file, 'r') as f: |
| 142 | + content = f.read() |
| 143 | + |
| 144 | + pattern = r'(image:\s+registry\.connect\.redhat\.com/sumologic/' + re.escape(operator_name) + r'):[\d\.]+-[\d\.]+(?:[-\.]rc\.[\d]+)?' |
| 145 | + content = re.sub(pattern, f'\\1:{version}', content) |
| 146 | + |
| 147 | + with open(bundle_file, 'w') as f: |
| 148 | + f.write(content) |
| 149 | + |
| 150 | +def main(): |
| 151 | + parser = argparse.ArgumentParser(description='Update operator version') |
| 152 | + parser.add_argument('--operator-version', required=True, help='Operator version') |
| 153 | + parser.add_argument('--operator-repo-dir', default='./', help='Repository directory') |
| 154 | + |
| 155 | + args = parser.parse_args() |
| 156 | + |
| 157 | + version = args.operator_version |
| 158 | + repo_dir = Path(args.operator_repo_dir) |
| 159 | + |
| 160 | + operator_name = "sumologic-kubernetes-collection-helm-operator" |
| 161 | + csv_file = repo_dir / "bundle/manifests/operator.clusterserviceversion.yaml" |
| 162 | + |
| 163 | + release_type = detect_release_type(version) |
| 164 | + validate_version_format(version, release_type) |
| 165 | + previous_version = get_previous_version_from_csv(csv_file) |
| 166 | + |
| 167 | + update_csv(csv_file, version, previous_version, operator_name) |
| 168 | + update_makefile(repo_dir / "Makefile", version, operator_name) |
| 169 | + update_bundle_yaml(repo_dir / "bundle.yaml", version, operator_name) |
| 170 | + update_kustomization(repo_dir / "config/manager/kustomization.yaml", version) |
| 171 | + update_deploy_test_script(repo_dir / "tests/deploy_helm_operator.sh", version, operator_name) |
| 172 | + |
| 173 | + print(f"::set-output name=release_type::{release_type}") |
| 174 | + print(f"::set-output name=previous_version::{previous_version}") |
| 175 | + |
| 176 | + |
| 177 | +if __name__ == '__main__': |
| 178 | + main() |
0 commit comments