Skip to content

Commit 323b3be

Browse files
authored
neonvm-controller: implement migration webhooks (#1295)
Not much we need to check for, but it guards us from creating a migration for the wrong VM. Closes #719. --------- Signed-off-by: Oleg Vasilev <[email protected]>
1 parent 4c49946 commit 323b3be

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

neonvm/apis/neonvm/v1/virtualmachinemigration_types.go

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const MigrationPort int32 = 20187
2828

2929
// VirtualMachineMigrationSpec defines the desired state of VirtualMachineMigration
3030
type VirtualMachineMigrationSpec struct {
31+
// +kubebuilder:validation:MinLength=1
3132
VmName string `json:"vmName"`
3233

3334
// TODO: not implemented

neonvm/apis/neonvm/v1/virtualmachinemigration_webhook.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ limitations under the License.
1717
package v1
1818

1919
import (
20+
"fmt"
21+
"reflect"
22+
2023
"sigs.k8s.io/controller-runtime/pkg/webhook"
2124
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2225

@@ -42,22 +45,24 @@ var _ webhook.Validator = &VirtualMachineMigration{}
4245
//
4346
// The controller wraps this logic so it can inject extra control in the webhook.
4447
func (r *VirtualMachineMigration) ValidateCreate() (admission.Warnings, error) {
45-
// TODO: implement creation validation webhook (?)
4648
return nil, nil
4749
}
4850

4951
// ValidateUpdate implements webhook.Validator
5052
//
5153
// The controller wraps this logic so it can inject extra control in the webhook.
5254
func (r *VirtualMachineMigration) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
53-
// TODO: implement update validation webhook
55+
oldVMM := old.(*VirtualMachineMigration)
56+
if !reflect.DeepEqual(r.Spec, oldVMM.Spec) {
57+
return nil, fmt.Errorf("updating migration spec is not allowed")
58+
}
5459
return nil, nil
5560
}
5661

5762
// ValidateDelete implements webhook.Validator
5863
//
5964
// The controller wraps this logic so it can inject extra control in the webhook.
6065
func (r *VirtualMachineMigration) ValidateDelete() (admission.Warnings, error) {
61-
// TODO: implement deletion validation webhook (?)
66+
// No validation needed - we have a finalizer that prevents deletion
6267
return nil, nil
6368
}

neonvm/config/crd/bases/vm.neon.tech_virtualmachinemigrations.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ spec:
300300
default: true
301301
type: boolean
302302
vmName:
303+
minLength: 1
303304
type: string
304305
required:
305306
- vmName

pkg/neonvm/controllers/functests/vm_controller_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,25 @@ var _ = Describe("VirtualMachine controller", func() {
149149
})
150150
})
151151
})
152+
153+
var _ = Describe("VirtualMachineMigration controller", func() {
154+
It("should reject invalid VirtualMachineMigration", func() {
155+
ctx := context.Background()
156+
157+
By("Creating the custom resource for the Kind VirtualMachineMigration")
158+
virtualmachinemigration := &vmv1.VirtualMachineMigration{
159+
ObjectMeta: metav1.ObjectMeta{
160+
Name: "test-migration",
161+
Namespace: "default",
162+
},
163+
}
164+
err := k8sClient.Create(ctx, virtualmachinemigration)
165+
Expect(err).To(HaveOccurred())
166+
Expect(err.Error()).To(ContainSubstring("spec.vmName in body should be at least 1 chars long"))
167+
168+
// Now fix it
169+
virtualmachinemigration.Spec.VmName = "test-vm"
170+
err = k8sClient.Create(ctx, virtualmachinemigration)
171+
Expect(err).To(Not(HaveOccurred()))
172+
})
173+
})

0 commit comments

Comments
 (0)