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

Feature Request: Add support for opentofu #1861

Open
mcwarman opened this issue Jan 11, 2024 · 7 comments
Open

Feature Request: Add support for opentofu #1861

mcwarman opened this issue Jan 11, 2024 · 7 comments
Labels
enhancement New feature or request resource-opentofu

Comments

@mcwarman
Copy link
Member

Is your feature request related to a problem?

OpenTofu went GA recently.

With terraform support added in: #1554, #1576, and #1621.

Similar support for opentofu could be added.

It would probably be a case of working our the similarities and refactoring a few parts out to a shared utils around HCL processing vs Terraform specific parts.

Also identifying where alternatives should be used, for example OpenTofu have a fork https://github.com/opentofu/registry-address which is their alternative to https://github.com/hashicorp/terraform-registry-address

Solution you'd like

No response

Alternatives you've considered

No response

Anything else?

No response

@quotidian-ennui
Copy link

Currently updatecli does not work with alternative registries with respect to terraform lock files (the upstream tfupdate library is the culprit it appears).

Steps to reproduce

bsh ❯ cd terraform
bsh ❯ rm -rf .terraform .terraform.lock.hcl
bsh ❯ tofu init
...
OpenTofu has been successfully initialized!
bsh ❯ cat .terraform.lock.hcl (the original .terraform.lock.hcl has registry.terraform.io/)
...
provider "registry.opentofu.org/hashicorp/kubernetes" {
...
bsh ❯ export GITHUB_TOKEN=$(gh auth token)
bsh ❯ updatecli apply -c updatecli.d/tf-k8s.yaml
...
tf-provider-k8s
---------------
⚠ - "kubernetes" updated from "2.21.0" to "2.25.2" in file "terraform/versions.tf"

tf-lock-k8s
-----------
2024/01/11 17:28:16 [DEBUG] providerIndex.createProviderVersion: hashicorp/kubernetes, 2.25.2, linux_amd64
2024/01/11 17:28:16 [DEBUG] Client.ProviderPackageMetadata: GET https://registry.terraform.io/v1/providers/hashicorp/kubernetes/2.25.2/download/linux/amd64
2024/01/11 17:28:16 [DEBUG] ProviderDownloaderClient.download: GET https://releases.hashicorp.com/terraform-provider-kubernetes/2.25.2/terraform-provider-kubernetes_2.25.2_linux_amd64.zip
2024/01/11 17:28:16 [DEBUG] ProviderDownloaderClient.download: GET https://releases.hashicorp.com/terraform-provider-kubernetes/2.25.2/terraform-provider-kubernetes_2.25.2_SHA256SUMS
2024/01/11 17:28:17 [DEBUG] providerIndex.createProviderVersion: hashicorp/kubernetes, 2.25.2, windows_amd64
2024/01/11 17:28:17 [DEBUG] Client.ProviderPackageMetadata: GET https://registry.terraform.io/v1/providers/hashicorp/kubernetes/2.25.2/download/windows/amd64
2024/01/11 17:28:17 [DEBUG] ProviderDownloaderClient.download: GET https://releases.hashicorp.com/terraform-provider-kubernetes/2.25.2/terraform-provider-kubernetes_2.25.2_windows_amd64.zip
2024/01/11 17:28:17 [DEBUG] ProviderDownloaderClient.download: GET https://releases.hashicorp.com/terraform-provider-kubernetes/2.25.2/terraform-provider-kubernetes_2.25.2_SHA256SUMS
2024/01/11 17:28:17 [DEBUG] providerIndex.createProviderVersion: hashicorp/kubernetes, 2.25.2, darwin_amd64
2024/01/11 17:28:17 [DEBUG] Client.ProviderPackageMetadata: GET https://registry.terraform.io/v1/providers/hashicorp/kubernetes/2.25.2/download/darwin/amd64
2024/01/11 17:28:17 [DEBUG] ProviderDownloaderClient.download: GET https://releases.hashicorp.com/terraform-provider-kubernetes/2.25.2/terraform-provider-kubernetes_2.25.2_darwin_amd64.zip
2024/01/11 17:28:18 [DEBUG] ProviderDownloaderClient.download: GET https://releases.hashicorp.com/terraform-provider-kubernetes/2.25.2/terraform-provider-kubernetes_2.25.2_SHA256SUMS
2024/01/11 17:28:18 [DEBUG] providerIndex.createProviderVersion: hashicorp/kubernetes, 2.25.2, darwin_arm64
2024/01/11 17:28:18 [DEBUG] Client.ProviderPackageMetadata: GET https://registry.terraform.io/v1/providers/hashicorp/kubernetes/2.25.2/download/darwin/arm64
2024/01/11 17:28:18 [DEBUG] ProviderDownloaderClient.download: GET https://releases.hashicorp.com/terraform-provider-kubernetes/2.25.2/terraform-provider-kubernetes_2.25.2_darwin_arm64.zip
2024/01/11 17:28:19 [DEBUG] ProviderDownloaderClient.download: GET https://releases.hashicorp.com/terraform-provider-kubernetes/2.25.2/terraform-provider-kubernetes_2.25.2_SHA256SUMS

ERROR: something went wrong in target "tf-lock-k8s" : "✗ cannot find value for \"registry.terraform.io/hashicorp/kubernetes\" from file \"terraform/.terraform.lock.hcl\""

Pipeline "terraform-provider-k8s" failed
Skipping due to:
        targets stage:  "something went wrong during target execution"

The underlying library 'tfupdate' doesn't seem to support other registries since doing this

bsh ❯ tfupdate lock --platform=windows_amd64 --platform=linux_amd64 --platform=darwin_amd64 --platform=darwin_arm64 .

Actually adds the equivalent registry.terraform.io/ entries in to the lock file, so there are effectively double the number of providers in the lock file.

@mcwarman
Copy link
Member Author

It looks like tfupdate might actually work.

We can provide a config that the CLI doesn't expose, doing some hacking locally to see whats possible:

lockIndex, err := lock.NewDefaultIndex()
if err != nil {
return nil, err
}
newResource.lockIndex = lockIndex

-       lockIndex, err := lock.NewDefaultIndex()
+       client, err := lock.NewProviderDownloaderClient(lock.TFRegistryConfig{
+               BaseURL: "https://registry.opentofu.org/",
+       })
        if err != nil {
                return nil, err
        }
 
-       newResource.lockIndex = lockIndex
+       newResource.lockIndex = lock.NewIndex(client)

Produces

2024/01/12 11:06:18 [DEBUG] providerIndex.createProviderVersion: hashicorp/kubernetes, 2.23.0, linux_amd64
2024/01/12 11:06:18 [DEBUG] Client.ProviderPackageMetadata: GET https://registry.opentofu.org/v1/providers/hashicorp/kubernetes/2.23.0/download/linux/amd64
2024/01/12 11:06:18 [DEBUG] ProviderDownloaderClient.download: GET https://github.com/opentofu/terraform-provider-kubernetes/releases/download/v2.23.0/terraform-provider-kubernetes_2.23.0_linux_amd64.zip
2024/01/12 11:06:21 [DEBUG] ProviderDownloaderClient.download: GET https://github.com/opentofu/terraform-provider-kubernetes/releases/download/v2.23.0/terraform-provider-kubernetes_2.23.0_SHA256SUMS

@quotidian-ennui
Copy link

Does this suggest a new optional config a-la

 spec:
      file: terraform/.terraform.lock.hcl
      provider: hashicorp/kubernetes
      registry_url: https://registry.opentofu.org (default to registry.terraform.io)

@mcwarman
Copy link
Member Author

mcwarman commented Jan 14, 2024

@quotidian-ennui you might be interested in testing #1870

Went with slightly different config, which played very nicely with how it had already been coded:

targets:
  provider:
    name: 'Bump hashicorp/hcp to {{ source "latestRelease" }}'
    kind: terraform/lock
    sourceid: latestRelease
    spec:
      file: .terraform.lock.hcl
      provider: registry.opentofu.org/hashicorp/hcp
      skipconstraints: true
      platforms:
        - darwin_amd64
        - linux_amd64
        - windows_amd64

@mcwarman
Copy link
Member Author

mcwarman commented Jan 14, 2024

terraform/provider support still needs some work, The endpoint that is currently used isn't available on registry.opentofu.org

https://registry.terraform.io/v1/providers/hashicorp/kubernetes

https://registry.opentofu.org/v1/providers/hashicorp/kubernetes - 404

But

https://registry.opentofu.org/v1/providers/hashicorp/kubernetes/versions - works

@quotidian-ennui
Copy link

quotidian-ennui commented Jan 16, 2024

I tested with the latest 0.71.0 which works

  tf-lock-k8s:
    name: Update hashicorp/kubernetes lockfile
    kind: terraform/lock
    sourceid: terraform-provider-k8s
    spec:
      file: terraform/.terraform.lock.hcl
      provider: registry.opentofu.org/hashicorp/kubernetes
      platforms:
        - linux_amd64
        - windows_amd64
        - darwin_amd64
        - darwin_arm64
    dependson:
      - tf-provider-k8s
    dependsonchange: true

@olblak olblak added enhancement New feature or request resource-opentofu labels Feb 6, 2024
@kvendingoldo
Copy link

At this moment if you need to have a support of Terraform as well as OpenTofu (and Terragrunt :) ) in one tool you can use https://github.com/tofuutils/tenv which my team wrote some months ago. A lot of users switched to that tool to unify version management in the world of Terraform.

You're welcome to open any issues or contribute to tenv.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request resource-opentofu
Projects
Status: Todo
Development

No branches or pull requests

4 participants