Skip to content

Commit d7903b7

Browse files
committed
Adds MaxIPs to OpenstackFloatingIPPool
1 parent 84e1f4e commit d7903b7

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

api/v1alpha1/conditions_consts.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ package v1alpha1
1919
const (
2020
// OpenstackFloatingIPPoolReadyCondition reports on the current status of the floating ip pool. Ready indicates that the pool is ready to be used.
2121
OpenstackFloatingIPPoolReadyCondition = "OpenstackFloatingIPPoolReadyCondition"
22+
23+
// MaxIPsReachedReason is set when the maximum number of floating IPs has been reached.
24+
MaxIPsReachedReason = "MaxIPsReached"
2225
)

api/v1alpha1/openstackfloatingippool_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ type OpenStackFloatingIPPoolSpec struct {
5656
// These are used before allocating new ones and are not deleted from OpenStack when the pool is deleted.
5757
PreAllocatedFloatingIPs []string `json:"preAllocatedFloatingIPs,omitempty"`
5858

59+
// MaxIPs is the maximum number of floating ips that can be allocated from this pool, if nil there is no limit.
60+
// If set, the pool will stop allocating floating ips when it reaches this number of ClaimedIPs.
61+
// +optional
62+
MaxIPs *int `json:"maxIPs,omitempty"`
63+
5964
// IdentityRef is a reference to a identity to be used when reconciling this pool.
6065
// +optional
6166
IdentityRef *infrav1alpha7.OpenStackIdentityReference `json:"identityRef,omitempty"`

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/infrastructure.cluster.x-k8s.io_openstackfloatingippools.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ spec:
8686
- kind
8787
- name
8888
type: object
89+
maxIPs:
90+
description: |-
91+
MaxIPs is the maximum number of floating ips that can be allocated from this pool, if nil there is no limit.
92+
If set, the pool will stop allocating floating ips when it reaches this number of ClaimedIPs.
93+
type: integer
8994
preAllocatedFloatingIPs:
9095
description: |-
9196
PreAllocatedFloatingIPs is a list of floating IPs precreated in OpenStack that should be used by this pool.

controllers/openstackfloatingippool_controller.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ const (
5151
openStackFloatingIPPool = "OpenStackFloatingIPPool"
5252
)
5353

54+
var errMaxIPsReached = errors.New("maximum number of IPs reached")
55+
5456
var backoff = wait.Backoff{
5557
Steps: 4,
5658
Duration: 10 * time.Millisecond,
@@ -139,6 +141,10 @@ func (r *OpenStackFloatingIPPoolReconciler) Reconcile(ctx context.Context, req c
139141
if apierrors.IsNotFound(err) {
140142
ip, err := r.getIP(ctx, scope, pool)
141143
if err != nil {
144+
if errors.Is(err, errMaxIPsReached) {
145+
log.Info("Maximum number of IPs reached, will not allocate more IPs.")
146+
return ctrl.Result{}, nil
147+
}
142148
return ctrl.Result{}, err
143149
}
144150

@@ -193,6 +199,7 @@ func (r *OpenStackFloatingIPPoolReconciler) Reconcile(ctx context.Context, req c
193199
scope.Logger().Info("Claimed IP", "ip", ipAddress.Spec.Address)
194200
}
195201
}
202+
conditions.MarkTrue(pool, infrav1alpha1.OpenstackFloatingIPPoolReadyCondition)
196203
return ctrl.Result{}, r.Client.Status().Update(ctx, pool)
197204
}
198205

@@ -296,8 +303,10 @@ func (r *OpenStackFloatingIPPoolReconciler) reconcileIPAddresses(ctx context.Con
296303
return err
297304
}
298305
}
306+
299307
unclaimedPreAllocatedIPs := diff(pool.Spec.PreAllocatedFloatingIPs, pool.Status.ClaimedIPs)
300308
unclaimedIPs := union(pool.Status.AvailableIPs, unclaimedPreAllocatedIPs)
309+
unclaimedIPs = diff(unclaimedIPs, pool.Status.ClaimedIPs)
301310
pool.Status.AvailableIPs = diff(unclaimedIPs, pool.Status.FailedIPs)
302311
return nil
303312
}
@@ -340,9 +349,17 @@ func (r *OpenStackFloatingIPPoolReconciler) getIP(ctx context.Context, scope sco
340349
return "", fmt.Errorf("get floating IP: %w", err)
341350
}
342351
if fp != nil {
352+
pool.Status.ClaimedIPs = append(pool.Status.ClaimedIPs, fp.FloatingIP)
343353
return fp.FloatingIP, nil
344354
}
345355
}
356+
maxIPs := pointer.IntDeref(pool.Spec.MaxIPs, -1)
357+
// If we have reached the maximum number of IPs, we should not create more IPs
358+
if maxIPs != -1 && len(pool.Status.ClaimedIPs) >= maxIPs {
359+
scope.Logger().Info("MaxIPs reached", "pool", pool.Name)
360+
conditions.MarkFalse(pool, infrav1alpha1.OpenstackFloatingIPPoolReadyCondition, infrav1alpha1.MaxIPsReachedReason, clusterv1.ConditionSeverityError, "Maximum number of IPs reached, we will not allocate more IPs for this pool")
361+
return "", errMaxIPsReached
362+
}
346363

347364
fp, err := networkingService.CreateFloatingIPForPool(pool)
348365
if err != nil {
@@ -369,7 +386,6 @@ func (r *OpenStackFloatingIPPoolReconciler) getIP(ctx context.Context, scope sco
369386
}()
370387

371388
conditions.MarkTrue(pool, infrav1alpha1.OpenstackFloatingIPPoolReadyCondition)
372-
373389
ip = fp.FloatingIP
374390
pool.Status.ClaimedIPs = append(pool.Status.ClaimedIPs, ip)
375391
return ip, nil

0 commit comments

Comments
 (0)