Skip to content

Commit

Permalink
openstack: Decouple OpenStack API calls from Machine generation
Browse files Browse the repository at this point in the history
To make unit tests possible (and separate responsibilities), remove OSP
API calls from the functions that generate Machines and MachineSets.
  • Loading branch information
pierreprinetti committed Mar 20, 2024
1 parent 0eafdbb commit b99470c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 30 deletions.
8 changes: 8 additions & 0 deletions pkg/asset/machines/clusterapi.go
Expand Up @@ -377,6 +377,13 @@ func (c *ClusterAPI) Generate(dependencies asset.Parents) error {
pool.Platform.OpenStack = &mpool

imageName, _ := rhcosutils.GenerateOpenStackImageName(string(*rhcosImage), clusterID.InfraID)
trunkSupport, err := openstack.CheckNetworkExtensionAvailability(
ic.Platform.OpenStack.Cloud,
"trunk",
)
if err != nil {
return fmt.Errorf("failed to check for trunk support: %w", err)
}

for _, role := range []string{"master", "bootstrap"} {
openStackMachines, err := openstack.GenerateMachines(
Expand All @@ -385,6 +392,7 @@ func (c *ClusterAPI) Generate(dependencies asset.Parents) error {
&pool,
imageName,
role,
trunkSupport,
)
if err != nil {
return fmt.Errorf("failed to create machine objects: %w", err)
Expand Down
11 changes: 9 additions & 2 deletions pkg/asset/machines/master.go
Expand Up @@ -310,9 +310,16 @@ func (m *Master) Generate(dependencies asset.Parents) error {

imageName, _ := rhcosutils.GenerateOpenStackImageName(string(*rhcosImage), clusterID.InfraID)

machines, controlPlaneMachineSet, err = openstack.Machines(clusterID.InfraID, ic, &pool, imageName, "master", masterUserDataSecretName)
trunkSupport, err := openstack.CheckNetworkExtensionAvailability(
ic.Platform.OpenStack.Cloud,
"trunk",
)
if err != nil {
return errors.Wrap(err, "failed to create master machine objects")
return fmt.Errorf("failed to check for trunk support: %w", err)
}
machines, controlPlaneMachineSet, err = openstack.Machines(clusterID.InfraID, ic, &pool, imageName, "master", masterUserDataSecretName, trunkSupport)
if err != nil {
return fmt.Errorf("failed to create master machine objects: %w", err)
}
openstack.ConfigMasters(machines, clusterID.InfraID)
case azuretypes.Name:
Expand Down
20 changes: 7 additions & 13 deletions pkg/asset/machines/openstack/machines.go
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/gophercloud/gophercloud"
netext "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
"github.com/gophercloud/utils/openstack/clientconfig"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -35,7 +34,7 @@ const (
)

// Machines returns a list of machines for a machinepool.
func Machines(clusterID string, config *types.InstallConfig, pool *types.MachinePool, osImage, role, userDataSecret string) ([]machineapi.Machine, *machinev1.ControlPlaneMachineSet, error) {
func Machines(clusterID string, config *types.InstallConfig, pool *types.MachinePool, osImage, role, userDataSecret string, trunkSupport bool) ([]machineapi.Machine, *machinev1.ControlPlaneMachineSet, error) {
if configPlatform := config.Platform.Name(); configPlatform != openstack.Name {
return nil, nil, fmt.Errorf("non-OpenStack configuration: %q", configPlatform)
}
Expand All @@ -44,11 +43,6 @@ func Machines(clusterID string, config *types.InstallConfig, pool *types.Machine
}

mpool := pool.Platform.OpenStack
platform := config.Platform.OpenStack
trunkSupport, err := checkNetworkExtensionAvailability(platform.Cloud, "trunk", nil)
if err != nil {
return nil, nil, err
}

total := int64(1)
if pool.Replicas != nil {
Expand All @@ -61,7 +55,7 @@ func Machines(clusterID string, config *types.InstallConfig, pool *types.Machine

providerSpec, err := generateProviderSpec(
clusterID,
platform,
config.Platform.OpenStack,
mpool,
osImage,
role,
Expand Down Expand Up @@ -99,7 +93,7 @@ func Machines(clusterID string, config *types.InstallConfig, pool *types.Machine

machineSetProviderSpec, err := generateProviderSpec(
clusterID,
platform,
config.Platform.OpenStack,
mpool,
osImage,
role,
Expand Down Expand Up @@ -379,10 +373,10 @@ func failureDomainsFromSpec(mpool openstack.MachinePool) []machinev1.OpenStackFa
return failureDomains
}

func checkNetworkExtensionAvailability(cloud, alias string, opts *clientconfig.ClientOpts) (bool, error) {
if opts == nil {
opts = openstackdefaults.DefaultClientOpts(cloud)
}
// CheckNetworkExtensionAvailability interrogates the OpenStack API to validate
// the availability of a given Neutron extension.
func CheckNetworkExtensionAvailability(cloud, alias string) (bool, error) {
opts := openstackdefaults.DefaultClientOpts(cloud)
conn, err := openstackdefaults.NewServiceClient("network", opts)
if err != nil {
return false, err
Expand Down
10 changes: 2 additions & 8 deletions pkg/asset/machines/openstack/machinesets.go
Expand Up @@ -4,7 +4,6 @@ package openstack
import (
"fmt"

"github.com/gophercloud/utils/openstack/clientconfig"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

Expand All @@ -24,19 +23,14 @@ const maxInt32 int64 = int64(^uint32(0)) >> 1
// availability zones, Storage availability zones and Root volume types), when
// more than one is specified, values of identical index are grouped in the
// same MachineSet.
func MachineSets(clusterID string, config *types.InstallConfig, pool *types.MachinePool, osImage, role, userDataSecret string, clientOpts *clientconfig.ClientOpts) ([]*clusterapi.MachineSet, error) {
func MachineSets(clusterID string, config *types.InstallConfig, pool *types.MachinePool, osImage, role, userDataSecret string, trunkSupport bool) ([]*clusterapi.MachineSet, error) {
if configPlatform := config.Platform.Name(); configPlatform != openstack.Name {
return nil, fmt.Errorf("non-OpenStack configuration: %q", configPlatform)
}
if poolPlatform := pool.Platform.Name(); poolPlatform != openstack.Name {
return nil, fmt.Errorf("non-OpenStack machine-pool: %q", poolPlatform)
}
platform := config.Platform.OpenStack
mpool := pool.Platform.OpenStack
trunkSupport, err := checkNetworkExtensionAvailability(platform.Cloud, "trunk", clientOpts)
if err != nil {
return nil, err
}

failureDomains := failureDomainsFromSpec(*mpool)
numberOfFailureDomains := int64(len(failureDomains))
Expand All @@ -60,7 +54,7 @@ func MachineSets(clusterID string, config *types.InstallConfig, pool *types.Mach

providerSpec, err := generateProviderSpec(
clusterID,
platform,
config.Platform.OpenStack,
mpool,
osImage,
role,
Expand Down
6 changes: 1 addition & 5 deletions pkg/asset/machines/openstack/openstackmachines.go
Expand Up @@ -18,7 +18,7 @@ import (
)

// GenerateMachines returns manifests and runtime objects to provision the control plane (including bootstrap, if applicable) nodes using CAPI.
func GenerateMachines(clusterID string, config *types.InstallConfig, pool *types.MachinePool, osImage, role string) ([]*asset.RuntimeFile, error) {
func GenerateMachines(clusterID string, config *types.InstallConfig, pool *types.MachinePool, osImage, role string, trunkSupport bool) ([]*asset.RuntimeFile, error) {
if configPlatform := config.Platform.Name(); configPlatform != openstack.Name {
return nil, fmt.Errorf("non-OpenStack configuration: %q", configPlatform)
}
Expand All @@ -27,10 +27,6 @@ func GenerateMachines(clusterID string, config *types.InstallConfig, pool *types
}

mpool := pool.Platform.OpenStack
trunkSupport, err := checkNetworkExtensionAvailability(config.Platform.OpenStack.Cloud, "trunk", nil)
if err != nil {
return nil, err
}

total := int64(1)
if role == "master" && pool.Replicas != nil {
Expand Down
11 changes: 9 additions & 2 deletions pkg/asset/machines/worker.go
Expand Up @@ -584,9 +584,16 @@ func (w *Worker) Generate(dependencies asset.Parents) error {

imageName, _ := rhcosutils.GenerateOpenStackImageName(string(*rhcosImage), clusterID.InfraID)

sets, err := openstack.MachineSets(clusterID.InfraID, ic, &pool, imageName, "worker", workerUserDataSecretName, nil)
trunkSupport, err := openstack.CheckNetworkExtensionAvailability(
ic.Platform.OpenStack.Cloud,
"trunk",
)
if err != nil {
return errors.Wrap(err, "failed to create worker machine objects")
return fmt.Errorf("failed to check for trunk support: %w", err)
}
sets, err := openstack.MachineSets(clusterID.InfraID, ic, &pool, imageName, "worker", workerUserDataSecretName, trunkSupport)
if err != nil {
return fmt.Errorf("failed to create worker machine objects: %w", err)
}
for _, set := range sets {
machineSets = append(machineSets, set)
Expand Down

0 comments on commit b99470c

Please sign in to comment.