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

Attaching os standard error and out stream to the copy command #1960

Merged
merged 7 commits into from
Apr 29, 2019

Conversation

prary
Copy link
Contributor

@prary prary commented Apr 15, 2019

Copy command doesn't output the logs to the user, hence attaching logs to stderr and stdout.

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@codecov-io
Copy link

Codecov Report

Merging #1960 into master will decrease coverage by 0.01%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1960      +/-   ##
==========================================
- Coverage   52.58%   52.56%   -0.02%     
==========================================
  Files         180      180              
  Lines        7898     7900       +2     
==========================================
  Hits         4153     4153              
- Misses       3356     3358       +2     
  Partials      389      389
Impacted Files Coverage Δ
pkg/skaffold/sync/kubectl/kubectl.go 9.09% <0%> (-0.59%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 30da253...29f3abb. Read the comment docs.

Copy link
Contributor

@corneliusweig corneliusweig left a comment

Choose a reason for hiding this comment

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

@prary I don't think it is generally acceptable to simply forward the output to stdout/stderr. Error logging should be done with the logger. In addition, the deleteFileFn has the same problem. What do you think about this approach:

diff --git a/pkg/skaffold/sync/kubectl/kubectl.go b/pkg/skaffold/sync/kubectl/kubectl.go
index 836e6252..6ba56f2f 100644
--- a/pkg/skaffold/sync/kubectl/kubectl.go
+++ b/pkg/skaffold/sync/kubectl/kubectl.go
@@ -19,7 +19,6 @@ package kubectl
 import (
 	"context"
 	"io"
-	"os"
 	"os/exec"
 
 	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/sync"
@@ -77,9 +76,6 @@ func copyFileFn(ctx context.Context, pod v1.Pod, container v1.Container, files m
 	copy := exec.CommandContext(ctx, "kubectl", "exec", pod.Name, "--namespace", pod.Namespace, "-c", container.Name, "-i",
 		"--", "tar", "xmf", "-", "-C", "/", "--no-same-owner")
 	copy.Stdin = reader
-	copy.Stdout = os.Stdout
-	copy.Stderr = os.Stderr
-
 	go func() {
 		defer writer.Close()
 
diff --git a/pkg/skaffold/sync/sync.go b/pkg/skaffold/sync/sync.go
index 1994fe77..93b0bf14 100644
--- a/pkg/skaffold/sync/sync.go
+++ b/pkg/skaffold/sync/sync.go
@@ -17,6 +17,7 @@ limitations under the License.
 package sync
 
 import (
+	"bytes"
 	"context"
 	"fmt"
 	"os/exec"
@@ -247,7 +248,11 @@ func Perform(ctx context.Context, image string, files map[string]string, cmdFn f
 
 				cmds := cmdFn(ctx, p, c, files)
 				for _, cmd := range cmds {
+					buf := &bytes.Buffer{}
+					cmd.Stdout = buf
+					cmd.Stderr = buf
 					if err := util.RunCmd(cmd); err != nil {
+						logrus.Warnf("Sync failed: %s", buf.String())
 						return err
 					}
 					numSynced++

@prary
Copy link
Contributor Author

prary commented Apr 16, 2019

+					buf := &bytes.Buffer{}
+					cmd.Stdout = buf
+					cmd.Stderr = buf
 					if err := util.RunCmd(cmd); err != nil {
+						logrus.Warnf("Sync failed: %s", buf.String())
@corneliusweig this one is even better and cover delete operation also, will incorporate the changes.

Thanks for the enhancement.

@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@priyawadhwa priyawadhwa added the kokoro:run runs the kokoro jobs on a PR label Apr 17, 2019
@kokoro-team kokoro-team removed the kokoro:run runs the kokoro jobs on a PR label Apr 17, 2019
@balopat balopat added the kokoro:run runs the kokoro jobs on a PR label Apr 18, 2019
@kokoro-team kokoro-team removed the kokoro:run runs the kokoro jobs on a PR label Apr 18, 2019
pkg/skaffold/sync/sync.go Outdated Show resolved Hide resolved
@prary prary requested a review from tejal29 as a code owner April 23, 2019 15:12
@corneliusweig
Copy link
Contributor

corneliusweig commented Apr 23, 2019

@prary This currently does not build.

However, I think that the suggestion from @dgageot to use RunCmdOut would actually not work here, because stderr is swallowed by RunCmdOut. And at least on my system, tar and rm log errors to stderr. So in essence, the error output from the sync commands would not be shown.

I didn't realize that both stdout and stderr will be put into the returned error. So all good with this approach!

pkg/skaffold/sync/sync.go Outdated Show resolved Hide resolved
@corneliusweig
Copy link
Contributor

stdout should be completely uninteresting here. It is just the output of rm or tar xf, which do not print to stdout. And even if they did, it would just clutter the skaffold log IMHO

@briandealwis
Copy link
Member

This is great. If there's an error (for example, the non-debug distroless images don't include tar), then the user sees an warning:

WARN[0021] Skipping deploy due to sync error: copying files: Running [kubectl exec web-64df5fc8c7-p5p5j --namespace default -c web -i -- tar xmf - -C / --no-same-owner]: stdout OCI runtime exec failed: exec failed: container_linux.go:344: starting container process caused "exec: \"tar\": executable file not found in $PATH": unknown
, stderr: command terminated with exit code 126
, err: exit status 126: exit status 126 

@balopat balopat added the kokoro:run runs the kokoro jobs on a PR label Apr 23, 2019
@kokoro-team kokoro-team removed the kokoro:run runs the kokoro jobs on a PR label Apr 23, 2019
@prary
Copy link
Contributor Author

prary commented Apr 26, 2019

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.

What to do if you already signed the CLA

Individual signers
Corporate signers

Googlers: Go here for more info.

I signed it!

@nkubala nkubala dismissed stale reviews from dgageot and tejal29 April 29, 2019 20:31

addressed

@nkubala nkubala merged commit 2b4f190 into GoogleContainerTools:master Apr 29, 2019
@prary prary deleted the sync-log-output branch July 26, 2019 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.