Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend/COS: remove COS object lock tag either when unlock remote state #31223

Merged
merged 1 commit into from Jun 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions internal/backend/remote-state/cos/client.go
Expand Up @@ -123,6 +123,11 @@ func (c *remoteClient) Unlock(check string) error {
return c.lockError(err)
}

err = c.cosUnlock(c.bucket, c.lockFile)
if err != nil {
return c.lockError(err)
}

return nil
}

Expand Down Expand Up @@ -362,6 +367,16 @@ func (c *remoteClient) cosUnlock(bucket, cosFile string) error {

var err error
for i := 0; i < 30; i++ {
tagExists, err := c.CheckTag(lockTagKey, lockTagValue)

if err != nil {
return err
}

if !tagExists {
return nil
}

err = c.DeleteTag(lockTagKey, lockTagValue)
if err == nil {
return nil
Expand All @@ -372,6 +387,30 @@ func (c *remoteClient) cosUnlock(bucket, cosFile string) error {
return err
}

// CheckTag checks if tag key:value exists
func (c *remoteClient) CheckTag(key, value string) (exists bool, err error) {
request := tag.NewDescribeTagsRequest()
request.TagKey = &key
request.TagValue = &value

response, err := c.tagClient.DescribeTags(request)
log.Printf("[DEBUG] create tag %s:%s: error: %v", key, value, err)
if err != nil {
return
}

if len(response.Response.Tags) == 0 {
return
}

tagKey := response.Response.Tags[0].TagKey
tagValue := response.Response.Tags[0].TagValue

exists = key == *tagKey && value == *tagValue

return
}

// CreateTag create tag by key and value
func (c *remoteClient) CreateTag(key, value string) error {
request := tag.NewCreateTagRequest()
Expand Down