rancher / terraform-provider-rancher2

Terraform Rancher2 provider
https://www.terraform.io/docs/providers/rancher2/
Mozilla Public License 2.0
253 stars 216 forks source link

[BUG] rancher2_cluster_v2 > machine_selector_config: │ Inappropriate value for attribute "config": string required. #1305

Closed zerofai closed 3 months ago

zerofai commented 5 months ago

Rancher Server Setup

Information about the Cluster

User Information

Provider Information

Describe the bug

With refer to https://registry.terraform.io/providers/rancher/rancher2/latest/docs/resources/cluster_v2 Example: Creating Rancher v2 harvester cluster v2 with harvester cloud provider Terraform cannot validate, report below: │ on main.tf line 33, in resource "rancher2_cluster_v2" "target-rke2-cluster": │ 33: config = { │ 34: cloud-provider-config = file(var.harvester-cloud-provider-kubeconfig) │ 35: cloud-provider-name = "harvester" │ 36: } │ │ Inappropriate value for attribute "config": string required.

To Reproduce

Use the example to creating Rancher v2 harvester cluster v2 with harvester cloud provider

Actual Result

Terraform validation failed

Expected Result

RKE2 cluster should provision properly with Harvester Cloud Provider install

zerofai commented 5 months ago

Issue still exist in v 4.0.0. provider

`Initializing provider plugins...

SebPlv commented 4 months ago

Hi, I encountered the same issue. Simply upgrading from rancher2 provider from 3.1.1 to 4.0.0 and I got the same error :

│ Error: Incorrect attribute value type │ │ on create_cluster_rke2.tf line 185, in resource "rancher2_cluster_v2" "moncluster": │ 185: config = { │ 186: system-default-registry = "myregistry.foo.com", │ 187: protect-kernel-defaults = false, │ 188: cloud-provider-name = "rancher-vsphere" │ 189: } │ │ Inappropriate value for attribute "config": string required.

I managed to fix this by converting the attribute to this format :

config = <<EOF system-default-registry: "myregistry.foo.com" protect-kernel-defaults: false cloud-provider-name: "rancher-vsphere" EOF

If this is the only supported syntax, the documentation should be updated. This syntax is mentioned in only one example (https://registry.terraform.io/providers/rancher/rancher2/latest/docs/resources/cluster_v2#create-a-cluster-with-machine-global-config-or-machine-selector-config)

Edit: It must be related to PR 1223

mallardduck commented 3 months ago

@SebPlv - I think that's a great fix/workaround, since it seems like the schema did change going from 3.1.1 -> 4.x.

One note is that I think the PR that caused the change is different than the one you linked. The one linked reverts it from a string field, to a map field. Notably, that change only affects clusterV2RKEConfigSystemConfigFieldsV0 which is the function preserved as part of the schema migration method terraform provides.

Essentially that just ensures that the state format is updated correctly, however AFAIK end-users still need to update their plans format to match the new syntax manually.


Again that's very close, but long story short the PR responsible for the new behavior (and not the migrations) is here: https://github.com/rancher/terraform-provider-rancher2/pull/1181 . And overall I believe the "official answer" (which this is not) will be that plans should be updated to use strings.

SebPlv commented 3 months ago

@mallardduck Thanks for these precisions and pointing out the right PR !

albinsun commented 1 month ago

Hi, Does this fix already included in any releases?

albinsun commented 2 weeks ago

Still get the same error in v4.1.0

$ ../bin/terraform -version
Terraform v1.8.5
on linux_amd64
+ provider registry.terraform.io/rancher/rancher2 v4.1.0

image

mallardduck commented 2 weeks ago

@albinsun - this issue is closed and I believe @matttrach did this because the correct solution is to do what Seb suggested. Please adjust your terraform plan accordingly, so that the config value is a string like:

config = <<EOF
system-default-registry: "myregistry.foo.com"
protect-kernel-defaults: false
cloud-provider-name: "rancher-vsphere"
EOF
albinsun commented 1 week ago

@albinsun - this issue is closed and I believe @matttrach did this because the correct solution is to do what Seb suggested. Please adjust your terraform plan accordingly, so that the config value is a string like:

Thank you @mallardduck and @matttrach, I just try to follow an example in latest doc to set machine_selector_config: https://registry.terraform.io/providers/rancher/rancher2/latest/docs/resources/cluster_v2#create-a-node-driver-cluster-with-harvester-as-both-the-infrastructure-provider-and-cloud-provider

image

Is the problem from the file() or path.module? If so, how to leverage them in the string form config?


Update

It works by jsonencode the original object.

    ...
    machine_selector_config {
      config = jsonencode({
        cloud-provider-config = file("${path.module}/kubeconfig")
        cloud-provider-name = "harvester"
      })
    }
    ...
mallardduck commented 5 days ago

@matttrach - Seems like we may have a docs issue, maybe the examples didn't get updated for the change?