Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 32e3bdc

Browse files
author
harry zhang
authored
Merge pull request #128 from YaoZengzeng/grpc-container-logs
grpc for ContainerLogs
2 parents 2cdc2ca + d59c340 commit 32e3bdc

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

pkg/kubelet/hyper/hyperclient.go

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -729,33 +729,46 @@ func (client *HyperClient) Exec(opts ExecInContainerOptions) error {
729729
}
730730

731731
func (client *HyperClient) ContainerLogs(opts ContainerLogsOptions) error {
732-
if opts.Container == "" {
733-
return fmt.Errorf("No Such Container %s", opts.Container)
732+
request := grpctypes.ContainerLogsRequest{
733+
Container: opts.Container,
734+
Follow: opts.Follow,
735+
Timestamps: opts.Timestamps,
736+
Tail: fmt.Sprintf("%d", opts.TailLines),
737+
Since: fmt.Sprintf("%d", opts.Since),
738+
Stdout: true,
739+
Stderr: true,
734740
}
735741

736-
v := url.Values{}
737-
v.Set(TYPE_CONTAINER, opts.Container)
738-
v.Set("stdout", "yes")
739-
v.Set("stderr", "yes")
740-
if opts.TailLines > 0 {
741-
v.Set("tail", fmt.Sprintf("%d", opts.TailLines))
742-
}
743-
if opts.Follow {
744-
v.Set("follow", "yes")
745-
}
746-
if opts.Timestamps {
747-
v.Set("timestamps", "yes")
748-
}
749-
if opts.Since > 0 {
750-
v.Set("since", fmt.Sprintf("%d", opts.Since))
742+
stream, err := client.client.ContainerLogs(context.Background(), &request)
743+
if err != nil {
744+
return err
751745
}
752746

753-
headers := make(map[string][]string)
754-
headers["User-Agent"] = []string{"kubelet"}
755-
headers["Content-Type"] = []string{"text/plain"}
747+
for {
748+
res, err := stream.Recv()
749+
if err == io.EOF {
750+
if opts.Follow == true {
751+
continue
752+
}
753+
break
754+
}
755+
if err != nil {
756+
return err
757+
}
758+
759+
if len(res.Log) > 0 {
760+
// there are 8 bytes of prefix in every line of hyperd's log
761+
n, err := opts.OutputStream.Write(res.Log[8:])
762+
if err != nil {
763+
return err
764+
}
765+
if n != len(res.Log)-8 {
766+
return io.ErrShortWrite
767+
}
768+
}
769+
}
756770

757-
path := "/container/logs?" + v.Encode()
758-
return client.stream("GET", path, nil, opts.OutputStream, headers)
771+
return nil
759772
}
760773

761774
func (client *HyperClient) IsImagePresent(repo, tag string) (bool, error) {

0 commit comments

Comments
 (0)