Terraform不能初始化用Go编写的自定义提供程序



我正在尝试使用插件框架创建一个自定义地形模块。

一切都很好,直到我需要使用terraform来安装&验证提供者。如果使用

,则失败。
➜  examples git:(main) ✗ terraform init
Initializing the backend...
Initializing provider plugins...
- Finding latest version of example.com/vladmiller/example...
│ Warning: Provider development overrides are in effect
│
│ The following provider development overrides are set in the CLI configuration:
│  - example.com/vladmiller/example in /Users/vladi/go/bin
│
│ Skip terraform init when using provider development overrides. It is not necessary and may
│ error unexpectedly.
╵
╷
│ Error: Invalid provider registry host
│
│ The host "example.com" given in in provider source address "example.com/vladmiller/example"
│ does not offer a Terraform provider registry.

我也试过:

  • 从教程中克隆repo,但它也失败了
  • 使用本地安装的Go代替GoLand
  • go1.18.8代替go1.19.x
  • 指向文件夹。terraformrc到我开发代码的文件夹(我认为也许terraform-registry-manifest.json需要在相同的文件夹w/binary)
  • 克隆并尝试这个repo -没有运气
  • 我目前正试图理解围绕他们如何加载插件的地形代码…

想知道是否有人有任何想法如何排除故障。提前谢谢你。


查看日志,很明显terraform试图通过向example.com发出请求来获取注册表详细信息。我认为它不应该这样做,因为有dev_overrides

...
Service discovery for example.com at https://example.com/.well-known/terraform.json
...

下面是我设置的更多日志和示例。

