Skip to content

Commit

Permalink
Move code to jib package
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot committed Jan 14, 2019
1 parent 967c35d commit 27f71d9
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 69 deletions.
17 changes: 2 additions & 15 deletions pkg/skaffold/build/local/jib_gradle.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (b *Builder) buildJibGradle(ctx context.Context, out io.Writer, workspace s

func (b *Builder) buildJibGradleToDocker(ctx context.Context, out io.Writer, workspace string, artifact *latest.JibGradleArtifact) (string, error) {
skaffoldImage := generateJibImageRef(workspace, artifact.Project)
args := generateGradleArgs("jibDockerBuild", skaffoldImage, artifact)
args := jib.GenerateGradleArgs("jibDockerBuild", skaffoldImage, artifact)

if err := runGradleCommand(ctx, out, workspace, args); err != nil {
return "", err
Expand All @@ -50,7 +50,7 @@ func (b *Builder) buildJibGradleToDocker(ctx context.Context, out io.Writer, wor
func (b *Builder) buildJibGradleToRegistry(ctx context.Context, out io.Writer, workspace string, artifact *latest.Artifact) (string, error) {
initialTag := util.RandomID()
skaffoldImage := fmt.Sprintf("%s:%s", artifact.ImageName, initialTag)
args := generateGradleArgs("jib", skaffoldImage, artifact.JibGradleArtifact)
args := jib.GenerateGradleArgs("jib", skaffoldImage, artifact.JibGradleArtifact)

if err := runGradleCommand(ctx, out, workspace, args); err != nil {
return "", err
Expand All @@ -59,19 +59,6 @@ func (b *Builder) buildJibGradleToRegistry(ctx context.Context, out io.Writer, w
return docker.RemoteDigest(skaffoldImage)
}

// generateGradleArgs generates the arguments to Gradle for building the project as an image called `skaffoldImage`.
func generateGradleArgs(task string, imageName string, artifact *latest.JibGradleArtifact) []string {
var command string
if artifact.Project == "" {
command = ":" + task
} else {
// multi-module
command = fmt.Sprintf(":%s:%s", artifact.Project, task)
}

return []string{command, "--image=" + imageName}
}

func runGradleCommand(ctx context.Context, out io.Writer, workspace string, args []string) error {
cmd := jib.GradleCommand.CreateCommand(ctx, workspace, args)
cmd.Stdout = out
Expand Down
22 changes: 2 additions & 20 deletions pkg/skaffold/build/local/jib_maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (b *Builder) buildJibMavenToDocker(ctx context.Context, out io.Writer, work
}

skaffoldImage := generateJibImageRef(workspace, artifact.Module)
args := generateMavenArgs("dockerBuild", skaffoldImage, artifact)
args := jib.GenerateMavenArgs("dockerBuild", skaffoldImage, artifact)

if err := runMavenCommand(ctx, out, workspace, args); err != nil {
return "", err
Expand All @@ -65,7 +65,7 @@ func (b *Builder) buildJibMavenToRegistry(ctx context.Context, out io.Writer, wo

initialTag := util.RandomID()
skaffoldImage := fmt.Sprintf("%s:%s", artifact.ImageName, initialTag)
args := generateMavenArgs("build", skaffoldImage, artifact.JibMavenArtifact)
args := jib.GenerateMavenArgs("build", skaffoldImage, artifact.JibMavenArtifact)

if err := runMavenCommand(ctx, out, workspace, args); err != nil {
return "", err
Expand All @@ -74,24 +74,6 @@ func (b *Builder) buildJibMavenToRegistry(ctx context.Context, out io.Writer, wo
return docker.RemoteDigest(skaffoldImage)
}

// generateMavenArgs generates the arguments to Maven for building the project as an image called `skaffoldImage`.
func generateMavenArgs(goal string, imageName string, artifact *latest.JibMavenArtifact) []string {
var command []string
if artifact.Module == "" {
// single-module project
command = []string{"--non-recursive", "prepare-package", "jib:" + goal}
} else {
// multi-module project: we assume `package` is bound to `jib:<goal>`
command = []string{"--projects", artifact.Module, "--also-make", "package"}
}
command = append(command, "-Dimage="+imageName)
if artifact.Profile != "" {
command = append(command, "--activate-profiles", artifact.Profile)
}

return command
}

// verifyJibPackageGoal verifies that the referenced module has `package` bound to a single jib goal.
// It returns `nil` if the goal is matched, and an error if there is a mismatch.
func verifyJibPackageGoal(ctx context.Context, requiredGoal string, workspace string, artifact *latest.JibMavenArtifact) error {
Expand Down
34 changes: 0 additions & 34 deletions pkg/skaffold/build/local/jib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,6 @@ import (
"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestGenerateMavenArgs(t *testing.T) {
var testCases = []struct {
in latest.JibMavenArtifact
out []string
}{
{latest.JibMavenArtifact{}, []string{"--non-recursive", "prepare-package", "jib:goal", "-Dimage=image"}},
{latest.JibMavenArtifact{Profile: "profile"}, []string{"--non-recursive", "prepare-package", "jib:goal", "-Dimage=image", "--activate-profiles", "profile"}},
{latest.JibMavenArtifact{Module: "module"}, []string{"--projects", "module", "--also-make", "package", "-Dimage=image"}},
{latest.JibMavenArtifact{Module: "module", Profile: "profile"}, []string{"--projects", "module", "--also-make", "package", "-Dimage=image", "--activate-profiles", "profile"}},
}

for _, tt := range testCases {
args := generateMavenArgs("goal", "image", &tt.in)

testutil.CheckDeepEqual(t, tt.out, args)
}
}

func TestMavenVerifyJibPackageGoal(t *testing.T) {
var testCases = []struct {
requiredGoal string
Expand Down Expand Up @@ -71,22 +53,6 @@ func TestMavenVerifyJibPackageGoal(t *testing.T) {
}
}

func TestGenerateGradleArgs(t *testing.T) {
var testCases = []struct {
in latest.JibGradleArtifact
out []string
}{
{latest.JibGradleArtifact{}, []string{":task", "--image=image"}},
{latest.JibGradleArtifact{Project: "project"}, []string{":project:task", "--image=image"}},
}

for _, tt := range testCases {
command := generateGradleArgs("task", "image", &tt.in)

testutil.CheckDeepEqual(t, tt.out, command)
}
}

func TestGenerateJibImageRef(t *testing.T) {
var testCases = []struct {
workspace string
Expand Down
13 changes: 13 additions & 0 deletions pkg/skaffold/jib/jib_gradle.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,16 @@ func getCommandGradle(ctx context.Context, workspace string, a *latest.JibGradle
}
return GradleCommand.CreateCommand(ctx, workspace, args)
}

// GenerateGradleArgs generates the arguments to Gradle for building the project as an image.
func GenerateGradleArgs(task string, imageName string, artifact *latest.JibGradleArtifact) []string {
var command string
if artifact.Project == "" {
command = ":" + task
} else {
// multi-module
command = fmt.Sprintf(":%s:%s", artifact.Project, task)
}

return []string{command, "--image=" + imageName}
}
16 changes: 16 additions & 0 deletions pkg/skaffold/jib/jib_gradle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,19 @@ func TestGetCommandGradle(t *testing.T) {
})
}
}

func TestGenerateGradleArgs(t *testing.T) {
var testCases = []struct {
in latest.JibGradleArtifact
out []string
}{
{latest.JibGradleArtifact{}, []string{":task", "--image=image"}},
{latest.JibGradleArtifact{Project: "project"}, []string{":project:task", "--image=image"}},
}

for _, tt := range testCases {
command := GenerateGradleArgs("task", "image", &tt.in)

testutil.CheckDeepEqual(t, tt.out, command)
}
}
18 changes: 18 additions & 0 deletions pkg/skaffold/jib/jib_maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,21 @@ func getCommandMaven(ctx context.Context, workspace string, a *latest.JibMavenAr

return MavenCommand.CreateCommand(ctx, workspace, args)
}

// GenerateMavenArgs generates the arguments to Maven for building the project as an image.
func GenerateMavenArgs(goal string, imageName string, artifact *latest.JibMavenArtifact) []string {
var command []string
if artifact.Module == "" {
// single-module project
command = []string{"--non-recursive", "prepare-package", "jib:" + goal}
} else {
// multi-module project: we assume `package` is bound to `jib:<goal>`
command = []string{"--projects", artifact.Module, "--also-make", "package"}
}
command = append(command, "-Dimage="+imageName)
if artifact.Profile != "" {
command = append(command, "--activate-profiles", artifact.Profile)
}

return command
}
18 changes: 18 additions & 0 deletions pkg/skaffold/jib/jib_maven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,21 @@ func TestGetCommandMaven(t *testing.T) {
})
}
}

func TestGenerateMavenArgs(t *testing.T) {
var testCases = []struct {
in latest.JibMavenArtifact
out []string
}{
{latest.JibMavenArtifact{}, []string{"--non-recursive", "prepare-package", "jib:goal", "-Dimage=image"}},
{latest.JibMavenArtifact{Profile: "profile"}, []string{"--non-recursive", "prepare-package", "jib:goal", "-Dimage=image", "--activate-profiles", "profile"}},
{latest.JibMavenArtifact{Module: "module"}, []string{"--projects", "module", "--also-make", "package", "-Dimage=image"}},
{latest.JibMavenArtifact{Module: "module", Profile: "profile"}, []string{"--projects", "module", "--also-make", "package", "-Dimage=image", "--activate-profiles", "profile"}},
}

for _, tt := range testCases {
args := GenerateMavenArgs("goal", "image", &tt.in)

testutil.CheckDeepEqual(t, tt.out, args)
}
}

0 comments on commit 27f71d9

Please sign in to comment.