Skip to content

Commit

Permalink
Introduce InstanceTypes
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Kvapil <[email protected]>
  • Loading branch information
kvaps committed Oct 8, 2024
1 parent cbadfef commit 41cf90b
Show file tree
Hide file tree
Showing 17 changed files with 3,679 additions and 95 deletions.
4 changes: 4 additions & 0 deletions packages/apps/kubernetes/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ image-cluster-autoscaler:
echo "$(REGISTRY)/cluster-autoscaler:$(call settag,$(KUBERNETES_PKG_TAG))@$$(yq e '."containerimage.digest"' images/cluster-autoscaler.json -o json -r)" \
> images/cluster-autoscaler.tag
rm -f images/cluster-autoscaler.json

prefs:
INSTANCE_TYPES=$(yq e '.metadata.name' -o=json -r ../../system/kubevirt-common-instancetypes/templates/instancetypes.yaml)
INSTANCE_PREFS=$(yq e '.metadata.name' -o=json -r ../../system/kubevirt-common-instancetypes/templates/preferences.yaml)
25 changes: 25 additions & 0 deletions packages/apps/kubernetes/templates/cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ spec:
node-role.kubernetes.io/{{ . }}: ""
{{- end }}
spec:
{{- with .group.instanceType }}
instancetype:
kind: VirtualMachineClusterInstancetype
name: {{ . }}
{{- end }}
runStrategy: Always
template:
metadata:
Expand All @@ -26,10 +31,12 @@ spec:
{{- end }}
spec:
domain:
{{- if and .group.resources .group.resources.cpu }}
cpu:
threads: 1
cores: {{ .group.resources.cpu }}
sockets: 1
{{- end }}
devices:
disks:
- name: system
Expand All @@ -43,8 +50,10 @@ spec:
interfaces:
- name: default
bridge: {}
{{- if and .group.resources .group.resources.memory }}
memory:
guest: {{ .group.resources.memory }}
{{- end }}
evictionStrategy: External
volumes:
- name: system
Expand Down Expand Up @@ -176,6 +185,14 @@ spec:
template:
{{- $kubevirtmachinetemplate | nindent 4 }}
---
{{- $instanceType := dict }}
{{- if $group.instanceType }}
{{- $instanceType = (lookup "instancetype.kubevirt.io/v1beta1" "VirtualMachineClusterInstancetype" "" $group.instanceType) }}
{{- if not $instanceType }}
{{- fail (printf "Specified instancetype not exists in cluster: %s" $group.instanceType) }}
{{- end }}
{{- end }}

apiVersion: cluster.x-k8s.io/v1beta1
kind: MachineDeployment
metadata:
Expand All @@ -184,8 +201,16 @@ metadata:
annotations:
cluster.x-k8s.io/cluster-api-autoscaler-node-group-min-size: "{{ $group.minReplicas }}"
cluster.x-k8s.io/cluster-api-autoscaler-node-group-max-size: "{{ $group.maxReplicas }}"
{{- if and $group.resources $group.resources.memory }}
capacity.cluster-autoscaler.kubernetes.io/memory: "{{ $group.resources.memory }}"
{{- else }}
capacity.cluster-autoscaler.kubernetes.io/memory: "{{ $instanceType.spec.memory.guest }}"
{{- end }}
{{- if and $group.resources $group.resources.cpu }}
capacity.cluster-autoscaler.kubernetes.io/cpu: "{{ $group.resources.cpu }}"
{{- else }}
capacity.cluster-autoscaler.kubernetes.io/cpu: "{{ $instanceType.spec.cpu.guest }}"
{{- end }}
spec:
clusterName: {{ $.Release.Name }}
template:
Expand Down
8 changes: 5 additions & 3 deletions packages/apps/kubernetes/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ nodeGroups:
md0:
minReplicas: 0
maxReplicas: 10
resources:
cpu: 2
memory: 1024Mi
instanceType: "u1.medium"
ephemeralStorage: 20Gi
roles:
- ingress-nginx