➜  examples git:(main) ✗ TF_LOG=trace terraform init
2022-11-10T16:39:54.255+0200 [INFO]  Terraform version: 1.3.4
2022-11-10T16:39:54.255+0200 [DEBUG] using github.com/hashicorp/go-tfe v1.9.0
2022-11-10T16:39:54.255+0200 [DEBUG] using github.com/hashicorp/hcl/v2 v2.14.1
2022-11-10T16:39:54.255+0200 [DEBUG] using github.com/hashicorp/terraform-config-inspect v0.0.0-20210209133302-4fd17a0faac2
2022-11-10T16:39:54.255+0200 [DEBUG] using github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734
2022-11-10T16:39:54.255+0200 [DEBUG] using github.com/zclconf/go-cty v1.12.0
2022-11-10T16:39:54.255+0200 [INFO]  Go runtime version: go1.19.3
2022-11-10T16:39:54.255+0200 [INFO]  CLI args: []string{"terraform", "init"}
2022-11-10T16:39:54.255+0200 [TRACE] Stdout is a terminal of width 206
2022-11-10T16:39:54.255+0200 [TRACE] Stderr is a terminal of width 206
2022-11-10T16:39:54.255+0200 [TRACE] Stdin is a terminal
2022-11-10T16:39:54.255+0200 [DEBUG] Attempting to open CLI config file: /Users/vladi/.terraformrc
2022-11-10T16:39:54.255+0200 [INFO]  Loading CLI configuration from /Users/vladi/.terraformrc
2022-11-10T16:39:54.255+0200 [DEBUG] Explicit provider installation configuration is set
2022-11-10T16:39:54.255+0200 [TRACE] Selected provider installation method cliconfig.ProviderInstallationDirect with includes [] and excludes []
2022-11-10T16:39:54.255+0200 [INFO]  CLI command args: []string{"init"}
Initializing the backend...
2022-11-10T16:39:54.256+0200 [TRACE] Meta.Backend: no config given or present on disk, so returning nil config
2022-11-10T16:39:54.256+0200 [TRACE] Meta.Backend: backend has not previously been initialized in this working directory
2022-11-10T16:39:54.256+0200 [DEBUG] New state was assigned lineage "b3cc2d29-4dd2-a955-32f4-55d7481d5445"
2022-11-10T16:39:54.256+0200 [TRACE] Meta.Backend: using default local state only (no backend configuration, and no existing initialized backend)
2022-11-10T16:39:54.256+0200 [TRACE] Meta.Backend: instantiated backend of type <nil>
2022-11-10T16:39:54.256+0200 [DEBUG] Provider example.com/vladmiller/example is overridden by dev_overrides
2022-11-10T16:39:54.256+0200 [DEBUG] Provider example.com/vladmiller/example is overridden to load from /Users/vladi/go/bin
2022-11-10T16:39:54.256+0200 [DEBUG] checking for provisioner in "."
2022-11-10T16:39:54.263+0200 [DEBUG] checking for provisioner in "/opt/homebrew/bin"
2022-11-10T16:39:54.263+0200 [TRACE] Meta.Backend: backend <nil> does not support operations, so wrapping it in a local backend
2022-11-10T16:39:54.263+0200 [TRACE] backend/local: state manager for workspace "default" will:
- read initial snapshot from terraform.tfstate
- write new snapshots to terraform.tfstate
- create any backup at terraform.tfstate.backup
2022-11-10T16:39:54.263+0200 [TRACE] statemgr.Filesystem: reading initial snapshot from terraform.tfstate
2022-11-10T16:39:54.263+0200 [TRACE] statemgr.Filesystem: snapshot file has nil snapshot, but that's okay
2022-11-10T16:39:54.263+0200 [TRACE] statemgr.Filesystem: read nil snapshot
2022-11-10T16:39:54.263+0200 [DEBUG] Provider example.com/vladmiller/example is overridden by dev_overrides
Initializing provider plugins...
- Finding latest version of example.com/vladmiller/example...
2022-11-10T16:39:54.263+0200 [DEBUG] Service discovery for example.com at https://example.com/.well-known/terraform.json
2022-11-10T16:39:54.263+0200 [TRACE] HTTP client GET request to https://example.com/.well-known/terraform.json
╷
│ Warning: Provider development overrides are in effect
│ 
│ The following provider development overrides are set in the CLI configuration:
│  - example.com/vladmiller/example in /Users/vladi/go/bin
│ 
│ Skip terraform init when using provider development overrides. It is not necessary and may error unexpectedly.
╵
╷
│ Error: Invalid provider registry host
│ 
│ The host "example.com" given in in provider source address "example.com/vladmiller/example" does not offer a Terraform provider registry.
╵
➜  examples git:(main) ✗ terraform version
Terraform v1.3.4
on darwin_arm64
➜  examples git:(main) ✗ cat ~/.terraformrc
provider_installation {
dev_overrides {
"example.com/vladmiller/example" = "/Users/vladi/go/bin/"
}
# For all other providers, install them directly from their origin provider
# registries as normal. If you omit this, Terraform will _only_ use
# the dev_overrides block, and so no other providers will be available.
direct {}
}
➜  examples git:(main) ✗ tree /Users/vladi/go/bin
/Users/vladi/go/bin
└── terraform-provider-example
0 directories, 1 file
➜  bin ./terraform-provider-example 
This binary is a plugin. These are not meant to be executed directly.
Please execute the program that consumes these plugins, which will
load any plugins automatically
➜  terraform-provider-example git:(main) ✗ cat main.go
package main
import (
"context"
"flag"
"log"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"terraform-provider-example/example"
)
var (
// these will be set by the goreleaser configuration
// to appropriate values for the compiled binary
version string = "dev"
// goreleaser can also pass the specific commit if you want
// commit  string = ""
)
func main() {
var debug bool
flag.BoolVar(&debug, "debug", false, "set to true to run the provider with support for debuggers like delve")
flag.Parse()
err := providerserver.Serve(context.Background(), example.New(version), providerserver.ServeOpts{
Address: "example.com/vladmiller/example",
Debug:   debug,
})
if err != nil {
log.Fatal(err.Error())
}
}
➜  examples git:(main) ✗ cat provider-verification.tf 
terraform {
required_providers {
example = {
source = "example.com/vladmiller/example"
}
}
}
provider "example" {}
➜  terraform-provider-example git:(main) ✗ cat go.mod 
module terraform-provider-example
go 1.18
require github.com/hashicorp/terraform-plugin-framework v0.15.0
require (
github.com/fatih/color v1.13.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
// truncated
)

更新1:我在想,也许是我的环境出了问题。我在MacOS Ventura 13.0 w/M2上。

我从ubuntu/latest aarch64启动一个容器,并在容器中安装了最新的terraform。克隆https://github.com/hashicorp/terraform-provider-hashicups-pf的provider分支。这仍然是同样的问题。

