Skip to content

Commit

Permalink
openstack: set region in cluster cloud client (#3375)
Browse files Browse the repository at this point in the history
  • Loading branch information
3u13r authored Sep 26, 2024
1 parent d65987c commit 882d602
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
15 changes: 15 additions & 0 deletions internal/cloud/openstack/imds.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,20 @@ func (c *imdsClient) userDomainName(ctx context.Context) (string, error) {
return c.userDataCache.UserDomainName, nil
}

func (c *imdsClient) regionName(ctx context.Context) (string, error) {
if c.timeForUpdate(c.cacheTime) || c.userDataCache.RegionName == "" {
if err := c.update(ctx); err != nil {
return "", err
}
}

if c.userDataCache.RegionName == "" {
return "", errors.New("unable to get user domain name")
}

return c.userDataCache.RegionName, nil
}

func (c *imdsClient) username(ctx context.Context) (string, error) {
if c.timeForUpdate(c.cacheTime) || c.userDataCache.Username == "" {
if err := c.update(ctx); err != nil {
Expand Down Expand Up @@ -295,6 +309,7 @@ type metadataTags struct {
type userDataResponse struct {
AuthURL string `json:"openstack-auth-url,omitempty"`
UserDomainName string `json:"openstack-user-domain-name,omitempty"`
RegionName string `json:"openstack-region-name,omitempty"`
Username string `json:"openstack-username,omitempty"`
Password string `json:"openstack-password,omitempty"`
LoadBalancerEndpoint string `json:"openstack-load-balancer-endpoint,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions internal/cloud/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func New(ctx context.Context) (*MetadataClient, error) {
if err != nil {
return nil, fmt.Errorf("getting user domain name: %w", err)
}
regionName, err := imds.regionName(ctx)
if err != nil {
return nil, fmt.Errorf("getting region name: %w", err)
}

clientOpts := &clientconfig.ClientOpts{
AuthType: clientconfig.AuthV3Password,
Expand All @@ -63,6 +67,7 @@ func New(ctx context.Context) (*MetadataClient, error) {
Username: username,
Password: password,
},
RegionName: regionName,
}

serversClient, err := clientconfig.NewServiceClient(ctx, "compute", clientOpts)
Expand Down
1 change: 1 addition & 0 deletions terraform/infrastructure/openstack/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ module "instance_group" {
openstack_username = local.cloudyaml["auth"]["username"]
openstack_password = local.cloudyaml["auth"]["password"]
openstack_user_domain_name = local.cloudyaml["auth"]["user_domain_name"]
openstack_region_name = local.cloudyaml["region_name"]
openstack_load_balancer_endpoint = openstack_networking_floatingip_v2.public_ip.address
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ resource "openstack_compute_instance_v2" "instance_group_member" {
openstack-username = var.openstack_username
openstack-password = var.openstack_password
openstack-user-domain-name = var.openstack_user_domain_name
openstack-region-name = var.openstack_region_name
openstack-load-balancer-endpoint = var.openstack_load_balancer_endpoint
})
availability_zone_hints = length(var.availability_zone) > 0 ? var.availability_zone : null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ variable "openstack_password" {
description = "OpenStack password."
}

variable "openstack_region_name" {
type = string
description = "OpenStack region name."
}

variable "openstack_load_balancer_endpoint" {
type = string
description = "OpenStack load balancer endpoint."
Expand Down

0 comments on commit 882d602

Please sign in to comment.