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

aliyun: make image replication idempotent #3872

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions mantle/platform/api/aliyun/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ func (a *API) CopyImage(source_id, dest_name, dest_region, dest_description, kms
Value: "mantle",
},
}

// Check if an image with the name has already been uploaded. This can
// happen when replication to a region fails which happens often on aliyun
images, err := a.GetImagesInRegion(dest_name, dest_region)
if err != nil {
return "", fmt.Errorf("getting image: %v", err)
}

// return early if there is already an image with that tag
if len(images.Images.Image) > 0 {
plog.Infof("image with name %v in %v region already exists--skipping copy", dest_name, dest_region)
return images.Images.Image[0].ImageId, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wonder if we should somehow respect wait_for_ready here too? I guess it's possible that the image pre-exists but isn't ready yet.

}

response, err := a.ecs.CopyImage(request)
if err != nil {
return "", fmt.Errorf("copying image: %v", err)
Expand Down Expand Up @@ -296,6 +310,16 @@ func (a *API) finishImportImageTask(importImageResponse *ecs.ImportImageResponse
return importImageResponse.ImageId, nil
}

// GetImagesInRegion retrieves a list of images by ImageName in a specified region
func (a *API) GetImagesInRegion(name string, region string) (*ecs.DescribeImagesResponse, error) {
request := ecs.CreateDescribeImagesRequest()
request.SetConnectTimeout(defaultConnectTimeout)
request.SetReadTimeout(defaultReadTimeout)
request.ImageName = name
request.RegionId = region
return a.ecs.DescribeImages(request)
}

// GetImages retrieves a list of images by ImageName
func (a *API) GetImages(name string) (*ecs.DescribeImagesResponse, error) {
request := ecs.CreateDescribeImagesRequest()
Expand Down
Loading