Skip to content

Error when updating multiple templates that use the same files #17442

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

Open
michvllni opened this issue Apr 17, 2025 · 2 comments
Open

Error when updating multiple templates that use the same files #17442

michvllni opened this issue Apr 17, 2025 · 2 comments
Labels
s2 Broken use cases or features (with a workaround). Only humans may set this.

Comments

@michvllni
Copy link

michvllni commented Apr 17, 2025

I have multiple templates that all use the same files (but have different vars).

When modifying more than one simultanously I always get an error from the file API on apply:

Log
2025-04-17T05:33:51.0920085Z coderd_template.templates["kubernetes"]: Modifying... [id=24f0a052-c445-42d1-a220-76394b0d2340]
2025-04-17T05:33:51.1042285Z coderd_template.templates["powershell"]: Modifying... [id=4022a9b8-ce13-41d8-ab4f-11bcc8f906a7]
2025-04-17T05:33:51.1105831Z coderd_template.templates["debian"]: Modifying... [id=5655ead2-2e39-477b-b944-9e8073d32ba8]
2025-04-17T05:33:51.1616036Z 2025-04-17T05:33:51.160Z [ERROR] provider.terraform-provider-coderd_v0.0.10: Response contains error diagnostic: tf_resource_type=coderd_template @module=sdk.proto diagnostic_summary="Provisioner Error" tf_proto_version=6.8 @caller=github.com/hashicorp/terraform-plugin-go@v0.26.0/tfprotov6/internal/diag/diagnostics.go:58
2025-04-17T05:33:51.1619883Z   diagnostic_detail=
2025-04-17T05:33:51.1622922Z   | failed to upload directory: POST https://<REDACTED>/api/v2/files: unexpected status code 500: Internal error saving file.
2025-04-17T05:33:51.1625917Z   | \tError: pq: duplicate key value violates unique constraint "files_hash_created_by_key"
2025-04-17T05:33:51.1628940Z    diagnostic_severity=ERROR tf_provider_addr=registry.terraform.io/coder/coderd tf_req_id=e0fe0334-b637-7894-6a23-46f1e5479bdb tf_rpc=ApplyResourceChange timestamp=2025-04-17T05:33:51.160Z
2025-04-17T05:33:51.1632235Z 2025-04-17T05:33:51.162Z [ERROR] provider.terraform-provider-coderd_v0.0.10: Response contains error diagnostic: tf_rpc=ApplyResourceChange @caller=github.com/hashicorp/terraform-plugin-go@v0.26.0/tfprotov6/internal/diag/diagnostics.go:58
2025-04-17T05:33:51.1636138Z   diagnostic_detail=
2025-04-17T05:33:51.1639451Z   | failed to upload directory: POST https://<REDACTED>/api/v2/files: unexpected status code 500: Internal error saving file.
2025-04-17T05:33:51.1640899Z   | \tError: pq: duplicate key value violates unique constraint "files_hash_created_by_key"
2025-04-17T05:33:51.1643867Z    diagnostic_severity=ERROR tf_req_id=78e54071-0a68-ff3f-99cc-c6db34233240 tf_resource_type=coderd_template @module=sdk.proto diagnostic_summary="Provisioner Error" tf_proto_version=6.8 tf_provider_addr=registry.terraform.io/coder/coderd timestamp=2025-04-17T05:33:51.161Z
2025-04-17T05:33:51.1665104Z 2025-04-17T05:33:51.166Z [ERROR] vertex "coderd_template.templates[\"debian\"]" error: Provisioner Error
2025-04-17T05:33:51.1667533Z 2025-04-17T05:33:51.166Z [ERROR] vertex "coderd_template.templates[\"powershell\"]" error: Provisioner Error
2025-04-17T05:33:59.9327162Z coderd_template.templates["kubernetes"]: Modifications complete after 9s [id=24f0a052-c445-42d1-a220-76394b0d2340]
2025-04-17T05:34:00.0456919Z ╷
2025-04-17T05:34:00.0459249Z │ Error: Provisioner Error
2025-04-17T05:34:00.0459761Z │ 
2025-04-17T05:34:00.0460095Z │   with coderd_template.templates["powershell"],
2025-04-17T05:34:00.0460513Z │   on templates.tf line 13, in resource "coderd_template" "templates":
2025-04-17T05:34:00.0460923Z │   13: resource "coderd_template" "templates" {
2025-04-17T05:34:00.0461309Z │ 
2025-04-17T05:34:00.0461605Z │ failed to upload directory: POST
2025-04-17T05:34:00.0462009Z │ https://<REDACTED>/api/v2/files: unexpected status code 500:
2025-04-17T05:34:00.0462358Z │ Internal error saving file.
2025-04-17T05:34:00.0462718Z │ 	Error: pq: duplicate key value violates unique constraint
2025-04-17T05:34:00.0463067Z │ "files_hash_created_by_key"
2025-04-17T05:34:00.0463319Z │ 
2025-04-17T05:34:00.0463549Z ╵
2025-04-17T05:34:00.0463772Z ╷
2025-04-17T05:34:00.0464093Z │ Error: Provisioner Error
2025-04-17T05:34:00.0464362Z │ 
2025-04-17T05:34:00.0464671Z │   with coderd_template.templates["debian"],
2025-04-17T05:34:00.0465078Z │   on templates.tf line 13, in resource "coderd_template" "templates":
2025-04-17T05:34:00.0465471Z │   13: resource "coderd_template" "templates" {
2025-04-17T05:34:00.0465736Z │ 
2025-04-17T05:34:00.0466020Z │ failed to upload directory: POST
2025-04-17T05:34:00.0466422Z │ https://<REDACTED>/api/v2/files: unexpected status code 500:
2025-04-17T05:34:00.0466762Z │ Internal error saving file.
2025-04-17T05:34:00.0467134Z │ 	Error: pq: duplicate key value violates unique constraint
2025-04-17T05:34:00.0467459Z │ "files_hash_created_by_key"
2025-04-17T05:34:00.0467709Z │ 
2025-04-17T05:34:00.0467932Z ╵

As seen in the log, the first template is successfully modified, but the other two fail.

If I run terraform plan && terraform apply again, the next template is okay and only one is failed.
If I run it again afterwards, all succeed. It seems that there can be only be one modify at the same time?

Template definition
resource "coderd_template" "templates" {
  for_each    = local.templates
  name        = each.value.name
  description = each.value.description
  icon        = each.value.icon

  default_ttl_ms = 7200000

  versions = [
    {
      name      = data.external.current_commit[each.key].result.commit
      message   = data.external.current_commit[each.key].result.message
      directory = "./template"
      active    = true
      tf_vars = concat(local.default_vars, [
        {
          name  = "ado_projects"
          value = jsonencode(each.value.ado_projects)
        },
        {
          name  = "default_ado_project"
          value = each.value.default_ado_project
        },
        {
          name  = "image_variants"
          value = jsonencode(each.value.image_variants)
        },
        {
          name  = "default_image_variant"
          value = each.value.default_image_variant
        },
        {
          name  = "repository"
          value = each.value.image_repository
        },
        {
          name  = "bash_aliases"
          value = jsonencode(each.value.bash_aliases)
        }
      ])
    }
  ]
}
Planned changes
Terraform will perform the following actions:

  # coderd_template.templates["debian"] will be updated in-place
  ~ resource "coderd_template" "templates" {
      ~ display_name                      = "debian" -> (known after apply)
        id                                = "5655ead2-2e39-477b-b944-9e8073d32ba8"
      ~ max_port_share_level              = "public" -> (known after apply)
        name                              = "debian"
      ~ organization_id                   = "<REDACTED>" -> (known after apply)
      ~ versions                          = [
          ~ {
              ~ id             = "e8bb03e4-d363-435f-9358-c0404b111edc" -> (known after apply)
              ~ message        = "Merged PR 17172: fix: spaces in alias and alias read #40634" -> "Merged PR 17174: fix: defaults #40644"
              ~ name           = "4d1ac972b5f9bb934f1e15ebef0536ffa24d2220" -> "8da2d897e1d9c8422f10d41bb6088a564ee9c4df"
              ~ tf_vars        = [
                  - {
                      - name  = "ado_projects" -> null
                      - value = jsonencode(
                            {
                              - Debian-Project-1     = {
                                  - image_variant = "bullseye"
                                  - repository    = "Debian-Project-1"
                                }
                              - Debian-Project-2 = {
                                  - image_variant = "bullseye"
                                  - repository    = "Debian-Project-2"
                                }
                            }
                        ) -> null
                    },
                  + {
                      + name  = "ado_projects"
                      + value = jsonencode(
                            {
                              + Debian-Project-1     = {
                                  + image_variant = "Default"
                                  + repository    = "Debian-Project-1"
                                }
                              + Debian-Project-2 = {
                                  + image_variant = "Default"
                                  + repository    = "Debian-Project-2"
                                }
                            }
                        )
                    },
                    # (9 unchanged elements hidden)
                ]
                # (3 unchanged attributes hidden)
            },
        ]
        # (14 unchanged attributes hidden)
    }

  # coderd_template.templates["kubernetes"] will be updated in-place
  ~ resource "coderd_template" "templates" {
      ~ display_name                      = "kubernetes" -> (known after apply)
        id                                = "24f0a052-c445-42d1-a220-76394b0d2340"
      ~ max_port_share_level              = "public" -> (known after apply)
        name                              = "kubernetes"
      ~ organization_id                   = "<REDACTED>" -> (known after apply)
      ~ versions                          = [
          ~ {
              ~ id             = "10d894a4-c968-41ae-80a8-64bce744a72e" -> (known after apply)
              ~ message        = "Merged PR 17172: fix: spaces in alias and alias read #40634" -> "Merged PR 17174: fix: defaults #40644"
              ~ name           = "4d1ac972b5f9bb934f1e15ebef0536ffa24d2220" -> "8da2d897e1d9c8422f10d41bb6088a564ee9c4df"
              ~ tf_vars        = [
                  - {
                      - name  = "ado_projects" -> null
                      - value = jsonencode(
                            {
                              - Gitops-Project = {
                                  - image_variant = "bullseye"
                                  - repository    = "Gitops-Project"
                                }
                            }
                        ) -> null
                    },
                  + {
                      + name  = "ado_projects"
                      + value = jsonencode(
                            {
                              + Gitops-Project = {
                                  + image_variant = "Default"
                                  + repository    = "Gitops-Project"
                                }
                            }
                        )
                    },
                    # (9 unchanged elements hidden)
                ]
                # (3 unchanged attributes hidden)
            },
        ]
        # (14 unchanged attributes hidden)
    }

  # coderd_template.templates["powershell"] will be updated in-place
  ~ resource "coderd_template" "templates" {
      ~ display_name                      = "powershell" -> (known after apply)
        id                                = "4022a9b8-ce13-41d8-ab4f-11bcc8f906a7"
      ~ max_port_share_level              = "public" -> (known after apply)
        name                              = "powershell"
      ~ organization_id                   = "<REDACTED>" -> (known after apply)
      ~ versions                          = [
          ~ {
              ~ id             = "24c31fc8-d197-476a-92d7-378b25bfb9b7" -> (known after apply)
              ~ message        = "Merged PR 17172: fix: spaces in alias and alias read #40634" -> "Merged PR 17174: fix: defaults #40644"
              ~ name           = "4d1ac972b5f9bb934f1e15ebef0536ffa24d2220" -> "8da2d897e1d9c8422f10d41bb6088a564ee9c4df"
              ~ tf_vars        = [
                  - {
                      - name  = "ado_projects" -> null
                      - value = jsonencode(
                            {
                              - PS-Project-1   = {
                                  - image_variant = "default"
                                  - repository    = "PS-Project-1"
                                }
                              - PS-Project-2 = {
                                  - image_variant = "default"
                                  - repository    = "PS-Project-2"
                                }
                            }
                        ) -> null
                    },
                  + {
                      + name  = "ado_projects"
                      + value = jsonencode(
                            {
                              + PS-Project-1   = {
                                  + image_variant = "Default"
                                  + repository    = "PS-Project-1"
                                }
                              + PS-Project-2 = {
                                  + image_variant = "Default"
                                  + repository    = "PS-Project-2"
                                }
                            }
                        )
                    },
                    # (9 unchanged elements hidden)
                ]
                # (3 unchanged attributes hidden)
            },
        ]
        # (14 unchanged attributes hidden)
    }

Plan: 0 to add, 3 to change, 0 to destroy.

Edit: Added the plan and collapsed template
Edit Edit: Added timestamps to the log

@ethanndickson ethanndickson transferred this issue from coder/terraform-provider-coderd Apr 17, 2025
@ethanndickson
Copy link
Member

ethanndickson commented Apr 17, 2025

Moving this to coder/coder as it sounds reproducible without the provider. Though I won't rule out the possibility of making provider changes to accommodate for it, if we need to.

@michvllni
Copy link
Author

I think it occured in context of the provider as in this case it sends three simultanous requests to the API. But I agree, it is most likely an issue with the API

@matifali matifali added the s2 Broken use cases or features (with a workaround). Only humans may set this. label Apr 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
s2 Broken use cases or features (with a workaround). Only humans may set this.
Projects
None yet
Development

No branches or pull requests

3 participants