Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions internal/identity/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ func DefaultZonal() *schema.ResourceIdentity {
})
}

// DefaultRegional should be used as the default identity schema for regional resources.
// For instance if you want an id with the form fr-par/11111111-1111-1111-1111-111111111111
func DefaultRegional() *schema.ResourceIdentity {
return WrapSchemaMap(map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "The id of the resource (UUID format)",
RequiredForImport: true,
},
"region": DefaultRegionAttribute(),
})
}

func WrapSchemaMap(m map[string]*schema.Schema) *schema.ResourceIdentity {
return &schema.ResourceIdentity{
SchemaFunc: func() map[string]*schema.Schema {
Expand Down
13 changes: 11 additions & 2 deletions internal/services/k8s/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/scaleway/terraform-provider-scaleway/v2/internal/cdf"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/identity"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
Expand Down Expand Up @@ -41,6 +42,7 @@ func ResourceACL() *schema.Resource {
SchemaVersion: 0,
SchemaFunc: aclSchema,
CustomizeDiff: cdf.LocalityCheck("cluster_id"),
Identity: identity.DefaultRegional(),
}
}

Expand Down Expand Up @@ -129,8 +131,10 @@ func ResourceACLCreate(ctx context.Context, d *schema.ResourceData, m any) diag.
return diag.FromErr(err)
}

regionalID := regional.NewID(region, clusterID).String()
d.SetId(regionalID)
err = identity.SetRegionalIdentity(d, region, clusterID)
if err != nil {
return diag.FromErr(err)
}

return ResourceACLRead(ctx, d, m)
}
Expand Down Expand Up @@ -160,6 +164,11 @@ func ResourceACLRead(ctx context.Context, d *schema.ResourceData, m any) diag.Di
return diag.FromErr(err)
}

err = identity.SetRegionalIdentity(d, region, clusterID)
if err != nil {
return diag.FromErr(err)
}

