Skip to content

Commit 26b39da

Browse files
authored
Adds BuildImageTask configuration. (GoogleContainerTools#180)
1 parent 493441c commit 26b39da

7 files changed

Lines changed: 477 additions & 3 deletions

File tree

‎jib-gradle-plugin/build.gradle‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ dependencies {
5151
compile gradleApi()
5252

5353
// NullAway errorprone plugin
54-
// TODO: Update to latest version once https://github.com/uber/NullAway/issues/133 is fixed.
55-
apt 'com.uber.nullaway:nullaway:0.3.2'
54+
apt 'com.uber.nullaway:nullaway:0.3.7'
5655
errorprone 'com.google.errorprone:error_prone_core:2.2.0'
5756
}
5857

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2018 Google LLC. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.jib.gradle;
18+
19+
import com.google.cloud.tools.jib.image.json.BuildableManifestTemplate;
20+
import java.util.List;
21+
import javax.annotation.Nullable;
22+
import org.gradle.api.DefaultTask;
23+
import org.gradle.api.tasks.Input;
24+
import org.gradle.api.tasks.Nested;
25+
import org.gradle.api.tasks.Optional;
26+
import org.gradle.api.tasks.TaskAction;
27+
28+
/** Builds a container image. */
29+
public class BuildImageTask extends DefaultTask {
30+
31+
@Nullable private ImageConfiguration from;
32+
@Nullable private ImageConfiguration to;
33+
@Nullable private List<String> jvmFlags;
34+
@Nullable private String mainClass;
35+
private boolean reproducible;
36+
@Nullable private Class<? extends BuildableManifestTemplate> format;
37+
38+
@Nested
39+
@Nullable
40+
public ImageConfiguration getFrom() {
41+
return from;
42+
}
43+
44+
@Nested
45+
@Nullable
46+
public ImageConfiguration getTo() {
47+
return to;
48+
}
49+
50+
@Input
51+
@Nullable
52+
public List<String> getJvmFlags() {
53+
return jvmFlags;
54+
}
55+
56+
@Input
57+
@Nullable
58+
@Optional
59+
public String getMainClass() {
60+
return mainClass;
61+
}
62+
63+
@Input
64+
public boolean getReproducible() {
65+
return reproducible;
66+
}
67+
68+
@Input
69+
@Nullable
70+
public Class<? extends BuildableManifestTemplate> getFormat() {
71+
return format;
72+
}
73+
74+
@TaskAction
75+
public void buildImage() {
76+
// TODO: Implement.
77+
}
78+
79+
void applyExtension(JibExtension jibExtension) {
80+
from = jibExtension.getFrom();
81+
to = jibExtension.getTo();
82+
jvmFlags = jibExtension.getJvmFlags();
83+
mainClass = jibExtension.getMainClass();
84+
reproducible = jibExtension.getReproducible();
85+
format = jibExtension.getFormat();
86+
}
87+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2018 Google LLC. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.jib.gradle;
18+
19+
import javax.annotation.Nullable;
20+
import org.gradle.api.tasks.Input;
21+
import org.gradle.api.tasks.Optional;
22+
23+
/**
24+
* A bean that configures an image to be used in the build steps. This is configurable with Groovy
25+
* closures and can be validated when used as a task input.
26+
*
27+
* <p>{@code image} (required) is the image reference and {@code credHelper} (optional) is the name
28+
* (after {@code docker-credential} of the credential helper for accessing the {@code image}.
29+
*/
30+
public class ImageConfiguration {
31+
32+
@Nullable private String image;
33+
@Nullable private String credHelper;
34+
35+
@Input
36+
@Nullable
37+
public String getImage() {
38+
return image;
39+
}
40+
41+
public void setImage(String image) {
42+
this.image = image;
43+
}
44+
45+
@Input
46+
@Nullable
47+
@Optional
48+
public String getCredHelper() {
49+
return credHelper;
50+
}
51+
52+
public void setCredHelper(String credHelper) {
53+
this.credHelper = credHelper;
54+
}
55+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* Copyright 2018 Google LLC. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.jib.gradle;
18+
19+
import com.google.cloud.tools.jib.image.json.BuildableManifestTemplate;
20+
import com.google.cloud.tools.jib.image.json.OCIManifestTemplate;
21+
import com.google.cloud.tools.jib.image.json.V22ManifestTemplate;
22+
import com.google.common.annotations.VisibleForTesting;
23+
import java.util.Collections;
24+
import java.util.List;
25+
import javax.annotation.Nullable;
26+
import javax.inject.Inject;
27+
import org.gradle.api.Action;
28+
import org.gradle.api.Project;
29+
import org.gradle.api.model.ObjectFactory;
30+
import org.gradle.api.provider.ListProperty;
31+
import org.gradle.api.provider.Property;
32+
33+
/**
34+
* Plugin extension for {@link JibPlugin}.
35+
*
36+
* <p>Example configuration:
37+
*
38+
* <pre>{@code
39+
* jib {
40+
* from {
41+
* image = ‘gcr.io/my-gcp-project/my-base-image’
42+
* credHelper = ‘gcr’
43+
* }
44+
* to {
45+
* image = ‘gcr.io/gcp-project/my-app:built-with-jib’
46+
* credHelper = ‘ecr-login’
47+
* }
48+
* jvmFlags = [‘-Xms512m’, ‘-Xdebug’]
49+
* mainClass = ‘com.mycompany.myproject.Main’
50+
* reproducible = true
51+
* format = OCI
52+
* }
53+
* }</pre>
54+
*/
55+
public class JibExtension {
56+
57+
// TODO: Consolidate with BuildImageMojo#ImageFormat.
58+
/** Enumeration of {@link BuildableManifestTemplate}s. */
59+
@VisibleForTesting
60+
enum ImageFormat {
61+
Docker(V22ManifestTemplate.class),
62+
OCI(OCIManifestTemplate.class);
63+
64+
private final Class<? extends BuildableManifestTemplate> manifestTemplateClass;
65+
66+
ImageFormat(Class<? extends BuildableManifestTemplate> manifestTemplateClass) {
67+
this.manifestTemplateClass = manifestTemplateClass;
68+
}
69+
70+
private Class<? extends BuildableManifestTemplate> getManifestTemplateClass() {
71+
return manifestTemplateClass;
72+
}
73+
}
74+
75+
// Defines default configuration values.
76+
private static final String DEFAULT_FROM_IMAGE = "gcr.io/distroless/java";
77+
private static final List<String> DEFAULT_JVM_FLAGS = Collections.emptyList();
78+
private static final ImageFormat DEFAULT_FORMAT = ImageFormat.Docker;
79+
private static final boolean DEFAULT_REPRODUCIBLE = true;
80+
81+
private final ImageConfiguration from;
82+
private final ImageConfiguration to;
83+
private final ListProperty<String> jvmFlags;
84+
private final Property<String> mainClass;
85+
private final Property<Boolean> reproducible;
86+
private final Property<ImageFormat> format;
87+
88+
@Inject
89+
public JibExtension(Project project) {
90+
ObjectFactory objectFactory = project.getObjects();
91+
92+
from = objectFactory.newInstance(ImageConfiguration.class);
93+
to = objectFactory.newInstance(ImageConfiguration.class);
94+
95+
jvmFlags = objectFactory.listProperty(String.class);
96+
mainClass = objectFactory.property(String.class);
97+
reproducible = objectFactory.property(Boolean.class);
98+
format = objectFactory.property(ImageFormat.class);
99+
100+
// Sets defaults.
101+
from.setImage(DEFAULT_FROM_IMAGE);
102+
jvmFlags.set(DEFAULT_JVM_FLAGS);
103+
reproducible.set(DEFAULT_REPRODUCIBLE);
104+
format.set(DEFAULT_FORMAT);
105+
}
106+
107+
public void from(Action<? super ImageConfiguration> action) {
108+
action.execute(from);
109+
}
110+
111+
public void to(Action<? super ImageConfiguration> action) {
112+
action.execute(to);
113+
}
114+
115+
public void setJvmFlags(List<String> jvmFlags) {
116+
this.jvmFlags.set(jvmFlags);
117+
}
118+
119+
public void setMainClass(String mainClass) {
120+
this.mainClass.set(mainClass);
121+
}
122+
123+
public void setReproducible(boolean isEnabled) {
124+
reproducible.set(isEnabled);
125+
}
126+
127+
public void setFormat(ImageFormat format) {
128+
this.format.set(format);
129+
}
130+
131+
ImageConfiguration getFrom() {
132+
return from;
133+
}
134+
135+
ImageConfiguration getTo() {
136+
return to;
137+
}
138+
139+
List<String> getJvmFlags() {
140+
return jvmFlags.get();
141+
}
142+
143+
@Nullable
144+
String getMainClass() {
145+
return mainClass.getOrNull();
146+
}
147+
148+
boolean getReproducible() {
149+
return reproducible.get();
150+
}
151+
152+
Class<? extends BuildableManifestTemplate> getFormat() {
153+
return format.get().getManifestTemplateClass();
154+
}
155+
}

‎jib-gradle-plugin/src/main/java/com/google/cloud/tools/jib/gradle/JibPlugin.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,14 @@
2222
public class JibPlugin implements Plugin<Project> {
2323

2424
@Override
25-
public void apply(Project project) {}
25+
public void apply(Project project) {
26+
JibExtension jibExtension = project.getExtensions().create("jib", JibExtension.class, project);
27+
project
28+
.getTasks()
29+
.create(
30+
"jib",
31+
BuildImageTask.class,
32+
buildImageTask ->
33+
project.afterEvaluate(ignored -> buildImageTask.applyExtension(jibExtension)));
34+
}
2635
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2018 Google LLC. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.jib.gradle;
18+
19+
import com.google.cloud.tools.jib.image.json.V22ManifestTemplate;
20+
import java.util.Arrays;
21+
import org.gradle.api.Project;
22+
import org.gradle.testfixtures.ProjectBuilder;
23+
import org.junit.Assert;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
/** Tests for {@link BuildImageTask}. */
28+
public class BuildImageTaskTest {
29+
30+
private BuildImageTask testBuildImageTask;
31+
private JibExtension fakeJibExtension;
32+
33+
@Before
34+
public void setUp() {
35+
Project fakeProject = ProjectBuilder.builder().build();
36+
testBuildImageTask = fakeProject.getTasks().create("task", BuildImageTask.class);
37+
fakeJibExtension = fakeProject.getExtensions().create("jib", JibExtension.class, fakeProject);
38+
}
39+
40+
@Test
41+
public void testSetExtension() {
42+
fakeJibExtension.from(
43+
from -> {
44+
from.setImage("some image");
45+
from.setCredHelper("some cred helper");
46+
});
47+
fakeJibExtension.to(
48+
to -> {
49+
to.setImage("another image");
50+
to.setCredHelper("another cred helper");
51+
});
52+
fakeJibExtension.setJvmFlags(Arrays.asList("flag1", "flag2"));
53+
fakeJibExtension.setMainClass("some main class");
54+
fakeJibExtension.setReproducible(false);
55+
fakeJibExtension.setFormat(JibExtension.ImageFormat.Docker);
56+
57+
testBuildImageTask.applyExtension(fakeJibExtension);
58+
59+
Assert.assertNotNull(testBuildImageTask.getFrom());
60+
Assert.assertEquals("some image", testBuildImageTask.getFrom().getImage());
61+
Assert.assertEquals("some cred helper", testBuildImageTask.getFrom().getCredHelper());
62+
Assert.assertNotNull(testBuildImageTask.getTo());
63+
Assert.assertEquals("another image", testBuildImageTask.getTo().getImage());
64+
Assert.assertEquals("another cred helper", testBuildImageTask.getTo().getCredHelper());
65+
Assert.assertEquals(Arrays.asList("flag1", "flag2"), testBuildImageTask.getJvmFlags());
66+
Assert.assertEquals("some main class", testBuildImageTask.getMainClass());
67+
Assert.assertEquals(false, testBuildImageTask.getReproducible());
68+
Assert.assertEquals(V22ManifestTemplate.class, testBuildImageTask.getFormat());
69+
}
70+
}

0 commit comments

Comments
 (0)