From db75092c2296ff170735b0a90843b29ada1864e4 Mon Sep 17 00:00:00 2001 From: ioito Date: Tue, 20 Sep 2022 17:27:27 +0800 Subject: [PATCH] fix(region): qcloud and google system tag --- pkg/compute/models/hosts.go | 9 +++++++- pkg/compute/options/options.go | 3 ++- pkg/multicloud/tag_base.go | 38 +++++++++++++++++++++++++++++----- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/pkg/compute/models/hosts.go b/pkg/compute/models/hosts.go index b18e853735..073b71b3b7 100644 --- a/pkg/compute/models/hosts.go +++ b/pkg/compute/models/hosts.go @@ -2449,7 +2449,7 @@ func (self *SHost) SyncHostVMs(ctx context.Context, userCred mcclient.TokenCrede } skipFunc := func(ext cloudprovider.ICloudVM) (bool, string) { - if len(options.Options.SkipServerBySysTagKeys) == 0 { + if len(options.Options.SkipServerBySysTagKeys) == 0 && len(options.Options.SkipServerBySysTagKeys) == 0 { return false, "" } keys := strings.Split(options.Options.SkipServerBySysTagKeys, ",") @@ -2458,6 +2458,13 @@ func (self *SHost) SyncHostVMs(ctx context.Context, userCred mcclient.TokenCrede return true, key } } + userKeys := strings.Split(options.Options.SkipServerByUserTagKeys, ",") + tags, _ := ext.GetTags() + for key := range tags { + if utils.IsInStringArray(key, userKeys) { + return true, key + } + } return false, "" } diff --git a/pkg/compute/options/options.go b/pkg/compute/options/options.go index 8234567e85..1318d55dd0 100644 --- a/pkg/compute/options/options.go +++ b/pkg/compute/options/options.go @@ -187,7 +187,8 @@ type ComputeOptions struct { DefaultIPAllocationDirection string `help:"default IP allocation direction" default:"stepdown"` // 弹性伸缩中的ecs一般会有特殊的系统标签,通过指定这些标签可以忽略这部分ecs的同步, 指定多个key需要以 ',' 分隔 - SkipServerBySysTagKeys string `help:"skip server sync and create with system tags" default:"acs:autoscaling:scalingGroupId"` + SkipServerBySysTagKeys string `help:"skip server sync and create with system tags" default:"acs:autoscaling:scalingGroupId"` + SkipServerByUserTagKeys string `help:"skip server sync and create with user tags" default:""` EnableAwsMonitorAgent bool `help:"enable aws monitor agent" default:"true"` diff --git a/pkg/multicloud/tag_base.go b/pkg/multicloud/tag_base.go index a89fe3408f..e659b3ae0c 100644 --- a/pkg/multicloud/tag_base.go +++ b/pkg/multicloud/tag_base.go @@ -54,7 +54,7 @@ type QcloudTags struct { ResourceTags []STag } -func (self *QcloudTags) GetTags() (map[string]string, error) { +func (self *QcloudTags) getTags() (map[string]string, error) { ret := map[string]string{} for _, tag := range self.TagSet { if tag.Value == "null" { @@ -95,8 +95,25 @@ func (self *QcloudTags) GetTags() (map[string]string, error) { return ret, nil } +func (self *QcloudTags) GetTags() (map[string]string, error) { + tags, _ := self.getTags() + for k := range tags { + if strings.HasPrefix(k, "tencentcloud:") { + delete(tags, k) + } + } + return tags, nil +} + func (self *QcloudTags) GetSysTags() map[string]string { - return nil + tags, _ := self.getTags() + ret := map[string]string{} + for k, v := range tags { + if strings.HasPrefix(k, "tencentcloud:") { + ret[k] = v + } + } + return ret } func (self *QcloudTags) SetTags(tags map[string]string, replace bool) error { @@ -124,7 +141,8 @@ func (self *AliyunTags) GetTags() (map[string]string, error) { ret := map[string]string{} for _, tag := range self.Tags.Tag { if strings.HasPrefix(tag.TagKey, "aliyun") || strings.HasPrefix(tag.TagKey, "acs:") || - strings.HasSuffix(tag.Key, "aliyun") || strings.HasPrefix(tag.Key, "acs:") { + strings.HasSuffix(tag.Key, "aliyun") || strings.HasPrefix(tag.Key, "acs:") || + strings.HasSuffix(tag.Key, "ack.") { // k8s continue } if len(tag.TagKey) > 0 { @@ -141,7 +159,8 @@ func (self *AliyunTags) GetSysTags() map[string]string { ret := map[string]string{} for _, tag := range self.Tags.Tag { if strings.HasPrefix(tag.TagKey, "aliyun") || strings.HasPrefix(tag.TagKey, "acs:") || - strings.HasPrefix(tag.Key, "aliyun") || strings.HasPrefix(tag.Key, "acs:") { + strings.HasPrefix(tag.Key, "aliyun") || strings.HasPrefix(tag.Key, "acs:") || + strings.HasPrefix(tag.Key, "ack.") { // k8s if len(tag.TagKey) > 0 { ret[tag.TagKey] = tag.TagValue } else if len(tag.Key) > 0 { @@ -204,13 +223,22 @@ type GoogleTags struct { func (self *GoogleTags) GetTags() (map[string]string, error) { ret := map[string]string{} for k, v := range self.Labels { + if strings.HasPrefix(k, "goog-") { + continue + } ret[encode.DecodeGoogleLable(k)] = encode.DecodeGoogleLable(v) } return ret, nil } func (self *GoogleTags) GetSysTags() map[string]string { - return nil + ret := map[string]string{} + for k, v := range self.Labels { + if strings.HasPrefix(k, "goog-") { + ret[k] = v + } + } + return ret } func (self *GoogleTags) SetTags(tags map[string]string, replace bool) error {