Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete node template custom_label #244

Merged
merged 9 commits into from
Nov 28, 2023
Merged
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
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,22 @@ generate-docs:
go install github.com/hashicorp/terraform-plugin-docs/cmd/[email protected]
tfplugindocs generate --rendered-provider-name "CAST AI" --ignore-deprecated

.PHONY: generate-all
generate-all: generate-sdk generate-docs

.PHONY: build
build: init-examples
build: generate-sdk
build:
@echo "==> Building terraform-provider-castai"
go build

.PHONY: lint
lint:
@echo "==> Running lint"
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
golangci-lint run

.PHONY: test
test:
@echo "==> Running tests"
Expand Down
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,46 @@ Example of node template import:
terraform import castai_node_template.default_by_castai 105e6fa3-20b1-424e-v589-9a64d1eeabea/default-by-castai
```

Migrating from 5.x.x to 6.x.x
---------------------------

Version 6.x.x changed:
* Removed `custom_label` attribute in `castai_node_template` resource. Use `custom_labels` instead.

Old configuration:
```terraform
module "castai-aks-cluster" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example should be for provider itself not for module

node_templates = {
spot_tmpl = {
custom_label = {
key = "custom-label-key-1"
value = "custom-label-value-1"
}
}
}
}
```

New configuration:
```terraform
module "castai-aks-cluster" {
node_templates = {
spot_tmpl = {
custom_labels = {
custom-label-key-1 = "custom-label-value-1"
}
}
}
}
```

For more information for `castai-aks-cluster` module follow:
https://github.com/castai/terraform-castai-aks/blob/main/README.md#migrating-from-2xx-to-3xx
If you have used `castai-eks-cluster` or other modules follow:
https://github.com/castai/terraform-castai-eks-cluster/blob/main/README.md#migrating-from-6xx-to-7xx
If you have used `castai-gke-cluster` or other modules follow:
https://github.com/castai/terraform-castai-gke-cluster/blob/main/README.md#migrating-from-3xx-to-4xx


Developing the provider
---------------------------
Expand Down
2 changes: 1 addition & 1 deletion castai/resource_node_configuration_eks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestAccResourceNodeConfiguration_eks(t *testing.T) {
resourceName := "castai_node_configuration.test"
clusterName := "core-tf-acc"

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckNodeConfigurationDestroy,
Expand Down
84 changes: 7 additions & 77 deletions castai/resource_node_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const (
FieldNodeTemplateConfigurationId = "configuration_id"
FieldNodeTemplateConstraints = "constraints"
FieldNodeTemplateCustomInstancesEnabled = "custom_instances_enabled"
FieldNodeTemplateCustomLabel = "custom_label"
FieldNodeTemplateCustomLabels = "custom_labels"
FieldNodeTemplateCustomTaints = "custom_taints"
FieldNodeTemplateEnableSpotDiversity = "enable_spot_diversity"
Expand Down Expand Up @@ -317,37 +316,13 @@ func resourceNodeTemplate() *schema.Resource {
},
},
},
FieldNodeTemplateCustomLabel: {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
FieldKey: {
Required: true,
Type: schema.TypeString,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringIsNotWhiteSpace),
Description: "Label key to be added to nodes created from this template.",
},
FieldValue: {
Required: true,
Type: schema.TypeString,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringIsNotWhiteSpace),
Description: "Label value to be added to nodes created from this template.",
},
},
},
Description: "Custom label key/value to be added to nodes created from this template.",
Deprecated: "Remove the use of `custom_label` field. The custom labels should be set through the `custom_labels` field.",
},
FieldNodeTemplateCustomLabels: {
ivanstankovcast marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "Custom labels to be added to nodes created from this template. " +
"If the field `custom_label` is present, the value of `custom_labels` will be ignored.",
Description: "Custom labels to be added to nodes created from this template.",
},
FieldNodeTemplateCustomTaints: {
Type: schema.TypeList,
Expand Down Expand Up @@ -448,9 +423,6 @@ func resourceNodeTemplateRead(ctx context.Context, d *schema.ResourceData, meta
return diag.FromErr(fmt.Errorf("setting constraints: %w", err))
}
}
if err := d.Set(FieldNodeTemplateCustomLabel, flattenCustomLabel(nodeTemplate.CustomLabel)); err != nil {
return diag.FromErr(fmt.Errorf("setting custom label: %w", err))
}
if err := d.Set(FieldNodeTemplateCustomLabels, nodeTemplate.CustomLabels.AdditionalProperties); err != nil {
return diag.FromErr(fmt.Errorf("setting custom labels: %w", err))
}
Expand Down Expand Up @@ -602,7 +574,6 @@ func updateNodeTemplate(ctx context.Context, d *schema.ResourceData, meta any, s
FieldNodeTemplateShouldTaint,
FieldNodeTemplateConfigurationId,
FieldNodeTemplateRebalancingConfigMinNodes,
FieldNodeTemplateCustomLabel,
FieldNodeTemplateCustomLabels,
FieldNodeTemplateCustomTaints,
FieldNodeTemplateCustomInstancesEnabled,
Expand Down Expand Up @@ -630,10 +601,6 @@ func updateNodeTemplate(ctx context.Context, d *schema.ResourceData, meta any, s
req.ConfigurationId = toPtr(v.(string))
}

if v, ok := d.Get(FieldNodeTemplateCustomLabel).([]any); ok && len(v) > 0 {
req.CustomLabel = toCustomLabel(v[0].(map[string]any))
}

if req.CustomLabel == nil {
if v, ok := d.Get(FieldNodeTemplateCustomLabels).(map[string]any); ok && len(v) > 0 {
customLabels := map[string]string{}
Expand Down Expand Up @@ -713,20 +680,14 @@ func resourceNodeTemplateCreate(ctx context.Context, d *schema.ResourceData, met
}
}

if v, ok := d.Get(FieldNodeTemplateCustomLabel).([]any); ok && len(v) > 0 {
req.CustomLabel = toCustomLabel(v[0].(map[string]any))
}

if req.CustomLabel == nil {
if v, ok := d.Get(FieldNodeTemplateCustomLabels).(map[string]any); ok && len(v) > 0 {
customLabels := map[string]string{}

for k, v := range v {
customLabels[k] = v.(string)
}
if v, ok := d.Get(FieldNodeTemplateCustomLabels).(map[string]any); ok && len(v) > 0 {
customLabels := map[string]string{}

req.CustomLabels = &sdk.NodetemplatesV1NewNodeTemplate_CustomLabels{AdditionalProperties: customLabels}
for k, v := range v {
customLabels[k] = v.(string)
}

req.CustomLabels = &sdk.NodetemplatesV1NewNodeTemplate_CustomLabels{AdditionalProperties: customLabels}
}

if v, ok := d.Get(FieldNodeTemplateCustomTaints).([]any); ok && len(v) > 0 {
Expand Down Expand Up @@ -858,22 +819,6 @@ func nodeTemplateStateImporter(ctx context.Context, d *schema.ResourceData, meta
return nil, fmt.Errorf("failed to find node template with the following name: %v", id)
}

func toCustomLabel(obj map[string]any) *sdk.NodetemplatesV1Label {
if obj == nil {
return nil
}

out := &sdk.NodetemplatesV1Label{}
if v, ok := obj[FieldKey]; ok && v != "" {
out.Key = toPtr(v.(string))
}
if v, ok := obj[FieldValue]; ok && v != "" {
out.Value = toPtr(v.(string))
}

return out
}

func toCustomTaintsWithOptionalEffect(objs []map[string]any) *[]sdk.NodetemplatesV1TaintWithOptionalEffect {
if len(objs) == 0 {
return nil
Expand All @@ -900,21 +845,6 @@ func toCustomTaintsWithOptionalEffect(objs []map[string]any) *[]sdk.Nodetemplate
return out
}

func flattenCustomLabel(label *sdk.NodetemplatesV1Label) []map[string]string {
if label == nil {
return nil
}

m := map[string]string{}
if v := label.Key; v != nil {
m[FieldKey] = toString(v)
}
if v := label.Value; v != nil {
m[FieldValue] = toString(v)
}
return []map[string]string{m}
}

func flattenCustomTaints(taints *[]sdk.NodetemplatesV1Taint) []map[string]string {
if taints == nil {
return nil
Expand Down
3 changes: 0 additions & 3 deletions castai/resource_node_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ constraints.0.spot_interruption_predictions_type = aws-rebalance-recommendations
constraints.0.storage_optimized = false
constraints.0.use_spot_fallbacks = false
custom_instances_enabled = true
custom_label.# = 0
custom_labels.% = 2
custom_labels.key-1 = value-1
custom_labels.key-2 = value-2
Expand Down Expand Up @@ -372,7 +371,6 @@ func TestAccResourceNodeTemplate_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "is_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "should_taint", "true"),
resource.TestCheckResourceAttr(resourceName, "custom_instances_enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "custom_label.#", "0"),
resource.TestCheckResourceAttr(resourceName, "custom_labels.%", "2"),
resource.TestCheckResourceAttr(resourceName, "custom_labels."+rName+"-label-key-1", rName+"-label-value-1"),
resource.TestCheckResourceAttr(resourceName, "custom_labels."+rName+"-label-key-2", rName+"-label-value-2"),
Expand Down Expand Up @@ -422,7 +420,6 @@ func TestAccResourceNodeTemplate_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "is_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "should_taint", "true"),
resource.TestCheckResourceAttr(resourceName, "custom_instances_enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "custom_label.#", "0"),
resource.TestCheckResourceAttr(resourceName, "custom_labels.%", "2"),
resource.TestCheckResourceAttr(resourceName, "custom_labels."+rName+"-label-key-1", rName+"-label-value-1"),
resource.TestCheckResourceAttr(resourceName, "custom_labels."+rName+"-label-key-2", rName+"-label-value-2"),
Expand Down
2 changes: 1 addition & 1 deletion castai/resource_rebalancing_job_eks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestAccResourceRebalancingJob_eks(t *testing.T) {
rName := fmt.Sprintf("%v-rebalancing-job-%v", ResourcePrefix, acctest.RandString(8))
clusterName := "core-tf-acc"
resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },

ProviderFactories: providerFactories,
Expand Down
Loading
Loading