_ = d.Set("cluster_id", regional.NewIDString(region, clusterID))
_ = d.Set("region", region)
_ = d.Set("acl_rules", flattenACL(acls.Rules))
Expand Down
41 changes: 32 additions & 9 deletions internal/services/k8s/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/identity"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
Expand Down Expand Up @@ -54,6 +55,7 @@ func ResourceCluster() *schema.Resource {
},
SchemaVersion: 0,
SchemaFunc: clusterSchema,
Identity: identity.DefaultRegional(),
CustomizeDiff: customdiff.All(
func(_ context.Context, diff *schema.ResourceDiff, _ any) error {
autoUpgradeEnable, okAutoUpgradeEnable := diff.GetOkExists("auto_upgrade.0.enable")
Expand Down Expand Up @@ -571,7 +573,10 @@ func ResourceK8SClusterCreate(ctx context.Context, d *schema.ResourceData, m any
return append(diag.FromErr(err), diags...)
}

d.SetId(regional.NewIDString(region, res.ID))
err = identity.SetRegionalIdentity(d, res.Region, res.ID)
if err != nil {
return diag.FromErr(err)
}

if strings.Contains(clusterType.(string), "multicloud") {
// In case of multi-cloud, we do not have the guarantee that a pool will be created in Scaleway.
Expand Down Expand Up @@ -608,7 +613,21 @@ func ResourceK8SClusterRead(ctx context.Context, d *schema.ResourceData, m any)
return diag.FromErr(err)
}

_ = d.Set("region", string(region))
diagnostics := setClusterState(ctx, d, cluster, k8sAPI)
if diagnostics.HasError() {
return diagnostics
}

err = identity.SetRegionalIdentity(d, cluster.Region, cluster.ID)
if err != nil {
return diag.FromErr(err)
}

return nil
}

func setClusterState(ctx context.Context, d *schema.ResourceData, cluster *k8s.Cluster, k8sAPI *k8s.API) diag.Diagnostics {
_ = d.Set("region", cluster.Region)
_ = d.Set("name", cluster.Name)
_ = d.Set("type", cluster.Type)
_ = d.Set("organization_id", cluster.OrganizationID)
Expand All @@ -628,6 +647,8 @@ func ResourceK8SClusterRead(ctx context.Context, d *schema.ResourceData, m any)

// if autoupgrade is enabled, we only set the minor k8s version (x.y)
version := cluster.Version

var err error
if cluster.AutoUpgrade != nil && cluster.AutoUpgrade.Enabled {
version, err = GetMinorVersionFromFull(version)
if err != nil {
Expand All @@ -653,15 +674,17 @@ func ResourceK8SClusterRead(ctx context.Context, d *schema.ResourceData, m any)
////
// Read kubeconfig
////
kubeconfig, err := flattenKubeconfig(ctx, k8sAPI, region, clusterID)
kubeconfig, err := flattenKubeconfig(ctx, k8sAPI, cluster.Region, cluster.ID)
if err != nil {
if httperrors.Is403(err) {
return diag.Diagnostics{diag.Diagnostic{
Severity: diag.Warning,
Summary: "Cannot read kubeconfig: unauthorized",
Detail: "Got 403 while reading kubeconfig, please check your permissions",
AttributePath: cty.GetAttrPath("kubeconfig"),
}}
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Warning,
Summary: "Cannot read kubeconfig: unauthorized",
Detail: "Got 403 while reading kubeconfig, please check your permissions",
AttributePath: cty.GetAttrPath("kubeconfig"),
},
}
}

return diag.FromErr(err)
Expand Down
14 changes: 13 additions & 1 deletion internal/services/k8s/cluster_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/scaleway/scaleway-sdk-go/api/k8s/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)
Expand Down Expand Up @@ -75,5 +76,16 @@ func DataSourceK8SClusterRead(ctx context.Context, d *schema.ResourceData, m any
d.SetId(regionalizedID)
_ = d.Set("cluster_id", regionalizedID)

return ResourceK8SClusterRead(ctx, d, m)
cluster, err := waitCluster(ctx, k8sAPI, region, clusterID.(string), d.Timeout(schema.TimeoutRead))
if err != nil {
if httperrors.Is404(err) {
d.SetId("")

return nil
}

return diag.FromErr(err)
}

return setClusterState(ctx, d, cluster, k8sAPI)
}
42 changes: 42 additions & 0 deletions internal/services/k8s/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ resource "scaleway_k8s_cluster" "minimal" {
resource.TestCheckResourceAttr("scaleway_k8s_cluster.minimal", "service_dns_ip", k8s.NetworkingDefaultValues["service_dns_ip"]),
),
},
{
ResourceName: "scaleway_k8s_cluster.minimal",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"delete_additional_resources"},
},
},
})
}
Expand Down Expand Up @@ -256,6 +262,12 @@ func TestAccCluster_Autoscaling(t *testing.T) {
resource.TestCheckResourceAttr("scaleway_k8s_cluster.autoscaler", "tags.2", "autoscaler-config"),
),
},
{
ResourceName: "scaleway_k8s_cluster.autoscaler",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"delete_additional_resources"},
},
},
})
}
Expand Down Expand Up @@ -320,6 +332,12 @@ func TestAccCluster_OIDC(t *testing.T) {
resource.TestCheckResourceAttr("scaleway_k8s_cluster.oidc", "tags.2", "oidc-config"),
),
},
{
ResourceName: "scaleway_k8s_cluster.oidc",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"delete_additional_resources"},
},
},
})
}
Expand Down Expand Up @@ -400,6 +418,12 @@ func TestAccCluster_AutoUpgrade(t *testing.T) {
resource.TestCheckResourceAttr("scaleway_k8s_cluster.auto_upgrade", "auto_upgrade.0.maintenance_window_start_hour", "0"),
),
},
{
ResourceName: "scaleway_k8s_cluster.auto_upgrade",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"delete_additional_resources"},
},
},
})
}
Expand Down Expand Up @@ -438,6 +462,12 @@ func TestAccCluster_PrivateNetwork(t *testing.T) {
acctest.CheckResourceIDChanged("scaleway_k8s_cluster.private_network", &clusterID),
),
},
{
ResourceName: "scaleway_k8s_cluster.private_network",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"delete_additional_resources"},
},
},
})
}
Expand All @@ -459,6 +489,12 @@ func TestAccCluster_Multicloud(t *testing.T) {
resource.TestCheckResourceAttr("scaleway_k8s_cluster.multicloud", "version", latestK8SVersion),
),
},
{
ResourceName: "scaleway_k8s_cluster.multicloud",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"delete_additional_resources"},
},
},
})
}
Expand Down Expand Up @@ -541,6 +577,12 @@ func TestAccCluster_TypeChange(t *testing.T) {
acctest.CheckResourceIDChanged("scaleway_k8s_cluster.type-change", &clusterID),
),
},
{
ResourceName: "scaleway_k8s_cluster.type-change",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"delete_additional_resources"},
},
},
})
}
Expand Down
24 changes: 20 additions & 4 deletions internal/services/k8s/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/identity"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
Expand Down Expand Up @@ -42,6 +43,7 @@ func ResourcePool() *schema.Resource {
},
SchemaVersion: 0,
SchemaFunc: poolSchema,
Identity: identity.DefaultRegional(),
}
}

