运行“地球形态计划”时如何显示警告/错误?

我正在构建一个Terraform插件/提供程序(链接),它将帮助用户在云平台上管理他们的云资源,例如云实例,Kubernetes集群等。


目前,云平台不支持 Kubernetes 节点在创建后的大小更改。如果用户想要更改节点大小,则需要使用新节点大小创建新的节点池。


所以我在我的插件代码中添加了这个块,特别是在Kubernetes集群更新方法中(链接):


if d.HasChange("target_nodes_size") {

    errMsg := []string{

        "[ERR] Unable to update 'target_nodes_size' after creation.",

        "Please create a new node pool with the new node size.",

    }

    return fmt.Errorf(strings.Join(errMsg, " "))

}

问题是,错误仅在我运行命令时出现。我想要的是,我希望它在用户运行命令时显示,以便他们尽早知道,如果不创建新的节点池,就不可能更改节点大小。terraform applyterraform plan


如何使该字段不可变并在输出的早期显示错误?target_nodes_sizeterraform plan

http://img2.mukewang.com/632833520001864112050843.jpg

元芳怎么了
浏览 185回答 1
1回答

Helenr

这里正确的做法是告诉Terraform,对资源的更改不能就地完成,而是需要重新创建资源(通常先销毁,然后创建,但你可以用lifecycle.create_before_destroy来逆转)。创建提供程序时,可以使用架构属性上的 ForceNew 参数执行此操作。例如,从 AWS 的 API 端来看,资源被认为是不可变的,因此架构中的每个非计算属性都标记为 ForceNew: true。aws_launch_configurationfunc resourceAwsLaunchConfiguration() *schema.Resource {    return &schema.Resource{        Create: resourceAwsLaunchConfigurationCreate,        Read:   resourceAwsLaunchConfigurationRead,        Delete: resourceAwsLaunchConfigurationDelete,        Importer: &schema.ResourceImporter{            State: schema.ImportStatePassthrough,        },        Schema: map[string]*schema.Schema{            "arn": {                Type:     schema.TypeString,                Computed: true,            },            "name": {                Type:          schema.TypeString,                Optional:      true,                Computed:      true,                ForceNew:      true,                ConflictsWith: []string{"name_prefix"},                ValidateFunc:  validation.StringLenBetween(1, 255),            },// ...如果您随后尝试修改任何字段,则Terraform的计划将显示它需要替换资源,并且在应用时,只要用户接受该计划,它就会自动执行此操作。ForceNew: true对于更复杂的示例,该资源允许就地进行版本更改,但仅适用于特定的版本升级路径(因此您无法直接从转到,而必须转到。这是通过在架构上使用 CustomizeDiff 属性来完成的,该属性允许您在计划时使用逻辑来提供与静态配置通常不同的结果。aws_elasticsearch_domain5.47.85.4 -> 5.6 -> 6.8 -> 7.8“aws_elasticsearch_domain elasticsearch_version”属性的自定义Diff 如下所示:func resourceAwsElasticSearchDomain() *schema.Resource {    return &schema.Resource{        Create: resourceAwsElasticSearchDomainCreate,        Read:   resourceAwsElasticSearchDomainRead,        Update: resourceAwsElasticSearchDomainUpdate,        Delete: resourceAwsElasticSearchDomainDelete,        Importer: &schema.ResourceImporter{            State: resourceAwsElasticSearchDomainImport,        },        Timeouts: &schema.ResourceTimeout{            Update: schema.DefaultTimeout(60 * time.Minute),        },        CustomizeDiff: customdiff.Sequence(            customdiff.ForceNewIf("elasticsearch_version", func(_ context.Context, d *schema.ResourceDiff, meta interface{}) bool {                newVersion := d.Get("elasticsearch_version").(string)                domainName := d.Get("domain_name").(string)                conn := meta.(*AWSClient).esconn                resp, err := conn.GetCompatibleElasticsearchVersions(&elasticsearch.GetCompatibleElasticsearchVersionsInput{                    DomainName: aws.String(domainName),                })                if err != nil {                    log.Printf("[ERROR] Failed to get compatible ElasticSearch versions %s", domainName)                    return false                }                if len(resp.CompatibleElasticsearchVersions) != 1 {                    return true                }                for _, targetVersion := range resp.CompatibleElasticsearchVersions[0].TargetVersions {                    if aws.StringValue(targetVersion) == newVersion {                        return false                    }                }                return true            }),            SetTagsDiff,        ),尝试在已接受的升级路径上升级 的 (例如 )将显示它是计划中的就地升级,并在应用时应用。另一方面,如果您尝试通过不允许的路径进行升级(例如直接),那么Terraform的计划将显示它需要销毁现有的弹性搜索域并创建一个新域。aws_elasticsearch_domainelasticsearch_version7.4 -> 7.85.4 -> 7.8
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go