亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

運行“地球形態計劃”時如何顯示警告/錯誤?

運行“地球形態計劃”時如何顯示警告/錯誤?

Go
元芳怎么了 2022-09-19 17:15:32
我正在構建一個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
查看完整描述

1 回答

?
Helenr

TA貢獻1780條經驗 獲得超4個贊

這里正確的做法是告訴Terraform,對資源的更改不能就地完成,而是需要重新創建資源(通常先銷毀,然后創建,但你可以用lifecycle.create_before_destroy來逆轉)。

創建提供程序時,可以使用架構屬性上的 ForceNew 參數執行此操作。

例如,從 AWS 的 API 端來看,資源被認為是不可變的,因此架構中的每個非計算屬性都標記為 ForceNew: trueaws_launch_configuration

func 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


查看完整回答
反對 回復 2022-09-19
  • 1 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號