@@ -23,7 +23,6 @@ import (
2323 "time"
2424
2525 netboxv1 "github.com/netbox-community/netbox-operator/api/v1"
26- "github.com/netbox-community/netbox-operator/pkg/config"
2726 "github.com/netbox-community/netbox-operator/pkg/netbox/api"
2827 "github.com/netbox-community/netbox-operator/pkg/netbox/models"
2928 "github.com/swisscom/leaselocker"
@@ -102,9 +101,9 @@ func (r *IpRangeClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request
102101
103102 // 4. try to reclaim ip range
104103 h := generateIpRangeRestorationHash (o )
105- ipRangeModel , err := r .NetboxClient .RestoreExistingIpRangeByHash (config . GetOperatorConfig (). NetboxRestorationHashFieldName , h )
104+ ipRangeModel , err := r .NetboxClient .RestoreExistingIpRangeByHash (h )
106105 if err != nil {
107- if err := r .SetConditionAndCreateEvent (ctx , o , netboxv1 .ConditionIpAssignedFalse , corev1 .EventTypeWarning , "" , err ); err != nil {
106+ if err := r .SetConditionAndCreateEvent (ctx , o , netboxv1 .ConditionIpRangeAssignedFalse , corev1 .EventTypeWarning , "" , err ); err != nil {
108107 return ctrl.Result {}, err
109108 }
110109 return ctrl.Result {Requeue : true }, nil
@@ -123,7 +122,7 @@ func (r *IpRangeClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request
123122 },
124123 )
125124 if err != nil {
126- if err := r .SetConditionAndCreateEvent (ctx , o , netboxv1 .ConditionIpAssignedFalse , corev1 .EventTypeWarning , "" , err ); err != nil {
125+ if err := r .SetConditionAndCreateEvent (ctx , o , netboxv1 .ConditionIpRangeAssignedFalse , corev1 .EventTypeWarning , "" , err ); err != nil {
127126 return ctrl.Result {}, err
128127 }
129128 return ctrl.Result {Requeue : true }, nil
@@ -133,10 +132,10 @@ func (r *IpRangeClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request
133132 // 5.b reassign reserved ip range from netbox
134133
135134 // check if the restored ip range has the size requested by the claim
136- err = correctSizeOrErr ( * ipRangeModel , o . Spec . Size )
137- if err != nil {
135+ availableIps , err := r . NetboxClient . GetAvailableIpAddressesByIpRange ( ipRangeModel . Id )
136+ if len ( availableIps . Payload ) != o . Spec . Size {
138137 ll .Unlock ()
139- err = r .SetConditionAndCreateEvent (ctx , o , netboxv1 .ConditionIpAssignedFalseSizeMissmatch , corev1 .EventTypeWarning , "" , err )
138+ err = r .SetConditionAndCreateEvent (ctx , o , netboxv1 .ConditionIpRangeAssignedFalseSizeMissmatch , corev1 .EventTypeWarning , "" , err )
140139 if err != nil {
141140 return ctrl.Result {}, err
142141 }
@@ -173,7 +172,7 @@ func (r *IpRangeClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request
173172 }
174173 }
175174
176- err = r .updateIpRangeClaimReadyStatus (ctx , o , ipRange )
175+ err = r .updateIpRangeClaimStatus (ctx , o , ipRange )
177176 if err != nil {
178177 return ctrl.Result {}, err
179178 }
@@ -256,21 +255,46 @@ func (r *IpRangeClaimReconciler) tryLockOnParentPrefix(ctx context.Context, o *n
256255 return ll , ctrl.Result {}, nil
257256}
258257
259- func (r * IpRangeClaimReconciler ) updateIpRangeClaimReadyStatus (ctx context.Context , o * netboxv1.IpRangeClaim , ipRange * netboxv1.IpRange ) error {
258+ func (r * IpRangeClaimReconciler ) updateIpRangeClaimStatus (ctx context.Context , o * netboxv1.IpRangeClaim , ipRange * netboxv1.IpRange ) error {
260259 debugLogger := log .FromContext (ctx ).V (4 )
261260
262261 if apismeta .IsStatusConditionTrue (ipRange .Status .Conditions , "Ready" ) {
263262 debugLogger .Info ("iprange status ready true" )
264- o .Status .IpRange = fmt .Sprintf ("%s-%s" , strings .Split (ipRange .Spec .StartAddress , "/" )[0 ],
265- strings .Split (ipRange .Spec .EndAddress , "/" )[0 ])
263+
264+ startAddressDotDecimal := strings .Split (ipRange .Spec .StartAddress , "/" )[0 ]
265+ endAddressDotDecimal := strings .Split (ipRange .Spec .EndAddress , "/" )[0 ]
266+
267+ o .Status .IpRange = fmt .Sprintf ("%s-%s" , ipRange .Spec .StartAddress , ipRange .Spec .EndAddress )
268+
269+ o .Status .IpRangeDotDecimal = fmt .Sprintf ("%s-%s" , startAddressDotDecimal , endAddressDotDecimal )
270+
271+ availableIps , err := r .NetboxClient .GetAvailableIpAddressesByIpRange (ipRange .Status .IpRangeId )
272+ if err != nil {
273+ return err
274+ }
275+
276+ o .Status .IpAddresses = []string {}
277+ o .Status .IpAddressesDotDecimal = []string {}
278+ for _ , ip := range availableIps .Payload {
279+ o .Status .IpAddresses = append (o .Status .IpAddresses , ip .Address )
280+ o .Status .IpAddressesDotDecimal = append (o .Status .IpAddressesDotDecimal , strings .Split (ip .Address , "/" )[0 ])
281+ }
282+
283+ o .Status .StartAddress = ipRange .Spec .StartAddress
284+ o .Status .StartAddressDotDecimal = startAddressDotDecimal
285+
286+ o .Status .EndAddress = ipRange .Spec .EndAddress
287+ o .Status .EndAddressDotDecimal = endAddressDotDecimal
288+
266289 o .Status .IpRangeName = ipRange .Name
267- err := r .SetConditionAndCreateEvent (ctx , o , netboxv1 .ConditionIpClaimReadyTrue , corev1 .EventTypeNormal , "" , nil )
290+
291+ err = r .SetConditionAndCreateEvent (ctx , o , netboxv1 .ConditionIpRangeReadyTrue , corev1 .EventTypeNormal , "" , nil )
268292 if err != nil {
269293 return err
270294 }
271295 } else {
272296 debugLogger .Info ("iprange status ready false" )
273- err := r .SetConditionAndCreateEvent (ctx , o , netboxv1 .ConditionIpClaimReadyFalse , corev1 .EventTypeWarning , "" , nil )
297+ err := r .SetConditionAndCreateEvent (ctx , o , netboxv1 .ConditionIpRangeReadyFalse , corev1 .EventTypeWarning , "" , nil )
274298 if err != nil {
275299 return err
276300 }
@@ -288,14 +312,14 @@ func (r *IpRangeClaimReconciler) createIpRangeCR(ctx context.Context, o *netboxv
288312
289313 err = r .Client .Create (ctx , ipRangeResource )
290314 if err != nil {
291- err := r .SetConditionAndCreateEvent (ctx , o , netboxv1 .ConditionIpAssignedFalse , corev1 .EventTypeWarning , "" , err )
315+ err := r .SetConditionAndCreateEvent (ctx , o , netboxv1 .ConditionIpRangeAssignedFalse , corev1 .EventTypeWarning , "" , err )
292316 if err != nil {
293317 return err
294318 }
295319 return err
296320 }
297321
298- err = r .SetConditionAndCreateEvent (ctx , o , netboxv1 .ConditionIpAssignedTrue , corev1 .EventTypeNormal , "" , nil )
322+ err = r .SetConditionAndCreateEvent (ctx , o , netboxv1 .ConditionIpRangeAssignedTrue , corev1 .EventTypeNormal , "" , nil )
299323 if err != nil {
300324 return err
301325 }
0 commit comments