Skip to content

Commit

Permalink
Bug 1824426: set primaryClusterNetwork tag for the primary network
Browse files Browse the repository at this point in the history
In the case of multiple addednetwrorks this tag should allow
cluster-api-provider-openstack to define which ip address to set
as the primary one for machines.
Now CAPO can't do this, because Neutron returns the list of networks
in alphabetical order.
  • Loading branch information
Fedosin committed Apr 20, 2020
1 parent afff10c commit 78c6745
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion data/data/openstack/topology/private-network.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ resource "openstack_networking_network_v2" "openshift-private" {
count = var.machines_subnet_id == "" ? 1 : 0
name = "${var.cluster_id}-openshift"
admin_state_up = "true"
tags = ["openshiftClusterID=${var.cluster_id}"]
tags = ["openshiftClusterID=${var.cluster_id}", "primaryClusterNetwork"]
}

resource "openstack_networking_subnet_v2" "nodes" {
Expand Down
33 changes: 33 additions & 0 deletions pkg/tfvars/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
"github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/attributestags"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
"github.com/gophercloud/utils/openstack/clientconfig"
"github.com/openshift/installer/pkg/rhcos"
Expand All @@ -21,6 +22,10 @@ import (
"sigs.k8s.io/cluster-api-provider-openstack/pkg/apis/openstackproviderconfig/v1alpha1"
)

const (
primaryClusterNetworkTag = "primaryClusterNetwork"
)

type config struct {
BaseImageName string `json:"openstack_base_image_name,omitempty"`
ExternalNetwork string `json:"openstack_external_network,omitempty"`
Expand Down Expand Up @@ -152,6 +157,15 @@ func TFVars(masterConfig *v1alpha1.OpenstackProviderSpec, cloud string, external
if err != nil {
return nil, err
}

// Make sure that the network has the primary cluster network tag.
// In the case of multiple networks this tag is required for
// cluster-api-provider-openstack to define which ip address to set as
// the primary one.
err = setNetworkTag(cloud, cfg.MachinesNetwork, primaryClusterNetworkTag)
if err != nil {
return nil, err
}
}

return json.MarshalIndent(cfg, "", " ")
Expand Down Expand Up @@ -265,3 +279,22 @@ func getNetworkFromSubnet(cloud string, subnetID string) (string, error) {

return subnet.NetworkID, nil
}

// setNetworkTag sets a tag for the network
func setNetworkTag(cloud string, networkID string, tag string) error {
opts := &clientconfig.ClientOpts{
Cloud: cloud,
}

networkClient, err := clientconfig.NewServiceClient("network", opts)
if err != nil {
return err
}

err = attributestags.Add(networkClient, "networks", networkID, tag).ExtractErr()
if err != nil {
return err
}

return nil
}

0 comments on commit 78c6745

Please sign in to comment.