argoproj-labs / terraform-provider-argocd

Terraform provider for Argo CD
Mozilla Public License 2.0
409 stars 89 forks source link

Directory block not saved in state for multi-source applications #418

Open Ravitejareddykamidi opened 1 month ago

Ravitejareddykamidi commented 1 month ago

Description:

I'm encountering an issue with the argocd Terraform provider (source: oboukili/argocd, version: 6.1.1) when trying to create a multi-source application. Specifically, the directory block for excluding and including files does not get saved in the Terraform state file. As a result, when I re-run terraform plan or terraform apply, it continuously shows that changes are required, even though no actual changes were made.

Terraform Version, ArgoCD Provider Version and ArgoCD Version

Terraform version: v1.3.4
ArgoCD provider version: oboukili/argocd v6.1.1
ArgoCD version: v2.8.0+

Affected Resource(s)

Terraform Configuration Files

resource "argocd_application" "traefik_source" {
  metadata {
    name      = "${lower(var.environment)}-traefik-app"
    namespace = "argocd"
  }

  spec {
    project = "default"

    # First source (Helm)
    source {
      repo_url        = "https://traefik.github.io/charts"
      chart           = "traefik"
      target_revision = var.apps_details["traefik"]["traefik-helm-revision"]
      helm {
        value_files = ["$values/${var.apps_details["traefik"]["values_file_path"]}"]
      }
    }

    # Second source (Git)
    source {
      repo_url        = "git@gitlab.com:XXXXX/XXXXXX/traefik_v2.git"
      target_revision = var.apps_details["traefik"]["traefik_git_revision"]
      ref             = "values"

      directory {
        exclude = "*.json"
        include = var.apps_details["traefik"]["include_files"]
      }
    }

    destination {
      server    = var.api_server_url
      namespace = var.apps_details["traefik"]["namespace"]
    }

    sync_policy {
      automated {
        prune       = false
        self_heal   = false
        allow_empty = false
      }
      sync_options = [
        "PruneLast=true",
        "CreateNamespace=true"
      ]

      retry {
        limit = 4
        backoff {
          duration     = "120s"
          factor       = 2
          max_duration = "3m0s"
        }
      }
    }

    revision_history_limit = 10
  }
}

Debug Output

 ~ source {
                # (3 unchanged attributes hidden)

              + directory {
                  + exclude = "*.json"
                  + include = "{staging-eks-ingress-cots.yaml,staging-eks-ingress-private.yaml,staging-eks-ingress-public.yaml,testing-eks-ingress-public.yaml}"
                }
            }

Panic Output

Steps to Reproduce

  1. Define a multi-source ArgoCD application using the argocd_application resource.
  2. Include the directory block in the second source with exclude and include options.
  3. Run terraform apply to create the application.
  4. Run terraform plan again and observe that the directory block is continuously reported as needing to be added.

Expected Behavior

The directory block should be saved in the Terraform state file after the first terraform apply, and no changes should be detected on subsequent terraform plan or terraform apply runs unless there are actual changes.

Actual Behavior

The directory block does not get saved in the Terraform state file, causing Terraform to continuously detect and attempt to apply changes related to this block on every terraform plan or terraform apply run, even though the ArgoCD UI already reflects the changes.

Terraform Plan shows the following every time:

+ directory {
    + exclude = "*.json"
    + include = "{staging-eks-ingress-cots.yaml,staging-eks-ingress-private.yaml,staging-eks-ingress-public.yaml,testing-eks-ingress-public.yaml}"
}

Important Factoids

The directory block is already present in the ArgoCD UI and contains the correct values. This behavior only affects the directory block, while other resources function as expected. This happens when defining multiple sources within an ArgoCD application.

directory:
      jsonnet: {}
      exclude: '*.json'
      include: >-
        {staging-eks-ingress-cots.yaml,staging-eks-ingress-private.yaml,staging-eks-ingress-public.yaml,testing-eks-ingress-public.yaml}

References

No specific references found yet.

Community Note

mkilchhofer commented 16 hours ago

Hi @Ravitejareddykamidi

Thanks for reporting. ~I remove the flag "bug" and add the "enhancement" label. The provider is still using the Argo CD client in version 2.4. Multi-Source apps were introduced inside Argo CD 2.6.x.~

~So we need to~ ~1. update the argocd library (go.mod)~

  1. make sure multi-source app is correctly represented inside Terraform (state/schema)

Update: I mixed up some things. We are already using the libary version 2.9.x:

require (
    # ..
    github.com/argoproj/argo-cd/v2 v2.9.21
    github.com/argoproj/gitops-engine v0.7.3
    github.com/argoproj/pkg v0.13.7-0.20230627120311-a4dd357b057e
    # ..
)

Then we need to make sure that the multi-source app is correctly reflected into TF. I will add the bug label again.