From aba5069d7fe4113f647e1d264906a21a6d02c377 Mon Sep 17 00:00:00 2001
From: Joshua Kraitberg <joshua.kraitberg@windriver.com>
Date: Wed, 19 Jun 2024 16:13:12 -0400
Subject: [PATCH] Auto-increment chart versions

Enable auto-versioning of helm charts to ensure the FluxCD
helm controller recognizes chart changes.

Test Plan:
PASS: Build all packages generating an application tarball verifying
      all versions on the charts and application make sense.
PASS: Introduce temporary chart changes and ensure that the versions
      increment as expected.
PASS: Validate basic application lifecycle operations:
      upload/apply/remove/delete.

Story: 2010929
Task: 50383
Change-Id: Ie57ea4a4f5a04f09c14a8855e6c1afdfd7dbccb8
Signed-off-by: Joshua Kraitberg <joshua.kraitberg@windriver.com>
---
 debian_pkg_dirs                               |  1 +
 .../oran-o2-helm/debian/deb_folder/changelog  |  5 ++
 .../oran-o2-helm/debian/deb_folder/control    | 15 ++++
 .../oran-o2-helm/debian/deb_folder/copyright  | 41 +++++++++++
 .../debian/deb_folder/oran-o2-helm.install    |  1 +
 .../oran-o2-helm/debian/deb_folder/rules      | 27 +++++++
 .../debian/deb_folder/source/format           |  1 +
 .../oran-o2-helm/debian/meta_data.yaml        | 16 +++++
 .../oran-o2-helm/oran-o2-helm}/files/Makefile | 10 ++-
 stx-oran-o2-helm/debian/deb_folder/changelog  |  8 ++-
 stx-oran-o2-helm/debian/deb_folder/control    |  6 +-
 stx-oran-o2-helm/debian/deb_folder/copyright  |  4 +-
 stx-oran-o2-helm/debian/deb_folder/rules      | 72 ++++++++-----------
 stx-oran-o2-helm/debian/meta_data.yaml        |  7 +-
 .../stx-oran-o2-helm/files/index.yaml         |  3 -
 .../stx-oran-o2-helm/files/repositories.yaml  | 12 ----
 16 files changed, 153 insertions(+), 76 deletions(-)
 create mode 100644 helm-charts/upstream/oran-o2-helm/debian/deb_folder/changelog
 create mode 100644 helm-charts/upstream/oran-o2-helm/debian/deb_folder/control
 create mode 100644 helm-charts/upstream/oran-o2-helm/debian/deb_folder/copyright
 create mode 100644 helm-charts/upstream/oran-o2-helm/debian/deb_folder/oran-o2-helm.install
 create mode 100755 helm-charts/upstream/oran-o2-helm/debian/deb_folder/rules
 create mode 100644 helm-charts/upstream/oran-o2-helm/debian/deb_folder/source/format
 create mode 100644 helm-charts/upstream/oran-o2-helm/debian/meta_data.yaml
 rename {stx-oran-o2-helm/stx-oran-o2-helm => helm-charts/upstream/oran-o2-helm/oran-o2-helm}/files/Makefile (66%)
 delete mode 100644 stx-oran-o2-helm/stx-oran-o2-helm/files/index.yaml
 delete mode 100644 stx-oran-o2-helm/stx-oran-o2-helm/files/repositories.yaml

diff --git a/debian_pkg_dirs b/debian_pkg_dirs
index 4657f8e..7f5ab1b 100644
--- a/debian_pkg_dirs
+++ b/debian_pkg_dirs
@@ -1,2 +1,3 @@
+helm-charts/upstream/oran-o2-helm
 python3-k8sapp-oran-o2
 stx-oran-o2-helm
