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

feature: add pouch save functionality #1592

Merged
merged 1 commit into from
Jul 12, 2018

Conversation

xiechengsheng
Copy link
Contributor

Signed-off-by: xiechengsheng XIE1995@whut.edu.cn

Ⅰ. Describe what this PR did

add pouch save functionality

Ⅱ. Does this pull request fix one issue?

NONE

Ⅲ. Describe how you did it

Ⅳ. Describe how to verify it

$ pouch save -o busybox.tar busybox:latest
$ pouch load -i busybox.tar foo
$ pouch images
IMAGE ID       IMAGE NAME                                           SIZE
8c811b4aec35   registry.hub.docker.com/library/busybox:latest       710.81 KB
8c811b4aec35   foo:latest                                           710.81 KB

Ⅴ. Special notes for reviews

@xiechengsheng
Copy link
Contributor Author

/assign @fuweid Please take a look this pr, thanks a lot~

}

output := newWriteFlusher(rw)
if _, err := io.Copy(output, r); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it better if we just return io.Copy(output, r)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Emmm, I don't think so, io.Copy(output, r) returns two variables, will make this saveImage function more complicated.

cli/save.go Outdated
Use: "save [OPTIONS] IMAGE [IMAGE...]",
Short: "Save an image to a tar archive or STDOUT",
Long: saveDescription,
Args: cobra.MinimumNArgs(1),
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we can save several images into one tar file.

ctx := context.Background()
apiClient := save.cli.Client()

r, err := apiClient.ImageSave(ctx, args[0])
Copy link
Contributor

Choose a reason for hiding this comment

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

It is very confusing to me because args[0] vs MinimumNArgs

Copy link
Contributor

Choose a reason for hiding this comment

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

solved

cli/save.go Outdated
if err := out.Close(); err != nil {
return err
}
return r.Close()
Copy link
Contributor

Choose a reason for hiding this comment

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

could we defer the r.Close() if the one of io.Copy or out.Close() fails?

ctrd/image.go Outdated
return nil, err
}

