Skip to content

Commit ee4365a

Browse files
committed
feat: allow generating testing values locally
Signed-off-by: Niccolò Fei <[email protected]>
1 parent 88d1fca commit ee4365a

File tree

6 files changed

+130
-21
lines changed

6 files changed

+130
-21
lines changed

.github/workflows/bake_targets.yml

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,6 @@ jobs:
130130
with:
131131
persist-credentials: false
132132

133-
- name: Install Go
134-
uses: actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6
135-
with:
136-
cache: false
137-
go-version: 'stable'
138-
139133
- name: Create kind cluster
140134
uses: helm/kind-action@92086f6be054225fa813e0a4b13787fc9088faab # v1.13.0
141135
with:
@@ -155,23 +149,27 @@ jobs:
155149
curl -sSfL "$operator_manifest" | kubectl apply --server-side -f -
156150
kubectl wait --for=condition=Available --timeout=2m -n cnpg-system deployments cnpg-controller-manager
157151
152+
- name: Set up Python
153+
uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6
154+
with:
155+
python-version: 3.14
156+
158157
- name: Generate Chainsaw runtime values
159158
env:
160159
EXT_NAME: ${{ inputs.extension_name }}
161160
EXT_IMAGE: ${{ matrix.image }}
162161
run: |
163-
# Get the PG base image
164-
export PG_IMAGE=$(skopeo inspect "docker://$EXT_IMAGE" -f '{{ json .Labels }}' | jq -r '."io.cloudnativepg.image.base.name"')
165-
166-
go install github.com/tmccombs/[email protected]
167-
go install github.com/mikefarah/yq/v4@v4
168-
169-
# Convert metadata.hcl to YAML and merge it with runtime values to generate a valid Chainsaw values.yaml
170-
yq eval -P '
171-
.metadata.extension_image = strenv(EXT_IMAGE) |
172-
.metadata.pg_image = strenv(PG_IMAGE) |
173-
.metadata
174-
' <(hcl2json "$EXT_NAME/metadata.hcl") > "$EXT_NAME/values.yaml"
162+
TAG="${EXT_IMAGE##*:}"
163+
PG_MAJOR="${TAG%%-*}"
164+
DISTRO="${TAG##*-}"
165+
166+
pip install -r hack/requirements.txt
167+
python3 hack/chainsaw-values.py \
168+
--dir "$EXT_NAME" \
169+
--pg-major "$PG_MAJOR" \
170+
--distro "$DISTRO" \
171+
--extension-image "$EXT_IMAGE"
172+
175173
cat "$EXT_NAME/values.yaml"
176174
177175
- name: Install Chainsaw

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ all: prereqs $(DIRS)
7777
@echo -e "$(GREEN)Build successful for all projects: $(DIRS)$(NC)"
7878
@echo -e "$(GREEN)======================================================$(NC)"
7979

80-
# Per-project build
80+
# --------------------------
81+
# Per-project Build
82+
# --------------------------
8183
$(DIRS): %: %/metadata.hcl
8284
@echo -e "$(BLUE)--- Starting Docker Buildx Bake for target: $@ ---$(NC)"
8385
ifeq ($(DRY_RUN),true)

hack/chainsaw-values.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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()

hack/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
python-hcl2
2+
pyyaml

pgvector/metadata.hcl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ metadata = {
22
name = "pgvector"
33
sql_name = "vector"
44
image_name = "pgvector"
5-
version = "0.8.1"
65
shared_preload_libraries = []
76
extension_control_path = []
87
dynamic_library_path = []

postgis/metadata.hcl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ metadata = {
22
name = "postgis"
33
sql_name = "postgis"
44
image_name = "postgis-extension"
5-
version = "3.6.1"
65
shared_preload_libraries = []
76
extension_control_path = []
87
dynamic_library_path = []

0 commit comments

Comments
 (0)