-
Notifications
You must be signed in to change notification settings - Fork 950
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
Conversation
/assign @fuweid Please take a look this pr, thanks a lot~ |
apis/server/image_bridge.go
Outdated
} | ||
|
||
output := newWriteFlusher(rw) | ||
if _, err := io.Copy(output, r); err != nil { |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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]) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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 != "" { |
There was a problem hiding this comment.
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.
@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 :) |
@fuweid I have updated codes according to your comments. |
Codecov Report
@@ 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
|
cli/save.go
Outdated
apiClient := save.cli.Client() | ||
|
||
r, err := apiClient.ImageSave(ctx, args[0]) | ||
defer r.Close() |
There was a problem hiding this comment.
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()
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defer out.Close()
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, will fix soon.
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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 == "" { |
There was a problem hiding this comment.
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.
Hi, I have updated codes, PTAL, thanks a lot. |
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: |
There was a problem hiding this comment.
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]) |
There was a problem hiding this comment.
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 | ||
} |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
test/cli_save_and_load_test.go
Outdated
c.Errorf("failed to decode inspect output: %v", err) | ||
} | ||
|
||
dir, err := filepath.Abs(filepath.Dir(os.Args[0])) |
There was a problem hiding this comment.
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.
test/cli_save_and_load_test.go
Outdated
} | ||
dir = strings.Replace(dir, "\\", "/", -1) | ||
|
||
res = command.PouchRun("save", "-o", dir+"busybox.tar", busyboxImage) |
There was a problem hiding this comment.
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"
test/cli_save_and_load_test.go
Outdated
c.Errorf("failed to decode inspect output: %v", err) | ||
} | ||
|
||
err = os.Remove(dir + "busybox.tar") |
There was a problem hiding this comment.
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
Hi @fuweid I have fixed codes. |
cli/save.go
Outdated
if _, err := io.Copy(out, r); err != nil { | ||
return err | ||
} | ||
if save.output != "" { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>
LGTM |
Next Step we can add API level test for that. |
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
Ⅴ. Special notes for reviews