Skip to content

Commit

Permalink
Adds MaxIPs to OpenstackFloatingIPPool
Browse files Browse the repository at this point in the history
  • Loading branch information
bilbobrovall committed Feb 6, 2024
1 parent 84e1f4e commit d7903b7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
3 changes: 3 additions & 0 deletions api/v1alpha1/conditions_consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ package v1alpha1
const (
// OpenstackFloatingIPPoolReadyCondition reports on the current status of the floating ip pool. Ready indicates that the pool is ready to be used.
OpenstackFloatingIPPoolReadyCondition = "OpenstackFloatingIPPoolReadyCondition"

// MaxIPsReachedReason is set when the maximum number of floating IPs has been reached.
MaxIPsReachedReason = "MaxIPsReached"
)
5 changes: 5 additions & 0 deletions api/v1alpha1/openstackfloatingippool_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ type OpenStackFloatingIPPoolSpec struct {
// These are used before allocating new ones and are not deleted from OpenStack when the pool is deleted.
PreAllocatedFloatingIPs []string `json:"preAllocatedFloatingIPs,omitempty"`

// MaxIPs is the maximum number of floating ips that can be allocated from this pool, if nil there is no limit.
// If set, the pool will stop allocating floating ips when it reaches this number of ClaimedIPs.
// +optional
MaxIPs *int `json:"maxIPs,omitempty"`

// IdentityRef is a reference to a identity to be used when reconciling this pool.
// +optional
IdentityRef *infrav1alpha7.OpenStackIdentityReference `json:"identityRef,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ spec:
- kind
- name
type: object
maxIPs:
description: |-
MaxIPs is the maximum number of floating ips that can be allocated from this pool, if nil there is no limit.
If set, the pool will stop allocating floating ips when it reaches this number of ClaimedIPs.
type: integer
preAllocatedFloatingIPs:
description: |-
PreAllocatedFloatingIPs is a list of floating IPs precreated in OpenStack that should be used by this pool.
Expand Down
18 changes: 17 additions & 1 deletion controllers/openstackfloatingippool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const (
openStackFloatingIPPool = "OpenStackFloatingIPPool"
)

var errMaxIPsReached = errors.New("maximum number of IPs reached")

var backoff = wait.Backoff{
Steps: 4,
Duration: 10 * time.Millisecond,
Expand Down Expand Up @@ -139,6 +141,10 @@ func (r *OpenStackFloatingIPPoolReconciler) Reconcile(ctx context.Context, req c
if apierrors.IsNotFound(err) {
ip, err := r.getIP(ctx, scope, pool)
if err != nil {
if errors.Is(err, errMaxIPsReached) {
log.Info("Maximum number of IPs reached, will not allocate more IPs.")
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
}

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

Expand Down Expand Up @@ -296,8 +303,10 @@ func (r *OpenStackFloatingIPPoolReconciler) reconcileIPAddresses(ctx context.Con
return err
}
}

unclaimedPreAllocatedIPs := diff(pool.Spec.PreAllocatedFloatingIPs, pool.Status.ClaimedIPs)
unclaimedIPs := union(pool.Status.AvailableIPs, unclaimedPreAllocatedIPs)
unclaimedIPs = diff(unclaimedIPs, pool.Status.ClaimedIPs)
pool.Status.AvailableIPs = diff(unclaimedIPs, pool.Status.FailedIPs)
return nil
}
Expand Down Expand Up @@ -340,9 +349,17 @@ func (r *OpenStackFloatingIPPoolReconciler) getIP(ctx context.Context, scope sco
return "", fmt.Errorf("get floating IP: %w", err)
}
if fp != nil {
pool.Status.ClaimedIPs = append(pool.Status.ClaimedIPs, fp.FloatingIP)
return fp.FloatingIP, nil
}
}
maxIPs := pointer.IntDeref(pool.Spec.MaxIPs, -1)
// If we have reached the maximum number of IPs, we should not create more IPs
if maxIPs != -1 && len(pool.Status.ClaimedIPs) >= maxIPs {
scope.Logger().Info("MaxIPs reached", "pool", pool.Name)
conditions.MarkFalse(pool, infrav1alpha1.OpenstackFloatingIPPoolReadyCondition, infrav1alpha1.MaxIPsReachedReason, clusterv1.ConditionSeverityError, "Maximum number of IPs reached, we will not allocate more IPs for this pool")
return "", errMaxIPsReached
}

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

conditions.MarkTrue(pool, infrav1alpha1.OpenstackFloatingIPPoolReadyCondition)

ip = fp.FloatingIP
pool.Status.ClaimedIPs = append(pool.Status.ClaimedIPs, ip)
return ip, nil
Expand Down

0 comments on commit d7903b7

Please sign in to comment.