Skip to content

Commit

Permalink
neonvm: add support for mounting service account tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
conradludgate committed Mar 11, 2025
1 parent e024b88 commit b2b7b28
Show file tree
Hide file tree
Showing 7 changed files with 429 additions and 2 deletions.
4 changes: 2 additions & 2 deletions neonvm-runner/cmd/disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func setupVMDisks(
discard = ",discard=unmap"
}
qemuCmd = append(qemuCmd, "-drive", fmt.Sprintf("id=%s,file=%s,if=virtio,media=disk,%s%s", disk.Name, dPath, diskCacheSettings, discard))
case disk.ConfigMap != nil || disk.Secret != nil:
case disk.ConfigMap != nil || disk.Secret != nil || disk.Projected != nil:
dPath := fmt.Sprintf("%s/%s.iso", mountedDiskPath, disk.Name)
mnt := fmt.Sprintf("/vm/mounts%s", disk.MountPath)
logger.Info("creating iso9660 image", zap.String("diskPath", dPath), zap.String("diskName", disk.Name), zap.String("mountPath", mnt))
Expand Down Expand Up @@ -206,7 +206,7 @@ func createISO9660runtime(
mounts = append(mounts, fmt.Sprintf(`/neonvm/bin/mount %s $(/neonvm/bin/blkid -L %s) %s`, opts, disk.Name, disk.MountPath))
// Note: chmod must be after mount, otherwise it gets overwritten by mount.
mounts = append(mounts, fmt.Sprintf(`/neonvm/bin/chmod 0777 %s`, disk.MountPath))
case disk.ConfigMap != nil || disk.Secret != nil:
case disk.ConfigMap != nil || disk.Secret != nil || disk.Projected != nil:
mounts = append(mounts, fmt.Sprintf(`/neonvm/bin/mount -t iso9660 -o ro,mode=0644 $(/neonvm/bin/blkid -L %s) %s`, disk.Name, disk.MountPath))
case disk.Tmpfs != nil:
mounts = append(mounts, fmt.Sprintf(`/neonvm/bin/chmod 0777 %s`, disk.MountPath))
Expand Down
3 changes: 3 additions & 0 deletions neonvm/apis/neonvm/v1/virtualmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,9 @@ type DiskSource struct {
// Secret represents a secret that should populate this disk.
// +optional
Secret *corev1.SecretVolumeSource `json:"secret,omitempty"`
// Projected represents a projected volume that should populate this disk.
// +optional
Projected *corev1.ProjectedVolumeSource `json:"projected,omitempty"`
// TmpfsDisk represents a tmpfs.
// +optional
Tmpfs *TmpfsDiskSource `json:"tmpfs,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions neonvm/apis/neonvm/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

349 changes: 349 additions & 0 deletions neonvm/config/crd/bases/vm.neon.tech_virtualmachines.yaml

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions pkg/neonvm/controllers/vm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,16 @@ func podSpec(
},
},
})
case disk.Projected != nil:
pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, mnt)
pod.Spec.Volumes = append(pod.Spec.Volumes, corev1.Volume{
Name: disk.Name,
VolumeSource: corev1.VolumeSource{
Projected: &corev1.ProjectedVolumeSource{
Sources: disk.Projected.Sources,
},
},
})
case disk.EmptyDisk != nil:
pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, mnt)
pod.Spec.Volumes = append(pod.Spec.Volumes, corev1.Volume{
Expand Down
21 changes: 21 additions & 0 deletions tests/e2e/vm-projected/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: 70
commands:
- script: |
set -eux
pod="$(kubectl get neonvm -n "$NAMESPACE" example -o jsonpath='{.status.podName}')"
kubectl exec -n "$NAMESPACE" $pod -- scp guest-vm:/var/run/secrets/tokens/token token
---
apiVersion: vm.neon.tech/v1
kind: VirtualMachine
metadata:
name: example
status:
phase: Running
restartCount: 0
conditions:
- type: Available
status: "True"
cpus: 250m
memorySize: 1Gi
39 changes: 39 additions & 0 deletions tests/e2e/vm-projected/00-create-vm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
unitTest: false
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: test-account
---
apiVersion: vm.neon.tech/v1
kind: VirtualMachine
metadata:
name: example
spec:
schedulerName: autoscale-scheduler
enableSSH: true
guest:
cpus:
min: 0.25
use: 0.25
max: 0.25
memorySlotSize: 1Gi
memorySlots:
min: 1
use: 1
max: 1
rootDisk:
image: vm-postgres:15-bullseye
size: 1Gi
serviceAccountName: "test-account"
disks:
- projected:
sources:
- serviceAccountToken:
expirationSeconds: 3600
path: token
mountPath: /var/run/secrets/tokens
name: token
watch: true

0 comments on commit b2b7b28

Please sign in to comment.