Skip to content

Commit

Permalink
MKAAS-1131 Prevent reordering cluster pools on read (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbernady authored Feb 5, 2024
1 parent 4bd6f5f commit 89d7c9f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
48 changes: 48 additions & 0 deletions gcore/data_source_gcore_k8sv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import (
"net"
"os"
"strconv"
"strings"
"testing"

gcorecloud "github.com/G-Core/gcorelabscloud-go"
"github.com/G-Core/gcorelabscloud-go/gcore/k8s/v2/clusters"
"github.com/G-Core/gcorelabscloud-go/gcore/k8s/v2/pools"
"github.com/G-Core/gcorelabscloud-go/gcore/keypair/v2/keypairs"
"github.com/G-Core/gcorelabscloud-go/gcore/network/v1/availablenetworks"
"github.com/G-Core/gcorelabscloud-go/gcore/network/v1/networks"
"github.com/G-Core/gcorelabscloud-go/gcore/router/v1/routers"
"github.com/G-Core/gcorelabscloud-go/gcore/subnet/v1/subnets"
"github.com/G-Core/gcorelabscloud-go/gcore/task/v1/tasks"
"github.com/G-Core/gcorelabscloud-go/gcore/volume/v1/volumes"
Expand Down Expand Up @@ -202,3 +205,48 @@ func deleteTestClusterV2(client *gcorecloud.ServiceClient, clusterID string) err
})
return err
}

func patchRouterForK8S(provider *gcorecloud.ProviderClient, networkID string) error {
routersClient, err := CreateTestClient(provider, RouterPoint, versionPointV1)
if err != nil {
return err
}

aNetClient, err := CreateTestClient(provider, sharedNetworksPoint, versionPointV1)
if err != nil {
return err
}

availableNetworks, err := availablenetworks.ListAll(aNetClient, nil)
if err != nil {
return err
}
var extNet availablenetworks.Network
for _, an := range availableNetworks {
if an.External {
extNet = an
break
}
}

rs, err := routers.ListAll(routersClient, nil)
if err != nil {
return err
}

var router routers.Router
for _, r := range rs {
if strings.Contains(r.Name, networkID) {
router = r
break
}
}

extSubnet := extNet.Subnets[0]
routerOpts := routers.UpdateOpts{Routes: extSubnet.HostRoutes}
_, err = routers.Update(routersClient, router.ID, routerOpts).Extract()
if err != nil {
return err
}
return nil
}
2 changes: 1 addition & 1 deletion gcore/data_source_gcore_region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestAccRegionDataSource(t *testing.T) {
t.Fatal(err)
}

rs, err := regions.ListAll(client)
rs, err := regions.ListAll(client, nil)
if err != nil {
t.Fatal(err)
}
Expand Down
16 changes: 12 additions & 4 deletions gcore/resource_gcore_k8sv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"slices"
"time"

gcorecloud "github.com/G-Core/gcorelabscloud-go"
Expand Down Expand Up @@ -350,9 +351,11 @@ func resourceK8sV2Read(ctx context.Context, d *schema.ResourceData, m interface{
d.Set("creator_task_id", cluster.CreatorTaskID)
d.Set("task_id", cluster.TaskID)

var ps []map[string]interface{}
// pools always come sorted alphabetically, so we need to reorder them to match TF state
poolData := d.Get("pool").([]interface{})
for _, pool := range cluster.Pools {
ps = append(ps, map[string]interface{}{
idx := slices.IndexFunc(poolData, func(old interface{}) bool { return old.(map[string]interface{})["name"] == pool.Name })
obj := map[string]interface{}{
"name": pool.Name,
"flavor_id": pool.FlavorID,
"min_node_count": pool.MinNodeCount,
Expand All @@ -364,9 +367,14 @@ func resourceK8sV2Read(ctx context.Context, d *schema.ResourceData, m interface{
"is_public_ipv4": pool.IsPublicIPv4,
"status": pool.Status,
"created_at": pool.CreatedAt.Format(time.RFC850),
})
}
if idx >= 0 {
poolData[idx] = obj
} else {
poolData = append(poolData, obj)
}
}
if err := d.Set("pool", ps); err != nil {
if err := d.Set("pool", poolData); err != nil {
return diag.FromErr(err)
}

Expand Down
4 changes: 2 additions & 2 deletions gcore/resource_gcore_k8sv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestAccK8sV2(t *testing.T) {
}

kpOpts := keypairs.CreateOpts{
Name: kpName,
Name: "testkp",
PublicKey: pkTest,
ProjectID: pid,
}
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestAccK8sV2(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
CheckDestroy: testAccK8sDestroy,
CheckDestroy: testAccK8sV2Destroy,
Steps: []resource.TestStep{
{
Config: ipTemplate,
Expand Down

0 comments on commit 89d7c9f

Please sign in to comment.