Skip to content

Plan fails when updating arrays #18

@silvpol

Description

@silvpol

Issue

I have come across an issue when updating BackendConfig object in GCP. When trying to add values to an array-type field, the request fails with an odd error message about request being invalid.

Based on my investigation, when the array-type field is empty, it gets deleted during apply. If you then try to add a value to it, the json patch that plugin generates, fails due to array field missing. The plugin should check if array field is missing, create it and then execute the original patch.

Plugin just generates this op:

[{"path":"/spec/customRequestHeaders/headers/0","value":"X-Client-Region:{client_region_subdivision}","op":"add"}]

but when headers key is missing it should do this first to prevent failed request:

[{"path":"/spec/customRequestHeaders/headers","value":[],"op":"add"}]

Steps to replicate

Option 1

  1. Comment out custom_request_headers section from example
  2. Apply plan
  3. Uncomment custom_request_headers section
  4. Apply plan

Option 2

  1. Create it using YAML
  2. Empty the headers array in TF
  3. Apply plan - it will execute [{"path":"/spec/customRequestHeaders/headers","op":"remove"}]
  4. Add the value back in
  5. Apply plan

Workaround

  1. Create headers key manually:
kubectl patch backendconfig www --type='json' -p='[{"path":"/spec/customRequestHeaders/headers","value":[],"op":"add"}]'
  1. Apply the plan

Test setup

I have tested with versions 1.0.6 and 1.0.2.

Example BackendConfig

resource "k8s_cloud_google_com_v1_backend_config" "www" {

  metadata {
    name      = "www"
    namespace = "test"
  }

  spec {
    timeout_sec = 600

    connection_draining {
      draining_timeout_sec = 121
    }

    cdn {
      enabled = true

      cache_policy {
        include_host         = true
        include_protocol     = true
        include_query_string = true
      }
    }

    custom_request_headers {
      headers = ["X-Client-Region: {client_region_subdivision}"]
    }
  }
}

Corresponding YAML from API

apiVersion: cloud.google.com/v1
kind: BackendConfig
metadata:
  name: www
  namespace: test
spec:
  cdn:
    cachePolicy:
      includeHost: true
      includeProtocol: true
      includeQueryString: true
    enabled: true
  connectionDraining:
    drainingTimeoutSec: 121
  customRequestHeaders:
    headers:
    - 'X-Client-Region: {client_region_subdivision}'
  timeoutSec: 600

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions