From 40cac70490032b9fe2815602cbbb3765c1b1e5cc Mon Sep 17 00:00:00 2001
From: Roberto Bonafiglia <roberto.bonafiglia@suse.com>
Date: Tue, 14 Jan 2025 15:52:56 +0100
Subject: [PATCH] Added automatic update for cni plugins image on cilium chart

Signed-off-by: Roberto Bonafiglia <roberto.bonafiglia@suse.com>
---
 updatecli/scripts/update-cilium.sh      | 10 ++++++++++
 updatecli/updatecli.d/updatecilium.yaml | 21 ++++++++++++++++++++-
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/updatecli/scripts/update-cilium.sh b/updatecli/scripts/update-cilium.sh
index 3dfdded..08decb7 100755
--- a/updatecli/scripts/update-cilium.sh
+++ b/updatecli/scripts/update-cilium.sh
@@ -1,5 +1,15 @@
 #!/bin/bash
 set -eu
+if [ -n "$CNI_PLUGINS_VERSION" ]; then
+	current_cni_plugins_version=$(sed -nr 's/\+    tag: \"(v'[0-9]+.[0-9]+.[0-9]+-build[0-9]+')\"/\1/p' packages/rke2-cilium/generated-changes/patch/values.yaml.patch)
+	if [ "$current_cni_plugins_version" != "$CNI_PLUGINS_VERSION" ]; then
+		sed -ie "s/$current_cni_plugins_version/$CNI_PLUGINS_VERSION/g" packages/rke2-cilium/generated-changes/patch/values.yaml.patch
+		sed -ie "s/$current_cni_plugins_version/$CNI_PLUGINS_VERSION/g" updatecli/scripts/cilium-values.yaml.patch.template
+		package_version=$(yq '.packageVersion' packages/rke2-cilium/package.yaml)
+		new_version=$(printf "%02d" $(($package_version + 1)))
+		yq -i ".packageVersion = $new_version" packages/rke2-cilium/package.yaml
+	fi
+fi
 if [ -n "$CILIUM_VERSION" ]; then
 	current_cilium_version=$(sed -nr 's/^\ version: ('[0-9]+.[0-9]+.[0-9]+')/\1/p' packages/rke2-cilium/generated-changes/patch/Chart.yaml.patch)
 	if [ "v$current_cilium_version" != "$CILIUM_VERSION" ]; then
diff --git a/updatecli/updatecli.d/updatecilium.yaml b/updatecli/updatecli.d/updatecilium.yaml
index 5536cfb..34be76c 100644
--- a/updatecli/updatecli.d/updatecilium.yaml
+++ b/updatecli/updatecli.d/updatecilium.yaml
@@ -16,6 +16,23 @@ sources:
         latest: true
       versionfilter:
         kind: latest
+  cni_plugins:
+    name: Get CNI plugins version
+    kind: githubrelease
+    spec:
+      owner: rancher
+      repository: image-build-cni-plugins
+      token: '{{ requiredEnv .github.token }}'
+      username: '{{ requiredEnv .github.username }}'
+      typefilter:
+        latest: true
+        release: true
+        draft: false
+        prerelease: false
+      versionfilter:
+        kind: regex
+        # pattern accepts any semver constraint
+        pattern: "v[0-9]+.[0-9]+.[0-9]+-build[0-9]+"
 
 targets:
   ciliumImage:
@@ -28,6 +45,8 @@ targets:
       environments:
         - name: CILIUM_VERSION
           value: '{{ source "cilium" }}'
+        - name: CNI_PLUGINS_VERSION
+          value: '{{ source "cni_plugins" }}'
         - name: PATH
 
 
@@ -45,7 +64,7 @@ scms:
       
 actions:
   default:
-    title: 'Update Cilium version to {{ source "cilium" }}'
+    title: 'Update Cilium version to {{ source "cilium" }} and CNI plugins image to {{ source "cni_plugins" }}'
     kind: github/pullrequest
     spec:
       automerge: false