Skip to content

Commit

Permalink
fix(alibaba): remove duplicate instance ids
Browse files Browse the repository at this point in the history
Signed-off-by: Jason-ZW <zhenyang@rancher.com>
  • Loading branch information
rancher-sy-bot committed Sep 25, 2020
1 parent 7076f88 commit fdd495f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pkg/providers/alibaba/alibaba.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions pkg/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit fdd495f

Please # to comment.