Skip to content

Commit

Permalink
Make container infra cluster aware of default worker nodegroup
Browse files Browse the repository at this point in the history
The node_count returned in magnum for the cluster is the sum of all
nodegroup. So once you add a node_count, the correct value is actually
the node_count of the default worker node.

This commit take the node_count of this default worker node group in
priority and if there are any errors it takes the node_count of the
cluster instead.

Signed-off-by: Arthur Outhenin-Chalandre <arthur.outhenin-chalandre@cern.ch>
  • Loading branch information
MrFreezeex committed Jun 22, 2022
1 parent e37afd5 commit a4c7fe2
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion openstack/resource_openstack_containerinfra_cluster_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openstack

import (
"context"
"fmt"
"log"
"strconv"
"strings"
Expand All @@ -11,7 +12,9 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/containerinfra/v1/clusters"
"github.com/gophercloud/gophercloud/openstack/containerinfra/v1/nodegroups"
)

func resourceContainerInfraClusterV1() *schema.Resource {
Expand Down Expand Up @@ -313,6 +316,29 @@ func resourceContainerInfraClusterV1Create(ctx context.Context, d *schema.Resour
return resourceContainerInfraClusterV1Read(ctx, d, meta)
}

func getDefaultNodegroupNodeCount(containerInfraClient *gophercloud.ServiceClient, clusterID string) (int, error) {
containerInfraClient.Microversion = containerInfraV1NodeGroupMinMicroversion
listOpts := nodegroups.ListOpts{}

allPages, err := nodegroups.List(containerInfraClient, clusterID, listOpts).AllPages()
if err != nil {
return 0, err
}

ngs, err := nodegroups.ExtractNodeGroups(allPages)
if err != nil {
return 0, err
}

for _, ng := range ngs {
if ng.IsDefault && ng.Role != "master" {
return ng.NodeCount, nil
}
}

return 0, fmt.Errorf("Default worker nodegroup not found")
}

func resourceContainerInfraClusterV1Read(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
containerInfraClient, err := config.ContainerInfraV1Client(GetRegion(d, config))
Expand All @@ -335,6 +361,13 @@ func resourceContainerInfraClusterV1Read(_ context.Context, d *schema.ResourceDa
return diag.Errorf("Unable to set openstack_containerinfra_cluster_v1 labels: %s", err)
}

nodeCount, err := getDefaultNodegroupNodeCount(containerInfraClient, d.Id())
if err != nil {
log.Printf("[DEBUG] Can't retrieve node_count of the default worker node group %s: %s", d.Id(), err)

nodeCount = s.NodeCount
}

d.Set("name", s.Name)
d.Set("api_address", s.APIAddress)
d.Set("coe_version", s.COEVersion)
Expand All @@ -347,7 +380,7 @@ func resourceContainerInfraClusterV1Read(_ context.Context, d *schema.ResourceDa
d.Set("master_flavor", s.MasterFlavorID)
d.Set("keypair", s.KeyPair)
d.Set("master_count", s.MasterCount)
d.Set("node_count", s.NodeCount)
d.Set("node_count", nodeCount)
d.Set("master_addresses", s.MasterAddresses)
d.Set("node_addresses", s.NodeAddresses)
d.Set("stack_id", s.StackID)
Expand Down

0 comments on commit a4c7fe2

Please sign in to comment.