fix(region): set ksyun tag (#23610)

This commit is contained in:
屈轩
2025-10-24 22:54:04 +08:00
committed by GitHub
parent c7b8129ed6
commit 282e1499e1
8 changed files with 125 additions and 22 deletions
+1 -1
View File
@@ -97,7 +97,7 @@ require (
k8s.io/cri-api v0.28.15
k8s.io/klog/v2 v2.20.0
moul.io/http2curl/v2 v2.3.0
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20251022062406-17f2571b6fd2
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20251024141952-4f8988d93f8e
yunion.io/x/executor v0.0.0-20250518005516-5402e9e0bed0
yunion.io/x/jsonutils v1.0.1-0.20250507052344-1abcf4f443b1
yunion.io/x/log v1.0.1-0.20240305175729-7cf2d6cd5a91
+2 -2
View File
@@ -1470,8 +1470,8 @@ sigs.k8s.io/structured-merge-diff/v4 v4.0.1/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20251022062406-17f2571b6fd2 h1:jLYvF9XjLII4t1eDGhWvE0pGgq3ec7/3mdIrwrHr0GQ=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20251022062406-17f2571b6fd2/go.mod h1:nya/IL1IXSMmFgB48P27CppyGpkT5coG150PD+xVJrc=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20251024141952-4f8988d93f8e h1:w4f/QxN1Y7w2KrCR9jPWj5B4ZuMhiLcI1jjJHej6wz4=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20251024141952-4f8988d93f8e/go.mod h1:nya/IL1IXSMmFgB48P27CppyGpkT5coG150PD+xVJrc=
yunion.io/x/executor v0.0.0-20250518005516-5402e9e0bed0 h1:msG4SiDSVU7CrXH06WuHlNEZXIooTcmNbfrIGHuIHBU=
yunion.io/x/executor v0.0.0-20250518005516-5402e9e0bed0/go.mod h1:Uxuou9WQIeJXNpy7t2fPLL0BYLvLiMvGQwY7Qc6aSws=
yunion.io/x/jsonutils v0.0.0-20190625054549-a964e1e8a051/go.mod h1:4N0/RVzsYL3kH3WE/H1BjUQdFiWu50JGCFQuuy+Z634=
@@ -69,13 +69,17 @@ func (self *GuestSyncstatusTask) getOriginStatus() string {
func (self *GuestSyncstatusTask) OnGetStatusComplete(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
guest := obj.(*models.SGuest)
log.Debugf("OnGetStatusSucc guest %s(%s) status %s", guest.Name, guest.Id, body)
resp := new(api.HostUploadGuestStatusInput)
body.Unmarshal(resp)
if err := guest.SetStatusFromHost(ctx, self.GetUserCred(), *resp, self.HasParentTask(), self.getOriginStatus()); err != nil {
log.Warningf("SetStatusFromHost for guest %s error: %v", guest.GetId(), err)
if len(guest.ExternalId) == 0 {
log.Debugf("OnGetStatusSucc guest %s(%s) status %s", guest.Name, guest.Id, body)
resp := new(api.HostUploadGuestStatusInput)
body.Unmarshal(resp)
if err := guest.SetStatusFromHost(ctx, self.GetUserCred(), *resp, self.HasParentTask(), self.getOriginStatus()); err != nil {
log.Warningf("SetStatusFromHost for guest %s error: %v", guest.GetId(), err)
}
logclient.AddSimpleActionLog(guest, logclient.ACT_VM_SYNC_STATUS, "", self.UserCred, true)
}
logclient.AddSimpleActionLog(guest, logclient.ACT_VM_SYNC_STATUS, "", self.UserCred, true)
self.SetStageComplete(ctx, nil)
}
+1 -1
View File
@@ -2039,7 +2039,7 @@ sigs.k8s.io/structured-merge-diff/v4/value
# sigs.k8s.io/yaml v1.2.0
## explicit; go 1.12
sigs.k8s.io/yaml
# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20251022062406-17f2571b6fd2
# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20251024141952-4f8988d93f8e
## explicit; go 1.24
yunion.io/x/cloudmux/pkg/apis
yunion.io/x/cloudmux/pkg/apis/billing
+1
View File
@@ -127,6 +127,7 @@ func (region *SRegion) CreateVM(opts *cloudprovider.SManagedVMCreateConfig) (*SI
for k, v := range opts.Tags {
params[fmt.Sprintf("Tag.%d.Key", tagIdx)] = k
params[fmt.Sprintf("Tag.%d.Value", tagIdx)] = v
tagIdx++
}
resp, err := region.ecsRequest("RunInstances", params)
if err != nil {
+5 -1
View File
@@ -212,6 +212,10 @@ func (ins *SInstance) GetTags() (map[string]string, error) {
return tags.GetTags(), nil
}
func (ins *SInstance) SetTags(tags map[string]string, replace bool) error {
return ins.getRegion().SetResourceTags("kec-instance", ins.InstanceId, tags, replace)
}
func (ins *SInstance) getRegion() *SRegion {
if ins.region != nil {
return ins.region
@@ -363,7 +367,7 @@ func (ins *SInstance) GetSecurityGroupIds() ([]string, error) {
func (ins *SInstance) GetStatus() string {
switch ins.InstanceState.Name {
case "block_device_mapping", "scheduling", "updating_password", "rebuilding":
case "block_device_mapping", "scheduling", "updating_password", "rebuilding", "updating_hostname":
return api.VM_DEPLOYING
case "active":
return api.VM_RUNNING
+13 -9
View File
@@ -17,6 +17,7 @@ package ksyun
import (
"fmt"
"strings"
"time"
"yunion.io/x/cloudmux/pkg/cloudprovider"
"yunion.io/x/jsonutils"
@@ -166,16 +167,19 @@ func (region *SRegion) CreateSecurityGroupRule(groupId string, opts *cloudprovid
if len(ruleId) == 0 {
return nil, fmt.Errorf("invalid rule create response %s", resp.String())
}
group, err := region.GetSecurityGroup(groupId)
if err != nil {
return nil, err
}
for i := range group.SecurityGroupEntrySet {
if group.SecurityGroupEntrySet[i].SecurityGroupEntryId == ruleId {
group.SecurityGroupEntrySet[i].region = region
group.SecurityGroupEntrySet[i].SecurityGroupId = groupId
return &group.SecurityGroupEntrySet[i], nil
for i := 0; i < 3; i++ {
group, err := region.GetSecurityGroup(groupId)
if err != nil {
return nil, err
}
for i := range group.SecurityGroupEntrySet {
if group.SecurityGroupEntrySet[i].SecurityGroupEntryId == ruleId {
group.SecurityGroupEntrySet[i].region = region
group.SecurityGroupEntrySet[i].SecurityGroupId = groupId
return &group.SecurityGroupEntrySet[i], nil
}
}
time.Sleep(time.Second * 3)
}
return nil, errors.Wrapf(cloudprovider.ErrNotFound, "after create %s", jsonutils.Marshal(opts))
}
+92 -2
View File
@@ -14,6 +14,15 @@
package ksyun
import (
"fmt"
"strings"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/gotypes"
"yunion.io/x/pkg/utils"
)
type STag struct {
ResourceType string
ResourceId string
@@ -31,7 +40,7 @@ func (vv TagSet) GetTags() map[string]string {
return ret
}
func (self *SRegion) ListTags(resType string, resId string) (*TagSet, error) {
func (region *SRegion) ListTags(resType string, resId string) (*TagSet, error) {
params := map[string]interface{}{
"MaxResults": "1000",
"Filter.1.Name": "resource-type",
@@ -39,7 +48,7 @@ func (self *SRegion) ListTags(resType string, resId string) (*TagSet, error) {
"Filter.2.Name": "resource-id",
"Filter.2.Value.1": resId,
}
resp, err := self.tagRequest("DescribeTags", params)
resp, err := region.tagRequest("DescribeTags", params)
if err != nil {
return nil, err
}
@@ -50,3 +59,84 @@ func (self *SRegion) ListTags(resType string, resId string) (*TagSet, error) {
}
return ret, nil
}
func (region *SRegion) DeleteTags(resType, resId string, tags map[string]string) error {
params := map[string]interface{}{
"Resource.1.Type": resType,
"Resource.1.Id": resId,
}
idx := 1
for k, v := range tags {
params[fmt.Sprintf("Tag.%d.Key", idx)] = k
params[fmt.Sprintf("Tag.%d.Value", idx)] = v
idx++
}
_, err := region.tagRequest("DeleteTags", params)
if err != nil {
return err
}
return nil
}
func (region *SRegion) CreateTags(resType, resId string, tags map[string]string) error {
params := map[string]interface{}{
"Resource.1.Type": resType,
"Resource.1.Id": resId,
}
idx := 1
for k, v := range tags {
params[fmt.Sprintf("Tag.%d.Key", idx)] = k
params[fmt.Sprintf("Tag.%d.Value", idx)] = v
idx++
}
_, err := region.tagRequest("CreateTags", params)
if err != nil {
return err
}
return nil
}
func (region *SRegion) SetResourceTags(resType string, resId string, tags map[string]string, replace bool) error {
_tags, err := region.ListTags(resType, resId)
if err != nil {
return errors.Wrapf(err, "ListTags")
}
if gotypes.IsNil(_tags) {
_tags = &TagSet{}
}
keys, upperKeys := []string{}, []string{}
for k := range tags {
keys = append(keys, k)
upperKeys = append(upperKeys, strings.ToUpper(k))
}
if replace {
if len(tags) > 0 {
removeKeys := map[string]string{}
for _, k := range *_tags {
if !utils.IsInStringArray(k.Key, keys) {
removeKeys[k.Key] = k.Value
}
}
if len(removeKeys) > 0 {
err := region.DeleteTags(resType, resId, removeKeys)
if err != nil {
return errors.Wrapf(err, "DeleteTags")
}
}
}
} else {
removeKeys := map[string]string{}
for _, k := range *_tags {
if !utils.IsInStringArray(k.Key, keys) && utils.IsInStringArray(strings.ToUpper(k.Key), upperKeys) {
removeKeys[k.Key] = k.Value
}
}
if len(removeKeys) > 0 {
err := region.DeleteTags(resType, resId, removeKeys)
if err != nil {
return errors.Wrapf(err, "DeleteTags")
}
}
}
return region.CreateTags(resType, resId, tags)
}