Skip to content

Commit 42c0ef9

Browse files
committed
Adds MaxIPs to OpenstackFloatingIPPool
1 parent 622defd commit 42c0ef9

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-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: 15 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

@@ -341,10 +348,18 @@ func (r *OpenStackFloatingIPPoolReconciler) getIP(ctx context.Context, scope sco
341348
return "", fmt.Errorf("get floating IP: %w", err)
342349
}
343350
if fp != nil {
351+
pool.Status.ClaimedIPs = append(pool.Status.ClaimedIPs, fp.FloatingIP)
344352
return fp.FloatingIP, nil
345353
}
346354
pool.Status.FailedIPs = append(pool.Status.FailedIPs, ip)
347355
}
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+
}
348363

349364
fp, err := networkingService.CreateFloatingIPForPool(pool)
350365
if err != nil {
@@ -368,7 +383,6 @@ func (r *OpenStackFloatingIPPoolReconciler) getIP(ctx context.Context, scope sco
368383
}()
369384

370385
conditions.MarkTrue(pool, infrav1alpha1.OpenstackFloatingIPPoolReadyCondition)
371-
372386
ip = fp.FloatingIP
373387
pool.Status.ClaimedIPs = append(pool.Status.ClaimedIPs, ip)
374388
return ip, nil

0 commit comments

Comments
 (0)