From e1ace8880a104e2c00ec87bd02f420241d741971 Mon Sep 17 00:00:00 2001 From: Emilio Garcia Date: Mon, 28 Sep 2020 15:37:06 -0400 Subject: [PATCH 1/2] make primary subnet a progmatic part of machine spec --- .../openstackproviderconfig/v1alpha1/types.go | 3 ++ pkg/cloud/openstack/machine/actuator.go | 54 ++----------------- 2 files changed, 8 insertions(+), 49 deletions(-) diff --git a/pkg/apis/openstackproviderconfig/v1alpha1/types.go b/pkg/apis/openstackproviderconfig/v1alpha1/types.go index aef7b92ffe..6091075e6d 100644 --- a/pkg/apis/openstackproviderconfig/v1alpha1/types.go +++ b/pkg/apis/openstackproviderconfig/v1alpha1/types.go @@ -91,6 +91,9 @@ type OpenstackProviderSpec struct { // ServerGroupName are non-empty, they must refer to the same OpenStack // resource. ServerGroupName string `json:"serverGroupName,omitempty"` + + // The subnet that a set of machines will get ingress/egress traffic from + PrimarySubnet string `json:"primarySubnet,omitempty"` } type SecurityGroupParam struct { diff --git a/pkg/cloud/openstack/machine/actuator.go b/pkg/cloud/openstack/machine/actuator.go index 50fd4a284d..8442118180 100644 --- a/pkg/cloud/openstack/machine/actuator.go +++ b/pkg/cloud/openstack/machine/actuator.go @@ -29,10 +29,6 @@ import ( "k8s.io/apimachinery/pkg/api/equality" "k8s.io/client-go/tools/record" - "github.com/gophercloud/gophercloud" - gophercloudopenstack "github.com/gophercloud/gophercloud/openstack" - "github.com/gophercloud/gophercloud/openstack/networking/v2/networks" - machinev1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1" apierrors "github.com/openshift/machine-api-operator/pkg/controller/machine" "github.com/openshift/machine-api-operator/pkg/util" @@ -457,30 +453,6 @@ func getIPsFromInstance(instance *clients.Instance) (map[string]string, error) { return addrMap, nil } -func getNetworkByPrimaryNetworkTag(client *gophercloud.ServiceClient, primaryNetworkTag string) (networks.Network, error) { - opts := networks.ListOpts{ - Tags: primaryNetworkTag, - } - - allPages, err := networks.List(client, opts).AllPages() - if err != nil { - return networks.Network{}, err - } - - allNetworks, err := networks.ExtractNetworks(allPages) - if err != nil { - return networks.Network{}, err - } - - switch len(allNetworks) { - case 0: - return networks.Network{}, fmt.Errorf("There are no networks with primary network tag: %v", primaryNetworkTag) - case 1: - return allNetworks[0], nil - } - return networks.Network{}, fmt.Errorf("Too many networks with the same primary network tag: %v", primaryNetworkTag) -} - func (oc *OpenstackClient) getPrimaryMachineIP(mapAddr map[string]string, machine *machinev1.Machine, clusterInfraName string) (string, error) { // If there is only one network in the list, we consider it as the primary one if len(mapAddr) == 1 { @@ -489,32 +461,16 @@ func (oc *OpenstackClient) getPrimaryMachineIP(mapAddr map[string]string, machin } } - cloud, err := clients.GetCloud(oc.params.KubeClient, machine) + config, err := openstackconfigv1.MachineSpecFromProviderSpec(machine.Spec.ProviderSpec) if err != nil { - return "", err + return "", fmt.Errorf("Invalid provider spec for machine %s", machine.Name) } - provider, err := clients.GetProviderClient(cloud, clients.GetCACertificate(oc.params.KubeClient)) - if err != nil { - return "", err - } - - networkingClient, err := gophercloudopenstack.NewNetworkV2(provider, gophercloud.EndpointOpts{ - Region: cloud.RegionName, - }) - if err != nil { - return "", err - } - - primaryNetworkTag := clusterInfraName + "-primaryClusterNetwork" - network, err := getNetworkByPrimaryNetworkTag(networkingClient, primaryNetworkTag) - if err != nil { - return "", err - } + // PrimarySubnet should always be set in the machine api in 4.6 + primarySubnet := config.PrimarySubnet - // We're looking for the tag to identify the primary network for networkName, addr := range mapAddr { - if networkName == network.Name { + if networkName == primarySubnet { return addr, nil } } From b2b45ed1bb0893760be2d6bd5be5af990563aecc Mon Sep 17 00:00:00 2001 From: Emilio Garcia Date: Tue, 29 Sep 2020 10:46:00 -0400 Subject: [PATCH 2/2] added support for prior versions --- pkg/cloud/openstack/machine/actuator.go | 73 ++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/pkg/cloud/openstack/machine/actuator.go b/pkg/cloud/openstack/machine/actuator.go index 8442118180..762eff02e4 100644 --- a/pkg/cloud/openstack/machine/actuator.go +++ b/pkg/cloud/openstack/machine/actuator.go @@ -29,6 +29,10 @@ import ( "k8s.io/apimachinery/pkg/api/equality" "k8s.io/client-go/tools/record" + "github.com/gophercloud/gophercloud" + gophercloudopenstack "github.com/gophercloud/gophercloud/openstack" + "github.com/gophercloud/gophercloud/openstack/networking/v2/networks" + "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets" machinev1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1" apierrors "github.com/openshift/machine-api-operator/pkg/controller/machine" "github.com/openshift/machine-api-operator/pkg/util" @@ -453,6 +457,43 @@ func getIPsFromInstance(instance *clients.Instance) (map[string]string, error) { return addrMap, nil } +func getNetworkBySubnet(client *gophercloud.ServiceClient, subnetID string) (*networks.Network, error) { + subnet, err := subnets.Get(client, subnetID).Extract() + if err != nil { + return nil, fmt.Errorf("Could not get subnet %s, %v", subnetID, err) + } + + network, err := networks.Get(client, subnet.NetworkID).Extract() + if err != nil { + return nil, fmt.Errorf("Could not get network %s, %v", subnet.NetworkID, err) + } + return network, nil +} + +func getNetworkByPrimaryNetworkTag(client *gophercloud.ServiceClient, primaryNetworkTag string) (*networks.Network, error) { + opts := networks.ListOpts{ + Tags: primaryNetworkTag, + } + + allPages, err := networks.List(client, opts).AllPages() + if err != nil { + return nil, err + } + + allNetworks, err := networks.ExtractNetworks(allPages) + if err != nil { + return nil, err + } + + switch len(allNetworks) { + case 0: + return nil, fmt.Errorf("There are no networks with primary network tag: %v", primaryNetworkTag) + case 1: + return &allNetworks[0], nil + } + return nil, fmt.Errorf("Too many networks with the same primary network tag: %v", primaryNetworkTag) +} + func (oc *OpenstackClient) getPrimaryMachineIP(mapAddr map[string]string, machine *machinev1.Machine, clusterInfraName string) (string, error) { // If there is only one network in the list, we consider it as the primary one if len(mapAddr) == 1 { @@ -469,8 +510,38 @@ func (oc *OpenstackClient) getPrimaryMachineIP(mapAddr map[string]string, machin // PrimarySubnet should always be set in the machine api in 4.6 primarySubnet := config.PrimarySubnet + cloud, err := clients.GetCloud(oc.params.KubeClient, machine) + if err != nil { + return "", err + } + provider, err := clients.GetProviderClient(cloud, clients.GetCACertificate(oc.params.KubeClient)) + if err != nil { + return "", err + } + netClient, err := gophercloudopenstack.NewNetworkV2(provider, gophercloud.EndpointOpts{ + Region: cloud.RegionName, + }) + if err != nil { + return "", err + } + + var primaryNetwork *networks.Network + if primarySubnet != "" { + primaryNetwork, err = getNetworkBySubnet(netClient, primarySubnet) + if err != nil { + return "", err + } + } else { + // Support legacy versions + primaryNetworkTag := clusterInfraName + "-primaryClusterNetwork" + primaryNetwork, err = getNetworkByPrimaryNetworkTag(netClient, primaryNetworkTag) + if err != nil { + return "", err + } + } + for networkName, addr := range mapAddr { - if networkName == primarySubnet { + if networkName == primaryNetwork.Name { return addr, nil } }