Expand Down Expand Up @@ -367,7 +369,10 @@ func ResourceK8SPoolCreate(ctx context.Context, d *schema.ResourceData, m any) d
return diag.FromErr(err)
}

d.SetId(regional.NewIDString(region, res.ID))
err = identity.SetRegionalIdentity(d, res.Region, res.ID)
if err != nil {
return diag.FromErr(err)
}

if d.Get("wait_for_pool_ready").(bool) { // wait for the pool to be ready if specified (including all its nodes)
_, err = waitPoolReady(ctx, k8sAPI, region, res.ID, d.Timeout(schema.TimeoutCreate))
Expand Down Expand Up @@ -414,7 +419,18 @@ func ResourceK8SPoolRead(ctx context.Context, d *schema.ResourceData, m any) dia
return diag.FromErr(err)
}

_ = d.Set("cluster_id", regional.NewIDString(region, pool.ClusterID))
diags := setPoolState(ctx, d, m, pool, k8sAPI, nodes)

err = identity.SetRegionalIdentity(d, pool.Region, pool.ID)
if err != nil {
return diag.FromErr(err)
}

return diags
}

func setPoolState(ctx context.Context, d *schema.ResourceData, m any, pool *k8s.Pool, k8sAPI *k8s.API, nodes []map[string]any) diag.Diagnostics {
_ = d.Set("cluster_id", regional.NewIDString(pool.Region, pool.ClusterID))
_ = d.Set("name", pool.Name)
_ = d.Set("node_type", pool.NodeType)
_ = d.Set("autoscaling", pool.Autoscaling)
Expand All @@ -440,7 +456,7 @@ func ResourceK8SPoolRead(ctx context.Context, d *schema.ResourceData, m any) dia
_ = d.Set("updated_at", pool.UpdatedAt.Format(time.RFC3339))
_ = d.Set("status", pool.Status)
_ = d.Set("kubelet_args", flattenKubeletArgs(pool.KubeletArgs))
_ = d.Set("region", region)
_ = d.Set("region", pool.Region)
_ = d.Set("zone", pool.Zone)
_ = d.Set("upgrade_policy", poolUpgradePolicyFlatten(pool))
_ = d.Set("public_ip_disabled", pool.PublicIPDisabled)
Expand Down Expand Up @@ -478,7 +494,7 @@ func ResourceK8SPoolRead(ctx context.Context, d *schema.ResourceData, m any) dia
ProjectID: &projectID,
}

privateIPs, err := ipam.GetResourcePrivateIPs(ctx, m, region, opts)
privateIPs, err := ipam.GetResourcePrivateIPs(ctx, m, pool.Region, opts)

switch {
case err == nil:
Expand Down
24 changes: 23 additions & 1 deletion internal/services/k8s/pool_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/scaleway/scaleway-sdk-go/api/k8s/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
Expand Down Expand Up @@ -78,5 +79,26 @@ func DataSourceK8SPoolRead(ctx context.Context, d *schema.ResourceData, m any) d
d.SetId(regionalizedID)
_ = d.Set("pool_id", regionalizedID)

return ResourceK8SPoolRead(ctx, d, m)
req := &k8s.GetPoolRequest{
Region: region,
PoolID: poolID.(string),
}

pool, err := k8sAPI.GetPool(req, scw.WithContext(ctx))
if err != nil {
if httperrors.Is404(err) {
d.SetId("")

return nil
}

return diag.FromErr(err)
}

nodes, err := getNodes(ctx, k8sAPI, pool)
if err != nil {
return diag.FromErr(err)
}

return setPoolState(ctx, d, m, pool, k8sAPI, nodes)
}
6 changes: 6 additions & 0 deletions internal/services/k8s/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ func TestAccPool_Basic(t *testing.T) {
testAccCheckK8SPoolServersAreInPrivateNetwork(tt, "scaleway_k8s_cluster.minimal", "scaleway_k8s_pool.default", "scaleway_vpc_private_network.minimal"),
),
},
{
ResourceName: "scaleway_k8s_pool.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait_for_pool_ready", "size"},
},
},
})
}
Expand Down
Loading
Loading