|
| 1 | +{{- $vmName := include "virtual-machine.fullname" . -}} |
| 2 | +{{- $namespace := .Release.Namespace -}} |
| 3 | + |
| 4 | +{{- $existingVM := lookup "kubevirt.io/v1" "VirtualMachine" $namespace $vmName -}} |
| 5 | +{{- $existingPVC := lookup "v1" "PersistentVolumeClaim" $namespace $vmName -}} |
| 6 | + |
| 7 | +{{- $instanceType := .Values.instanceType | default "" -}} |
| 8 | +{{- $instanceProfile := .Values.instanceProfile | default "" -}} |
| 9 | +{{- $desiredStorage := .Values.systemDisk.storage | default "" -}} |
| 10 | + |
| 11 | +{{- $needUpdateType := false -}} |
| 12 | +{{- $needUpdateProfile := false -}} |
| 13 | +{{- $needResizePVC := false -}} |
| 14 | + |
| 15 | +{{- if and $existingVM $instanceType -}} |
| 16 | + {{- if not (eq $existingVM.spec.instancetype.name $instanceType) -}} |
| 17 | + {{- $needUpdateType = true -}} |
| 18 | + {{- end -}} |
| 19 | +{{- end -}} |
| 20 | + |
| 21 | +{{- if and $existingVM $instanceProfile -}} |
| 22 | + {{- if not (eq $existingVM.spec.preference.name $instanceProfile) -}} |
| 23 | + {{- $needUpdateProfile = true -}} |
| 24 | + {{- end -}} |
| 25 | +{{- end -}} |
| 26 | + |
| 27 | +{{- if and $existingPVC $desiredStorage -}} |
| 28 | + {{- $currentStorage := $existingPVC.spec.resources.requests.storage | toString -}} |
| 29 | + {{- if not (eq $currentStorage $desiredStorage) -}} |
| 30 | + {{- $needResizePVC = true -}} |
| 31 | + {{- end -}} |
| 32 | +{{- end -}} |
| 33 | + |
| 34 | +{{- if or $needUpdateType $needUpdateProfile $needResizePVC }} |
| 35 | +apiVersion: batch/v1 |
| 36 | +kind: Job |
| 37 | +metadata: |
| 38 | + name: "{{ $.Release.Name }}-update-hook" |
| 39 | + annotations: |
| 40 | + helm.sh/hook: pre-install,pre-upgrade |
| 41 | + helm.sh/hook-weight: "0" |
| 42 | + helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation |
| 43 | +spec: |
| 44 | + template: |
| 45 | + metadata: |
| 46 | + labels: |
| 47 | + app: "{{ $.Release.Name }}-update-hook" |
| 48 | + spec: |
| 49 | + serviceAccountName: {{ $.Release.Name }}-update-hook |
| 50 | + restartPolicy: Never |
| 51 | + containers: |
| 52 | + - name: update-resources |
| 53 | + image: bitnami/kubectl:latest |
| 54 | + command: ["sh", "-exc"] |
| 55 | + args: |
| 56 | + - | |
| 57 | + {{- if $needUpdateType }} |
| 58 | + echo "Patching VirtualMachine for instancetype update..." |
| 59 | + kubectl patch virtualmachine {{ $vmName }} -n {{ $namespace }} \ |
| 60 | + --type merge \ |
| 61 | + -p '{"spec":{"instancetype":{"name": "{{ $instanceType }}", "revisionName": null}}}' |
| 62 | + {{- end }} |
| 63 | + |
| 64 | + {{- if $needUpdateProfile }} |
| 65 | + echo "Patching VirtualMachine for preference update..." |
| 66 | + kubectl patch virtualmachine {{ $vmName }} -n {{ $namespace }} \ |
| 67 | + --type merge \ |
| 68 | + -p '{"spec":{"preference":{"name": "{{ $instanceProfile }}", "revisionName": null}}}' |
| 69 | + {{- end }} |
| 70 | +
|
| 71 | + {{- if $needResizePVC }} |
| 72 | + echo "Patching PVC for storage resize..." |
| 73 | + kubectl patch pvc {{ $vmName }} -n {{ $namespace }} \ |
| 74 | + --type merge \ |
| 75 | + -p '{"spec":{"resources":{"requests":{"storage":"{{ $desiredStorage }}"}}}}' |
| 76 | + {{- end }} |
| 77 | +--- |
| 78 | +apiVersion: v1 |
| 79 | +kind: ServiceAccount |
| 80 | +metadata: |
| 81 | + name: {{ $.Release.Name }}-update-hook |
| 82 | + annotations: |
| 83 | + helm.sh/hook: pre-install,pre-upgrade |
| 84 | + helm.sh/hook-weight: "-5" |
| 85 | + helm.sh/hook-delete-policy: before-hook-creation |
| 86 | +--- |
| 87 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 88 | +kind: Role |
| 89 | +metadata: |
| 90 | + name: {{ $.Release.Name }}-update-hook |
| 91 | + annotations: |
| 92 | + helm.sh/hook: pre-install,pre-upgrade |
| 93 | + helm.sh/hook-weight: "-5" |
| 94 | + helm.sh/hook-delete-policy: before-hook-creation |
| 95 | +rules: |
| 96 | + - apiGroups: ["kubevirt.io"] |
| 97 | + resources: ["virtualmachines"] |
| 98 | + verbs: ["patch", "get", "list", "watch"] |
| 99 | + - apiGroups: [""] |
| 100 | + resources: ["persistentvolumeclaims"] |
| 101 | + verbs: ["patch", "get", "list", "watch"] |
| 102 | +--- |
| 103 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 104 | +kind: RoleBinding |
| 105 | +metadata: |
| 106 | + name: {{ $.Release.Name }}-update-hook |
| 107 | + annotations: |
| 108 | + helm.sh/hook: pre-install,pre-upgrade |
| 109 | + helm.sh/hook-weight: "-5" |
| 110 | + helm.sh/hook-delete-policy: before-hook-creation |
| 111 | +subjects: |
| 112 | + - kind: ServiceAccount |
| 113 | + name: {{ $.Release.Name }}-update-hook |
| 114 | +roleRef: |
| 115 | + kind: Role |
| 116 | + name: {{ $.Release.Name }}-update-hook |
| 117 | + apiGroup: rbac.authorization.k8s.io |
| 118 | +{{- end }} |
0 commit comments