From 391629e4333f2bf0ed2558bf0a7b19646cf263a2 Mon Sep 17 00:00:00 2001 From: gsquared94 Date: Wed, 14 Oct 2020 18:27:31 +0530 Subject: [PATCH 1/4] rename baseImage to fromImage --- docs/content/en/schemas/v2beta9.json | 8 +++- integration/examples/jib-sync/README.md | 39 +++++++------------ integration/examples/jib-sync/build.gradle | 6 --- integration/examples/jib-sync/pom.xml | 3 -- .../examples/jib-sync/skaffold-gradle.yaml | 1 + .../examples/jib-sync/skaffold-maven.yaml | 1 + .../src/main/java/hello/HelloController.java | 2 +- pkg/skaffold/build/jib/gradle.go | 4 +- pkg/skaffold/build/jib/gradle_test.go | 16 ++++++++ pkg/skaffold/build/jib/maven.go | 3 ++ pkg/skaffold/build/jib/maven_test.go | 14 +++++++ pkg/skaffold/schema/latest/config.go | 3 ++ 12 files changed, 63 insertions(+), 37 deletions(-) diff --git a/docs/content/en/schemas/v2beta9.json b/docs/content/en/schemas/v2beta9.json index 4674dc459e9..27babe89252 100755 --- a/docs/content/en/schemas/v2beta9.json +++ b/docs/content/en/schemas/v2beta9.json @@ -1503,6 +1503,11 @@ "[\"--no-build-cache\"]" ] }, + "fromImage": { + "type": "string", + "description": "overrides the default jib base image.", + "x-intellij-html-description": "overrides the default jib base image." + }, "project": { "type": "string", "description": "selects which sub-project to build for multi-module builds.", @@ -1521,7 +1526,8 @@ "preferredOrder": [ "project", "args", - "type" + "type", + "fromImage" ], "additionalProperties": false, "description": "builds images using the [Jib plugins for Maven and Gradle](https://github.com/GoogleContainerTools/jib/).", diff --git a/integration/examples/jib-sync/README.md b/integration/examples/jib-sync/README.md index a8e99ada543..c2ef3f59c34 100644 --- a/integration/examples/jib-sync/README.md +++ b/integration/examples/jib-sync/README.md @@ -63,41 +63,30 @@ build: This example is designed around the functionality available in [Spring Boot Developer Tools](https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-devtools) for developing against running applications. -Some additional steps in your java build are required for this to work: -- Sync requires `tar` on the running container to copy files over. The default base image that Jib uses `gcr.io/distroless/java` does not include `tar` or any utilities. During development you must use a base image that includes `tar`, in this example we use the `debug` flavor of distroless: `gcr.io/distroless/java:debug` +Some additional steps are required for this to work: +- Sync requires `tar` on the running container to copy files over. The default base image that Jib uses `gcr.io/distroless/java` does not include `tar` or any utilities. During development, you must use a base image that includes `tar`, in this example we use the `debug` flavor of distroless: `gcr.io/distroless/java:debug` -`maven` -```xml - - com.google.cloud.tools - jib-maven-plugin - ${jib.maven-plugin-version} - - ... - - gcr.io/distroless/java:debug - - - -``` +This can be done directly in the artifact configuration by overriding the `fromImage` property. -`gradle` -```groovy -jib { - ... - from { - image = "gcr.io/distroless/java:debug" - } -} +```yaml +build: + artifacts: + - image: skaffold-example + context: . + jib: + fromImage: gcr.io/distroless/java:debug + sync: + auto: {} ``` + - You must include the `spring-boot-devtools` dependency at the `compile/implementation` scope, which is contrary to the configuration outlined in the [official docs](https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-devtools). Because jib is unaware of any special spring only configuration in your builds, we recommend using profiles to turn on or off devtools support in your jib container builds. `maven` ```xml - sync + sync org.springframework.boot diff --git a/integration/examples/jib-sync/build.gradle b/integration/examples/jib-sync/build.gradle index ff3f6949177..d5202b46fb0 100644 --- a/integration/examples/jib-sync/build.gradle +++ b/integration/examples/jib-sync/build.gradle @@ -21,9 +21,3 @@ dependencies { testImplementation "org.springframework.boot:spring-boot-starter-test" } - -jib { - from { - image = "gcr.io/distroless/java:debug" - } -} diff --git a/integration/examples/jib-sync/pom.xml b/integration/examples/jib-sync/pom.xml index 8030aa71376..c04cfc4d295 100644 --- a/integration/examples/jib-sync/pom.xml +++ b/integration/examples/jib-sync/pom.xml @@ -37,9 +37,6 @@ jib-maven-plugin ${jib.maven-plugin-version} - - gcr.io/distroless/java:debug - -Djava.security.egd=file:/dev/./urandom diff --git a/integration/examples/jib-sync/skaffold-gradle.yaml b/integration/examples/jib-sync/skaffold-gradle.yaml index 5672464c690..ca9521b8874 100644 --- a/integration/examples/jib-sync/skaffold-gradle.yaml +++ b/integration/examples/jib-sync/skaffold-gradle.yaml @@ -7,6 +7,7 @@ build: type: gradle args: - -Psync + fromImage: gcr.io/distroless/java:debug sync: auto: {} diff --git a/integration/examples/jib-sync/skaffold-maven.yaml b/integration/examples/jib-sync/skaffold-maven.yaml index 6a1d0f107e7..d808bd1cc55 100644 --- a/integration/examples/jib-sync/skaffold-maven.yaml +++ b/integration/examples/jib-sync/skaffold-maven.yaml @@ -8,6 +8,7 @@ build: args: - --no-transfer-progress - -Psync + fromImage: gcr.io/distroless/java:debug sync: auto: {} diff --git a/integration/examples/jib-sync/src/main/java/hello/HelloController.java b/integration/examples/jib-sync/src/main/java/hello/HelloController.java index 886953a7de6..740ed2e4e73 100644 --- a/integration/examples/jib-sync/src/main/java/hello/HelloController.java +++ b/integration/examples/jib-sync/src/main/java/hello/HelloController.java @@ -13,6 +13,6 @@ public class HelloController { @RequestMapping("/") public String index() throws Exception { - return "text-to-replace\n"; + return "text-to-change\n"; } } diff --git a/pkg/skaffold/build/jib/gradle.go b/pkg/skaffold/build/jib/gradle.go index 3229c2845b6..3984629646d 100644 --- a/pkg/skaffold/build/jib/gradle.go +++ b/pkg/skaffold/build/jib/gradle.go @@ -103,7 +103,9 @@ func GenerateGradleBuildArgs(task string, imageName string, a *latest.JibArtifac // jib doesn't support marking specific registries as insecure args = append(args, "-Djib.allowInsecureRegistries=true") } - + if a.BaseImage != "" { + args = append(args, fmt.Sprintf("-Djib.from.image=%s", a.BaseImage)) + } args = append(args, "--image="+imageName) return args } diff --git a/pkg/skaffold/build/jib/gradle_test.go b/pkg/skaffold/build/jib/gradle_test.go index 02e72f545f4..a7a8fad948c 100644 --- a/pkg/skaffold/build/jib/gradle_test.go +++ b/pkg/skaffold/build/jib/gradle_test.go @@ -55,6 +55,13 @@ func TestBuildJibGradleToDocker(t *testing.T) { "gradle fake-gradleBuildArgs-for-project-for-jibDockerBuild --image=img:tag", ), }, + { + description: "build with custom base image", + artifact: &latest.JibArtifact{BaseImage: "docker://busybox"}, + commands: testutil.CmdRun( + "gradle fake-gradleBuildArgs-for-jibDockerBuild -Djib.from.image=docker://busybox --image=img:tag", + ), + }, { description: "fail build", artifact: &latest.JibArtifact{}, @@ -114,6 +121,13 @@ func TestBuildJibGradleToRegistry(t *testing.T) { "gradle fake-gradleBuildArgs-for-project-for-jib --image=img:tag", ), }, + { + description: "build with custom base image", + artifact: &latest.JibArtifact{BaseImage: "docker://busybox"}, + commands: testutil.CmdRun( + "gradle fake-gradleBuildArgs-for-jib -Djib.from.image=docker://busybox --image=img:tag", + ), + }, { description: "fail build", artifact: &latest.JibArtifact{}, @@ -337,6 +351,8 @@ func TestGenerateGradleBuildArgs(t *testing.T) { {"multi module", latest.JibArtifact{Project: "project"}, "image", false, nil, []string{"fake-gradleBuildArgs-for-project-for-testTask", "--image=image"}}, {"multi module without tests", latest.JibArtifact{Project: "project"}, "image", true, nil, []string{"fake-gradleBuildArgs-for-project-for-testTask-skipTests", "--image=image"}}, {"multi module without tests with insecure registries", latest.JibArtifact{Project: "project"}, "registry.tld/image", true, map[string]bool{"registry.tld": true}, []string{"fake-gradleBuildArgs-for-project-for-testTask-skipTests", "-Djib.allowInsecureRegistries=true", "--image=registry.tld/image"}}, + {"single module with custom base image", latest.JibArtifact{BaseImage: "docker://busybox"}, "image", false, nil, []string{"fake-gradleBuildArgs-for-testTask", "-Djib.from.image=docker://busybox", "--image=image"}}, + {"multi module with custom base image", latest.JibArtifact{Project: "project", BaseImage: "docker://busybox"}, "image", false, nil, []string{"fake-gradleBuildArgs-for-project-for-testTask", "-Djib.from.image=docker://busybox", "--image=image"}}, } for _, test := range tests { testutil.Run(t, test.description, func(t *testutil.T) { diff --git a/pkg/skaffold/build/jib/maven.go b/pkg/skaffold/build/jib/maven.go index 43a7981f5e5..4b860e28d35 100644 --- a/pkg/skaffold/build/jib/maven.go +++ b/pkg/skaffold/build/jib/maven.go @@ -104,6 +104,9 @@ func GenerateMavenBuildArgs(goal string, imageName string, a *latest.JibArtifact // jib doesn't support marking specific registries as insecure args = append(args, "-Djib.allowInsecureRegistries=true") } + if a.BaseImage != "" { + args = append(args, fmt.Sprintf("-Djib.from.image=%s", a.BaseImage)) + } args = append(args, "-Dimage="+imageName) return args diff --git a/pkg/skaffold/build/jib/maven_test.go b/pkg/skaffold/build/jib/maven_test.go index cb6b965f106..f4be49a84f7 100644 --- a/pkg/skaffold/build/jib/maven_test.go +++ b/pkg/skaffold/build/jib/maven_test.go @@ -55,6 +55,13 @@ func TestBuildJibMavenToDocker(t *testing.T) { "mvn fake-mavenBuildArgs-for-module-for-dockerBuild -Dimage=img:tag", ), }, + { + description: "build with custom base image", + artifact: &latest.JibArtifact{BaseImage: "docker://busybox"}, + commands: testutil.CmdRun( + "mvn fake-mavenBuildArgs-for-dockerBuild -Djib.from.image=docker://busybox -Dimage=img:tag", + ), + }, { description: "fail build", artifact: &latest.JibArtifact{}, @@ -110,6 +117,11 @@ func TestBuildJibMavenToRegistry(t *testing.T) { artifact: &latest.JibArtifact{Project: "module"}, commands: testutil.CmdRun("mvn fake-mavenBuildArgs-for-module-for-build -Dimage=img:tag"), }, + { + description: "build with custom base image", + artifact: &latest.JibArtifact{BaseImage: "docker://busybox"}, + commands: testutil.CmdRun("mvn fake-mavenBuildArgs-for-build -Djib.from.image=docker://busybox -Dimage=img:tag"), + }, { description: "fail build", artifact: &latest.JibArtifact{}, @@ -325,6 +337,8 @@ func TestGenerateMavenBuildArgs(t *testing.T) { {"multi module", latest.JibArtifact{Project: "module"}, "image", false, nil, []string{"fake-mavenBuildArgs-for-module-for-test-goal", "-Dimage=image"}}, {"multi module without tests", latest.JibArtifact{Project: "module"}, "image", true, nil, []string{"fake-mavenBuildArgs-for-module-for-test-goal-skipTests", "-Dimage=image"}}, {"multi module without tests with insecure-registry", latest.JibArtifact{Project: "module"}, "registry.tld/image", true, map[string]bool{"registry.tld": true}, []string{"fake-mavenBuildArgs-for-module-for-test-goal-skipTests", "-Djib.allowInsecureRegistries=true", "-Dimage=registry.tld/image"}}, + {"single module with custom base image", latest.JibArtifact{BaseImage: "docker://busybox"}, "image", false, nil, []string{"fake-mavenBuildArgs-for-test-goal", "-Djib.from.image=docker://busybox", "-Dimage=image"}}, + {"multi module with custom base image", latest.JibArtifact{Project: "module", BaseImage: "docker://busybox"}, "image", false, nil, []string{"fake-mavenBuildArgs-for-module-for-test-goal", "-Djib.from.image=docker://busybox", "-Dimage=image"}}, } for _, test := range tests { testutil.Run(t, test.description, func(t *testutil.T) { diff --git a/pkg/skaffold/schema/latest/config.go b/pkg/skaffold/schema/latest/config.go index a592893db93..e568926d5ee 100644 --- a/pkg/skaffold/schema/latest/config.go +++ b/pkg/skaffold/schema/latest/config.go @@ -1077,4 +1077,7 @@ type JibArtifact struct { // `maven`: for Maven. // `gradle`: for Gradle. Type string `yaml:"type,omitempty"` + + // BaseImage overrides the default jib base image. + BaseImage string `yaml:"fromImage,omitempty"` } From 30a9b5d9f4e10c33e468c2d00d2f902820679a05 Mon Sep 17 00:00:00 2001 From: Gaurav <39389231+gsquared94@users.noreply.github.com> Date: Wed, 14 Oct 2020 18:29:06 +0530 Subject: [PATCH 2/4] Update HelloController.java --- .../examples/jib-sync/src/main/java/hello/HelloController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration/examples/jib-sync/src/main/java/hello/HelloController.java b/integration/examples/jib-sync/src/main/java/hello/HelloController.java index 740ed2e4e73..886953a7de6 100644 --- a/integration/examples/jib-sync/src/main/java/hello/HelloController.java +++ b/integration/examples/jib-sync/src/main/java/hello/HelloController.java @@ -13,6 +13,6 @@ public class HelloController { @RequestMapping("/") public String index() throws Exception { - return "text-to-change\n"; + return "text-to-replace\n"; } } From b1546333071f34e4543e069aa488fa681bf55c2c Mon Sep 17 00:00:00 2001 From: Gaurav <39389231+gsquared94@users.noreply.github.com> Date: Thu, 15 Oct 2020 13:49:01 +0530 Subject: [PATCH 3/4] Update pkg/skaffold/schema/latest/config.go Co-authored-by: Brian de Alwis --- pkg/skaffold/schema/latest/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/skaffold/schema/latest/config.go b/pkg/skaffold/schema/latest/config.go index e568926d5ee..4bb98357e2e 100644 --- a/pkg/skaffold/schema/latest/config.go +++ b/pkg/skaffold/schema/latest/config.go @@ -1078,6 +1078,6 @@ type JibArtifact struct { // `gradle`: for Gradle. Type string `yaml:"type,omitempty"` - // BaseImage overrides the default jib base image. + // BaseImage overrides the configured jib base image. BaseImage string `yaml:"fromImage,omitempty"` } From faf8679bfbe551152260094f7636d70d74653faf Mon Sep 17 00:00:00 2001 From: Gaurav <39389231+gsquared94@users.noreply.github.com> Date: Thu, 15 Oct 2020 13:50:33 +0530 Subject: [PATCH 4/4] Update v2beta9.json --- docs/content/en/schemas/v2beta9.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/content/en/schemas/v2beta9.json b/docs/content/en/schemas/v2beta9.json index 27babe89252..e1d9ad82b73 100755 --- a/docs/content/en/schemas/v2beta9.json +++ b/docs/content/en/schemas/v2beta9.json @@ -1505,8 +1505,8 @@ }, "fromImage": { "type": "string", - "description": "overrides the default jib base image.", - "x-intellij-html-description": "overrides the default jib base image." + "description": "overrides the configured jib base image.", + "x-intellij-html-description": "overrides the configured jib base image." }, "project": { "type": "string",