#!/bin/sh
# Migration 36 --> 37
#
# Copy spec.subnets to spec.networks in VMInstance resources.
# The old "subnets" field is kept intact because migrations run before
# the new CRD is applied; removing it now would lose data.

set -e

if ! kubectl get crd vminstances.apps.cozystack.io >/dev/null 2>&1; then
  echo "VMInstance CRD not found, skipping migration"
else
kubectl get vminstances.apps.cozystack.io -A -o json | jq -r '
  .items[]
  | select(.spec.subnets != null and (.spec.subnets | length) > 0)
  | select(.spec.networks == null or (.spec.networks | length) == 0)
  | "kubectl -n \(.metadata.namespace) patch vminstances.apps.cozystack.io \(.metadata.name) --type=json -p '\''[{\"op\":\"add\",\"path\":\"/spec/networks\",\"value\":\(.spec.subnets|tojson)}]'\''"
' | sh -ex
fi

# Write version to cozystack-version config
kubectl create configmap -n cozy-system cozystack-version --from-literal=version=37 --dry-run=client -o yaml | kubectl apply -f-
