Skip to content

Commit

Permalink
Improve error output when kompose fails
Browse files Browse the repository at this point in the history
+ Improve error when compose fails
+ Check if the compose file exists
+ Add test

Fix #3288

Signed-off-by: David Gageot <david@gageot.net>
  • Loading branch information
dgageot committed Nov 27, 2019
1 parent 580a176 commit 2441c36
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
19 changes: 14 additions & 5 deletions pkg/skaffold/initializer/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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.
Expand Down
42 changes: 42 additions & 0 deletions pkg/skaffold/initializer/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/skaffold/util/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2441c36

Please sign in to comment.