我也尝试过terraform 1.3.0, 1.2.0和0.15.5,以防万一。

root@23de1551280a:~/terraform-provider-hashicups-pf/examples/provider-install-verification# TF_LOG=debug terraform init
2022-11-11T12:45:23.284Z [INFO]  Terraform version: 1.3.4
2022-11-11T12:45:23.284Z [DEBUG] using github.com/hashicorp/go-tfe v1.9.0
2022-11-11T12:45:23.284Z [DEBUG] using github.com/hashicorp/hcl/v2 v2.14.1
2022-11-11T12:45:23.284Z [DEBUG] using github.com/hashicorp/terraform-config-inspect v0.0.0-20210209133302-4fd17a0faac2
2022-11-11T12:45:23.284Z [DEBUG] using github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734
2022-11-11T12:45:23.284Z [DEBUG] using github.com/zclconf/go-cty v1.12.0
2022-11-11T12:45:23.284Z [INFO]  Go runtime version: go1.19.3
2022-11-11T12:45:23.284Z [INFO]  CLI args: []string{"terraform", "init"}
2022-11-11T12:45:23.284Z [DEBUG] Attempting to open CLI config file: /root/.terraformrc
2022-11-11T12:45:23.284Z [INFO]  Loading CLI configuration from /root/.terraformrc
2022-11-11T12:45:23.285Z [DEBUG] Explicit provider installation configuration is set
2022-11-11T12:45:23.286Z [INFO]  CLI command args: []string{"init"}
Initializing the backend...
2022-11-11T12:45:23.287Z [DEBUG] New state was assigned lineage "91db86b6-1777-8a07-27fd-aac42c238c35"
2022-11-11T12:45:23.287Z [DEBUG] Provider hashicorp.com/edu/hashicups-pf is overridden by dev_overrides
2022-11-11T12:45:23.287Z [DEBUG] Provider hashicorp.com/edu/hashicups-pf is overridden to load from /root/go/bin
2022-11-11T12:45:23.287Z [DEBUG] checking for provisioner in "."
2022-11-11T12:45:23.287Z [DEBUG] checking for provisioner in "/root"
2022-11-11T12:45:23.287Z [DEBUG] Provider hashicorp.com/edu/hashicups-pf is overridden by dev_overrides
Initializing provider plugins...
- Finding latest version of hashicorp.com/edu/hashicups-pf...
2022-11-11T12:45:23.287Z [DEBUG] Service discovery for hashicorp.com at https://hashicorp.com/.well-known/terraform.json
2022-11-11T12:45:23.377Z [DEBUG] Service discovery redirected to https://www.hashicorp.com/.well-known/terraform.json
╷
│ Warning: Provider development overrides are in effect
│
│ The following provider development overrides are set in the CLI configuration:
│  - hashicorp.com/edu/hashicups-pf in /root/go/bin
│
│ Skip terraform init when using provider development overrides. It is not necessary and may error unexpectedly.
╵
╷
│ Error: Invalid provider registry host
│
│ The host "hashicorp.com" given in in provider source address "hashicorp.com/edu/hashicups-pf" does not offer a Terraform provider registry.
╵

更新2:在这一点上,我倾向于相信回购是有问题的,好像它缺少了什么。


更新3:因此,terraform init不工作,然而,terraform plan像预期的那样工作。: thinking_face:

我想我可以继续开发过程了。再次阅读教程,似乎没有做terraform init那里。

您提到的dev_overrides参数告诉其他Terraform命令(如terraform apply)忽略由terraform init填充的依赖锁文件和提供程序缓存目录,而使用您指定的路径。它不影响terraform init本身的行为;安装程序将始终尝试找到一个合理的包来下载并记录在依赖锁文件中,以避免创建一个不完整的锁文件。

正如警告消息所提到的,如果您的目标只是测试覆盖的提供程序,则实际上不需要运行terraform init。如果你只是直接运行terraform apply,那么你配置的覆盖将阻止Terraform查找依赖锁文件来找到该提供商,而是直接使用你指定的目录。在这种情况下,不需要自动安装,因为软件包已经在计算机上的指定位置可用。

使用提供程序开发覆盖时跳过terraform init。这是不必要的,可能会意外出错。

最新更新