Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Adds MaxIPs to OpenstackFloatingIPPool #1862

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 15 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 @@ -341,10 +348,18 @@ 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
}
pool.Status.FailedIPs = append(pool.Status.FailedIPs, ip)
}
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 @@ -368,7 +383,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