|
| 1 | +# |
| 2 | +# Copyright © contributors to CloudNativePG, established as |
| 3 | +# CloudNativePG a Series of LF Projects, LLC. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +# SPDX-License-Identifier: Apache-2.0 |
| 18 | +# |
| 19 | + |
| 20 | +import argparse |
| 21 | +import re |
| 22 | +import sys |
| 23 | +import hcl2 |
| 24 | +import yaml |
| 25 | +from pathlib import Path |
| 26 | + |
| 27 | + |
| 28 | +def parse_hcl(metadata_path): |
| 29 | + with open(metadata_path, "r") as f: |
| 30 | + return hcl2.load(f)["metadata"] |
| 31 | + |
| 32 | + |
| 33 | +def get_default_image(metadata, distro, pg_major): |
| 34 | + fullVersion = metadata["versions"][distro][pg_major] |
| 35 | + match = re.match(r"^(\d+(?:\.\d+)+)", fullVersion) |
| 36 | + if not match: |
| 37 | + raise ValueError(f"Cannot extract extension version from '{s}'") |
| 38 | + version = match.group(1) |
| 39 | + image_name = metadata["image_name"] |
| 40 | + return f"ghcr.io/cloudnativepg/{image_name}:{version}-{pg_major}-{distro}" |
| 41 | + |
| 42 | + |
| 43 | +def get_ext_version(ext_image): |
| 44 | + tag = ext_image.split(":", 1)[1] |
| 45 | + version = tag.split("-", 1)[0] |
| 46 | + return version |
| 47 | + |
| 48 | + |
| 49 | +def main(): |
| 50 | + parser = argparse.ArgumentParser( |
| 51 | + description="Generate Chainsaw values.yaml for a target Extension" |
| 52 | + ) |
| 53 | + parser.add_argument( |
| 54 | + "--dir", type=Path, required=True, help="Path to the extension's directory" |
| 55 | + ) |
| 56 | + parser.add_argument( |
| 57 | + "--pg-major", |
| 58 | + choices=["18"], |
| 59 | + required=True, |
| 60 | + dest="pg_major", |
| 61 | + help="The PG major version to test", |
| 62 | + ) |
| 63 | + parser.add_argument( |
| 64 | + "--distro", |
| 65 | + choices=["bookworm", "trixie"], |
| 66 | + required=True, |
| 67 | + help="The distribution to test", |
| 68 | + ) |
| 69 | + parser.add_argument( |
| 70 | + "--extension-image", |
| 71 | + dest="ext_image", |
| 72 | + help="Target a specific extension image (Optional). Defaults to the version defined in the extension's metadata", |
| 73 | + ) |
| 74 | + args = parser.parse_args() |
| 75 | + |
| 76 | + ext_dir = Path(args.dir) |
| 77 | + metadata_file = ext_dir / "metadata.hcl" |
| 78 | + if not metadata_file.exists(): |
| 79 | + print(f"Error: {metadata_file} does not exist") |
| 80 | + sys.exit(1) |
| 81 | + |
| 82 | + metadata = parse_hcl(metadata_file) |
| 83 | + |
| 84 | + ext_image = args.ext_image or get_default_image( |
| 85 | + metadata, args.distro, args.pg_major |
| 86 | + ) |
| 87 | + version = get_ext_version(ext_image) |
| 88 | + pg_image = ( |
| 89 | + f"ghcr.io/cloudnative-pg/postgresql:{args.pg_major}-minimal-{args.distro}" |
| 90 | + ) |
| 91 | + |
| 92 | + # Build the values.yaml dictionary |
| 93 | + values = { |
| 94 | + **metadata, |
| 95 | + "extension_image": ext_image, |
| 96 | + "pg_image": pg_image, |
| 97 | + "version": version, |
| 98 | + } |
| 99 | + |
| 100 | + # Write values.yaml |
| 101 | + values_yaml_path = ext_dir / "values.yaml" |
| 102 | + with open(values_yaml_path, "w") as f: |
| 103 | + yaml.dump(values, f, sort_keys=False) |
| 104 | + |
| 105 | + print(f"Generated {values_yaml_path}") |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + main() |
0 commit comments