From 2441c362fbf7495bca83838378c200172f0701d8 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Wed, 27 Nov 2019 09:25:18 +0100 Subject: [PATCH] Improve error output when kompose fails + Improve error when compose fails + Check if the compose file exists + Add test Fix #3288 Signed-off-by: David Gageot --- pkg/skaffold/initializer/init.go | 19 ++++++++---- pkg/skaffold/initializer/init_test.go | 42 +++++++++++++++++++++++++++ pkg/skaffold/util/cmd.go | 2 +- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/pkg/skaffold/initializer/init.go b/pkg/skaffold/initializer/init.go index 53664515bbc..c8a81f58918 100644 --- a/pkg/skaffold/initializer/init.go +++ b/pkg/skaffold/initializer/init.go @@ -117,11 +117,8 @@ func DoInit(ctx context.Context, out io.Writer, c Config) error { rootDir := "." if c.ComposeFile != "" { - // run kompose first to generate k8s manifests, then run skaffold init - logrus.Infof("running 'kompose convert' for file %s", c.ComposeFile) - komposeCmd := exec.CommandContext(ctx, "kompose", "convert", "-f", c.ComposeFile) - if err := util.RunCmd(komposeCmd); err != nil { - return errors.Wrap(err, "running kompose") + if err := runKompose(ctx, c.ComposeFile); err != nil { + return err } } @@ -228,6 +225,18 @@ func DoInit(ctx context.Context, out io.Writer, c Config) error { return nil } +// runKompose runs the `kompose` CLI before running skaffold init +func runKompose(ctx context.Context, composeFile string) error { + if _, err := os.Stat(composeFile); os.IsNotExist(err) { + return err + } + + logrus.Infof("running 'kompose convert' for file %s", composeFile) + komposeCmd := exec.CommandContext(ctx, "kompose", "convert", "-f", composeFile) + _, err := util.RunCmdOut(komposeCmd) + return err +} + // autoSelectBuilders takes a list of builders and images, checks if any of the builders' configured target // images match an image in the image list, and returns a list of the matching builder/image pairs. Also // separately returns the builder configs and images that didn't have any matches. diff --git a/pkg/skaffold/initializer/init_test.go b/pkg/skaffold/initializer/init_test.go index 6c1b767f84e..23ffe6d6e1b 100644 --- a/pkg/skaffold/initializer/init_test.go +++ b/pkg/skaffold/initializer/init_test.go @@ -18,12 +18,15 @@ package initializer import ( "bytes" + "context" "strings" "testing" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/jib" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" "github.com/GoogleContainerTools/skaffold/testutil" + "github.com/pkg/errors" ) func TestPrintAnalyzeJSON(t *testing.T) { @@ -613,3 +616,42 @@ func Test_canonicalizeName(t *testing.T) { }) } } + +func TestRunKompose(t *testing.T) { + tests := []struct { + description string + composeFile string + commands util.Command + expectedError string + }{ + { + description: "success", + composeFile: "docker-compose.yaml", + commands: testutil.CmdRunOut("kompose convert -f docker-compose.yaml", ""), + }, + { + description: "not found", + composeFile: "not-found.yaml", + expectedError: "no such file or directory", + }, + { + description: "failure", + composeFile: "docker-compose.yaml", + commands: testutil.CmdRunOutErr("kompose convert -f docker-compose.yaml", "", errors.New("BUG")), + expectedError: "BUG", + }, + } + + for _, test := range tests { + testutil.Run(t, test.description, func(t *testutil.T) { + t.NewTempDir().Touch("docker-compose.yaml").Chdir() + t.Override(&util.DefaultExecCommand, test.commands) + + err := runKompose(context.Background(), test.composeFile) + + if test.expectedError != "" { + t.CheckErrorContains(test.expectedError, err) + } + }) + } +} diff --git a/pkg/skaffold/util/cmd.go b/pkg/skaffold/util/cmd.go index 6fb29f65ae6..2efad86ebb0 100644 --- a/pkg/skaffold/util/cmd.go +++ b/pkg/skaffold/util/cmd.go @@ -74,7 +74,7 @@ func (*Commander) RunCmdOut(cmd *exec.Cmd) ([]byte, error) { err = cmd.Wait() if err != nil { - return stdout, errors.Wrapf(err, "Running %s: stdout %s, stderr: %s, err: %v", cmd.Args, stdout, stderr, err) + return stdout, errors.Wrapf(err, "Running %s\n - stdout: %s\n - stderr: %s", cmd.Args, stdout, stderr) } if len(stderr) > 0 {