Skip to content

Commit b00ec1f

Browse files
authored
fix(vmip): add validation when updating the vmip (#1530)
- Fix VirtualMachineIP controller: - Added validation to ensure that an IP address is not already in use when updating the VirtualMachineIP address. - Enhancements to Complex Test: - Enabled the previously skipped section of the test involving the patching of custom IP addresses. - Corrected the test case to ensure that all virtual machines are listed for power state checks, migration checks, and other verifications. - Added validations to ensure all virtual machines are correctly identified and included in checks. Signed-off-by: Dmitry Lopatin <[email protected]>
1 parent ee892a3 commit b00ec1f

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

images/virtualization-artifact/pkg/controller/vmip/internal/step/take_lease_step.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (s TakeLeaseStep) Take(ctx context.Context, vmip *v1alpha2.VirtualMachineIP
7878
s.cb.
7979
Status(metav1.ConditionFalse).
8080
Reason(vmipcondition.VirtualMachineIPAddressLeaseNotReady).
81-
Message(fmt.Sprintf("The VirtualMachineIPAddressLease %q alrady has a reference to another VirtualMachineIPAddress.", s.lease.Name))
81+
Message(fmt.Sprintf("The VirtualMachineIPAddressLease %q already has a reference to another VirtualMachineIPAddress.", s.lease.Name))
8282
return &reconcile.Result{}, nil
8383
}
8484

images/virtualization-artifact/pkg/controller/vmip/vmip_webhook.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (v *Validator) ValidateCreate(ctx context.Context, obj runtime.Object) (adm
8282
return warnings, nil
8383
}
8484

85-
func (v *Validator) ValidateUpdate(_ context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
85+
func (v *Validator) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
8686
oldVmip, ok := oldObj.(*v1alpha2.VirtualMachineIPAddress)
8787
if !ok {
8888
return nil, fmt.Errorf("expected an old VirtualMachineIP but got a %T", oldObj)
@@ -103,6 +103,20 @@ func (v *Validator) ValidateUpdate(_ context.Context, oldObj, newObj runtime.Obj
103103
return nil, fmt.Errorf("error validating VirtualMachineIP update: %w", err)
104104
}
105105

106+
var warnings admission.Warnings
107+
108+
if newVmip.Spec.StaticIP != "" && oldVmip.Spec.StaticIP != newVmip.Spec.StaticIP {
109+
err = v.validateAllocatedIPAddresses(ctx, newVmip.Spec.StaticIP)
110+
switch {
111+
case err == nil:
112+
// OK.
113+
case errors.Is(err, service.ErrIPAddressOutOfRange):
114+
warnings = append(warnings, fmt.Sprintf("The requested address %s is out of the valid range", newVmip.Spec.StaticIP))
115+
default:
116+
return nil, err
117+
}
118+
}
119+
106120
boundCondition, _ := conditions.GetCondition(vmipcondition.BoundType, oldVmip.Status.Conditions)
107121
if boundCondition.Status == metav1.ConditionTrue {
108122
if oldVmip.Spec.Type == v1alpha2.VirtualMachineIPAddressTypeAuto && newVmip.Spec.Type == v1alpha2.VirtualMachineIPAddressTypeStatic {
@@ -123,7 +137,7 @@ func (v *Validator) ValidateUpdate(_ context.Context, oldObj, newObj runtime.Obj
123137
}
124138
}
125139

126-
return nil, nil
140+
return warnings, nil
127141
}
128142

129143
func (v *Validator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {

tests/e2e/complex_test.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import (
3030
kc "github.com/deckhouse/virtualization/tests/e2e/kubectl"
3131
)
3232

33+
const VirtualMachineCount = 12
34+
3335
var _ = Describe("ComplexTest", Serial, framework.CommonE2ETestDecorators(), func() {
3436
var (
3537
testCaseLabel = map[string]string{"testcase": "complex-test"}
@@ -38,6 +40,7 @@ var _ = Describe("ComplexTest", Serial, framework.CommonE2ETestDecorators(), fun
3840
notAlwaysOnLabel = map[string]string{"notAlwaysOn": "complex-test"}
3941
ns string
4042
phaseByVolumeBindingMode = GetPhaseByVolumeBindingModeForTemplateSc()
43+
f = framework.NewFramework("")
4144
)
4245

4346
AfterEach(func() {
@@ -78,7 +81,10 @@ var _ = Describe("ComplexTest", Serial, framework.CommonE2ETestDecorators(), fun
7881
})
7982

8083
It("should fill empty virtualMachineClassName with the default class name", func() {
81-
defaultVMLabels := testCaseLabel
84+
defaultVMLabels := make(map[string]string, len(testCaseLabel)+1)
85+
for k, v := range testCaseLabel {
86+
defaultVMLabels[k] = v
87+
}
8288
defaultVMLabels["vm"] = "default"
8389
res := kubectl.List(kc.ResourceVM, kc.GetOptions{
8490
Labels: testCaseLabel,
@@ -125,13 +131,12 @@ var _ = Describe("ComplexTest", Serial, framework.CommonE2ETestDecorators(), fun
125131
})
126132

127133
Context("When virtual machines IP addresses are applied", func() {
128-
// TODO: fix: the custom IP address loses its lease and becomes Lost.
129-
// It("patches custom VMIP with unassigned address", func() {
130-
// vmipName := fmt.Sprintf("%s-%s", namePrefix, "vm-custom-ip")
131-
// Eventually(func() error {
132-
// return AssignIPToVMIP(f, ns, vmipName)
133-
// }).WithTimeout(LongWaitDuration).WithPolling(Interval).Should(Succeed())
134-
// })
134+
It("patches custom VMIP with unassigned address", func() {
135+
vmipName := fmt.Sprintf("%s-%s", namePrefix, "vm-custom-ip")
136+
Eventually(func() error {
137+
return AssignIPToVMIP(f, ns, vmipName)
138+
}).WithTimeout(LongWaitDuration).WithPolling(Interval).Should(Succeed())
139+
})
135140

136141
It("checks VMIPs phases", func() {
137142
By(fmt.Sprintf("VMIPs should be in %s phases", PhaseAttached))
@@ -219,6 +224,7 @@ var _ = Describe("ComplexTest", Serial, framework.CommonE2ETestDecorators(), fun
219224
Namespace: ns,
220225
})
221226
Expect(err).ShouldNot(HaveOccurred())
227+
Expect(len(vmList.Items)).To(Equal(VirtualMachineCount))
222228

223229
for _, vmObj := range vmList.Items {
224230
if vmObj.Spec.RunPolicy == v1alpha2.AlwaysOnPolicy {
@@ -278,6 +284,7 @@ var _ = Describe("ComplexTest", Serial, framework.CommonE2ETestDecorators(), fun
278284
Labels: testCaseLabel,
279285
})
280286
Expect(err).NotTo(HaveOccurred())
287+
Expect(len(vms.Items)).To(Equal(VirtualMachineCount))
281288

282289
var notAlwaysOnVMs []string
283290
for _, vm := range vms.Items {
@@ -313,6 +320,7 @@ var _ = Describe("ComplexTest", Serial, framework.CommonE2ETestDecorators(), fun
313320
Namespace: ns,
314321
})
315322
Expect(err).ShouldNot(HaveOccurred())
323+
Expect(len(vmList.Items)).To(Equal(VirtualMachineCount))
316324

317325
alwaysOnVMs = []string{}
318326
notAlwaysOnVMs = []string{}

tests/e2e/testdata/complex-test/vm/kustomization.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ resources:
44
- overlays/default
55
- overlays/always-on
66
- overlays/embedded-cloudinit
7-
# TODO: the custom IP address loses its lease and becomes Lost.
8-
# - overlays/custom-ip
7+
- overlays/custom-ip
98
- overlays/automatic
109
- overlays/automatic-with-hotplug
1110
- overlays/hotplug

0 commit comments

Comments
 (0)