diff --git a/helm-charts/upstream/oran-o2-helm/debian/deb_folder/changelog b/helm-charts/upstream/oran-o2-helm/debian/deb_folder/changelog
new file mode 100644
index 0000000..7e7baf4
--- /dev/null
+++ b/helm-charts/upstream/oran-o2-helm/debian/deb_folder/changelog
@@ -0,0 +1,5 @@
+oran-o2-helm (2.0.4-1) unstable; urgency=medium
+
+  * ORAN release J
+
+ --  Joshua Kraitberg  <joshua.kraitberg@windriver.com>  Tue, 9 Jul 2024 12:13:14 +0000
diff --git a/helm-charts/upstream/oran-o2-helm/debian/deb_folder/control b/helm-charts/upstream/oran-o2-helm/debian/deb_folder/control
new file mode 100644
index 0000000..7446d5b
--- /dev/null
+++ b/helm-charts/upstream/oran-o2-helm/debian/deb_folder/control
@@ -0,0 +1,15 @@
+Source: oran-o2-helm
+Section: libs
+Priority: optional
+Maintainer: StarlingX Developers <starlingx-discuss@lists.starlingx.io>
+Build-Depends: debhelper-compat (= 13),
+ helm
+Standards-Version: 4.5.1
+Homepage: https://www.starlingx.io
+
+Package: oran-o2-helm
+Section: libs
+Architecture: any
+Depends: ${misc:Depends}
+Description: StarlingX ORAN O2 FluxCD Helm Charts
+ This package contains Fluxcd helm charts for the ORAN O2.
diff --git a/helm-charts/upstream/oran-o2-helm/debian/deb_folder/copyright b/helm-charts/upstream/oran-o2-helm/debian/deb_folder/copyright
new file mode 100644
index 0000000..a838385
--- /dev/null
+++ b/helm-charts/upstream/oran-o2-helm/debian/deb_folder/copyright
@@ -0,0 +1,41 @@
+Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
+Upstream-Name: oran-o2-helm
+Source: https://opendev.org/starlingx/app-oran-o2/
+
+Files: *
+Copyright: (c) 2024 Wind River Systems, Inc
+License: Apache-2
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+ .
+    https://www.apache.org/licenses/LICENSE-2.0
+ .
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ .
+ On Debian-based systems the full text of the Apache version 2.0 license
+ can be found in `/usr/share/common-licenses/Apache-2.0'.
+
+# If you want to use GPL v2 or later for the /debian/* files use
+# the following clauses, or change it to suit. Delete these two lines
+Files: debian/*
+Copyright: 2024 Wind River Systems, Inc
+License: Apache-2
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+ .
+    https://www.apache.org/licenses/LICENSE-2.0
+ .
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ .
+ On Debian-based systems the full text of the Apache version 2.0 license
+ can be found in `/usr/share/common-licenses/Apache-2.0'.
diff --git a/helm-charts/upstream/oran-o2-helm/debian/deb_folder/oran-o2-helm.install b/helm-charts/upstream/oran-o2-helm/debian/deb_folder/oran-o2-helm.install
new file mode 100644
index 0000000..8a0c6de
--- /dev/null
+++ b/helm-charts/upstream/oran-o2-helm/debian/deb_folder/oran-o2-helm.install
@@ -0,0 +1 @@
+usr/lib/helm/*
diff --git a/helm-charts/upstream/oran-o2-helm/debian/deb_folder/rules b/helm-charts/upstream/oran-o2-helm/debian/deb_folder/rules
new file mode 100755
index 0000000..deef569
--- /dev/null
+++ b/helm-charts/upstream/oran-o2-helm/debian/deb_folder/rules
@@ -0,0 +1,27 @@
+#!/usr/bin/make -f
+export DH_VERBOSE = 1
+
+export DEB_VERSION = $(shell dpkg-parsechangelog | egrep '^Version:' | cut -f 2 -d ' ')
+export PATCH_VERSION = $(shell echo $(DEB_VERSION) | cut -f 4 -d '.')
+export CHART_BASE_VERSION = $(shell echo $(DEB_VERSION) | sed 's/-/./' | cut -d '.' -f 1-3)
+export CHART_VERSION = v$(CHART_BASE_VERSION)+STX.$(PATCH_VERSION)
+
+export ROOT = debian/tmp
+export APP_FOLDER = $(ROOT)/usr/lib/helm
+
+%:
+	dh $@
+
+override_dh_auto_build:
+	mkdir -p oran-o2
+	# Extract oran-o2 chart
+	tar -C oran-o2 --strip-components 2 -xvf pti-o2.tgz pti-o2/charts
+	make CHART_VERSION=$(CHART_VERSION) oran-o2
+	ls -alR
+
+override_dh_auto_install:
+	# Install the app tar file.
+	install -d -m 755 $(APP_FOLDER)
+	install -p -D -m 755 oran-o2*.tgz $(APP_FOLDER)
+
+override_dh_auto_test:
diff --git a/helm-charts/upstream/oran-o2-helm/debian/deb_folder/source/format b/helm-charts/upstream/oran-o2-helm/debian/deb_folder/source/format
new file mode 100644
index 0000000..163aaf8
--- /dev/null
+++ b/helm-charts/upstream/oran-o2-helm/debian/deb_folder/source/format
@@ -0,0 +1 @@
+3.0 (quilt)
diff --git a/helm-charts/upstream/oran-o2-helm/debian/meta_data.yaml b/helm-charts/upstream/oran-o2-helm/debian/meta_data.yaml
new file mode 100644
index 0000000..fe4fe9f
--- /dev/null
+++ b/helm-charts/upstream/oran-o2-helm/debian/meta_data.yaml
@@ -0,0 +1,16 @@
+---
+debname: oran-o2-helm
+debver: 2.0.4-1
+src_path: oran-o2-helm
+src_files:
+  - oran-o2-helm/files/Makefile
+dl_files:
+  pti-o2.tgz:
+    topdir: pti-o2
+    url: https://github.com/o-ran-sc/pti-o2/archive/585eb7242c683657997e8b5ab0ce01213086dcec.tar.gz
+    md5sum: 2483280a37e674059172e6f4510df50f
+revision:
+  dist: $STX_DIST
+  GITREVCOUNT:
+    SRC_DIR: ${MY_REPO}/stx/app-oran-o2/helm-charts/upstream/oran-o2-helm
+    BASE_SRCREV: 49916455ef6c8d6fbee5293424a41c3d11d2eefb
diff --git a/stx-oran-o2-helm/stx-oran-o2-helm/files/Makefile b/helm-charts/upstream/oran-o2-helm/oran-o2-helm/files/Makefile
similarity index 66%
rename from stx-oran-o2-helm/stx-oran-o2-helm/files/Makefile
rename to helm-charts/upstream/oran-o2-helm/oran-o2-helm/files/Makefile
index 90fb4fb..8a93cbb 100644
--- a/stx-oran-o2-helm/stx-oran-o2-helm/files/Makefile
+++ b/helm-charts/upstream/oran-o2-helm/oran-o2-helm/files/Makefile
@@ -1,7 +1,7 @@
 #
 # Copyright 2017 The Openstack-Helm Authors.
 #
-# Copyright (c) 2022 Wind River Systems, Inc.
+# Copyright (c) 2022,2024 Wind River Systems, Inc.
 #
 # SPDX-License-Identifier: Apache-2.0
 #
@@ -9,8 +9,8 @@
 SHELL := /bin/bash
 TASK  := build
 
-EXCLUDES := helm-toolkit doc tests tools logs tmp
-CHARTS := helm-toolkit $(filter-out $(EXCLUDES), $(patsubst %/.,%,$(wildcard */.)))
+EXCLUDES := doc tests tools logs tmp
+CHARTS := $(filter-out $(EXCLUDES), $(patsubst %/.,%,$(wildcard */.)))
 
 .PHONY: $(EXCLUDES) $(CHARTS)
 
