Skip to content

Commit

Permalink
Create Nova flavors for Octavia
Browse files Browse the repository at this point in the history
Add support for system-scope Compute client.

Ensure that 2 nova flavors are created:
- octavia-amphora: 1 vCPU, 1GB RAM, 3GB disk
- octavia-amphora-4vcpus: 4 vCPU, 4GB RAM, 3GB disk

octavia-amphora is the default flavor in Octavia
  • Loading branch information
gthiemonge committed Nov 20, 2023
1 parent 57226ee commit 60cabfa
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 13 deletions.
10 changes: 9 additions & 1 deletion controllers/amphoracontroller_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,13 @@ func (r *OctaviaAmphoraControllerReconciler) reconcileNormal(ctx context.Context
}
r.Log.Info(fmt.Sprintf("Using management network \"%s\"", networkID))

err = r.generateServiceConfigMaps(ctx, instance, helper, &configMapVars, networkID)
defaultFlavorID, err := amphoracontrollers.EnsureFlavors(ctx, instance, &r.Log, helper)
if err != nil {
return ctrl.Result{}, err
}
r.Log.Info(fmt.Sprintf("Using default flavor \"%s\"", defaultFlavorID))

err = r.generateServiceConfigMaps(ctx, instance, helper, &configMapVars, networkID, defaultFlavorID)
if err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
condition.ServiceConfigReadyCondition,
Expand Down Expand Up @@ -380,6 +386,7 @@ func (r *OctaviaAmphoraControllerReconciler) generateServiceConfigMaps(
helper *helper.Helper,
envVars *map[string]env.Setter,
lbMgmtNetworkID string,
defaultFlavorID string,
) error {
r.Log.Info(fmt.Sprintf("generating service config map for %s (%s)", instance.Name, instance.Kind))
cmLabels := labels.GetLabels(instance, labels.GetGroupLabel(octavia.ServiceName), map[string]string{})
Expand All @@ -406,6 +413,7 @@ func (r *OctaviaAmphoraControllerReconciler) generateServiceConfigMaps(
templateParameters["KeystonePublicURL"] = keystonePublicURL
templateParameters["ServiceRoleName"] = instance.Spec.Role
templateParameters["LbMgmtNetworkId"] = lbMgmtNetworkID
templateParameters["AmpFlavorId"] = defaultFlavorID

// TODO(beagles): populate the template parameters
cms := []util.Template{
Expand Down
122 changes: 122 additions & 0 deletions pkg/amphoracontrollers/flavors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
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 amphoracontrollers

import (
"context"
"fmt"
"strings"

"github.com/go-logr/logr"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1"
"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"
"github.com/openstack-k8s-operators/octavia-operator/pkg/octavia"
)

func ensureNovaFlavors(osclient *openstack.OpenStack, log *logr.Logger) (string, error) {
client, err := octavia.GetComputeClient(osclient)
if err != nil {
return "", err
}

// Get Octavia flavors
listOpts := flavors.ListOpts{
AccessType: flavors.AllAccess,
}
allPages, err := flavors.ListDetail(client, listOpts).AllPages()
if err != nil {
return "", err
}
allFlavors, err := flavors.ExtractFlavors(allPages)
if err != nil {
return "", err
}
amphoraFlavors := make(map[string]flavors.Flavor)
for _, flavor := range allFlavors {
if strings.HasPrefix(flavor.Name, "octavia-") {
amphoraFlavors[flavor.Name] = flavor
}
}

isPublic := false
defaultFlavorsCreateOpts := []flavors.CreateOpts{
{
Name: "octavia-amphora",
VCPUs: 1,
RAM: 1024,
Disk: gophercloud.IntToPointer(3),
RxTxFactor: 1.0,
IsPublic: &isPublic,
}, {
Name: "octavia-amphora-4vcpus",
VCPUs: 4,
RAM: 4096,
Disk: gophercloud.IntToPointer(3),
RxTxFactor: 1.0,
IsPublic: &isPublic,
},
}
defaultFlavorID := ""
defaultFlavorName := defaultFlavorsCreateOpts[0].Name

// Default flavor already exists, get its ID
if flavor, ok := amphoraFlavors[defaultFlavorName]; ok {
defaultFlavorID = flavor.ID
}

// Create missing flavors
for idx, defaultFlavorOpts := range defaultFlavorsCreateOpts {
if _, ok := amphoraFlavors[defaultFlavorOpts.Name]; !ok {
log.Info(fmt.Sprintf("Creating Amphora flavor \"%s\"", defaultFlavorOpts.Name))
flavor, err := flavors.Create(client, defaultFlavorOpts).Extract()
if err != nil {
return "", err
}
if idx == 0 {
defaultFlavorID = flavor.ID
}
}
}

return defaultFlavorID, nil
}

// EnsureFlavors - enable that the Nova flavors for the amphora VMs are created
//
// returns the UUID of the default Nova flavor
func EnsureFlavors(ctx context.Context, instance *octaviav1.OctaviaAmphoraController, log *logr.Logger, helper *helper.Helper) (string, error) {
keystoneAPI, err := keystonev1.GetKeystoneAPI(ctx, helper, instance.Namespace, map[string]string{})
if err != nil {
return "", err
}
osclient, _, err := octavia.GetAdminServiceClient(ctx, helper, keystoneAPI, octavia.ClientOpts{SystemScope: false})
if err != nil {
return "", err
}

defaultNovaFlavorID, err := ensureNovaFlavors(osclient, log)
if err != nil {
return "", err

}

// TODO(gthiemonge) Create Octavia flavorprofiles and flavors when gophercloud support them

return defaultNovaFlavorID, nil
}
2 changes: 1 addition & 1 deletion pkg/amphoracontrollers/lb_mgmt_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func EnsureLbMgmtNetworks(ctx context.Context, instance *octaviav1.OctaviaAmphor
if err != nil {
return "", err
}
o, _, err := octavia.GetAdminServiceClient(ctx, helper, keystoneAPI)
o, _, err := octavia.GetAdminServiceClient(ctx, helper, keystoneAPI, octavia.ClientOpts{SystemScope: true})
if err != nil {
return "", err
}
Expand Down
43 changes: 32 additions & 11 deletions pkg/octavia/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
)

// ClientOpts - enable features in the OpenStack client
type ClientOpts struct {
// SystemScope indicates that the client uses system scope instead of project scope
SystemScope bool
}

// GetAdminServiceClient -
func GetAdminServiceClient(
ctx context.Context,
h *helper.Helper,
keystoneAPI *keystonev1.KeystoneAPI,
clientOpts ClientOpts,
) (*openstack.OpenStack, ctrl.Result, error) {
// get public endpoint as authurl from keystone instance
authURL, err := keystoneAPI.GetEndpoint(endpoint.EndpointPublic)
Expand All @@ -57,19 +64,24 @@ func GetAdminServiceClient(
return nil, ctrlResult, nil
}

authOpts := openstack.AuthOpts{
AuthURL: authURL,
Username: keystoneAPI.Spec.AdminUser,
Password: authPassword,
TenantName: keystoneAPI.Spec.AdminProject,
DomainName: "Default",
Region: keystoneAPI.Spec.Region,
}
if clientOpts.SystemScope {
authOpts.Scope = &gophercloud.AuthScope{
System: clientOpts.SystemScope,
}
}

os, err := openstack.NewOpenStack(
h.GetLogger(),
openstack.AuthOpts{
AuthURL: authURL,
Username: keystoneAPI.Spec.AdminUser,
Password: authPassword,
TenantName: keystoneAPI.Spec.AdminProject,
DomainName: "Default",
Region: keystoneAPI.Spec.Region,
Scope: &gophercloud.AuthScope{
System: true,
},
})
authOpts,
)
if err != nil {
return nil, ctrl.Result{}, err
}
Expand Down Expand Up @@ -101,3 +113,12 @@ func GetNetworkClient(o *openstack.OpenStack) (*gophercloud.ServiceClient, error
}
return gophercloudopenstack.NewNetworkV2(o.GetOSClient().ProviderClient, endpointOpts)
}

// GetComputeClient -
func GetComputeClient(o *openstack.OpenStack) (*gophercloud.ServiceClient, error) {
endpointOpts := gophercloud.EndpointOpts{
Region: o.GetRegion(),
Availability: gophercloud.AvailabilityInternal,
}
return gophercloudopenstack.NewComputeV2(o.GetOSClient().ProviderClient, endpointOpts)
}
1 change: 1 addition & 0 deletions templates/octaviaamphoracontroller/config/octavia.conf
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ port_detach_timeout=300
[haproxy_amphora]
[controller_worker]
amp_boot_network_list={{ .LbMgmtNetworkId }}
amp_flavor_id={{ .AmpFlavorId }}
[task_flow]
[oslo_messaging]
# topic=octavia-rpc
Expand Down

0 comments on commit 60cabfa

Please sign in to comment.