diff --git a/pkg/cloud/services/compute/bastion.go b/pkg/cloud/services/compute/bastion.go index 63bd733dbe..90c697762b 100644 --- a/pkg/cloud/services/compute/bastion.go +++ b/pkg/cloud/services/compute/bastion.go @@ -33,7 +33,7 @@ func (s *Service) CreateBastion(openStackCluster *infrav1.OpenStackCluster, clus RootVolume: openStackCluster.Spec.Bastion.Instance.RootVolume, } - securityGroups, err := getSecurityGroups(s, openStackCluster.Spec.Bastion.Instance.SecurityGroups) + securityGroups, err := s.getSecurityGroups(openStackCluster.Spec.Bastion.Instance.SecurityGroups) if err != nil { return nil, err } @@ -45,7 +45,7 @@ func (s *Service) CreateBastion(openStackCluster *infrav1.OpenStackCluster, clus var nets []infrav1.Network if len(openStackCluster.Spec.Bastion.Instance.Networks) > 0 { var err error - nets, err = getServerNetworks(s.networkClient, openStackCluster.Spec.Bastion.Instance.Networks) + nets, err = s.getServerNetworks(openStackCluster.Spec.Bastion.Instance.Networks) if err != nil { return nil, err } @@ -59,7 +59,7 @@ func (s *Service) CreateBastion(openStackCluster *infrav1.OpenStackCluster, clus } input.Networks = &nets - out, err := createInstance(s, openStackCluster, clusterName, input) + out, err := s.createInstance(openStackCluster, clusterName, input) if err != nil { return nil, err } diff --git a/pkg/cloud/services/compute/instance.go b/pkg/cloud/services/compute/instance.go index d800e6b794..d550356baf 100644 --- a/pkg/cloud/services/compute/instance.go +++ b/pkg/cloud/services/compute/instance.go @@ -24,7 +24,6 @@ import ( "strconv" "time" - "github.com/gophercloud/gophercloud" "github.com/gophercloud/gophercloud/openstack/common/extensions" "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/attachinterfaces" "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/bootfromvolume" @@ -46,7 +45,6 @@ import ( "sigs.k8s.io/cluster-api/util" infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha4" - "sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/networking" "sigs.k8s.io/cluster-api-provider-openstack/pkg/record" capoerrors "sigs.k8s.io/cluster-api-provider-openstack/pkg/utils/errors" ) @@ -87,7 +85,7 @@ func (s *Service) CreateInstance(openStackCluster *infrav1.OpenStackCluster, mac } if openStackMachine.Spec.Trunk { - trunkSupport, err := getTrunkSupport(s) + trunkSupport, err := s.getTrunkSupport() if err != nil { return nil, fmt.Errorf("there was an issue verifying whether trunk support is available, please disable it: %v", err) } @@ -111,7 +109,7 @@ func (s *Service) CreateInstance(openStackCluster *infrav1.OpenStackCluster, mac input.Tags = machineTags // Get security groups - securityGroups, err := getSecurityGroups(s, openStackMachine.Spec.SecurityGroups) + securityGroups, err := s.getSecurityGroups(openStackMachine.Spec.SecurityGroups) if err != nil { return nil, err } @@ -127,7 +125,7 @@ func (s *Service) CreateInstance(openStackCluster *infrav1.OpenStackCluster, mac var nets []infrav1.Network if len(openStackMachine.Spec.Networks) > 0 { var err error - nets, err = getServerNetworks(s.networkClient, openStackMachine.Spec.Networks) + nets, err = s.getServerNetworks(openStackMachine.Spec.Networks) if err != nil { return nil, err } @@ -141,14 +139,14 @@ func (s *Service) CreateInstance(openStackCluster *infrav1.OpenStackCluster, mac } input.Networks = &nets - out, err := createInstance(s, openStackMachine, clusterName, input) + out, err := s.createInstance(openStackMachine, clusterName, input) if err != nil { return nil, err } return out, nil } -func createInstance(is *Service, eventObject runtime.Object, clusterName string, i *infrav1.Instance) (*infrav1.Instance, error) { +func (s *Service) createInstance(eventObject runtime.Object, clusterName string, i *infrav1.Instance) (*infrav1.Instance, error) { accessIPv4 := "" portList := []servers.Network{} @@ -157,18 +155,18 @@ func createInstance(is *Service, eventObject runtime.Object, clusterName string, return nil, fmt.Errorf("no network was found or provided. Please check your machine configuration and try again") } - port, err := getOrCreatePort(is, eventObject, clusterName, i.Name, network, i.SecurityGroups) + port, err := s.getOrCreatePort(eventObject, clusterName, i.Name, network, i.SecurityGroups) if err != nil { return nil, err } if i.Trunk { - trunk, err := getOrCreateTrunk(is, eventObject, i.Name, port.ID) + trunk, err := s.getOrCreateTrunk(eventObject, i.Name, port.ID) if err != nil { return nil, err } - if err = replaceAllAttributesTags(is, eventObject, trunk.ID, i.Tags); err != nil { + if err = s.replaceAllAttributesTags(eventObject, trunk.ID, i.Tags); err != nil { return nil, err } } @@ -185,18 +183,18 @@ func createInstance(is *Service, eventObject runtime.Object, clusterName string, } if i.Subnet != "" && accessIPv4 == "" { - if err := deletePorts(is, eventObject, portList); err != nil { + if err := s.deletePorts(eventObject, portList); err != nil { return nil, err } return nil, fmt.Errorf("no ports with fixed IPs found on Subnet %q", i.Subnet) } - imageID, err := getImageID(is, i.Image) + imageID, err := s.getImageID(i.Image) if err != nil { return nil, fmt.Errorf("create new server: %v", err) } - flavorID, err := flavors.IDFromName(is.computeClient, i.Flavor) + flavorID, err := flavors.IDFromName(s.computeClient, i.Flavor) if err != nil { return nil, fmt.Errorf("error getting flavor id from flavor name %s: %v", i.Flavor, err) } @@ -219,12 +217,12 @@ func createInstance(is *Service, eventObject runtime.Object, clusterName string, serverCreateOpts = applyServerGroupID(serverCreateOpts, i.ServerGroupID) - server, err := servers.Create(is.computeClient, keypairs.CreateOptsExt{ + server, err := servers.Create(s.computeClient, keypairs.CreateOptsExt{ CreateOptsBuilder: serverCreateOpts, KeyName: i.SSHKeyName, }).Extract() if err != nil { - if err = deletePorts(is, eventObject, portList); err != nil { + if err = s.deletePorts(eventObject, portList); err != nil { return nil, err } return nil, fmt.Errorf("error creating Openstack instance: %v", err) @@ -233,7 +231,7 @@ func createInstance(is *Service, eventObject runtime.Object, clusterName string, instanceCreateTimeout *= time.Minute var instance *infrav1.Instance err = util.PollImmediate(RetryIntervalInstanceStatus, instanceCreateTimeout, func() (bool, error) { - instance, err = is.GetInstance(server.ID) + instance, err = s.GetInstance(server.ID) if err != nil { if capoerrors.IsRetryable(err) { return false, nil @@ -285,8 +283,8 @@ func applyServerGroupID(opts servers.CreateOptsBuilder, serverGroupID string) se return opts } -func getTrunkSupport(is *Service) (bool, error) { - allPages, err := netext.List(is.networkClient).AllPages() +func (s *Service) getTrunkSupport() (bool, error) { + allPages, err := netext.List(s.networkClient).AllPages() if err != nil { return false, err } @@ -304,16 +302,16 @@ func getTrunkSupport(is *Service) (bool, error) { return false, nil } -func getSecurityGroups(is *Service, securityGroupParams []infrav1.SecurityGroupParam) ([]string, error) { +func (s *Service) getSecurityGroups(securityGroupParams []infrav1.SecurityGroupParam) ([]string, error) { var sgIDs []string for _, sg := range securityGroupParams { listOpts := groups.ListOpts(sg.Filter) if listOpts.ProjectID == "" { - listOpts.ProjectID = is.projectID + listOpts.ProjectID = s.projectID } listOpts.Name = sg.Name listOpts.ID = sg.UUID - pages, err := groups.List(is.networkClient, listOpts).AllPages() + pages, err := groups.List(s.networkClient, listOpts).AllPages() if err != nil { return nil, err } @@ -337,12 +335,12 @@ func getSecurityGroups(is *Service, securityGroupParams []infrav1.SecurityGroupP return sgIDs, nil } -func getServerNetworks(networkClient *gophercloud.ServiceClient, networkParams []infrav1.NetworkParam) ([]infrav1.Network, error) { +func (s *Service) getServerNetworks(networkParams []infrav1.NetworkParam) ([]infrav1.Network, error) { var nets []infrav1.Network for _, networkParam := range networkParams { opts := networks.ListOpts(networkParam.Filter) opts.ID = networkParam.UUID - ids, err := networking.GetNetworkIDsByFilter(networkClient, &opts) + ids, err := s.networkingService.GetNetworkIDsByFilter(&opts) if err != nil { return nil, err } @@ -358,7 +356,7 @@ func getServerNetworks(networkClient *gophercloud.ServiceClient, networkParams [ subnetOpts := subnets.ListOpts(subnet.Filter) subnetOpts.ID = subnet.UUID subnetOpts.NetworkID = netID - subnetsByFilter, err := networking.GetSubnetsByFilter(networkClient, &subnetOpts) + subnetsByFilter, err := s.networkingService.GetSubnetsByFilter(&subnetOpts) if err != nil { return nil, err } @@ -376,8 +374,8 @@ func getServerNetworks(networkClient *gophercloud.ServiceClient, networkParams [ return nets, nil } -func getOrCreatePort(is *Service, eventObject runtime.Object, clusterName string, portName string, net infrav1.Network, securityGroups *[]string) (*ports.Port, error) { - allPages, err := ports.List(is.networkClient, ports.ListOpts{ +func (s *Service) getOrCreatePort(eventObject runtime.Object, clusterName string, portName string, net infrav1.Network, securityGroups *[]string) (*ports.Port, error) { + allPages, err := ports.List(s.networkClient, ports.ListOpts{ Name: portName, NetworkID: net.ID, }).AllPages() @@ -402,7 +400,7 @@ func getOrCreatePort(is *Service, eventObject runtime.Object, clusterName string if net.Subnet.ID != "" { portCreateOpts.FixedIPs = []ports.IP{{SubnetID: net.Subnet.ID}} } - port, err := ports.Create(is.networkClient, portCreateOpts).Extract() + port, err := ports.Create(s.networkClient, portCreateOpts).Extract() if err != nil { record.Warnf(eventObject, "FailedCreatePort", "Failed to create port %s: %v", portName, err) return nil, err @@ -412,8 +410,8 @@ func getOrCreatePort(is *Service, eventObject runtime.Object, clusterName string return port, nil } -func getOrCreateTrunk(is *Service, eventObject runtime.Object, trunkName, portID string) (*trunks.Trunk, error) { - allPages, err := trunks.List(is.networkClient, trunks.ListOpts{ +func (s *Service) getOrCreateTrunk(eventObject runtime.Object, trunkName, portID string) (*trunks.Trunk, error) { + allPages, err := trunks.List(s.networkClient, trunks.ListOpts{ Name: trunkName, PortID: portID, }).AllPages() @@ -433,7 +431,7 @@ func getOrCreateTrunk(is *Service, eventObject runtime.Object, trunkName, portID Name: trunkName, PortID: portID, } - trunk, err := trunks.Create(is.networkClient, trunkCreateOpts).Extract() + trunk, err := trunks.Create(s.networkClient, trunkCreateOpts).Extract() if err != nil { record.Warnf(eventObject, "FailedCreateTrunk", "Failed to create trunk %s: %v", trunkName, err) return nil, err @@ -443,8 +441,8 @@ func getOrCreateTrunk(is *Service, eventObject runtime.Object, trunkName, portID return trunk, nil } -func replaceAllAttributesTags(is *Service, eventObject runtime.Object, trunkID string, tags []string) error { - _, err := attributestags.ReplaceAll(is.networkClient, "trunks", trunkID, attributestags.ReplaceAllOpts{ +func (s *Service) replaceAllAttributesTags(eventObject runtime.Object, trunkID string, tags []string) error { + _, err := attributestags.ReplaceAll(s.networkClient, "trunks", trunkID, attributestags.ReplaceAllOpts{ Tags: tags, }).Extract() if err != nil { @@ -457,7 +455,7 @@ func replaceAllAttributesTags(is *Service, eventObject runtime.Object, trunkID s } // Helper function for getting image ID from name. -func getImageID(is *Service, imageName string) (string, error) { +func (s *Service) getImageID(imageName string) (string, error) { if imageName == "" { return "", nil } @@ -466,7 +464,7 @@ func getImageID(is *Service, imageName string) (string, error) { Name: imageName, } - pages, err := images.List(is.imagesClient, opts).AllPages() + pages, err := images.List(s.imagesClient, opts).AllPages() if err != nil { return "", err } @@ -516,35 +514,35 @@ func (s *Service) DeleteInstance(eventObject runtime.Object, instanceName string return err } if len(instanceInterfaces) < 1 { - return deleteInstance(s, eventObject, instance.ID) + return s.deleteInstance(eventObject, instance.ID) } - trunkSupport, err := getTrunkSupport(s) + trunkSupport, err := s.getTrunkSupport() if err != nil { return fmt.Errorf("obtaining network extensions: %v", err) } // get and delete trunks for _, port := range instanceInterfaces { - if err = deleteAttachInterface(s, eventObject, instance.ID, port.PortID); err != nil { + if err = s.deleteAttachInterface(eventObject, instance.ID, port.PortID); err != nil { return err } if trunkSupport { - if err = deleteTrunk(s, eventObject, port.PortID); err != nil { + if err = s.deleteTrunk(eventObject, port.PortID); err != nil { return err } } - if err = deletePort(s, eventObject, port.PortID); err != nil { + if err = s.deletePort(eventObject, port.PortID); err != nil { return err } } - return deleteInstance(s, eventObject, instance.ID) + return s.deleteInstance(eventObject, instance.ID) } -func deletePort(is *Service, eventObject runtime.Object, portID string) error { - port, err := getPort(is, portID) +func (s *Service) deletePort(eventObject runtime.Object, portID string) error { + port, err := s.getPort(portID) if err != nil { return err } @@ -553,7 +551,7 @@ func deletePort(is *Service, eventObject runtime.Object, portID string) error { } err = util.PollImmediate(RetryIntervalPortDelete, TimeoutPortDelete, func() (bool, error) { - err := ports.Delete(is.networkClient, port.ID).ExtractErr() + err := ports.Delete(s.networkClient, port.ID).ExtractErr() if err != nil { if capoerrors.IsRetryable(err) { return false, nil @@ -571,17 +569,17 @@ func deletePort(is *Service, eventObject runtime.Object, portID string) error { return nil } -func deletePorts(is *Service, eventObject runtime.Object, nets []servers.Network) error { +func (s *Service) deletePorts(eventObject runtime.Object, nets []servers.Network) error { for _, n := range nets { - if err := deletePort(is, eventObject, n.Port); err != nil { + if err := s.deletePort(eventObject, n.Port); err != nil { return err } } return nil } -func deleteAttachInterface(is *Service, eventObject runtime.Object, instanceID, portID string) error { - instance, err := is.GetInstance(instanceID) +func (s *Service) deleteAttachInterface(eventObject runtime.Object, instanceID, portID string) error { + instance, err := s.GetInstance(instanceID) if err != nil { return err } @@ -589,7 +587,7 @@ func deleteAttachInterface(is *Service, eventObject runtime.Object, instanceID, return nil } - port, err := getPort(is, portID) + port, err := s.getPort(portID) if err != nil { return err } @@ -597,7 +595,7 @@ func deleteAttachInterface(is *Service, eventObject runtime.Object, instanceID, return nil } - err = attachinterfaces.Delete(is.computeClient, instanceID, portID).ExtractErr() + err = attachinterfaces.Delete(s.computeClient, instanceID, portID).ExtractErr() if err != nil { if capoerrors.IsNotFound(err) { return nil @@ -610,8 +608,8 @@ func deleteAttachInterface(is *Service, eventObject runtime.Object, instanceID, return nil } -func deleteTrunk(is *Service, eventObject runtime.Object, portID string) error { - port, err := getPort(is, portID) +func (s *Service) deleteTrunk(eventObject runtime.Object, portID string) error { + port, err := s.getPort(portID) if err != nil { return err } @@ -622,7 +620,7 @@ func deleteTrunk(is *Service, eventObject runtime.Object, portID string) error { listOpts := trunks.ListOpts{ PortID: port.ID, } - trunkList, err := trunks.List(is.networkClient, listOpts).AllPages() + trunkList, err := trunks.List(s.networkClient, listOpts).AllPages() if err != nil { return err } @@ -635,7 +633,7 @@ func deleteTrunk(is *Service, eventObject runtime.Object, portID string) error { } err = util.PollImmediate(RetryIntervalTrunkDelete, TimeoutTrunkDelete, func() (bool, error) { - if err := trunks.Delete(is.networkClient, trunkInfo[0].ID).ExtractErr(); err != nil { + if err := trunks.Delete(s.networkClient, trunkInfo[0].ID).ExtractErr(); err != nil { if capoerrors.IsRetryable(err) { return false, nil } @@ -652,11 +650,11 @@ func deleteTrunk(is *Service, eventObject runtime.Object, portID string) error { return nil } -func getPort(is *Service, portID string) (port *ports.Port, err error) { +func (s *Service) getPort(portID string) (port *ports.Port, err error) { if portID == "" { return nil, fmt.Errorf("portID should be specified to get detail") } - port, err = ports.Get(is.networkClient, portID).Extract() + port, err = ports.Get(s.networkClient, portID).Extract() if err != nil { if capoerrors.IsNotFound(err) { return nil, nil @@ -666,8 +664,8 @@ func getPort(is *Service, portID string) (port *ports.Port, err error) { return port, nil } -func deleteInstance(is *Service, eventObject runtime.Object, instanceID string) error { - instance, err := is.GetInstance(instanceID) +func (s *Service) deleteInstance(eventObject runtime.Object, instanceID string) error { + instance, err := s.GetInstance(instanceID) if err != nil { return err } @@ -676,13 +674,13 @@ func deleteInstance(is *Service, eventObject runtime.Object, instanceID string) return nil } - if err = servers.Delete(is.computeClient, instance.ID).ExtractErr(); err != nil { + if err = servers.Delete(s.computeClient, instance.ID).ExtractErr(); err != nil { record.Warnf(eventObject, "FailedDeleteServer", "Failed to deleted server %s with id %s: %v", instance.Name, instance.ID, err) return err } err = util.PollImmediate(RetryIntervalInstanceStatus, TimeoutInstanceDelete, func() (bool, error) { - i, err := is.GetInstance(instance.ID) + i, err := s.GetInstance(instance.ID) if err != nil { return false, err } diff --git a/pkg/cloud/services/compute/service.go b/pkg/cloud/services/compute/service.go index 18cd7a310f..3db6bb3922 100644 --- a/pkg/cloud/services/compute/service.go +++ b/pkg/cloud/services/compute/service.go @@ -24,17 +24,19 @@ import ( "github.com/gophercloud/gophercloud/openstack" "github.com/gophercloud/utils/openstack/clientconfig" + "sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/networking" "sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/provider" ) type Service struct { - provider *gophercloud.ProviderClient - projectID string - computeClient *gophercloud.ServiceClient - identityClient *gophercloud.ServiceClient - networkClient *gophercloud.ServiceClient - imagesClient *gophercloud.ServiceClient - logger logr.Logger + provider *gophercloud.ProviderClient + projectID string + computeClient *gophercloud.ServiceClient + identityClient *gophercloud.ServiceClient + networkClient *gophercloud.ServiceClient + imagesClient *gophercloud.ServiceClient + networkingService *networking.Service + logger logr.Logger } // NewService returns an instance of the compute service. @@ -82,13 +84,19 @@ func NewService(client *gophercloud.ProviderClient, clientOpts *clientconfig.Cli return nil, fmt.Errorf("failed to get project id") } + networkingService, err := networking.NewService(client, clientOpts, logger) + if err != nil { + return nil, fmt.Errorf("failed to create networking service: %v", err) + } + return &Service{ - provider: client, - projectID: projectID, - identityClient: identityClient, - computeClient: computeClient, - networkClient: networkingClient, - imagesClient: imagesClient, - logger: logger, + provider: client, + projectID: projectID, + identityClient: identityClient, + computeClient: computeClient, + networkClient: networkingClient, + networkingService: networkingService, + imagesClient: imagesClient, + logger: logger, }, nil } diff --git a/pkg/cloud/services/loadbalancer/loadbalancer.go b/pkg/cloud/services/loadbalancer/loadbalancer.go index f1bab1bc56..3156c8d114 100644 --- a/pkg/cloud/services/loadbalancer/loadbalancer.go +++ b/pkg/cloud/services/loadbalancer/loadbalancer.go @@ -21,8 +21,6 @@ import ( "fmt" "time" - "github.com/go-logr/logr" - "github.com/gophercloud/gophercloud" "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/listeners" "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/loadbalancers" "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/monitors" @@ -45,7 +43,7 @@ func (s *Service) ReconcileLoadBalancer(openStackCluster *infrav1.OpenStackClust s.logger.Info("Reconciling load balancer", "name", loadBalancerName) // lb - lb, err := checkIfLbExists(s.loadbalancerClient, loadBalancerName) + lb, err := s.checkIfLbExists(loadBalancerName) if err != nil { return err } @@ -63,7 +61,7 @@ func (s *Service) ReconcileLoadBalancer(openStackCluster *infrav1.OpenStackClust } record.Eventf(openStackCluster, "SuccessfulCreateLoadBalancer", "Created load balancer %s with id %s", loadBalancerName, lb.ID) } - if err := waitForLoadBalancerActive(s.logger, s.loadbalancerClient, lb.ID); err != nil { + if err := s.waitForLoadBalancerActive(lb.ID); err != nil { return err } @@ -85,7 +83,7 @@ func (s *Service) ReconcileLoadBalancer(openStackCluster *infrav1.OpenStackClust for _, port := range portList { lbPortObjectsName := fmt.Sprintf("%s-%d", loadBalancerName, port) - listener, err := checkIfListenerExists(s.loadbalancerClient, lbPortObjectsName) + listener, err := s.checkIfListenerExists(lbPortObjectsName) if err != nil { return err } @@ -102,16 +100,16 @@ func (s *Service) ReconcileLoadBalancer(openStackCluster *infrav1.OpenStackClust return fmt.Errorf("error creating listener: %s", err) } } - if err := waitForLoadBalancerActive(s.logger, s.loadbalancerClient, lb.ID); err != nil { + if err := s.waitForLoadBalancerActive(lb.ID); err != nil { return err } - if err := waitForListener(s.logger, s.loadbalancerClient, listener.ID, "ACTIVE"); err != nil { + if err := s.waitForListener(listener.ID, "ACTIVE"); err != nil { return err } // lb pool - pool, err := checkIfPoolExists(s.loadbalancerClient, lbPortObjectsName) + pool, err := s.checkIfPoolExists(lbPortObjectsName) if err != nil { return err } @@ -128,12 +126,12 @@ func (s *Service) ReconcileLoadBalancer(openStackCluster *infrav1.OpenStackClust return fmt.Errorf("error creating pool: %s", err) } } - if err := waitForLoadBalancerActive(s.logger, s.loadbalancerClient, lb.ID); err != nil { + if err := s.waitForLoadBalancerActive(lb.ID); err != nil { return err } // lb monitor - monitor, err := checkIfMonitorExists(s.loadbalancerClient, lbPortObjectsName) + monitor, err := s.checkIfMonitorExists(lbPortObjectsName) if err != nil { return err } @@ -152,7 +150,7 @@ func (s *Service) ReconcileLoadBalancer(openStackCluster *infrav1.OpenStackClust return fmt.Errorf("error creating monitor: %s", err) } } - if err = waitForLoadBalancerActive(s.logger, s.loadbalancerClient, lb.ID); err != nil { + if err = s.waitForLoadBalancerActive(lb.ID); err != nil { return err } } @@ -191,7 +189,7 @@ func (s *Service) ReconcileLoadBalancerMember(openStackCluster *infrav1.OpenStac lbPortObjectsName := fmt.Sprintf("%s-%d", loadBalancerName, port) name := lbPortObjectsName + "-" + openStackMachine.Name - pool, err := checkIfPoolExists(s.loadbalancerClient, lbPortObjectsName) + pool, err := s.checkIfPoolExists(lbPortObjectsName) if err != nil { return err } @@ -199,7 +197,7 @@ func (s *Service) ReconcileLoadBalancerMember(openStackCluster *infrav1.OpenStac return errors.New("load balancer pool does not exist yet") } - lbMember, err := checkIfLbMemberExists(s.loadbalancerClient, pool.ID, name) + lbMember, err := s.checkIfLbMemberExists(pool.ID, name) if err != nil { return err } @@ -214,7 +212,7 @@ func (s *Service) ReconcileLoadBalancerMember(openStackCluster *infrav1.OpenStac s.logger.Info("Deleting load balancer member (because the IP of the machine changed)", "name", name) // lb member changed so let's delete it so we can create it again with the correct IP - err = waitForLoadBalancerActive(s.logger, s.loadbalancerClient, lbID) + err = s.waitForLoadBalancerActive(lbID) if err != nil { return err } @@ -222,7 +220,7 @@ func (s *Service) ReconcileLoadBalancerMember(openStackCluster *infrav1.OpenStac if err != nil { return fmt.Errorf("error deleting lbmember: %s", err) } - err = waitForLoadBalancerActive(s.logger, s.loadbalancerClient, lbID) + err = s.waitForLoadBalancerActive(lbID) if err != nil { return err } @@ -237,13 +235,13 @@ func (s *Service) ReconcileLoadBalancerMember(openStackCluster *infrav1.OpenStac Address: ip, } - if err := waitForLoadBalancerActive(s.logger, s.loadbalancerClient, lbID); err != nil { + if err := s.waitForLoadBalancerActive(lbID); err != nil { return err } if _, err := pools.CreateMember(s.loadbalancerClient, pool.ID, lbMemberOpts).Extract(); err != nil { return fmt.Errorf("error create lbmember: %s", err) } - if err := waitForLoadBalancerActive(s.logger, s.loadbalancerClient, lbID); err != nil { + if err := s.waitForLoadBalancerActive(lbID); err != nil { return err } } @@ -252,7 +250,7 @@ func (s *Service) ReconcileLoadBalancerMember(openStackCluster *infrav1.OpenStac func (s *Service) DeleteLoadBalancer(openStackCluster *infrav1.OpenStackCluster, clusterName string) error { loadBalancerName := getLoadBalancerName(clusterName) - lb, err := checkIfLbExists(s.loadbalancerClient, loadBalancerName) + lb, err := s.checkIfLbExists(loadBalancerName) if err != nil { return err } @@ -297,7 +295,7 @@ func (s *Service) DeleteLoadBalancerMember(openStackCluster *infrav1.OpenStackCl } loadBalancerName := getLoadBalancerName(clusterName) - lb, err := checkIfLbExists(s.loadbalancerClient, loadBalancerName) + lb, err := s.checkIfLbExists(loadBalancerName) if err != nil { return err } @@ -314,7 +312,7 @@ func (s *Service) DeleteLoadBalancerMember(openStackCluster *infrav1.OpenStackCl lbPortObjectsName := fmt.Sprintf("%s-%d", loadBalancerName, port) name := lbPortObjectsName + "-" + openStackMachine.Name - pool, err := checkIfPoolExists(s.loadbalancerClient, lbPortObjectsName) + pool, err := s.checkIfPoolExists(lbPortObjectsName) if err != nil { return err } @@ -323,7 +321,7 @@ func (s *Service) DeleteLoadBalancerMember(openStackCluster *infrav1.OpenStackCl continue } - lbMember, err := checkIfLbMemberExists(s.loadbalancerClient, pool.ID, name) + lbMember, err := s.checkIfLbMemberExists(pool.ID, name) if err != nil { return err } @@ -331,7 +329,7 @@ func (s *Service) DeleteLoadBalancerMember(openStackCluster *infrav1.OpenStackCl if lbMember != nil { // lb member changed so let's delete it so we can create it again with the correct IP - err = waitForLoadBalancerActive(s.logger, s.loadbalancerClient, lbID) + err = s.waitForLoadBalancerActive(lbID) if err != nil { return err } @@ -339,7 +337,7 @@ func (s *Service) DeleteLoadBalancerMember(openStackCluster *infrav1.OpenStackCl if err != nil { return fmt.Errorf("error deleting load balancer member: %s", err) } - err = waitForLoadBalancerActive(s.logger, s.loadbalancerClient, lbID) + err = s.waitForLoadBalancerActive(lbID) if err != nil { return err } @@ -352,8 +350,8 @@ func getLoadBalancerName(clusterName string) string { return fmt.Sprintf("%s-cluster-%s-%s", networkPrefix, clusterName, kubeapiLBSuffix) } -func checkIfLbExists(client *gophercloud.ServiceClient, name string) (*loadbalancers.LoadBalancer, error) { - allPages, err := loadbalancers.List(client, loadbalancers.ListOpts{Name: name}).AllPages() +func (s *Service) checkIfLbExists(name string) (*loadbalancers.LoadBalancer, error) { + allPages, err := loadbalancers.List(s.loadbalancerClient, loadbalancers.ListOpts{Name: name}).AllPages() if err != nil { return nil, err } @@ -367,8 +365,8 @@ func checkIfLbExists(client *gophercloud.ServiceClient, name string) (*loadbalan return &lbList[0], nil } -func checkIfListenerExists(client *gophercloud.ServiceClient, name string) (*listeners.Listener, error) { - allPages, err := listeners.List(client, listeners.ListOpts{Name: name}).AllPages() +func (s *Service) checkIfListenerExists(name string) (*listeners.Listener, error) { + allPages, err := listeners.List(s.loadbalancerClient, listeners.ListOpts{Name: name}).AllPages() if err != nil { return nil, err } @@ -382,8 +380,8 @@ func checkIfListenerExists(client *gophercloud.ServiceClient, name string) (*lis return &listenerList[0], nil } -func checkIfPoolExists(client *gophercloud.ServiceClient, name string) (*pools.Pool, error) { - allPages, err := pools.List(client, pools.ListOpts{Name: name}).AllPages() +func (s *Service) checkIfPoolExists(name string) (*pools.Pool, error) { + allPages, err := pools.List(s.loadbalancerClient, pools.ListOpts{Name: name}).AllPages() if err != nil { return nil, err } @@ -397,8 +395,8 @@ func checkIfPoolExists(client *gophercloud.ServiceClient, name string) (*pools.P return &poolList[0], nil } -func checkIfMonitorExists(client *gophercloud.ServiceClient, name string) (*monitors.Monitor, error) { - allPages, err := monitors.List(client, monitors.ListOpts{Name: name}).AllPages() +func (s *Service) checkIfMonitorExists(name string) (*monitors.Monitor, error) { + allPages, err := monitors.List(s.loadbalancerClient, monitors.ListOpts{Name: name}).AllPages() if err != nil { return nil, err } @@ -412,8 +410,8 @@ func checkIfMonitorExists(client *gophercloud.ServiceClient, name string) (*moni return &monitorList[0], nil } -func checkIfLbMemberExists(client *gophercloud.ServiceClient, poolID, name string) (*pools.Member, error) { - allPages, err := pools.ListMembers(client, poolID, pools.ListMembersOpts{Name: name}).AllPages() +func (s *Service) checkIfLbMemberExists(poolID, name string) (*pools.Member, error) { + allPages, err := pools.ListMembers(s.loadbalancerClient, poolID, pools.ListMembersOpts{Name: name}).AllPages() if err != nil { return nil, err } @@ -435,10 +433,10 @@ var backoff = wait.Backoff{ } // Possible LoadBalancer states are documented here: https://developer.openstack.org/api-ref/network/v2/?expanded=show-load-balancer-status-tree-detail#load-balancer-statuses -func waitForLoadBalancerActive(logger logr.Logger, client *gophercloud.ServiceClient, id string) error { - logger.Info("Waiting for load balancer", "id", id, "targetStatus", "ACTIVE") +func (s *Service) waitForLoadBalancerActive(id string) error { + s.logger.Info("Waiting for load balancer", "id", id, "targetStatus", "ACTIVE") return wait.ExponentialBackoff(backoff, func() (bool, error) { - lb, err := loadbalancers.Get(client, id).Extract() + lb, err := loadbalancers.Get(s.loadbalancerClient, id).Extract() if err != nil { return false, err } @@ -446,10 +444,10 @@ func waitForLoadBalancerActive(logger logr.Logger, client *gophercloud.ServiceCl }) } -func waitForListener(logger logr.Logger, client *gophercloud.ServiceClient, id, target string) error { - logger.Info("Waiting for load balancer listener", "id", id, "targetStatus", target) +func (s *Service) waitForListener(id, target string) error { + s.logger.Info("Waiting for load balancer listener", "id", id, "targetStatus", target) return wait.ExponentialBackoff(backoff, func() (bool, error) { - _, err := listeners.Get(client, id).Extract() + _, err := listeners.Get(s.loadbalancerClient, id).Extract() if err != nil { return false, err } diff --git a/pkg/cloud/services/networking/floatingip.go b/pkg/cloud/services/networking/floatingip.go index b947bc3c05..2acfe87bdc 100644 --- a/pkg/cloud/services/networking/floatingip.go +++ b/pkg/cloud/services/networking/floatingip.go @@ -19,7 +19,6 @@ package networking import ( "time" - "github.com/gophercloud/gophercloud" "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips" "k8s.io/apimachinery/pkg/util/wait" @@ -33,7 +32,7 @@ func (s *Service) GetOrCreateFloatingIP(openStackCluster *infrav1.OpenStackClust var fpCreateOpts floatingips.CreateOpts if ip != "" { - fp, err = checkIfFloatingIPExists(s.client, ip) + fp, err = s.checkIfFloatingIPExists(ip) if err != nil { return nil, err } @@ -56,8 +55,8 @@ func (s *Service) GetOrCreateFloatingIP(openStackCluster *infrav1.OpenStackClust return fp, nil } -func checkIfFloatingIPExists(client *gophercloud.ServiceClient, ip string) (*floatingips.FloatingIP, error) { - allPages, err := floatingips.List(client, floatingips.ListOpts{FloatingIP: ip}).AllPages() +func (s *Service) checkIfFloatingIPExists(ip string) (*floatingips.FloatingIP, error) { + allPages, err := floatingips.List(s.client, floatingips.ListOpts{FloatingIP: ip}).AllPages() if err != nil { return nil, err } @@ -87,7 +86,7 @@ func (s *Service) GetFloatingIPByPortID(portID string) (*floatingips.FloatingIP, } func (s *Service) DeleteFloatingIP(openStackCluster *infrav1.OpenStackCluster, ip string) error { - fip, err := checkIfFloatingIPExists(s.client, ip) + fip, err := s.checkIfFloatingIPExists(ip) if err != nil { return err } @@ -136,7 +135,7 @@ func (s *Service) AssociateFloatingIP(openStackCluster *infrav1.OpenStackCluster } func (s *Service) DisassociateFloatingIP(openStackCluster *infrav1.OpenStackCluster, ip string) error { - fip, err := checkIfFloatingIPExists(s.client, ip) + fip, err := s.checkIfFloatingIPExists(ip) if err != nil { return err } diff --git a/pkg/cloud/services/networking/network.go b/pkg/cloud/services/networking/network.go index 7ac0a88dd4..add1ac5845 100644 --- a/pkg/cloud/services/networking/network.go +++ b/pkg/cloud/services/networking/network.go @@ -194,7 +194,7 @@ func (s *Service) ReconcileSubnet(openStackCluster *infrav1.OpenStackCluster, cl var subnet *subnets.Subnet if len(subnetList) == 0 { var err error - subnet, err = createSubnet(s.client, openStackCluster, subnetName) + subnet, err = s.createSubnet(openStackCluster, subnetName) if err != nil { return err } @@ -212,7 +212,7 @@ func (s *Service) ReconcileSubnet(openStackCluster *infrav1.OpenStackCluster, cl return nil } -func createSubnet(client *gophercloud.ServiceClient, openStackCluster *infrav1.OpenStackCluster, name string) (*subnets.Subnet, error) { +func (s *Service) createSubnet(openStackCluster *infrav1.OpenStackCluster, name string) (*subnets.Subnet, error) { opts := subnets.CreateOpts{ NetworkID: openStackCluster.Status.Network.ID, Name: name, @@ -220,7 +220,7 @@ func createSubnet(client *gophercloud.ServiceClient, openStackCluster *infrav1.O CIDR: openStackCluster.Spec.NodeCIDR, DNSNameservers: openStackCluster.Spec.DNSNameservers, } - subnet, err := subnets.Create(client, opts).Extract() + subnet, err := subnets.Create(s.client, opts).Extract() if err != nil { record.Warnf(openStackCluster, "FailedCreateSubnet", "Failed to create subnet %s: %v", name, err) return nil, err @@ -228,7 +228,7 @@ func createSubnet(client *gophercloud.ServiceClient, openStackCluster *infrav1.O record.Eventf(openStackCluster, "SuccessfulCreateSubnet", "Created subnet %s with id %s", name, subnet.ID) if len(openStackCluster.Spec.Tags) > 0 { - _, err = attributestags.ReplaceAll(client, "subnets", subnet.ID, attributestags.ReplaceAllOpts{ + _, err = attributestags.ReplaceAll(s.client, "subnets", subnet.ID, attributestags.ReplaceAllOpts{ Tags: openStackCluster.Spec.Tags, }).Extract() if err != nil { @@ -287,29 +287,12 @@ func (s *Service) getNetworkByName(networkName string) (networks.Network, error) return networks.Network{}, fmt.Errorf("found %d networks with the name %s, which should not happen", len(networkList), networkName) } -func (s *Service) GetNetworksByFilter(opts networks.ListOptsBuilder) ([]networks.Network, error) { - return GetNetworksByFilter(s.client, opts) -} - -// getNetworkIDsByFilter retrieves network ids by querying openstack with filters. -func GetNetworkIDsByFilter(networkClient *gophercloud.ServiceClient, opts networks.ListOptsBuilder) ([]string, error) { - nets, err := GetNetworksByFilter(networkClient, opts) - if err != nil { - return nil, err - } - ids := []string{} - for _, network := range nets { - ids = append(ids, network.ID) - } - return ids, nil -} - // GetNetworksByFilter retrieves networks by querying openstack with filters. -func GetNetworksByFilter(networkClient *gophercloud.ServiceClient, opts networks.ListOptsBuilder) ([]networks.Network, error) { +func (s *Service) GetNetworksByFilter(opts networks.ListOptsBuilder) ([]networks.Network, error) { if opts == nil { return nil, fmt.Errorf("no Filters were passed") } - pager := networks.List(networkClient, opts) + pager := networks.List(s.client, opts) var nets []networks.Network err := pager.EachPage(func(page pagination.Page) (bool, error) { networkList, err := networks.ExtractNetworks(page) @@ -327,16 +310,25 @@ func GetNetworksByFilter(networkClient *gophercloud.ServiceClient, opts networks return nets, nil } -func (s *Service) GetSubnetsByFilter(opts subnets.ListOptsBuilder) ([]subnets.Subnet, error) { - return GetSubnetsByFilter(s.client, opts) +// getNetworkIDsByFilter retrieves network ids by querying openstack with filters. +func (s *Service) GetNetworkIDsByFilter(opts networks.ListOptsBuilder) ([]string, error) { + nets, err := s.GetNetworksByFilter(opts) + if err != nil { + return nil, err + } + ids := []string{} + for _, network := range nets { + ids = append(ids, network.ID) + } + return ids, nil } // A function for getting the id of a subnet by querying openstack with filters. -func GetSubnetsByFilter(networkClient *gophercloud.ServiceClient, opts subnets.ListOptsBuilder) ([]subnets.Subnet, error) { +func (s *Service) GetSubnetsByFilter(opts subnets.ListOptsBuilder) ([]subnets.Subnet, error) { if opts == nil { return []subnets.Subnet{}, fmt.Errorf("no Filters were passed") } - pager := subnets.List(networkClient, opts) + pager := subnets.List(s.client, opts) var snets []subnets.Subnet err := pager.EachPage(func(page pagination.Page) (bool, error) { subnetList, err := subnets.ExtractSubnets(page) diff --git a/pkg/cloud/services/networking/router.go b/pkg/cloud/services/networking/router.go index 5686416f35..d52692c814 100644 --- a/pkg/cloud/services/networking/router.go +++ b/pkg/cloud/services/networking/router.go @@ -19,7 +19,6 @@ package networking import ( "fmt" - "github.com/gophercloud/gophercloud" "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/attributestags" "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers" "github.com/gophercloud/gophercloud/openstack/networking/v2/ports" @@ -66,7 +65,7 @@ func (s *Service) ReconcileRouter(openStackCluster *infrav1.OpenStackCluster, cl var router *routers.Router if len(routerList) == 0 { var err error - router, err = createRouter(s.client, openStackCluster, routerName) + router, err = s.createRouter(openStackCluster, routerName) if err != nil { return err } @@ -82,7 +81,7 @@ func (s *Service) ReconcileRouter(openStackCluster *infrav1.OpenStackCluster, cl } if len(openStackCluster.Spec.ExternalRouterIPs) > 0 { - if err := setRouterExternalIPs(s.client, openStackCluster, router); err != nil { + if err := s.setRouterExternalIPs(openStackCluster, router); err != nil { return err } } @@ -118,7 +117,7 @@ INTERFACE_LOOP: return nil } -func createRouter(client *gophercloud.ServiceClient, openStackCluster *infrav1.OpenStackCluster, name string) (*routers.Router, error) { +func (s *Service) createRouter(openStackCluster *infrav1.OpenStackCluster, name string) (*routers.Router, error) { opts := routers.CreateOpts{ Name: name, } @@ -131,7 +130,7 @@ func createRouter(client *gophercloud.ServiceClient, openStackCluster *infrav1.O NetworkID: openStackCluster.Status.ExternalNetwork.ID, } } - router, err := routers.Create(client, opts).Extract() + router, err := routers.Create(s.client, opts).Extract() if err != nil { record.Warnf(openStackCluster, "FailedCreateRouter", "Failed to create router %s: %v", name, err) return nil, err @@ -139,7 +138,7 @@ func createRouter(client *gophercloud.ServiceClient, openStackCluster *infrav1.O record.Eventf(openStackCluster, "SuccessfulCreateRouter", "Created router %s with id %s", name, router.ID) if len(openStackCluster.Spec.Tags) > 0 { - _, err = attributestags.ReplaceAll(client, "routers", router.ID, attributestags.ReplaceAllOpts{ + _, err = attributestags.ReplaceAll(s.client, "routers", router.ID, attributestags.ReplaceAllOpts{ Tags: openStackCluster.Spec.Tags, }).Extract() if err != nil { @@ -150,7 +149,7 @@ func createRouter(client *gophercloud.ServiceClient, openStackCluster *infrav1.O return router, nil } -func setRouterExternalIPs(client *gophercloud.ServiceClient, openStackCluster *infrav1.OpenStackCluster, router *routers.Router) error { +func (s *Service) setRouterExternalIPs(openStackCluster *infrav1.OpenStackCluster, router *routers.Router) error { updateOpts := routers.UpdateOpts{ GatewayInfo: &routers.GatewayInfo{ NetworkID: openStackCluster.Status.ExternalNetwork.ID, @@ -161,7 +160,7 @@ func setRouterExternalIPs(client *gophercloud.ServiceClient, openStackCluster *i subnetID := externalRouterIP.Subnet.UUID if subnetID == "" { listOpts := subnets.ListOpts(externalRouterIP.Subnet.Filter) - subnetsByFilter, err := GetSubnetsByFilter(client, &listOpts) + subnetsByFilter, err := s.GetSubnetsByFilter(&listOpts) if err != nil { return err } @@ -176,7 +175,7 @@ func setRouterExternalIPs(client *gophercloud.ServiceClient, openStackCluster *i }) } - if _, err := routers.Update(client, router.ID, updateOpts).Extract(); err != nil { + if _, err := routers.Update(s.client, router.ID, updateOpts).Extract(); err != nil { record.Warnf(openStackCluster, "FailedUpdateRouter", "Failed to update router %s with id %s: %v", router.Name, router.ID, err) return err }