Skip to content

Commit

Permalink
Merge pull request #95 from zheng1/master
Browse files Browse the repository at this point in the history
Bugfix: attach and detach not work
  • Loading branch information
zheng1 authored Apr 16, 2020
2 parents 7b98a3c + 9959aee commit e1604b3
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 31 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func main() {
defer logs.FlushLogs()

if err := command.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cobra v0.0.3 // indirect
github.com/yunify/qingcloud-sdk-go v0.0.0-20190806070725-5b519baeba90
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.2.2
k8s.io/api v0.0.0-20190409092523-d687e77c8ae9
k8s.io/apiextensions-apiserver v0.0.0-20190409093611-d42ba5c63e88 // indirect
Expand Down
10 changes: 0 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,6 @@ github.com/xiang90/probing v0.0.0-20160813154853-07dd2e8dfe18 h1:MPPkRncZLN9Kh4M
github.com/xiang90/probing v0.0.0-20160813154853-07dd2e8dfe18/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/yunify/qingcloud-sdk-go v0.0.0-20190806070725-5b519baeba90 h1:5GVQwiAa41tV9XFHMx1qLjWO2rC1WQ5OoabqOOgoSAM=
github.com/yunify/qingcloud-sdk-go v0.0.0-20190806070725-5b519baeba90/go.mod h1:q4o/lbL+PCUIGbzyYzAHsbCgh3bQ/LA7VLFRQTlvrcM=
github.com/yunify/qingcloud-sdk-go v2.0.0-alpha.35+incompatible h1:KSesALlaxBZ+pX4rTSDPPwl3PXo7m002D1eGOQjFosc=
github.com/yunify/qingcloud-sdk-go v2.0.0-alpha.35+incompatible/go.mod h1:q4o/lbL+PCUIGbzyYzAHsbCgh3bQ/LA7VLFRQTlvrcM=
github.com/yunify/qingcloud-sdk-go v2.0.0-alpha.35.0.20190513053909-5e6152f06771+incompatible h1:Pmj6eGFEzKydhD9nYTbN1C7YQJhVvFu5b86SzpqbTc4=
github.com/yunify/qingcloud-sdk-go v2.0.0-alpha.35.0.20190513053909-5e6152f06771+incompatible/go.mod h1:q4o/lbL+PCUIGbzyYzAHsbCgh3bQ/LA7VLFRQTlvrcM=
go.uber.org/atomic v0.0.0-20181018215023-8dc6146f7569 h1:nSQar3Y0E3VQF/VdZ8PTAilaXpER+d7ypdABCrpwMdg=
go.uber.org/atomic v0.0.0-20181018215023-8dc6146f7569/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v0.0.0-20180122172545-ddea229ff1df h1:shvkWr0NAZkg4nPuE3XrKP0VuBPijjk3TfX6Y6acFNg=
Expand Down Expand Up @@ -256,8 +252,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/gcfg.v1 v1.2.3 h1:m8OOJ4ccYHnx2f4gQwpno8nAX5OGOh7RLaaz0pj3Ogs=
gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o=
gopkg.in/inf.v0 v0.9.0 h1:3zYtXIO92bvsdS3ggAdA8Gb4Azj0YU+TVY1uGYNFA8o=
gopkg.in/inf.v0 v0.9.0/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
gopkg.in/natefinch/lumberjack.v2 v2.0.0-20150622162204-20b71e5b60d7 h1:986b60BAz5vO2Vaf48yQaq+wb2bU4JsXxKu1+itW6x8=
Expand All @@ -266,8 +260,6 @@ gopkg.in/square/go-jose.v2 v2.0.0-20180411045311-89060dee6a84 h1:ELQJ5WuT+ydETLC
gopkg.in/square/go-jose.v2 v2.0.0-20180411045311-89060dee6a84/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0 h1:POO/ycCATvegFmVuPpQzZFJ+pGZeX22Ufu6fibxDVjU=
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0/go.mod h1:WDnlLJ4WF5VGsH/HVa3CI79GS0ol3YnhVnKP89i0kNg=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand All @@ -292,8 +284,6 @@ k8s.io/component-base v0.0.0-20190409093041-e34633071963 h1:b0EoSROSsjY+8KrBR0bh
k8s.io/component-base v0.0.0-20190409093041-e34633071963/go.mod h1:2AUf0gALRFIf31Zup6Torh8msIgiDioNziBnI8wpuk4=
k8s.io/gengo v0.0.0-20190116091435-f8a0810f38af/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
k8s.io/klog v0.0.0-20190306015804-8e90cee79f82/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
k8s.io/klog v0.2.0 h1:0ElL0OHzF3N+OhoJTL0uca20SxtYt4X4+bzHeqrB83c=
k8s.io/klog v0.2.0/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
k8s.io/klog v0.4.0 h1:lCJCxf/LIowc2IGS9TPjWDyXY4nOmdGdfcwwDQCOURQ=
k8s.io/klog v0.4.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I=
k8s.io/kube-controller-manager v0.0.0-20190409093943-63563cf7ef9a h1:rNaiSRjwZw/EtkFBuR1OVWvdQwpWhDfO205T/gn5+9c=
Expand Down
12 changes: 10 additions & 2 deletions pkg/eip/qingcloud_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,17 @@ func (q *qingcloudEIPHelper) AllocateEIP() (*EIP, error) {
return nil, errors.NewCommonServerError(ResourceNameEIP, *output.EIPs[0], "waitEIPStatus", err.Error())
}
if len(q.TagIDs) > 0 {
err = executor.AddTagsToResource(q.TagAPI, q.TagIDs, *output.EIPs[0], "eip")
var eips []string
for _, eip := range output.EIPs {
if eip != nil {
eips = append(eips, *eip)
}
}
err = executor.AttachTagsToResources(q.TagAPI, q.TagIDs, eips, "eip")
if err != nil {
klog.Errorf("Failed to add tags to Eip %s, err: %s", *output.EIPs[0], err.Error())
klog.Errorf("Failed to add tags to Eip %v, err: %s", eips, err.Error())
} else {
klog.Infof("Add tag %s to eip %v done", q.TagIDs, eips)
}
}
return eip, nil
Expand Down
78 changes: 71 additions & 7 deletions pkg/executor/lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@ func (q *qingCloudLoadBalanceExecutor) Stop(id string) error {
return qcclient.WaitJob(q.jobapi, *output.JobID, operationWaitTimeout, waitInterval)
}

func getEipsFromLB(lb *qcservice.LoadBalancer) []string {
var eips []string
if lb == nil {
return eips
}
var checkEip = func(eip *qcservice.EIP) {
if eip.EIPID != nil && *eip.EIPID != "" {
eips = append(eips, *eip.EIPID)
}
}
for _, eip := range lb.EIPs {
checkEip(eip)
}
for _, eip := range lb.Cluster {
checkEip(eip)
}
return eips
}

func (q *qingCloudLoadBalanceExecutor) Create(input *qcservice.CreateLoadBalancerInput) (*qcservice.LoadBalancer, error) {
klog.V(2).Infof("Creating LB: %+v", *input)
name := *input.LoadBalancerName
Expand All @@ -114,20 +133,32 @@ func (q *qingCloudLoadBalanceExecutor) Create(input *qcservice.CreateLoadBalance
return nil, newServerErrorOfLoadBalancer(name, "Create", err)
}
klog.V(2).Infof("Waiting for Lb %s starting", name)
err = q.waitLoadBalancerActive(*output.LoadBalancerID)
var lbID = *output.LoadBalancerID
err = q.waitLoadBalancerActive(lbID)
if err != nil {
klog.Errorf("LoadBalancer %s start failed", *output.LoadBalancerID)
klog.Errorf("LoadBalancer %s start failed", lbID)
return nil, newServerErrorOfLoadBalancer(name, "waitLoadBalancerActive", err)
}
klog.V(2).Infof("Lb %s is successfully started", name)
lb, err := q.GetLoadBalancerByID(*output.LoadBalancerID)
lb, err := q.GetLoadBalancerByID(lbID)
if err != nil {
return nil, newServerErrorOfLoadBalancer(name, "GetLoadBalancerByID", err)
}
if q.addTag {
err = AddTagsToResource(q.tagapi, q.tagIDs, *output.LoadBalancerID, "loadbalancer")
err = AttachTagsToResources(q.tagapi, q.tagIDs, []string{lbID}, "loadbalancer")
if err != nil {
klog.Errorf("Failed to add tag to loadBalancer %s, err: %s", *output.LoadBalancerID, err.Error())
klog.Errorf("Failed to attach tag to loadBalancer %s, err: %s", lbID, err.Error())
} else {
klog.Infof("Attach tag %s to loadBalancer %s done", q.tagIDs, lbID)
}
var eips = getEipsFromLB(lb)
if len(eips) > 0 {
err = AttachTagsToResources(q.tagapi, q.tagIDs, eips, "eip")
if err != nil {
klog.Errorf("Failed to attach tag to eip %v, err: %s", eips, err.Error())
} else {
klog.Infof("Attach tag %s to eip %v done", q.tagIDs, eips)
}
}
}
return lb, nil
Expand Down Expand Up @@ -179,7 +210,17 @@ func (q *qingCloudLoadBalanceExecutor) AssociateEip(id string, eips ...string) e
if err != nil {
return newServerErrorOfLoadBalancer(id, "AssociateEip", err)
}
return qcclient.WaitJob(q.jobapi, *output.JobID, operationWaitTimeout, waitInterval)
err = qcclient.WaitJob(q.jobapi, *output.JobID, operationWaitTimeout, waitInterval)
if err != nil {
return err
}
err = AttachTagsToResources(q.tagapi, q.tagIDs, eips, "eip")
if err != nil {
klog.Errorf("Failed to attach tag to eip %v, err: %s", eips, err.Error())
} else {
klog.Infof("Attach tag %s to eip %v done", q.tagIDs, eips)
}
return nil
}

func (q *qingCloudLoadBalanceExecutor) DissociateEip(id string, eips ...string) error {
Expand All @@ -194,7 +235,17 @@ func (q *qingCloudLoadBalanceExecutor) DissociateEip(id string, eips ...string)
if err != nil {
return newServerErrorOfLoadBalancer(id, "DissociateEip", err)
}
return qcclient.WaitJob(q.jobapi, *output.JobID, operationWaitTimeout, waitInterval)
err = qcclient.WaitJob(q.jobapi, *output.JobID, operationWaitTimeout, waitInterval)
if err != nil {
return err
}
err = DetachTagsFromResources(q.tagapi, q.tagIDs, eips, "eip")
if err != nil {
klog.Errorf("Failed to detach tag from eip %v, err: %s", eips, err.Error())
} else {
klog.Infof("Detach tag %s from eip %v done", q.tagIDs, eips)
}
return nil
}

func (q *qingCloudLoadBalanceExecutor) waitLoadBalancerActive(id string) error {
Expand All @@ -218,6 +269,10 @@ func (q *qingCloudLoadBalanceExecutor) Confirm(id string) error {
}

func (q *qingCloudLoadBalanceExecutor) Delete(id string) error {
lb, err := q.GetLoadBalancerByID(id)
if err != nil {
return newServerErrorOfLoadBalancer(id, "GetLoadBalancerByID", err)
}
output, err := q.lbapi.DeleteLoadBalancers(&qcservice.DeleteLoadBalancersInput{LoadBalancers: []*string{&id}})
if err != nil {
return newServerErrorOfLoadBalancer(id, "Delete", err)
Expand All @@ -226,6 +281,15 @@ func (q *qingCloudLoadBalanceExecutor) Delete(id string) error {
if err != nil {
return newServerErrorOfLoadBalancer(id, "Wait Deletion Done", err)
}
var eips = getEipsFromLB(lb)
if len(eips) > 0 {
err = DetachTagsFromResources(q.tagapi, q.tagIDs, eips, "eip")
if err != nil {
klog.Errorf("Failed to detach tag from eip %v, err: %s", eips, err.Error())
} else {
klog.Infof("Detach tag %s from eip %v done", q.tagIDs, eips)
}
}
return nil
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/executor/sg.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ func (q *qingcloudSecurityGroupExecutor) CreateSecurityGroup(sgName string, rule
}
sg, _ := q.GetSecurityGroupByID(*sgID)
if q.addTag {
err = AddTagsToResource(q.tagapi, q.tagIDs, *sgID, "security_group")
err = AttachTagsToResources(q.tagapi, q.tagIDs, []string{*sgID}, "security_group")
if err != nil {
klog.Errorf("Failed to add tag to security group %s, err: %s", *sgID, err.Error())
klog.Errorf("Failed to attach tag to security group %s, err: %s", *sgID, err.Error())
} else {
klog.Infof("Attach tag %s to security group %s done", q.tagIDs, *sgID)
}
}
return sg, nil
Expand Down
41 changes: 33 additions & 8 deletions pkg/executor/tags.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,51 @@
package executor

import (
"fmt"
"github.com/yunify/qingcloud-cloud-controller-manager/pkg/errors"
"github.com/yunify/qingcloud-sdk-go/service"
)

func AddTagsToResource(tagapi *service.TagService, tags []string, resourceID string, resourceType string) error {
func AttachTagsToResources(tagapi *service.TagService, tags []string, resourceIDs []string, resourceType string) error {
input := &service.AttachTagsInput{}
for _, tag := range tags {
p := &service.ResourceTagPair{
ResourceID: &resourceID,
TagID: service.String(tag),
ResourceType: &resourceType,
for _, resourceID := range resourceIDs {
p := &service.ResourceTagPair{
ResourceID: &resourceID,
TagID: service.String(tag),
ResourceType: &resourceType,
}
input.ResourceTagPairs = append(input.ResourceTagPairs, p)
}
input.ResourceTagPairs = append(input.ResourceTagPairs, p)
}
output, err := tagapi.AttachTags(input)
if err != nil {
return errors.NewCommonServerError("tag", resourceID, "AddTagsToResource", err.Error())
return errors.NewCommonServerError("tag", fmt.Sprintf("%v", resourceIDs), "AttachTagsToResources", err.Error())
}
if *output.RetCode != 0 {
return errors.NewCommonServerError("tag", resourceID, "AddTagsToResource", *output.Message)
return errors.NewCommonServerError("tag", fmt.Sprintf("%v", resourceIDs), "AttachTagsToResources", *output.Message)
}
return nil
}

func DetachTagsFromResources(tagapi *service.TagService, tags []string, resourceIDs []string, resourceType string) error {
input := &service.DetachTagsInput{}
for _, tag := range tags {
for _, resourceID := range resourceIDs {
p := &service.ResourceTagPair{
ResourceID: &resourceID,
TagID: service.String(tag),
ResourceType: &resourceType,
}
input.ResourceTagPairs = append(input.ResourceTagPairs, p)
}
}
output, err := tagapi.DetachTags(input)
if err != nil {
return errors.NewCommonServerError("tag", fmt.Sprintf("%v", resourceIDs), "DetachTagsFromResources", err.Error())
}
if *output.RetCode != 0 {
return errors.NewCommonServerError("tag", fmt.Sprintf("%v", resourceIDs), "DetachTagsFromResources", *output.Message)
}
return nil
}

0 comments on commit e1604b3

Please sign in to comment.