@@ -25,18 +25,16 @@ $(CHARTS):
 
 init-%:
 	if [ -f $*/Makefile ]; then make -C $*; fi
-	if [ -f $*/requirements.yaml ]; then helm dep up $*; fi
 
 lint-%: init-%
 	if [ -d $* ]; then helm lint $*; fi
 
 build-%: lint-%
-	if [ -d $* ]; then helm package $*; fi
+	if [ -d $* ]; then helm package --version $(CHART_VERSION) $*; fi
 
 clean:
 	@echo "Clean all build artifacts"
 	rm -f */templates/_partials.tpl */templates/_globals.tpl
-	rm -f *tgz */charts/*tgz */requirements.lock
 	rm -rf */charts */tmpcharts
 
 %:
diff --git a/stx-oran-o2-helm/debian/deb_folder/changelog b/stx-oran-o2-helm/debian/deb_folder/changelog
index a00ded3..4a6583a 100644
--- a/stx-oran-o2-helm/debian/deb_folder/changelog
+++ b/stx-oran-o2-helm/debian/deb_folder/changelog
@@ -1,8 +1,14 @@
+stx-oran-o2-helm (1.2-1) unstable; urgency=medium
+
+  * Auto chart increment update
+
+ --  Joshua Kraitberg <joshua.kraitberg@windriver.com>  Fri, 12 Jul 2024 13:40:00 +0000
+
 stx-oran-o2-helm (1.2-0) unstable; urgency=medium
 
   * Upstream release J
 
