Skip to content

Commit 7d591da

Browse files
authored
fix(vmip): refactor vmip and vmiplease controllers (#1081)
Refactored the controllers for vmip and vmiplease. vmip: - Introduced a new handler, BoundHandler, with a Steps-based architecture and three corresponding steps for managing the respective condition; - Added a new handler, AttachedHandler, for managing the respective condition; - The indexer IndexVMIPByVM now also looks at ownerRef; - Removed the reconciliationstate; - Removed the IPLeaseHandler. Management of the Lease resource is now handled by BoundHandler; - Watchers have been moved to internal/watchers, and the event subscriptions and filtering have been updated; - LifeCycleHandler now only sets the phase; - The IPAddressService has become an internal service of vmip. vmiplease: - The order of handlers has changed; - Removed the reconciliation state; - Watchers have been moved to internal/watchers, and the event subscriptions and filtering have been updated; - Resource deletion has been moved to RetentionHandler. Signed-off-by: Isteb4k <dmitry.rakitin@flant.com>
1 parent 9bc3fde commit 7d591da

43 files changed

Lines changed: 1601 additions & 1235 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

api/core/v1alpha2/events.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ const (
124124
// ReasonNotAttached is event reason that VirtualMachineIPAddress is not attached to VirtualMachine.
125125
ReasonNotAttached = "NotAttached"
126126

127+
// ReasonIPAddressHasBeenAllocated is the event reason indicating that a new IP address has been allocated.
128+
ReasonIPAddressHasBeenAllocated = "IPAddressHasBeenAllocated"
127129
// ReasonBound is the event reason indicating that a VirtualMachineIPLease is bound to a VirtualMachineIPAddress.
128130
ReasonBound = "Bound"
129131
// ReasonFailed is the event reason indicating that the binding of a VirtualMachineIPLease to a VirtualMachineIPAddress has failed.

api/core/v1alpha2/vmipcondition/condition.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,19 @@ func (r AttachedReason) String() string {
4949
const (
5050
// VirtualMachineIPAddressIsOutOfTheValidRange is a BoundReason indicating when specified IP address is out of the range in controller settings.
5151
VirtualMachineIPAddressIsOutOfTheValidRange BoundReason = "VirtualMachineIPAddressIsOutOfTheValidRange"
52-
5352
// VirtualMachineIPAddressLeaseAlreadyExists is a BoundReason indicating the IP address lease already exists.
5453
VirtualMachineIPAddressLeaseAlreadyExists BoundReason = "VirtualMachineIPAddressLeaseAlreadyExists"
55-
5654
// VirtualMachineIPAddressLeaseLost is a BoundReason indicating the IP address lease was lost.
5755
VirtualMachineIPAddressLeaseLost BoundReason = "VirtualMachineIPAddressLeaseLost"
58-
5956
// VirtualMachineIPAddressLeaseNotFound is a BoundReason indicating the IP address lease was not found.
6057
VirtualMachineIPAddressLeaseNotFound BoundReason = "VirtualMachineIPAddressLeaseNotFound"
61-
6258
// VirtualMachineIPAddressLeaseNotReady is a BoundReason indicating the IP address lease was not ready.
6359
VirtualMachineIPAddressLeaseNotReady BoundReason = "VirtualMachineIPAddressLeaseNotReady"
64-
6560
// Bound is a BoundReason indicating the IP address lease is successfully bound.
6661
Bound BoundReason = "Bound"
6762

6863
// VirtualMachineNotFound is an AttachedReason indicating the Virtual Machine was not found.
6964
VirtualMachineNotFound AttachedReason = "VirtualMachineNotFound"
70-
7165
// Attached is an AttachedReason indicating the IP address was successfully attached to the Virtual Machine.
7266
Attached AttachedReason = "Attached"
7367
)

images/virtualization-artifact/pkg/common/blockdevice/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
)
2727

2828
type Resource interface {
29-
*virtv2.VirtualDisk | *virtv2.VirtualImage
29+
*virtv2.VirtualDisk | *virtv2.VirtualImage | *virtv2.VirtualMachineIPAddress
3030
}
3131

3232
type StepTaker[R Resource] interface {

images/virtualization-artifact/pkg/common/ip/ip.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ package ip
1919
import (
2020
"net"
2121
"strings"
22-
23-
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
2422
)
2523

2624
const ipPrefix = "ip-"
2725

28-
type AllocatedIPs map[string]*virtv2.VirtualMachineIPAddressLease
26+
type AllocatedIPs map[string]struct{}
2927

30-
// IpToLeaseName generate the Virtual Machine IP Address Lease's name from the ip address
28+
// IpToLeaseName generate the Virtual Machine IP Address Lease's name from the ip address.
3129
func IpToLeaseName(ip string) string {
3230
addr := net.ParseIP(ip)
3331
if addr.To4() != nil {
@@ -38,7 +36,7 @@ func IpToLeaseName(ip string) string {
3836
return ""
3937
}
4038

41-
// LeaseNameToIP generate the ip address from the Virtual Machine IP Address Lease's name
39+
// LeaseNameToIP generate the ip address from the Virtual Machine IP Address Lease's name.
4240
func LeaseNameToIP(leaseName string) string {
4341
if strings.HasPrefix(leaseName, ipPrefix) && len(leaseName) > len(ipPrefix) {
4442
return strings.ReplaceAll(leaseName[len(ipPrefix):], "-", ".")

images/virtualization-artifact/pkg/controller/indexer/vmip_indexer.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,25 @@ func IndexVMIPByVM() (obj client.Object, field string, extractValue client.Index
2828
if !ok || vmip == nil {
2929
return nil
3030
}
31-
return []string{vmip.Status.VirtualMachine}
31+
32+
var vmNames []string
33+
if vmip.Status.VirtualMachine != "" {
34+
vmNames = append(vmNames, vmip.Status.VirtualMachine)
35+
}
36+
37+
for _, ownerRef := range vmip.OwnerReferences {
38+
if ownerRef.Kind != virtv2.VirtualMachineKind {
39+
continue
40+
}
41+
42+
if ownerRef.Name == "" || ownerRef.Name == vmip.Status.VirtualMachine {
43+
continue
44+
}
45+
46+
vmNames = append(vmNames, ownerRef.Name)
47+
}
48+
49+
return vmNames
3250
}
3351
}
3452

images/virtualization-artifact/pkg/controller/ipam/ipam.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import (
2525
"sigs.k8s.io/controller-runtime/pkg/client"
2626

2727
"github.com/deckhouse/virtualization-controller/pkg/common/annotations"
28+
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
2829
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
30+
"github.com/deckhouse/virtualization/api/core/v1alpha2/vmipcondition"
2931
)
3032

3133
const AnnoIPAddressCNIRequest = "cni.cilium.io/ipAddress"
@@ -41,7 +43,13 @@ func (m IPAM) IsBound(vmName string, vmip *virtv2.VirtualMachineIPAddress) bool
4143
return false
4244
}
4345

44-
if vmip.Status.Phase != virtv2.VirtualMachineIPAddressPhaseBound && vmip.Status.Phase != virtv2.VirtualMachineIPAddressPhaseAttached {
46+
boundCondition, _ := conditions.GetCondition(vmipcondition.BoundType, vmip.Status.Conditions)
47+
if boundCondition.Status != metav1.ConditionTrue || !conditions.IsLastUpdated(boundCondition, vmip) {
48+
return false
49+
}
50+
51+
attachedCondition, _ := conditions.GetCondition(vmipcondition.AttachedType, vmip.Status.Conditions)
52+
if attachedCondition.Status != metav1.ConditionTrue || !conditions.IsLastUpdated(attachedCondition, vmip) {
4553
return false
4654
}
4755

images/virtualization-artifact/pkg/controller/reconciler/resource.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,14 @@ func (r *Resource[T, ST]) Update(ctx context.Context) error {
210210
return nil
211211
}
212212

213-
return fmt.Errorf("error patching metadata: %w", err)
213+
return fmt.Errorf("error patching metadata (%s): %w", string(metadataPatchBytes), err)
214214
}
215215

216216
return nil
217217
}
218218

219219
func (r *Resource[T, ST]) JSONPatchOpsForFinalizers() []patch.JsonPatchOperation {
220220
return []patch.JsonPatchOperation{
221-
patch.NewJsonPatchOperation(patch.PatchTestOp, "/metadata/finalizers", r.currentObj.GetFinalizers()),
222221
patch.NewJsonPatchOperation(patch.PatchReplaceOp, "/metadata/finalizers", r.changedObj.GetFinalizers()),
223222
}
224223
}

images/virtualization-artifact/pkg/controller/service/errors.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,3 @@ var (
2626
ErrDataVolumeNotRunning = errors.New("pvc importer is not running")
2727
ErrDataVolumeProvisionerUnschedulable = errors.New("provisioner unschedulable")
2828
)
29-
30-
var (
31-
ErrIPAddressAlreadyExist = errors.New("the IP address is already allocated")
32-
ErrIPAddressOutOfRange = errors.New("the IP address is out of range")
33-
)

images/virtualization-artifact/pkg/controller/service/ip_address_service.go

Lines changed: 0 additions & 132 deletions
This file was deleted.

images/virtualization-artifact/pkg/controller/vm/internal/validators/ipam_validator.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,15 @@ import (
2525
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2626

2727
"github.com/deckhouse/virtualization-controller/pkg/common/object"
28-
"github.com/deckhouse/virtualization-controller/pkg/controller/vm/internal"
2928
"github.com/deckhouse/virtualization/api/core/v1alpha2"
3029
)
3130

3231
type IPAMValidator struct {
33-
ipam internal.IPAM
3432
client client.Client
3533
}
3634

37-
func NewIPAMValidator(ipam internal.IPAM, client client.Client) *IPAMValidator {
38-
return &IPAMValidator{ipam: ipam, client: client}
35+
func NewIPAMValidator(client client.Client) *IPAMValidator {
36+
return &IPAMValidator{client: client}
3937
}
4038

4139
func (v *IPAMValidator) ValidateCreate(ctx context.Context, vm *v1alpha2.VirtualMachine) (admission.Warnings, error) {
@@ -60,7 +58,7 @@ func (v *IPAMValidator) ValidateCreate(ctx context.Context, vm *v1alpha2.Virtual
6058
" already exists: set spec.virtualMachineIPAddress field to %s to use IP %s", vmip.Name, vmip.Status.Address)
6159
}
6260

63-
return nil, v.ipam.CheckIpAddressAvailableForBinding(vm.Name, vmip)
61+
return nil, nil
6462
}
6563

6664
func (v *IPAMValidator) ValidateUpdate(ctx context.Context, oldVM, newVM *v1alpha2.VirtualMachine) (admission.Warnings, error) {
@@ -82,5 +80,5 @@ func (v *IPAMValidator) ValidateUpdate(ctx context.Context, oldVM, newVM *v1alph
8280
return nil, nil
8381
}
8482

85-
return nil, v.ipam.CheckIpAddressAvailableForBinding(newVM.Name, vmip)
83+
return nil, nil
8684
}

0 commit comments

Comments
 (0)