Skip to content

Commit f54473e

Browse files
authored
Merge pull request #1366 from dgageot/minor-jib-refactoring
Extract push/no-push logic into builder
2 parents e8faa62 + 55c353b commit f54473e

File tree

3 files changed

+40
-32
lines changed

3 files changed

+40
-32
lines changed

pkg/skaffold/build/local/jib_gradle.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,16 @@ import (
2828
"github.com/sirupsen/logrus"
2929
)
3030

31-
func (b *Builder) buildJibGradleToDocker(ctx context.Context, out io.Writer, workspace string, a *latest.JibGradleArtifact) (string, error) {
32-
skaffoldImage := generateJibImageRef(workspace, a.Project)
33-
args := generateGradleArgs("jibDockerBuild", skaffoldImage, a)
31+
func (b *Builder) buildJibGradle(ctx context.Context, out io.Writer, workspace string, artifact *latest.Artifact) (string, error) {
32+
if b.pushImages {
33+
return b.buildJibGradleToRegistry(ctx, out, workspace, artifact)
34+
}
35+
return b.buildJibGradleToDocker(ctx, out, workspace, artifact.JibGradleArtifact)
36+
}
37+
38+
func (b *Builder) buildJibGradleToDocker(ctx context.Context, out io.Writer, workspace string, artifact *latest.JibGradleArtifact) (string, error) {
39+
skaffoldImage := generateJibImageRef(workspace, artifact.Project)
40+
args := generateGradleArgs("jibDockerBuild", skaffoldImage, artifact)
3441

3542
if err := runGradleCommand(ctx, out, workspace, args); err != nil {
3643
return "", err
@@ -52,16 +59,16 @@ func (b *Builder) buildJibGradleToRegistry(ctx context.Context, out io.Writer, w
5259
}
5360

5461
// generateGradleArgs generates the arguments to Gradle for building the project as an image called `skaffoldImage`.
55-
func generateGradleArgs(task string, skaffoldImage string, a *latest.JibGradleArtifact) []string {
62+
func generateGradleArgs(task string, imageName string, artifact *latest.JibGradleArtifact) []string {
5663
var command string
57-
if a.Project == "" {
64+
if artifact.Project == "" {
5865
command = ":" + task
5966
} else {
6067
// multi-module
61-
command = fmt.Sprintf(":%s:%s", a.Project, task)
68+
command = fmt.Sprintf(":%s:%s", artifact.Project, task)
6269
}
6370

64-
return []string{command, "--image=" + skaffoldImage}
71+
return []string{command, "--image=" + imageName}
6572
}
6673

6774
func runGradleCommand(ctx context.Context, out io.Writer, workspace string, args []string) error {

pkg/skaffold/build/local/jib_maven.go

+24-17
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,23 @@ import (
2929
"github.com/sirupsen/logrus"
3030
)
3131

32-
func (b *Builder) buildJibMavenToDocker(ctx context.Context, out io.Writer, workspace string, a *latest.JibMavenArtifact) (string, error) {
32+
func (b *Builder) buildJibMaven(ctx context.Context, out io.Writer, workspace string, artifact *latest.Artifact) (string, error) {
33+
if b.pushImages {
34+
return buildJibMavenToRegistry(ctx, out, workspace, artifact)
35+
}
36+
return buildJibMavenToDocker(ctx, out, workspace, artifact.JibMavenArtifact)
37+
}
38+
39+
func buildJibMavenToDocker(ctx context.Context, out io.Writer, workspace string, artifact *latest.JibMavenArtifact) (string, error) {
3340
// If this is a multi-module project, we require `package` be bound to jib:dockerBuild
34-
if a.Module != "" {
35-
if err := verifyJibPackageGoal(ctx, "dockerBuild", workspace, a); err != nil {
41+
if artifact.Module != "" {
42+
if err := verifyJibPackageGoal(ctx, "dockerBuild", workspace, artifact); err != nil {
3643
return "", err
3744
}
3845
}
3946

40-
skaffoldImage := generateJibImageRef(workspace, a.Module)
41-
args := generateMavenArgs("dockerBuild", skaffoldImage, a)
47+
skaffoldImage := generateJibImageRef(workspace, artifact.Module)
48+
args := generateMavenArgs("dockerBuild", skaffoldImage, artifact)
4249

4350
if err := runMavenCommand(ctx, out, workspace, args); err != nil {
4451
return "", err
@@ -47,7 +54,7 @@ func (b *Builder) buildJibMavenToDocker(ctx context.Context, out io.Writer, work
4754
return skaffoldImage, nil
4855
}
4956

50-
func (b *Builder) buildJibMavenToRegistry(ctx context.Context, out io.Writer, workspace string, artifact *latest.Artifact) (string, error) {
57+
func buildJibMavenToRegistry(ctx context.Context, out io.Writer, workspace string, artifact *latest.Artifact) (string, error) {
5158
// If this is a multi-module project, we require `package` be bound to jib:build
5259
if artifact.JibMavenArtifact.Module != "" {
5360
if err := verifyJibPackageGoal(ctx, "build", workspace, artifact.JibMavenArtifact); err != nil {
@@ -67,30 +74,30 @@ func (b *Builder) buildJibMavenToRegistry(ctx context.Context, out io.Writer, wo
6774
}
6875

6976
// generateMavenArgs generates the arguments to Maven for building the project as an image called `skaffoldImage`.
70-
func generateMavenArgs(goal string, skaffoldImage string, a *latest.JibMavenArtifact) []string {
77+
func generateMavenArgs(goal string, imageName string, artifact *latest.JibMavenArtifact) []string {
7178
var command []string
72-
if a.Module == "" {
79+
if artifact.Module == "" {
7380
// single-module project
7481
command = []string{"--non-recursive", "prepare-package", "jib:" + goal}
7582
} else {
7683
// multi-module project: we assume `package` is bound to `jib:<goal>`
77-
command = []string{"--projects", a.Module, "--also-make", "package"}
84+
command = []string{"--projects", artifact.Module, "--also-make", "package"}
7885
}
79-
command = append(command, "-Dimage="+skaffoldImage)
80-
if a.Profile != "" {
81-
command = append(command, "--activate-profiles", a.Profile)
86+
command = append(command, "-Dimage="+imageName)
87+
if artifact.Profile != "" {
88+
command = append(command, "--activate-profiles", artifact.Profile)
8289
}
8390

8491
return command
8592
}
8693

8794
// verifyJibPackageGoal verifies that the referenced module has `package` bound to a single jib goal.
8895
// It returns `nil` if the goal is matched, and an error if there is a mismatch.
89-
func verifyJibPackageGoal(ctx context.Context, requiredGoal string, workspace string, a *latest.JibMavenArtifact) error {
96+
func verifyJibPackageGoal(ctx context.Context, requiredGoal string, workspace string, artifact *latest.JibMavenArtifact) error {
9097
// cannot use --non-recursive
91-
command := []string{"--quiet", "--projects", a.Module, "jib:_skaffold-package-goals"}
92-
if a.Profile != "" {
93-
command = append(command, "--activate-profiles", a.Profile)
98+
command := []string{"--quiet", "--projects", artifact.Module, "jib:_skaffold-package-goals"}
99+
if artifact.Profile != "" {
100+
command = append(command, "--activate-profiles", artifact.Profile)
94101
}
95102

96103
cmd := jib.MavenCommand.CreateCommand(ctx, workspace, command)
@@ -100,7 +107,7 @@ func verifyJibPackageGoal(ctx context.Context, requiredGoal string, workspace st
100107
return errors.Wrap(err, "could not obtain jib package goals")
101108
}
102109
goals := util.NonEmptyLines(stdout)
103-
logrus.Debugf("jib bound package goals for %s %s: %v (%d)", workspace, a.Module, goals, len(goals))
110+
logrus.Debugf("jib bound package goals for %s %s: %v (%d)", workspace, artifact.Module, goals, len(goals))
104111
if len(goals) != 1 {
105112
return errors.New("skaffold requires a single jib goal bound to 'package'")
106113
}

pkg/skaffold/build/local/local.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,10 @@ func (b *Builder) runBuildForArtifact(ctx context.Context, out io.Writer, artifa
9090
return b.buildBazel(ctx, out, artifact.Workspace, artifact.BazelArtifact)
9191

9292
case artifact.JibMavenArtifact != nil:
93-
if b.pushImages {
94-
return b.buildJibMavenToRegistry(ctx, out, artifact.Workspace, artifact)
95-
}
96-
return b.buildJibMavenToDocker(ctx, out, artifact.Workspace, artifact.JibMavenArtifact)
93+
return b.buildJibMaven(ctx, out, artifact.Workspace, artifact)
9794

9895
case artifact.JibGradleArtifact != nil:
99-
if b.pushImages {
100-
return b.buildJibGradleToRegistry(ctx, out, artifact.Workspace, artifact)
101-
}
102-
return b.buildJibGradleToDocker(ctx, out, artifact.Workspace, artifact.JibGradleArtifact)
96+
return b.buildJibGradle(ctx, out, artifact.Workspace, artifact)
10397

10498
default:
10599
return "", fmt.Errorf("undefined artifact type: %+v", artifact.ArtifactType)

0 commit comments

Comments
 (0)