- --  Jon Zhang <rong.zhang@windriver.com>  Mon, 10 June 2024 13:40:00 +0000
+ --  Jon Zhang <rong.zhang@windriver.com>  Mon, 10 Jun 2024 13:40:00 +0000
 
 stx-oran-o2-helm (1.1-0) unstable; urgency=medium
 
diff --git a/stx-oran-o2-helm/debian/deb_folder/control b/stx-oran-o2-helm/debian/deb_folder/control
index b665d3e..c19131a 100644
--- a/stx-oran-o2-helm/debian/deb_folder/control
+++ b/stx-oran-o2-helm/debian/deb_folder/control
@@ -3,9 +3,7 @@ Section: libs
 Priority: optional
 Maintainer: StarlingX Developers <starlingx-discuss@lists.starlingx.io>
 Build-Depends: debhelper-compat (= 13),
- chartmuseum,
- helm,
- procps,
+ oran-o2-helm,
  python3-k8sapp-oran-o2,
  python3-k8sapp-oran-o2-wheels,
  build-info
@@ -17,4 +15,4 @@ Section: libs
 Architecture: any
 Depends: ${misc:Depends}
 Description: StarlingX ORAN O2 FluxCD Helm Charts
- This package contains Fluxcd helm charts for the ORAN O2.
+ This package contains Fluxcd manifests for the ORAN O2.
diff --git a/stx-oran-o2-helm/debian/deb_folder/copyright b/stx-oran-o2-helm/debian/deb_folder/copyright
index cb73284..a859927 100644
--- a/stx-oran-o2-helm/debian/deb_folder/copyright
+++ b/stx-oran-o2-helm/debian/deb_folder/copyright
@@ -3,7 +3,7 @@ Upstream-Name: stx-oran-o2-helm
 Source: https://opendev.org/starlingx/app-oran-o2/
 
 Files: *
