diff --git a/.github/workflows/packer.yml b/.github/workflows/packer.yml index 28d9517ce..00fb61621 100644 --- a/.github/workflows/packer.yml +++ b/.github/workflows/packer.yml @@ -624,4 +624,5 @@ jobs: image_builder/deploy/00controller.yaml image_builder/deploy/01ui.yaml image_builder/deploy/version-config.yaml + image_builder/deploy/version-checker.yaml image_builder/deploy/vjailbreak-settings.yaml \ No newline at end of file diff --git a/Makefile b/Makefile index c0002a673..a3eed5e77 100644 --- a/Makefile +++ b/Makefile @@ -55,6 +55,7 @@ generate-manifests: vjail-controller ui rm -rf image_builder/deploy && mkdir -p image_builder/deploy && chmod 755 image_builder/deploy envsubst < ui/deploy/ui.yaml > image_builder/deploy/01ui.yaml envsubst < image_builder/configs/version-config.yaml > image_builder/deploy/version-config.yaml + cp image_builder/cronjob/version-checker.yaml image_builder/deploy/version-checker.yaml cp image_builder/configs/vjailbreak-settings.yaml image_builder/deploy/vjailbreak-settings.yaml make -C k8s/migration/ build-installer && cp k8s/migration/dist/install.yaml image_builder/deploy/00controller.yaml @@ -81,4 +82,4 @@ build-image: generate-manifests docker build --platform linux/amd64 --output=artifacts/ -t vjailbreak-image:local image_builder/ run-local: - cd k8s/migration/cmd/ && go run main.go --kubeconfig ${KUBECONFIG} --local true + cd k8s/migration/cmd/ && go run main.go --kubeconfig ${KUBECONFIG} --local true \ No newline at end of file diff --git a/image_builder/cronjob/version-checker.yaml b/image_builder/cronjob/version-checker.yaml new file mode 100644 index 000000000..587d18bd5 --- /dev/null +++ b/image_builder/cronjob/version-checker.yaml @@ -0,0 +1,92 @@ +apiVersion: v1 +kind: ServiceAccount +metadata: + name: version-checker-sa + namespace: migration-system +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: configmap-editor-role + namespace: migration-system +rules: + - apiGroups: [""] + resources: ["configmaps"] + resourceNames: ["version-config"] + verbs: ["get", "patch"] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: version-checker-binding + namespace: migration-system +subjects: + - kind: ServiceAccount + name: version-checker-sa + namespace: migration-system +roleRef: + kind: Role + name: configmap-editor-role + apiGroup: rbac.authorization.k8s.io +--- +apiVersion: batch/v1 +kind: CronJob +metadata: + name: vjailbreak-version-checker + namespace: migration-system +spec: + schedule: "0 0 * * *" + concurrencyPolicy: Forbid + jobTemplate: + spec: + ttlSecondsAfterFinished: 300 + template: + spec: + serviceAccountName: version-checker-sa + restartPolicy: OnFailure + containers: + - name: version-checker + image: alpine:3.18 + command: + - /bin/sh + - -c + - | + set -e + apk add --no-cache curl jq + + curl -LO "https://dl.k8s.io/release/v1.34.0/bin/linux/amd64/kubectl" + + chmod +x kubectl + mv kubectl /usr/local/bin/ + + echo "Checking for latest version" + CURRENT_VERSION=$(kubectl get configmap version-config -n migration-system -o jsonpath='{.data.version}') + echo "Current version: $CURRENT_VERSION" + + if [ -z "$CURRENT_VERSION" ]; then + echo "Error: 'version' key not found in ConfigMap. Exiting." + exit 1 + fi + + LATEST_TAG=$(curl -s "https://api.github.com/repos/platform9/vjailbreak/tags" | jq -r '.[0].name') + + if [ -z "$LATEST_TAG" ] || [ "$LATEST_TAG" == "null" ]; then + echo "Error: Could not fetch latest tag from GitHub. Exiting." + exit 1 + fi + echo "Latest tag on GitHub: $LATEST_TAG" + + CURRENT_SEMVER=${CURRENT_VERSION#v} + LATEST_SEMVER=${LATEST_TAG#v} + HIGHEST_VERSION=$(printf "%s\n%s" "$CURRENT_SEMVER" "$LATEST_SEMVER" | sort -V | tail -n1) + + if [ "$LATEST_SEMVER" != "$CURRENT_SEMVER" ] && [ "$HIGHEST_VERSION" == "$LATEST_SEMVER" ]; then + echo "New version found! Updating ConfigMap..." + PATCH_DATA="{\"data\":{\"upgradeAvailable\":\"true\",\"upgradeVersion\":\"$LATEST_TAG\"}}" + kubectl patch configmap version-config -n migration-system --type='merge' -p="$PATCH_DATA" + echo "ConfigMap 'version-config' updated successfully." + else + echo "You are on the latest version. No update needed." + fi + + echo "Version Check Complete" \ No newline at end of file diff --git a/image_builder/vjailbreak-image.pkr.hcl b/image_builder/vjailbreak-image.pkr.hcl index afe892ea1..8a6fd1113 100644 --- a/image_builder/vjailbreak-image.pkr.hcl +++ b/image_builder/vjailbreak-image.pkr.hcl @@ -73,6 +73,11 @@ build { destination = "/tmp/vjailbreak-settings.yaml" } + provisioner "file" { + source = "${path.root}/cronjob/version-checker.yaml" + destination = "/tmp/version-checker.yaml" + } + provisioner "file" { source = "${path.root}/images" destination = "/home/ubuntu" @@ -97,6 +102,7 @@ build { "sudo mv /tmp/daemonset.yaml /etc/pf9/yamls/daemonset.yaml", "sudo mv /tmp/env /etc/pf9/env", "sudo mv /tmp/vjailbreak-settings.yaml /etc/pf9/yamls/vjailbreak-settings.yaml", + "sudo mv /tmp/version-checker.yaml /etc/pf9/yamls/version-checker.yaml", "sudo mv /tmp/opensource.txt /home/ubuntu/opensource.txt", "sudo chmod +x /etc/pf9/install.sh", "sudo chown root:root /etc/pf9/k3s.env",