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

🐛 Fix the wait conditions issue in the kubernetes_manifest resource #2173

Merged
merged 6 commits into from
Jul 5, 2023

Conversation

arybolovlev
Copy link
Contributor

@arybolovlev arybolovlev commented Jul 3, 2023

Description

This PR fixes an issue in the kubernetes_manifest manifest when it is used with the wait conditions. This happens because the provider works with the object that the Kubernetes cluster returns after creation and doesn't update the object(request a newer version) once the conditions are met. Because of that the initial object(the one that the provider receives right after the create call) ends up in the TF state and some attributes are not available for the reference. A good example here can be metadata.labels and metadata.annotations that controllers assign to the Kubernetes objects after their creation. metadata.labels and metadata.annotations are often used in the wait conditions and as an input for other resources to be created or outputs for further usage.

Changes:

  • Move the wait condition code right after the resource creation to fail earlier in the case of any issue that occurs during the waiting time.
  • Add read operation when the wait conditions are met successfully.

For example, the following code will be executed with the "Invalid index" error message:

resource "kubernetes_manifest" "this" {
  manifest = {
    apiVersion = "v1"
    kind       = "Secret"
    metadata = {
      name      = "this"
      namespace = "default"

      annotations = {
        "kubernetes.io/service-account.name" = "default"
      }
    }
    type = "kubernetes.io/service-account-token"
  }
  wait {
    fields = {
      "metadata.annotations[\"kubernetes.io/service-account.uid\"]" = "^.*$",
    }
  }

  timeouts {
    create = "10s"
  }
}

output "this" {
  value = kubernetes_manifest.this.object.metadata.annotations["kubernetes.io/service-account.uid"]
}

That happens because the annotation kubernetes.io/service-account.uid is not available right after the resource creation and added by a controller for some time(that is why the condition is used here). The second apply will be successful.

Acceptance tests

  • Have you added an acceptance test for the functionality being added?
  • Have you run the acceptance tests on this branch?

Output from acceptance testing:

$ go test -count=1 -tags acceptance -v ./manifest/test/acceptance/... -timeout 120m -run TestKubernetesManifest_WaitFields_Annotations

2023/07/03 17:23:03 Testing against Kubernetes API version: v1.26.6
=== RUN   TestKubernetesManifest_WaitFields_Annotations
2023-07-03T17:23:04.036+0200 [INFO]  [ApplyResourceChange][Wait] Waiting until ready...

2023-07-03T17:23:04.038+0200 [INFO]  [ApplyResourceChange][Wait] Done waiting.

--- PASS: TestKubernetesManifest_WaitFields_Annotations (1.05s)
PASS
ok  	github.com/hashicorp/terraform-provider-kubernetes/manifest/test/acceptance	1.867s

Release Note

Release note for CHANGELOG:

`resource/kubernetes_manifest`: fix an issue with the `kubernetes_manifest` resource, where an object fails to update correctly when employing wait conditions and thus some attributes are not available for the reference after creation.

References

Fix: #1957

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

@github-actions github-actions bot added the size/S label Jul 3, 2023
@@ -36,7 +40,7 @@ resource "google_service_account" "default" {

resource "google_container_cluster" "primary" {
provider = google-beta
name = "tf-acc-test-${random_id.cluster_name.hex}"
name = var.cluster_name != "" ? var.cluster_name : "tf-acc-test-${random_id.cluster_name.hex}"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change is not related to the fix directly, however, I found this useful to configure the cluster name for manual testing purposes while working on this issue.

@github-actions github-actions bot added size/L and removed size/S labels Jul 3, 2023
@arybolovlev arybolovlev force-pushed the fix-manifest-wait-conditions branch 2 times, most recently from 7d2c7e1 to 161995e Compare July 3, 2023 15:06
@arybolovlev arybolovlev force-pushed the fix-manifest-wait-conditions branch from 2bbdbd7 to b69b7f4 Compare July 4, 2023 07:02
@arybolovlev arybolovlev marked this pull request as ready for review July 4, 2023 07:02
@arybolovlev arybolovlev requested a review from a team as a code owner July 4, 2023 07:02
Copy link
Collaborator

@jrhouston jrhouston left a comment

Choose a reason for hiding this comment

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

Looks good, thanks for fixing this 🚀

@arybolovlev arybolovlev merged commit a24d5d9 into main Jul 5, 2023
@arybolovlev arybolovlev deleted the fix-manifest-wait-conditions branch July 5, 2023 07:37
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

On kubernetes_manifest, the returned object field content is not updated after the wait completion
2 participants