resources:
cpu: ""
memory: ""

## @section Cluster Addons
##
addons:
Expand Down
2 changes: 1 addition & 1 deletion packages/apps/virtual-machine/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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.4.0
version: 0.5.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
Expand Down
13 changes: 6 additions & 7 deletions packages/apps/virtual-machine/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
include ../../../scripts/package.mk

generate:
readme-generator -v values.yaml -s values.schema.json.tmp -r README.md
cat values.schema.json.tmp | \
jq '.properties.image.enum = ["ubuntu", "cirros", "alpine", "fedora", "talos"]' | \
jq '.properties.resources.properties.memory["x-display"] = "slider"' | \
jq '.properties.externalPorts.items.type = "integer"' \
> values.schema.json
rm -f values.schema.json.tmp
readme-generator -v values.yaml -s values.schema.json -r README.md
INSTANCE_TYPES=$$(yq e '.metadata.name' -o=json -r ../../system/kubevirt-common-instancetypes/templates/instancetypes.yaml | yq 'split(" ")' -o json) \
&& yq -i -o json ".properties.instanceType.optional=true | .properties.instanceType.enum = $${INSTANCE_TYPES}" values.schema.json
PREFERENCES=$$(yq e '.metadata.name' -o=json -r ../../system/kubevirt-common-instancetypes/templates/preferences.yaml | yq 'split(" ")' -o json) \
&& yq -i -o json ".properties.instanceProfile.optional=true | .properties.instanceProfile.enum = $${PREFERENCES}" values.schema.json
yq -i -o json '.properties.externalPorts.items.type = "integer"' values.schema.json
53 changes: 16 additions & 37 deletions packages/apps/virtual-machine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,19 @@ virtctl ssh <user>@<vm>

### Common parameters

| Name | Description | Value |
| ------------------ | ---------------------------------------------------------------------------------------------------------- | ---------------- |
| `external` | Enable external access from outside the cluster | `false` |
| `externalPorts` | Specify ports to forward from outside the cluster | `[]` |
| `running` | Determines if the virtual machine should be running | `true` |
| `image` | The base image for the virtual machine. Allowed values: `ubuntu`, `cirros`, `alpine`, `fedora` and `talos` | `ubuntu` |
| `storageClass` | StorageClass used to store the data | `replicated` |
| `resources.cpu` | The number of CPU cores allocated to the virtual machine | `1` |
| `resources.memory` | The amount of memory allocated to the virtual machine | `1024M` |
| `resources.disk` | The size of the disk allocated for the virtual machine | `5Gi` |
| `sshKeys` | List of SSH public keys for authentication. Can be a single key or a list of keys. | `[]` |
| `cloudInit` | cloud-init user data config. See cloud-init documentation for more details. | `#cloud-config
` |

You can customize the exposed ports by specifying them under `service.ports` in the `values.yaml` file.

## Example virtual machine:

