rancher / terraform-provider-rancher2

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

rancher2_app_v2 values argument not suppressing diff when it should #533

Open armsnyder opened 3 years ago

armsnyder commented 3 years ago

Hello!

I'm using the rancher2_app_v2 resource and specifying the values argument using JSON. Each subsequent plan is showing a diff, because the actual values are stored as YAML in the upstream Rancher API, even though both YAML and JSON are supported in the resource. I suggest suppressing the Terraform diff here. I didn't check, but there may also be a similar issue if the YAML keys are represented in a different order upstream.

Config:

resource "rancher2_app_v2" "cluster_autoscaler" {
  repo_name     = rancher2_catalog_v2.autoscaler.name
  chart_name    = "cluster-autoscaler"
  chart_version = "9.1.0"
  project_id    = rancher2_cluster_sync.this.system_project_id
  cluster_id    = rancher2_cluster_sync.this.cluster_id
  name          = "cluster-autoscaler"
  namespace     = "kube-system"
  values = jsonencode({
    "image" : {
      "tag" : "v1.18.1"
    }
    "autoDiscovery" : {
      "clusterName" : module.eks.cluster_id
    }
    "awsRegion" : data.aws_region.this.name
    "cloudProvider" : "aws"
    "extraArgs" : {
      "expander" : "priority"
      "balance-similar-node-groups" : true
    }
    "rbac" : {
      "create" : true
      "serviceAccount" : {
        "create" : true
        "annotations" : {
          "eks.amazonaws.com/role-arn" : module.iam_assumable_role_admin.this_iam_role_arn
        }
      }
    }
    "priorityClassName" : "system-cluster-critical"
  })
}
rawmind0 commented 3 years ago

Hi @armsnyder , as shown in the docs, values should be passed in yaml format json format is not supported, https://registry.terraform.io/providers/rancher/rancher2/latest/docs/resources/app_v2#values

armsnyder commented 3 years ago

@rawmind0 I think there's a bug in the ValidateFunc then, since this error condition isn't being hit: https://github.com/rancher/terraform-provider-rancher2/blob/fa0c09dbee71145c60707aa2c98c9b42667c15aa/rancher2/schema_app_v2.go#L89-L93

Using jsonencode works fine for us, and it creates the correct YAML in the upstream Rancher app.

rawmind0 commented 3 years ago

@armsnyder , it seems the ghodssyaml.Unmarshal() function is also accepting json format, but the provider is not supporting it, as expressed at the docs. We'll work on checking ghodssyaml functions issues, but yaml format is required at values field.

armsnyder commented 3 years ago

@rawmind0 Hi! I wanted to update you that we are still seeing unexpected diffs, this time using yamlencode as you suggested, even though we have not changed any values.

Using provider version 1.10.6

Gist: https://gist.github.com/armsnyder/f49c5bab1841076c43fb4d70db772ce1

It looks like maybe the provider is doing a string comparison instead of comparing the yaml structures, so it is seeing quoted keys from yamlencode, which are stripped when stored in Rancher upstream.

rawmind0 commented 3 years ago

Hi @armsnyder , the values argument comparison is not done as string, it's done at low level using map[string]interface{} and reflect.DeepEqual, https://github.com/rancher/terraform-provider-rancher2/blob/master/rancher2/schema_app_v2.go#L129

The gist link that you've attached, is showing some real diffs on some values fields, so values diff is not suppressed, Ex:

...
          - core:
          -   resources:
          -     requests:
          -       cpu: 60m
          -       memory: 1Gi
...
          + "core":
          +   "replicas": 1
          +   "resources":
          +     "requests":
          +       "cpu": "60m"
          +       "memory": "1Gi"
...