Skip to content

Commit

Permalink
Merge pull request #273 from gthiemonge/quotas
Browse files Browse the repository at this point in the history
Set default quotas for Octavia
  • Loading branch information
beagles authored Mar 21, 2024
2 parents 5cce433 + 6167a7c commit 7ec5687
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
10 changes: 10 additions & 0 deletions controllers/octavia_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,16 @@ func (r *OctaviaReconciler) reconcileNormal(ctx context.Context, instance *octav
return ctrl.Result{}, err
}

if err = octavia.EnsureQuotas(ctx, instance, &r.Log, helper); err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.InputReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
condition.InputReadyErrorMessage,
err.Error()))
return ctrl.Result{}, err
}

instance.Status.Conditions.MarkTrue(condition.InputReadyCondition, condition.InputReadyMessage)

//
Expand Down
140 changes: 140 additions & 0 deletions pkg/octavia/quotas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package octavia

import (
"context"
"fmt"

"github.com/go-logr/logr"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/quotasets"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/quotas"
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
"github.com/openstack-k8s-operators/lib-common/modules/openstack"
octaviav1 "github.com/openstack-k8s-operators/octavia-operator/api/v1beta1"
)

func ensureComputeQuotas(
ctx context.Context,
instance *octaviav1.Octavia,
log *logr.Logger,
helper *helper.Helper,
osclient *openstack.OpenStack,
serviceTenantID string,
) error {

computeClient, err := GetComputeClient(osclient)
if err != nil {
return err
}

// Get the current quotas
quotaset, err := quotasets.Get(computeClient, serviceTenantID).Extract()
if err != nil {
return err
}

updateOpts := quotasets.UpdateOpts{
RAM: gophercloud.IntToPointer(-1),
Cores: gophercloud.IntToPointer(-1),
Instances: gophercloud.IntToPointer(-1),
ServerGroups: gophercloud.IntToPointer(-1),
ServerGroupMembers: gophercloud.IntToPointer(-1),
}
// Update only if needed
if quotaset.RAM != *updateOpts.RAM ||
quotaset.Cores != *updateOpts.Cores ||
quotaset.Instances != *updateOpts.Instances ||
quotaset.ServerGroups != *updateOpts.ServerGroups ||
quotaset.ServerGroupMembers != *updateOpts.ServerGroupMembers {

quotaset, err := quotasets.Update(computeClient, serviceTenantID, updateOpts).Extract()
if err != nil {
return err
}
log.Info(fmt.Sprintf("Compute quotas updated to %+v", *quotaset))
}

return nil
}

func ensureNetworkQuotas(
ctx context.Context,
instance *octaviav1.Octavia,
log *logr.Logger,
helper *helper.Helper,
osclient *openstack.OpenStack,
serviceTenantID string,
) error {

networkClient, err := GetNetworkClient(osclient)
if err != nil {
return err
}

// Get the current quotas
quotasInfo, err := quotas.Get(networkClient, serviceTenantID).Extract()
if err != nil {
return err
}

updateOpts := quotas.UpdateOpts{
Port: gophercloud.IntToPointer(-1),
SecurityGroup: gophercloud.IntToPointer(-1),
SecurityGroupRule: gophercloud.IntToPointer(-1),
}
// Update only if needed
if quotasInfo.Port != *updateOpts.Port ||
quotasInfo.SecurityGroup != *updateOpts.SecurityGroup ||
quotasInfo.SecurityGroupRule != *updateOpts.SecurityGroupRule {

quotasInfo, err := quotas.Update(networkClient, serviceTenantID, updateOpts).Extract()
if err != nil {
return err
}
log.Info(fmt.Sprintf("Network quotas updated to %+v", *quotasInfo))
}

return nil
}

// EnsureQuotas -- set the quotas for the Octavia project
func EnsureQuotas(
ctx context.Context,
instance *octaviav1.Octavia,
log *logr.Logger,
helper *helper.Helper,
) error {

osclient, err := GetOpenstackClient(ctx, instance.Namespace, helper)
if err != nil {
return fmt.Errorf("error while getting a service client when set quotas: %w", err)
}

serviceTenant, err := GetProject(osclient, instance.Spec.TenantName)
if err != nil {
return fmt.Errorf("error while getting the project %s: %w", instance.Spec.TenantName, err)
}

if err := ensureComputeQuotas(ctx, instance, log, helper, osclient, serviceTenant.ID); err != nil {
return fmt.Errorf("error while setting the compute quotas: %w", err)
}
if err := ensureNetworkQuotas(ctx, instance, log, helper, osclient, serviceTenant.ID); err != nil {
return fmt.Errorf("error while setting the network quotas: %w", err)
}

return nil
}

0 comments on commit 7ec5687

Please sign in to comment.