Skip to content

Commit

Permalink
Add hooks to update instanceType, instanceProfile, and storage (#590)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
	- Added update hook for Virtual Machine configurations
- Enhanced version management for virtual machine and VM instance
packages

- **Version Updates**
	- Virtual Machine package version updated from 0.6.0 to 0.7.0
	- VM Instance package version updated from 0.3.0 to 0.4.0

- **Improvements**
- Introduced dynamic configuration update mechanisms for Kubernetes
deployments
- Added service account and role permissions for VM configuration
management

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
kvaps authored Jan 17, 2025
1 parent af58018 commit 2f53363
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 11 deletions.
6 changes: 4 additions & 2 deletions packages/apps/versions_map
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,13 @@ virtual-machine 0.2.0 5ca8823
virtual-machine 0.3.0 b908400
virtual-machine 0.4.0 4746d51
virtual-machine 0.5.0 cad9cde
virtual-machine 0.6.0 HEAD
virtual-machine 0.6.0 0e728870
virtual-machine 0.7.0 HEAD
vm-disk 0.1.0 HEAD
vm-instance 0.1.0 ced8e5b9
vm-instance 0.2.0 4f767ee3
vm-instance 0.3.0 HEAD
vm-instance 0.3.0 0e728870
vm-instance 0.4.0 HEAD
vpn 0.1.0 f642698
vpn 0.2.0 7151424
vpn 0.3.0 a2bcf100
Expand Down
4 changes: 2 additions & 2 deletions packages/apps/virtual-machine/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.6.0
version: 0.7.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.16.1"
appVersion: "0.7.0"
118 changes: 118 additions & 0 deletions packages/apps/virtual-machine/templates/vm-update-hook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{{- $vmName := include "virtual-machine.fullname" . -}}
{{- $namespace := .Release.Namespace -}}

{{- $existingVM := lookup "kubevirt.io/v1" "VirtualMachine" $namespace $vmName -}}
{{- $existingPVC := lookup "v1" "PersistentVolumeClaim" $namespace $vmName -}}

{{- $instanceType := .Values.instanceType | default "" -}}
{{- $instanceProfile := .Values.instanceProfile | default "" -}}
{{- $desiredStorage := .Values.systemDisk.storage | default "" -}}

{{- $needUpdateType := false -}}
{{- $needUpdateProfile := false -}}
{{- $needResizePVC := false -}}

{{- if and $existingVM $instanceType -}}
{{- if not (eq $existingVM.spec.instancetype.name $instanceType) -}}
{{- $needUpdateType = true -}}
{{- end -}}
{{- end -}}

{{- if and $existingVM $instanceProfile -}}
{{- if not (eq $existingVM.spec.preference.name $instanceProfile) -}}
{{- $needUpdateProfile = true -}}
{{- end -}}
{{- end -}}

{{- if and $existingPVC $desiredStorage -}}
{{- $currentStorage := $existingPVC.spec.resources.requests.storage | toString -}}
{{- if not (eq $currentStorage $desiredStorage) -}}
{{- $needResizePVC = true -}}
{{- end -}}
{{- end -}}

{{- if or $needUpdateType $needUpdateProfile $needResizePVC }}
apiVersion: batch/v1
kind: Job
metadata:
name: "{{ $.Release.Name }}-update-hook"
annotations:
helm.sh/hook: pre-install,pre-upgrade
helm.sh/hook-weight: "0"
helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation
spec:
template:
metadata:
labels:
app: "{{ $.Release.Name }}-update-hook"
spec:
serviceAccountName: {{ $.Release.Name }}-update-hook
restartPolicy: Never
containers:
- name: update-resources
image: bitnami/kubectl:latest
command: ["sh", "-exc"]
args:
- |
{{- if $needUpdateType }}
echo "Patching VirtualMachine for instancetype update..."
kubectl patch virtualmachine {{ $vmName }} -n {{ $namespace }} \
--type merge \
-p '{"spec":{"instancetype":{"name": "{{ $instanceType }}", "revisionName": null}}}'
{{- end }}
{{- if $needUpdateProfile }}
echo "Patching VirtualMachine for preference update..."
kubectl patch virtualmachine {{ $vmName }} -n {{ $namespace }} \
--type merge \
-p '{"spec":{"preference":{"name": "{{ $instanceProfile }}", "revisionName": null}}}'
{{- end }}
{{- if $needResizePVC }}
echo "Patching PVC for storage resize..."
kubectl patch pvc {{ $vmName }} -n {{ $namespace }} \
--type merge \
-p '{"spec":{"resources":{"requests":{"storage":"{{ $desiredStorage }}"}}}}'
{{- end }}
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ $.Release.Name }}-update-hook
annotations:
helm.sh/hook: pre-install,pre-upgrade
helm.sh/hook-weight: "-5"
helm.sh/hook-delete-policy: before-hook-creation
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: {{ $.Release.Name }}-update-hook
annotations:
helm.sh/hook: pre-install,pre-upgrade
helm.sh/hook-weight: "-5"
helm.sh/hook-delete-policy: before-hook-creation
rules:
- apiGroups: ["kubevirt.io"]
resources: ["virtualmachines"]
verbs: ["patch", "get", "list", "watch"]
- apiGroups: [""]
resources: ["persistentvolumeclaims"]
verbs: ["patch", "get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: {{ $.Release.Name }}-update-hook
annotations:
helm.sh/hook: pre-install,pre-upgrade
helm.sh/hook-weight: "-5"
helm.sh/hook-delete-policy: before-hook-creation
subjects:
- kind: ServiceAccount
name: {{ $.Release.Name }}-update-hook
roleRef:
kind: Role
name: {{ $.Release.Name }}-update-hook
apiGroup: rbac.authorization.k8s.io
{{- end }}
4 changes: 2 additions & 2 deletions packages/apps/vm-instance/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.3.0
version: 0.4.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "0.2.0"
appVersion: "0.4.0"
98 changes: 98 additions & 0 deletions packages/apps/vm-instance/templates/vm-update-hook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{{- $vmName := include "virtual-machine.fullname" . -}}
{{- $namespace := .Release.Namespace -}}

{{- $existingVM := lookup "kubevirt.io/v1" "VirtualMachine" $namespace $vmName -}}

{{- $instanceType := .Values.instanceType | default "" -}}
{{- $instanceProfile := .Values.instanceProfile | default "" -}}

{{- $needUpdateType := false -}}
{{- $needUpdateProfile := false -}}

{{- if and $existingVM $instanceType -}}
{{- if not (eq $existingVM.spec.instancetype.name $instanceType) -}}
{{- $needUpdateType = true -}}
{{- end -}}
{{- end -}}

{{- if and $existingVM $instanceProfile -}}
{{- if not (eq $existingVM.spec.preference.name $instanceProfile) -}}
{{- $needUpdateProfile = true -}}
{{- end -}}
{{- end -}}

{{- if or $needUpdateType $needUpdateProfile }}
apiVersion: batch/v1
kind: Job
metadata:
name: "{{ $.Release.Name }}-update-hook"
annotations:
helm.sh/hook: pre-install,pre-upgrade
helm.sh/hook-weight: "0"
helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation
spec:
template:
metadata:
labels:
app: "{{ $.Release.Name }}-update-hook"
spec:
serviceAccountName: {{ $.Release.Name }}-update-hook
restartPolicy: Never
containers:
- name: update-resources
image: bitnami/kubectl:latest
command: ["sh", "-exc"]
args:
- |
{{- if $needUpdateType }}
echo "Patching VirtualMachine for instancetype update..."
kubectl patch virtualmachine {{ $vmName }} -n {{ $namespace }} \
--type merge \
-p '{"spec":{"instancetype":{"name": "{{ $instanceType }}", "revisionName": null}}}'
{{- end }}
{{- if $needUpdateProfile }}
echo "Patching VirtualMachine for preference update..."
kubectl patch virtualmachine {{ $vmName }} -n {{ $namespace }} \
--type merge \
-p '{"spec":{"preference":{"name": "{{ $instanceProfile }}", "revisionName": null}}}'
{{- end }}
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ $.Release.Name }}-update-hook
annotations:
helm.sh/hook: pre-install,pre-upgrade
helm.sh/hook-weight: "-5"
helm.sh/hook-delete-policy: before-hook-creation
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: {{ $.Release.Name }}-update-hook
annotations:
helm.sh/hook: pre-install,pre-upgrade
helm.sh/hook-weight: "-5"
helm.sh/hook-delete-policy: before-hook-creation
rules:
- apiGroups: ["kubevirt.io"]
resources: ["virtualmachines"]
verbs: ["patch", "get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: {{ $.Release.Name }}-update-hook
annotations:
helm.sh/hook: pre-install,pre-upgrade
helm.sh/hook-weight: "-5"
helm.sh/hook-delete-policy: before-hook-creation
subjects:
- kind: ServiceAccount
name: {{ $.Release.Name }}-update-hook
roleRef:
kind: Role
name: {{ $.Release.Name }}-update-hook
apiGroup: rbac.authorization.k8s.io
{{- end }}
7 changes: 2 additions & 5 deletions packages/apps/vm-instance/templates/vm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ spec:
instancetype:
kind: VirtualMachineClusterInstancetype
name: {{ . }}
revisionName: null
{{- end }}
{{- end }}
{{- with .Values.instanceProfile }}
preference:
kind: VirtualMachineClusterPreference
name: {{ . }}
revisionName: null
{{- end }}

{{- end }}
template:
metadata:
annotations:
Expand Down

0 comments on commit 2f53363

Please sign in to comment.