@@ -75,10 +75,6 @@ func (h IPLeaseHandler) Handle(ctx context.Context, state state.VMIPState) (reco
7575 log .Info ("Lease is not ready: waiting for the lease" )
7676 return reconcile.Result {}, nil
7777
78- case vmip .Status .Address == "" :
79- vmip .Status .Address = ip .LeaseNameToIP (lease .Name )
80- return reconcile.Result {}, nil
81-
8278 case util .IsBoundLease (lease , vmip ):
8379 log .Info ("Lease is bound, VirtualMachineIP ref is valid" )
8480 return reconcile.Result {}, nil
@@ -111,63 +107,66 @@ func (h IPLeaseHandler) updateLease(ctx context.Context, lease *virtv2.VirtualMa
111107 if err != nil {
112108 return reconcile.Result {}, err
113109 }
114- // fixme dlopatin delete this line
115- vmip .Status .Address = ip .LeaseNameToIP (lease .Name )
110+
116111 return reconcile.Result {}, nil
117112}
118113
119114func (h IPLeaseHandler ) createNewLease (ctx context.Context , state state.VMIPState ) (reconcile.Result , error ) {
120115 log := logger .FromContext (ctx )
121116
122117 vmip := state .VirtualMachineIP ()
123- ipAddress := ""
124- if vmip .Spec .Type == virtv2 .VirtualMachineIPAddressTypeAuto {
125- log .Info ("allocate the new VirtualMachineIP address" )
126- var err error
127- ipAddress , err = h .ipService .AllocateNewIP (state .AllocatedIPs ())
128- if err != nil {
129- return reconcile.Result {}, err
118+ if vmip .Status .Address == "" {
119+ ipAddress := ""
120+ if vmip .Spec .Type == virtv2 .VirtualMachineIPAddressTypeAuto {
121+ log .Info ("allocate the new VirtualMachineIP address" )
122+ var err error
123+ ipAddress , err = h .ipService .AllocateNewIP (state .AllocatedIPs ())
124+ if err != nil {
125+ return reconcile.Result {}, err
126+ }
127+ } else {
128+ ipAddress = vmip .Spec .StaticIP
130129 }
131- } else {
132- ipAddress = vmip .Spec .StaticIP
133- }
134130
135- err := h .ipService .IsAvailableAddress (ipAddress , state .AllocatedIPs ())
136- if err != nil {
137- msg := fmt .Sprintf ("the VirtualMachineIP cannot be created: %s" , err .Error ())
138- log .Warn (msg )
139-
140- conditionBound := conditions .NewConditionBuilder (vmipcondition .BoundType ).
141- Generation (vmip .GetGeneration ())
142-
143- switch {
144- case errors .Is (err , service .ErrIPAddressOutOfRange ):
145- vmip .Status .Phase = virtv2 .VirtualMachineIPAddressPhasePending
146- msg = fmt .Sprintf ("The requested address %s is out of the valid range." , vmip .Spec .StaticIP )
147- conditionBound .Status (metav1 .ConditionFalse ).
148- Reason (vmipcondition .VirtualMachineIPAddressIsOutOfTheValidRange ).
149- Message (msg )
150- h .recorder .Event (vmip , corev1 .EventTypeWarning , virtv2 .ReasonFailed , msg )
151- case errors .Is (err , service .ErrIPAddressAlreadyExist ):
152- vmip .Status .Phase = virtv2 .VirtualMachineIPAddressPhasePending
153- msg = fmt .Sprintf ("VirtualMachineIPAddressLease %s is bound to another VirtualMachineIPAddress." ,
154- ip .IpToLeaseName (ipAddress ))
155- conditionBound .Status (metav1 .ConditionFalse ).
156- Reason (vmipcondition .VirtualMachineIPAddressLeaseAlreadyExists ).
157- Message (msg )
158- h .recorder .Event (vmip , corev1 .EventTypeWarning , virtv2 .ReasonBound , msg )
131+ err := h .ipService .IsAvailableAddress (ipAddress , state .AllocatedIPs ())
132+ if err != nil {
133+ msg := fmt .Sprintf ("the VirtualMachineIP cannot be created: %s" , err .Error ())
134+ log .Warn (msg )
135+
136+ conditionBound := conditions .NewConditionBuilder (vmipcondition .BoundType ).
137+ Generation (vmip .GetGeneration ())
138+
139+ switch {
140+ case errors .Is (err , service .ErrIPAddressOutOfRange ):
141+ vmip .Status .Phase = virtv2 .VirtualMachineIPAddressPhasePending
142+ msg = fmt .Sprintf ("The requested address %s is out of the valid range." , ipAddress )
143+ conditionBound .Status (metav1 .ConditionFalse ).
144+ Reason (vmipcondition .VirtualMachineIPAddressIsOutOfTheValidRange ).
145+ Message (msg )
146+ h .recorder .Event (vmip , corev1 .EventTypeWarning , virtv2 .ReasonFailed , msg )
147+ case errors .Is (err , service .ErrIPAddressAlreadyExist ):
148+ vmip .Status .Phase = virtv2 .VirtualMachineIPAddressPhasePending
149+ msg = fmt .Sprintf ("VirtualMachineIPAddressLease %s is bound to another VirtualMachineIPAddress." ,
150+ ip .IpToLeaseName (ipAddress ))
151+ conditionBound .Status (metav1 .ConditionFalse ).
152+ Reason (vmipcondition .VirtualMachineIPAddressLeaseAlreadyExists ).
153+ Message (msg )
154+ h .recorder .Event (vmip , corev1 .EventTypeWarning , virtv2 .ReasonBound , msg )
155+ }
156+ conditions .SetCondition (conditionBound , & vmip .Status .Conditions )
157+ return reconcile.Result {}, nil
159158 }
160- conditions . SetCondition ( conditionBound , & vmip .Status .Conditions )
159+ vmip .Status .Address = ipAddress
161160 return reconcile.Result {}, nil
162161 }
163162
164- leaseName := ip .IpToLeaseName (ipAddress )
163+ leaseName := ip .IpToLeaseName (vmip . Status . Address )
165164
166165 log .Info ("Create lease" , "leaseName" , leaseName ,
167166 "refName" , vmip .Name , "refNamespace" , vmip .Namespace ,
168167 )
169168
170- err = h .client .Create (ctx , & virtv2.VirtualMachineIPAddressLease {
169+ err : = h .client .Create (ctx , & virtv2.VirtualMachineIPAddressLease {
171170 ObjectMeta : metav1.ObjectMeta {
172171 Name : leaseName ,
173172 },
@@ -188,7 +187,6 @@ func (h IPLeaseHandler) createNewLease(ctx context.Context, state state.VMIPStat
188187 }
189188
190189 h .recorder .Event (vmip , corev1 .EventTypeNormal , virtv2 .ReasonBound , "VirtualMachineIPAddress is bound to a new VirtualMachineIPAddressLease." )
191-
192190 return reconcile.Result {}, nil
193191}
194192
0 commit comments