Skip to content

Commit

Permalink
Support PVCs with BYOK
Browse files Browse the repository at this point in the history
This patch allows PVCs to be attached to VMs encrypted with BYOK.
  • Loading branch information
akutz committed Oct 17, 2024
1 parent 8c5e06c commit 0bad8f4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 72 deletions.
21 changes: 5 additions & 16 deletions webhooks/virtualmachine/validation/virtualmachine_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,40 +827,29 @@ func (v validator) validateVolumes(ctx *pkgctx.WebhookRequestContext, vm *vmopv1
if vol.PersistentVolumeClaim == nil {
allErrs = append(allErrs, field.Required(volPath.Child("persistentVolumeClaim"), ""))
} else {
allErrs = append(allErrs, v.validateVolumeWithPVC(ctx, vm, vol, volPath)...)
allErrs = append(allErrs, v.validateVolumeWithPVC(vol, volPath)...)
}
}

return allErrs
}

func (v validator) validateVolumeWithPVC(
ctx *pkgctx.WebhookRequestContext,
vm *vmopv1.VirtualMachine,
vol vmopv1.VirtualMachineVolume,
volPath *field.Path) field.ErrorList {

var (
allErrs field.ErrorList
encClassName string
pvcPath = volPath.Child("persistentVolumeClaim")
claimName = vol.PersistentVolumeClaim.ClaimName
allErrs field.ErrorList
pvcPath = volPath.Child("persistentVolumeClaim")
claimName = vol.PersistentVolumeClaim.ClaimName
)

if vm.Spec.Crypto != nil {
encClassName = vm.Spec.Crypto.EncryptionClassName
}

if claimName == "" {
allErrs = append(
allErrs,
field.Required(pvcPath.Child("claimName"), ""))
} else if encClassName != "" && pkgcfg.FromContext(ctx).Features.BringYourOwnEncryptionKey {
allErrs = append(allErrs, field.Invalid(
pvcPath.Child("claimName"),
claimName,
fmt.Sprintf(invalidPVCBYOKFmt, encClassName)))
}

if vol.PersistentVolumeClaim.ReadOnly {
allErrs = append(allErrs, field.NotSupported(pvcPath.Child("readOnly"), true, []string{"false"}))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ func unitTestsValidateCreate() {
`spec.crypto.encryptionClassName: Invalid value: "fake": requires spec.storageClass specify an encryption storage class`),
},
),
Entry("disallow volume when spec.crypto.encryptionClassName is non-empty when FSS_WCP_VMSERVICE_BYOK is enabled",
Entry("allow volume when spec.crypto.encryptionClassName is non-empty when FSS_WCP_VMSERVICE_BYOK is enabled",
testParams{
setup: func(ctx *unitValidatingWebhookContext) {
storageClass1 := builder.DummyStorageClass()
Expand Down Expand Up @@ -902,61 +902,6 @@ func unitTestsValidateCreate() {
config.Features.BringYourOwnEncryptionKey = true
})
},
validate: doValidateWithMsg(
`spec.volumes[0].persistentVolumeClaim.claimName: Invalid value: "dummyPVCName": cannot attach volume to vm with spec.crypto.encryptionClassName="fake"`),
},
),
Entry("allow volume when spec.crypto.encryptionClassName is empty when FSS_WCP_VMSERVICE_BYOK is enabled",
testParams{
setup: func(ctx *unitValidatingWebhookContext) {
storageClass1 := builder.DummyStorageClass()
Expect(ctx.Client.Create(ctx, storageClass1)).To(Succeed())

storageClass2 := builder.DummyStorageClass()
storageClass2.Name += "2"
Expect(ctx.Client.Create(ctx, storageClass2)).To(Succeed())

resourceQuota := builder.DummyResourceQuota(
ctx.vm.Namespace,
storageClass1.Name+".storageclass.storage.k8s.io/persistentvolumeclaims",
storageClass2.Name+".storageclass.storage.k8s.io/persistentvolumeclaims")
Expect(ctx.Client.Create(ctx, resourceQuota)).To(Succeed())

pvc := builder.DummyPersistentVolumeClaim()
pvc.Name = builder.DummyPVCName
pvc.Namespace = ctx.vm.Namespace
pvc.Spec.StorageClassName = ptr.To(storageClass2.Name)
Expect(ctx.Client.Create(ctx, pvc)).To(Succeed())

ctx.vm.Spec.StorageClass = storageClass1.Name
ctx.vm.Spec.Crypto = &vmopv1.VirtualMachineCryptoSpec{}

var vmStorageClass storagev1.StorageClass
Expect(ctx.Client.Get(
ctx,
client.ObjectKey{Name: ctx.vm.Spec.StorageClass},
&vmStorageClass)).To(Succeed())
Expect(kubeutil.MarkEncryptedStorageClass(
ctx,
ctx.Client,
vmStorageClass,
false)).To(Succeed())

var pvcStorageClass storagev1.StorageClass
Expect(ctx.Client.Get(
ctx,
client.ObjectKey{Name: *pvc.Spec.StorageClassName},
&pvcStorageClass)).To(Succeed())
Expect(kubeutil.MarkEncryptedStorageClass(
ctx,
ctx.Client,
pvcStorageClass,
false)).To(Succeed())

pkgcfg.SetContext(ctx, func(config *pkgcfg.Config) {
config.Features.BringYourOwnEncryptionKey = true
})
},
expectAllowed: true,
},
),
Expand Down

0 comments on commit 0bad8f4

Please sign in to comment.