-Copyright: (c) 2022 Wind River Systems, Inc
+Copyright: (c) 2022,2024 Wind River Systems, Inc
 License: Apache-2
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
@@ -23,7 +23,7 @@ License: Apache-2
 # If you want to use GPL v2 or later for the /debian/* files use
 # the following clauses, or change it to suit. Delete these two lines
 Files: debian/*
-Copyright: 2021 Wind River Systems, Inc
+Copyright: 2021,2024 Wind River Systems, Inc
 License: Apache-2
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
diff --git a/stx-oran-o2-helm/debian/deb_folder/rules b/stx-oran-o2-helm/debian/deb_folder/rules
index dc77e75..501e77f 100755
--- a/stx-oran-o2-helm/debian/deb_folder/rules
+++ b/stx-oran-o2-helm/debian/deb_folder/rules
@@ -10,66 +10,54 @@ export REVISION = $(shell echo $(DEB_VERSION) | cut -f 4 -d '.')
 
 export APP_NAME = oran-o2
 export APP_VERSION = $(RELEASE)-$(REVISION)
-export APP_TARBALL_FLUXCD = $(APP_NAME)-$(APP_VERSION).tgz
+export APP_TARBALL = $(APP_NAME)-$(APP_VERSION).tgz
+export HELM_FOLDER = /usr/lib/helm
 export HELM_REPO = stx-platform
-export STAGING_FLUXCD = staging-fluxcd
-export FLUXCD_O2_PKG = oran-o2-2.0.4.tar.gz
+export STAGING = staging
 
 %:
 	dh $@
 
 override_dh_auto_build:
-
-	############
-	#  FLUXCD  #
-	############
-	mkdir -p fluxcd
-	mkdir -p ${APP_NAME}
-	tar -C ${APP_NAME} --strip-components=1 -xvf $(FLUXCD_O2_PKG)
-
-	# Host a server for the helm charts.
-	cd fluxcd; chartmuseum --debug --port=8879 --context-path='/charts' --storage="local" \
-		--storage-local-rootdir="." &
-	sleep 2
-	helm repo add local http://localhost:8879/charts
-
-	# Create the TGZ file.
-	mkdir -p fluxcd/charts/${APP_NAME}
-	mv ${APP_NAME}/charts/* fluxcd/charts/${APP_NAME}
-	cp files/Makefile fluxcd/charts
-	cd fluxcd/charts && make ${APP_NAME}
-
-	# Terminate the helm chart server.
-	pkill chartmuseum
-
 	# Setup the staging directory.
-	mkdir -p $(STAGING_FLUXCD)
-	cp files/metadata.yaml  $(STAGING_FLUXCD)
-	mkdir -p $(STAGING_FLUXCD)/charts
-	cp fluxcd/charts/*.tgz $(STAGING_FLUXCD)/charts
+	mkdir -p $(STAGING)
+	cp files/metadata.yaml $(STAGING)
+	cp -Rv fluxcd-manifests $(STAGING)
+	mkdir -p $(STAGING)/charts
+	cp /usr/lib/helm/*.tgz $(STAGING)/charts
+
+	# Adjust the helmrelease yamls based on the chart versions
+	for c in $(STAGING)/charts/*; do \
+		chart=$$(basename $$c .tgz); \
+		chart_name=$${chart%-*}; \
+		chart_version=$${chart##*-}; \
+		echo "Found $$chart; name: $$chart_name, version: $$chart_version"; \
+		chart_manifest=$$(find $(STAGING)/fluxcd-manifests -name helmrelease.yaml -exec grep -q "chart:.*$$chart_name" {} \; -print); \
+		echo "Updating manifest: $$chart_manifest"; \
+		sed -i "s/REPLACE_HELM_CHART_VERSION/$$chart_version/g" $$chart_manifest; \
+		grep version $$chart_manifest; \
+	done
 
 	# Populate metadata.
-	sed -i 's/APP_REPLACE_NAME/$(APP_NAME)/g' $(STAGING_FLUXCD)/metadata.yaml
-	sed -i 's/APP_REPLACE_VERSION/$(APP_VERSION)/g' $(STAGING_FLUXCD)/metadata.yaml
-	sed -i 's/HELM_REPLACE_REPO/$(HELM_REPO)/g' $(STAGING_FLUXCD)/metadata.yaml
+	sed -i 's/APP_REPLACE_NAME/$(APP_NAME)/g' $(STAGING)/metadata.yaml
+	sed -i 's/APP_REPLACE_VERSION/$(APP_VERSION)/g' $(STAGING)/metadata.yaml
+	sed -i 's/HELM_REPLACE_REPO/$(HELM_REPO)/g' $(STAGING)/metadata.yaml
 
 	# Copy the plugins: installed in the buildroot
-	mkdir -p $(STAGING_FLUXCD)/plugins
-	cp /plugins/*.whl $(STAGING_FLUXCD)/plugins
+	mkdir -p $(STAGING)/plugins
+	cp /plugins/*.whl $(STAGING)/plugins
 
-	cp -R fluxcd-manifests $(STAGING_FLUXCD)/
-
-	# calculate checksum of all files in app_staging
-	cd $(STAGING_FLUXCD) && find . -type f ! -name '*.md5' -print0 | xargs -0 md5sum > checksum.md5
-	tar cfz $(APP_TARBALL_FLUXCD) -C $(STAGING_FLUXCD)/ .
+	# Create the app package.
+	cd $(STAGING) && find . -type f ! -name '*.md5' -print0 | xargs -0 md5sum > checksum.md5
+	tar cfz $(APP_TARBALL) -C $(STAGING)/ .
 
 	# Cleanup staging
-	rm -rf $(STAGING_FLUXCD)
+	rm -rf $(STAGING)
 
 override_dh_auto_install:
 	# Install the app tar file.
 	install -d -m 755 $(APP_FOLDER)
-	install -p -D -m 755 $(APP_TARBALL_FLUXCD) $(APP_FOLDER)
+	install -p -D -m 755 $(APP_TARBALL) $(APP_FOLDER)
 
 override_dh_auto_test:
 
diff --git a/stx-oran-o2-helm/debian/meta_data.yaml b/stx-oran-o2-helm/debian/meta_data.yaml
index 52d90b6..9478f10 100644
--- a/stx-oran-o2-helm/debian/meta_data.yaml
+++ b/stx-oran-o2-helm/debian/meta_data.yaml
@@ -1,12 +1,7 @@
 ---
 debname: stx-oran-o2-helm
-debver: 1.2-0
+debver: 1.2-1
 src_path: stx-oran-o2-helm
-dl_files:
-  oran-o2-2.0.4.tar.gz:
-    topdir: pti-o2-2.0.4
-    url: https://github.com/o-ran-sc/pti-o2/archive/585eb7242c683657997e8b5ab0ce01213086dcec.tar.gz
-    md5sum: 2483280a37e674059172e6f4510df50f
 revision:
   dist: $STX_DIST
   GITREVCOUNT:
diff --git a/stx-oran-o2-helm/stx-oran-o2-helm/files/index.yaml b/stx-oran-o2-helm/stx-oran-o2-helm/files/index.yaml
deleted file mode 100644
index 36db709..0000000
--- a/stx-oran-o2-helm/stx-oran-o2-helm/files/index.yaml
+++ /dev/null
@@ -1,3 +0,0 @@
-apiVersion: v1
-entries: {}
-generated: 2019-01-07T12:33:46.098166523-06:00
diff --git a/stx-oran-o2-helm/stx-oran-o2-helm/files/repositories.yaml b/stx-oran-o2-helm/stx-oran-o2-helm/files/repositories.yaml
deleted file mode 100644
index e613b63..0000000
--- a/stx-oran-o2-helm/stx-oran-o2-helm/files/repositories.yaml
+++ /dev/null
@@ -1,12 +0,0 @@
-apiVersion: v1
-generated: 2019-01-02T15:19:36.215111369-06:00
-repositories:
-- caFile: ""
-  cache: /builddir/.helm/repository/cache/local-index.yaml
-  certFile: ""
-  keyFile: ""
-  name: local
-  password: ""
-  url: http://127.0.0.1:8879/charts
-  username: ""
-