In our Terraform provider, the pf9_cluster
resource supports the addons
attribute, which can include configurations for various addons like coredns. The addon, in particular, has the following schema:
resource "pf9_cluster" "example" {
name = "example"
addons = {
coredns = {
version = "1.11.1"
params = {
"dnsDomain": "cluster.local"
}
}
}
}
The addons
and the coredns
sub-attribute is of type single_nested
. The version
and params
are marked as computed_optional
, meaning they can be set by the backend API or explicitly by the practitioner in the Terraform configuration.
However, if the practitioner attempts to create the pf9_cluster
resource without specifying the coredns
attribute, like this:
resource "pf9_cluster" "example" {
name = "tf-cluster-81"
addons = {}
}
The provider returns an error "Provider produced inconsistent result after apply"
.
provider produced an unexpected new value: .addons.coredns: was null, but now
│ cty.ObjectVal(map[string]cty.Value{"params":cty.MapVal(map[string]cty.Value{"dnsDomain":cty.StringVal("cluster.local"), "dnsMemoryLimit":cty.StringVal("170Mi")}), "phase":cty.StringVal(""),
│ "version":cty.StringVal("1.11.1")}).
This should not have happened because the coredns attribute is marked as computed_optional
. The backend API automatically enables it with default settings, hence the state contains its actual value but the plan does not.
How can this error be avoided? Let’s discuss strategies and best practices for handling scenarios associated with nested attributes in Terraform. Feel free to suggest an alternate schema for this use case.