Skip to content

Commit

Permalink
Make starter Chart.yaml the source of truth for all versions (#25)
Browse files Browse the repository at this point in the history
* Make starter Chart.yaml the source of truth for all versions

Signed-off-by: Miles Wilson <[email protected]>

* formatting nits and raise from e

* Remove prints

---------

Signed-off-by: Miles Wilson <[email protected]>
Co-authored-by: Don Naro <[email protected]>
  • Loading branch information
miles-w-3 and oraNod authored Nov 13, 2024
1 parent b558106 commit 3f16dc0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
8 changes: 5 additions & 3 deletions .helm/starter/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
---
apiVersion: v2
appVersion: 0.1.0
description: A Helm chart for Kubernetes
name: starter
description: The community-supported AWX Operator Helm Chart
name: awx-operator
type: application
# the current version of this helm chart
version: 0.1.0
# The version of the awx-operator to install
appVersion: 2.19.1
23 changes: 13 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ endif

# Helm variables
CHART_NAME ?= awx-operator
CHART_DESCRIPTION ?= A Helm chart for the AWX Operator
CHART_OWNER ?= $(GH_REPO_OWNER)
CHART_REPO ?= awx-operator
CHART_BRANCH ?= gh-pages
CHART_DIR ?= gh-pages
CHART_INDEX ?= index.yaml
# use python3 if python isn't in the path
PYTHON ?= $(shell which python >/dev/null 2>&1 && echo python || echo python3)

.PHONY: kustomize
KUSTOMIZE = $(shell pwd)/bin/kustomize
Expand Down Expand Up @@ -67,6 +68,10 @@ YQ = $(shell which yq)
endif
endif

.PHONY: chart-version
chart-version: yq
CHART_VERSION = $(shell cat .helm/starter/Chart.yaml | $(YQ) '.version')

.PHONY: helm
HELM = $(shell pwd)/bin/helm
helm: ## Download helm locally if necessary.
Expand Down Expand Up @@ -111,7 +116,7 @@ helm-chart: helm-chart-generate
helm-chart-generate: kustomize helm kubectl-slice yq charts

@echo "== Clone the AWX Operator repository =="
python clone-awx-operator.py
$(PYTHON) clone-awx-operator.py

@echo "== KUSTOMIZE: Set image and chart label =="
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
Expand All @@ -120,13 +125,11 @@ helm-chart-generate: kustomize helm kubectl-slice yq charts

@echo "== Gather Helm Chart Metadata =="
# remove the existing chart if it exists
# edit and copy the Chart.yaml ourselves since helm doesn't do it: https://github.com/helm/helm/issues/9551#issuecomment-811217822
rm -rf charts/$(CHART_NAME)
# create new chart metadata in Chart.yaml
cd charts && \
$(HELM) create $(CHART_NAME) --starter $(shell pwd)/.helm/starter ;\
$(YQ) -i '.version = "$(VERSION)"' $(CHART_NAME)/Chart.yaml ;\
$(YQ) -i '.appVersion = "$(VERSION)" | .appVersion style="double"' $(CHART_NAME)/Chart.yaml ;\
$(YQ) -i '.description = "$(CHART_DESCRIPTION)"' $(CHART_NAME)/Chart.yaml ;\
$(YQ) '.name = "$(CHART_NAME)"' $(shell pwd)/.helm/starter/Chart.yaml > $(CHART_NAME)/Chart.yaml;\

@echo "Generated chart metadata:"
@cat charts/$(CHART_NAME)/Chart.yaml
Expand Down Expand Up @@ -174,17 +177,17 @@ helm-chart-generate: kustomize helm kubectl-slice yq charts
rm -rf charts/$(CHART_NAME)/raw-files

# create and populate NOTES.txt
@echo "AWX Operator installed with Helm Chart version $(VERSION)" > charts/$(CHART_NAME)/templates/NOTES.txt
@echo "AWX Operator installed with Helm Chart version {{ .Chart.Version }}" > charts/$(CHART_NAME)/templates/NOTES.txt

@echo "Helm chart successfully configured for $(CHART_NAME) version $(VERSION)"
@echo "Helm chart successfully configured for $(CHART_NAME)"


.PHONY: helm-package
helm-package: helm-chart
helm-package: helm-chart chart-version
@echo "== Package Current Chart Version =="
mkdir -p .cr-release-packages
# package the chart and put it in .cr-release-packages dir
$(HELM) package ./charts/$(CHART_NAME) -d .cr-release-packages/$(VERSION)
$(HELM) package ./charts/$(CHART_NAME) -d .cr-release-packages/$(CHART_VERSION)

# List all tags oldest to newest.
TAGS := $(shell git ls-remote --tags --sort=version:refname --refs -q | cut -d/ -f3)
Expand Down
34 changes: 29 additions & 5 deletions clone-awx-operator.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,28 @@
import sys
import tempfile

DEFAULT_BRANCH = "devel"
DEFAULT_AWX_OPERATOR_REPO = "https://github.com/ansible/awx-operator"


# get the appVersion configured in Chart.yaml
def get_app_version():
try:
chart_yaml_path = "./.helm/starter/Chart.yaml"
with open(chart_yaml_path, "r", encoding="utf-8") as chart:
for line in chart:
if line.startswith("appVersion:"):
result = line.split(":", 1)
if len(result) != 2:
raise KeyError("Malformed appVersion in Chart.yaml")
app_version = result[1].strip()
if not app_version:
raise KeyError("No appVersion value found")
return app_version
except FileNotFoundError as e:
raise FileNotFoundError("Failed to open Chart.yaml") from e
raise KeyError("Could not find appVersion in Chart.yaml")


@dataclasses.dataclass()
class Args:
branch: str | None
Expand All @@ -30,8 +48,8 @@ def parse_args(args: list[str] | None = None) -> Args:
"-b",
"--branch",
help="Set the branch of awx-operator to clone."
" Defaults to current branch (%(default)s)",
default=DEFAULT_BRANCH,
" Defaults to configured appVersion",
default=get_app_version(),
)
parser.add_argument(
"--no-branch",
Expand Down Expand Up @@ -65,7 +83,13 @@ def main(args: Args) -> None:
]

with tempfile.TemporaryDirectory() as temp_dir:
cmd: list[str] = ["git", "clone", args.repo, "--depth=1"]
cmd: list[str] = [
"git",
"clone",
args.repo,
"--depth=1",
"-c advice.detachedHead=false",
]
if args.branch is not None:
cmd.append(f"--branch={args.branch}")
cmd.append(temp_dir)
Expand All @@ -86,7 +110,7 @@ def main(args: Args) -> None:

for keep_file in keep_files:
src = pathlib.Path(temp_dir, keep_file)
if keep_file == 'Makefile':
if keep_file == "Makefile":
dst = pathlib.Path.cwd() / "Makefile.awx-operator"
else:
dst = pathlib.Path.cwd() / keep_file
Expand Down

0 comments on commit 3f16dc0

Please sign in to comment.