```yaml
running: true
image: fedora
storageClass: replicated
resources:
cpu: 1
memory: 1024M
disk: 10Gi

sshKeys:
- ssh-rsa ...

cloudInit: |
#cloud-config
user: fedora
password: fedora
chpasswd: { expire: False }
ssh_pwauth: True
```
| Name | Description | Value |
| ------------------------- | ---------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- |
| `external` | Enable external access from outside the cluster | `false` |
| `externalPorts` | Specify ports to forward from outside the cluster | `[]` |
| `running` | Determines if the virtual machine should be running | `true` |
| `instanceType` | Virtual Machine instance type | `u1.medium` |
| `instanceProfile` | Virtual Machine prefferences profile | `ubuntu` |
| `systemDisk.origin` | The HTTP URL for source image | `https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img` |
| `systemDisk.storage` | The size of the disk allocated for the virtual machine | `5Gi` |
| `systemDisk.storageClass` | StorageClass used to store the data | `replicated` |
| `resources.cpu` | The number of CPU cores allocated to the virtual machine | `""` |
| `resources.memory` | The amount of memory allocated to the virtual machine | `""` |
| `sshKeys` | List of SSH public keys for authentication. Can be a single key or a list of keys. | `[]` |
| `cloudInit` | cloud-init user data config. See cloud-init documentation for more details. | `#cloud-config
` |

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#!/bin/sh
48 changes: 32 additions & 16 deletions packages/apps/virtual-machine/templates/vm.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{{- if and .Values.instanceType (not (lookup "instancetype.kubevirt.io/v1beta1" "VirtualMachineClusterInstancetype" "" .Values.instanceType)) }}
{{- fail (printf "Specified instancetype not exists in cluster: %s" .Values.instanceType) }}
{{- end }}
{{- if and .Values.instanceProfile (not (lookup "instancetype.kubevirt.io/v1beta1" "VirtualMachineClusterPreference" "" .Values.instanceProfile)) }}
{{- fail (printf "Specified profile not exists in cluster: %s" .Values.instanceProfile) }}
{{- end }}

apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
Expand All @@ -6,6 +13,16 @@ metadata:
{{- include "virtual-machine.labels" . | nindent 4 }}
spec:
running: {{ .Values.running | default "true" }}
{{- with .Values.instanceType }}
instancetype:
kind: VirtualMachineClusterInstancetype
name: {{ . }}
{{- end }}
{{- with .Values.instanceProfile }}
preference:
kind: VirtualMachineClusterPreference
name: {{ . }}
{{- end }}
dataVolumeTemplates:
- metadata:
name: {{ include "virtual-machine.fullname" . }}
Expand All @@ -16,23 +33,18 @@ spec:
- ReadWriteMany
resources:
requests:
storage: {{ .Values.resources.disk | quote }}
{{- with $.Values.storageClass }}
storage: {{ $.Values.systemDisk.storage | quote }}
{{- with $.Values.systemDisk.storageClass }}
storageClassName: {{ . }}
{{- end }}
source:
{{- if $.Values.systemDisk.origin }}
http:
{{- if eq .Values.image "cirros" }}
url: https://download.cirros-cloud.net/0.6.2/cirros-0.6.2-x86_64-disk.img
{{- else if eq .Values.image "ubuntu" }}
url: https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img
{{- else if eq .Values.image "fedora" }}
url: https://download.fedoraproject.org/pub/fedora/linux/releases/40/Cloud/x86_64/images/Fedora-Cloud-Base-Generic.x86_64-40-1.14.qcow2
{{- else if eq .Values.image "alpine" }}
url: https://dl-cdn.alpinelinux.org/alpine/v3.20/releases/cloud/nocloud_alpine-3.20.2-x86_64-bios-tiny-r0.qcow2
{{- else if eq .Values.image "talos" }}
url: https://github.com/siderolabs/talos/releases/download/v1.7.6/nocloud-amd64.raw.xz
{{- end }}
url: {{ $.Values.systemDisk.origin }}
{{- else }}
blank: {}
{{- end }}

template:
metadata:
annotations:
Expand All @@ -41,8 +53,15 @@ spec:
{{- include "virtual-machine.labels" . | nindent 8 }}
spec:
domain:
{{- if and .Values.resources .Values.resources.cpu }}
cpu:
cores: {{ .Values.resources.cpu }}
{{- end }}
{{- if and .Values.resources .Values.resources.memory }}
resources:
requests:
memory: {{ .Values.resources.memory | quote }}
{{- end }}
devices:
disks:
- disk:
Expand All @@ -58,9 +77,6 @@ spec:
bridge: {}
machine:
type: ""
resources:
requests:
memory: {{ .Values.resources.memory | quote }}
{{- with .Values.sshKeys }}
accessCredentials:
- sshPublicKey:
Expand Down
Loading

0 comments on commit 41cf90b

Please sign in to comment.