From fdd495feed1b04120effe5811494e075fe9f3062 Mon Sep 17 00:00:00 2001 From: Jason-ZW Date: Mon, 14 Sep 2020 13:58:49 +0800 Subject: [PATCH] fix(alibaba): remove duplicate instance ids Signed-off-by: Jason-ZW --- pkg/providers/alibaba/alibaba.go | 7 ++++--- pkg/utils/util.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/pkg/providers/alibaba/alibaba.go b/pkg/providers/alibaba/alibaba.go index 6beed0da..c5286dc7 100644 --- a/pkg/providers/alibaba/alibaba.go +++ b/pkg/providers/alibaba/alibaba.go @@ -11,6 +11,7 @@ import ( "github.com/cnrancher/autok3s/pkg/common" "github.com/cnrancher/autok3s/pkg/types" "github.com/cnrancher/autok3s/pkg/types/alibaba" + "github.com/cnrancher/autok3s/pkg/utils" "github.com/cnrancher/autok3s/pkg/viper" "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests" @@ -224,10 +225,10 @@ func (p *Alibaba) IsClusterExist() (bool, []string, error) { for _, resource := range response.TagResources.TagResource { ids = append(ids, resource.ResourceId) } - return true, ids, err + // ecs will return multiple instance ids based on the value of tag key.n by n, so duplicate items need to be removed. + return true, utils.UniqueArray(ids), err } - - return false, ids, nil + return false, utils.UniqueArray(ids), nil } func (p *Alibaba) Rollback() error { diff --git a/pkg/utils/util.go b/pkg/utils/util.go index 74f25b9f..dd8983b3 100644 --- a/pkg/utils/util.go +++ b/pkg/utils/util.go @@ -13,3 +13,20 @@ func RandomToken(size int) (string, error) { } return hex.EncodeToString(token), err } + +func UniqueArray(origin []string) (unique []string) { + unique = make([]string, 0) + for i := 0; i < len(origin); i++ { + repeat := false + for j := i + 1; j < len(origin); j++ { + if origin[i] == origin[j] { + repeat = true + break + } + } + if !repeat { + unique = append(unique, origin[i]) + } + } + return +}