if ociRefName := namedRef.(reference.Tagged).Tag(); ociRefName != "" {
Copy link
Contributor

Choose a reason for hiding this comment

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

how do you make sure the namedRef is the reference.Tagged? if not, it will panic.

@fuweid
Copy link
Contributor

fuweid commented Jun 26, 2018

@xiechengsheng , Could you give more information about this feature? For example, about oci-ref-name? Without those information, I can't understand what you do. Thanks :)

@xiechengsheng
Copy link
Contributor Author

@fuweid I have updated codes according to your comments. desc.Annotations[ocispec.AnnotationRefName] is the tag of image to be saved.

@codecov-io
Copy link

codecov-io commented Jun 27, 2018

Codecov Report

Merging #1592 into master will increase coverage by 0.08%.
The diff coverage is 36.04%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1592      +/-   ##
==========================================
+ Coverage   41.26%   41.34%   +0.08%     
==========================================
  Files         274      277       +3     
  Lines       18123    18209      +86     
==========================================
+ Hits         7478     7529      +51     
- Misses       9733     9749      +16     
- Partials      912      931      +19
Impacted Files Coverage Δ
daemon/mgr/image_load.go 47.36% <ø> (+47.36%) ⬆️
daemon/mgr/image.go 68.46% <ø> (ø) ⬆️
cli/save.go 0% <0%> (ø)
cli/main.go 0% <0%> (ø) ⬆️
client/image_save.go 100% <100%> (ø)
apis/server/router.go 91.42% <100%> (+0.06%) ⬆️
daemon/mgr/image_save.go 50% <50%> (ø)
ctrd/image.go 79.2% <60%> (+1.73%) ⬆️
apis/server/image_bridge.go 70% <63.63%> (+4.17%) ⬆️
apis/server/utils.go 61.9% <0%> (-4.77%) ⬇️
... and 7 more

cli/save.go Outdated
apiClient := save.cli.Client()

r, err := apiClient.ImageSave(ctx, args[0])
defer r.Close()
Copy link
Contributor

Choose a reason for hiding this comment

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

if err!=nil{
return err
}
defer r.Close()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, will fix soon.

cli/save.go Outdated
if _, err := io.Copy(out, r); err != nil {
return err
}
if err := out.Close(); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

if out is os.Stdout, we should not close it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@shaloulcy I'm just curious to ask why we shouldn't close client stdout...

Copy link
Contributor

Choose a reason for hiding this comment

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

if we did like

func main() {
        out := os.Stdout
        fmt.Println(out.Close())
        fmt.Println("haha")
}

The following stdout will missing so that we can't close the stdout.

out, err = os.Create(save.output)
if err != nil {
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

defer out.Close()

Copy link
Contributor

Choose a reason for hiding this comment

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

please put the defer out.Close() in the if save.output != "" so that we should not close stdout.


rw.Header().Set("Content-Type", "application/x-tar")

r, err := s.ImageMgr.SaveImage(ctx, imageName)
Copy link
Contributor

Choose a reason for hiding this comment

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

we should close the reader

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, will fix soon.

Copy link
Contributor

Choose a reason for hiding this comment

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

solved

cli/save.go Outdated
Use: "save [OPTIONS] IMAGE [IMAGE...]",
Short: "Save an image to a tar archive or STDOUT",
Long: saveDescription,
Args: cobra.ExactArgs(1),
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this part is incompatible with save [OPTIONS] IMAGE [IMAGE...]. please make sure the input args number. @xiechengsheng

ctrd/image.go Outdated
if desc.Annotations == nil {
desc.Annotations = make(map[string]string)
}
if s, ok := desc.Annotations[ocispec.AnnotationRefName]; !ok || s == "" {
Copy link
Collaborator

Choose a reason for hiding this comment

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

how about using exist rather than ok? I think it seems to improve the readability.

@xiechengsheng
Copy link
Contributor Author

Hi, I have updated codes, PTAL, thanks a lot.

@allencloud
Copy link
Collaborator

This is a quite useful feature for PouchContainer. I update the priority to P2 to show the emergency. @xiechengsheng @fuweid

schema:
type: "string"
format: "binary"
404:
Copy link
Contributor

Choose a reason for hiding this comment

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

For 404, 500 status code, please use format like

404:
  $ref: "#/responses/404ErrorResponse"
500:
   $ref: "#/responses/500ErrorResponse"

Since the someone has changed the file, please rebase your commit and regenerate code.

ctx := context.Background()
apiClient := save.cli.Client()

r, err := apiClient.ImageSave(ctx, args[0])
Copy link
Contributor

Choose a reason for hiding this comment

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

solved

out, err = os.Create(save.output)
if err != nil {
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

please put the defer out.Close() in the if save.output != "" so that we should not close stdout.

cli/save.go Outdated
if _, err := io.Copy(out, r); err != nil {
return err
}
if err := out.Close(); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

if we did like

func main() {
        out := os.Stdout
        fmt.Println(out.Close())
        fmt.Println("haha")
}

The following stdout will missing so that we can't close the stdout.

c.Errorf("failed to decode inspect output: %v", err)
}

dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use tmp dir to handle this.

}
dir = strings.Replace(dir, "\\", "/", -1)

res = command.PouchRun("save", "-o", dir+"busybox.tar", busyboxImage)
Copy link
Contributor

Choose a reason for hiding this comment

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

filepath.Join(dir, "busybox.tar") instead of dir+"busybox.tar"

c.Errorf("failed to decode inspect output: %v", err)
}

err = os.Remove(dir + "busybox.tar")
Copy link
Contributor

Choose a reason for hiding this comment

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

use defer to remove the file after the load success

@xiechengsheng
Copy link
Contributor Author

Hi @fuweid I have fixed codes.

cli/save.go Outdated
if _, err := io.Copy(out, r); err != nil {
return err
}
if save.output != "" {
Copy link
Contributor

Choose a reason for hiding this comment

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

this if condition is duplicate, please remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I misunderstood yours meaning.

Signed-off-by: xiechengsheng <XIE1995@whut.edu.cn>
@fuweid
Copy link
Contributor

fuweid commented Jul 12, 2018

LGTM

@fuweid
Copy link
Contributor

fuweid commented Jul 12, 2018

Next Step we can add API level test for that.

@fuweid fuweid merged commit ca12fe6 into AliyunContainerService:master Jul 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants