Skip to content

Commit

Permalink
Add 'container' configuration object to wrap container-specific param…
Browse files Browse the repository at this point in the history
…eters (#421)

* Add 'container' configuration object to plugins

* Add deprecation warnings
  • Loading branch information
TadCordle authored Jun 20, 2018
1 parent 71511a6 commit 4077a22
Show file tree
Hide file tree
Showing 18 changed files with 389 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Blob getContainerConfigurationBlob() {
// Sets the entrypoint.
template.setContainerEntrypoint(image.getEntrypoint());

// Sets the entrypoint.
// Sets the main method arguments.
template.setContainerCmd(image.getJavaArguments());

// Serializes into JSON.
Expand Down
1 change: 1 addition & 0 deletions jib-gradle-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
### Changed

- Fetches credentials from inferred credential helper before Docker config ([#401](https://github.com/GoogleContainerTools/jib/issues/401))
- `jvmFlags`, `mainClass`, `args`, and `format` are now grouped under `container` configuration object ([#384](https://github.com/GoogleContainerTools/jib/issues/384))

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ dependencies {
}

jib {
args = ['An argument.']
container {
args = ['An argument.']
}

// Does not have tests use user-level cache for base image layers.
useOnlyProjectCache = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ jib {
image = 'gcr.io/jib-integration-testing/simpleimage:gradle'
credHelper = 'gcr'
}
args = ['An argument.']
container {
args = ['An argument.']
}

// Does not have tests use user-level cache for base image layers.
useOnlyProjectCache = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,15 @@ public void buildDocker() throws InvalidImageReferenceException {

// Asserts required @Input parameters are not null.
Preconditions.checkNotNull(jibExtension);
GradleBuildLogger gradleBuildLogger = new GradleBuildLogger(getLogger());
jibExtension.handleDeprecatedParameters(gradleBuildLogger);

RegistryCredentials knownBaseRegistryCredentials = null;
Authorization fromAuthorization = jibExtension.getFrom().getImageAuthorization();
if (fromAuthorization != null) {
knownBaseRegistryCredentials = new RegistryCredentials("jib.from.auth", fromAuthorization);
}

GradleBuildLogger gradleBuildLogger = new GradleBuildLogger(getLogger());
GradleProjectProperties gradleProjectProperties =
GradleProjectProperties.getForProject(getProject(), gradleBuildLogger);
String mainClass = gradleProjectProperties.getMainClass(jibExtension);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public void setTargetImage(String targetImage) {
public void buildImage() throws InvalidImageReferenceException {
// Asserts required @Input parameters are not null.
Preconditions.checkNotNull(jibExtension);
GradleBuildLogger gradleBuildLogger = new GradleBuildLogger(getLogger());
jibExtension.handleDeprecatedParameters(gradleBuildLogger);

if (Strings.isNullOrEmpty(jibExtension.getTargetImage())) {
throw new GradleException(
Expand All @@ -91,7 +93,6 @@ public void buildImage() throws InvalidImageReferenceException {
knownTargetRegistryCredentials = new RegistryCredentials("jib.to.auth", toAuthorization);
}

GradleBuildLogger gradleBuildLogger = new GradleBuildLogger(getLogger());
GradleProjectProperties gradleProjectProperties =
GradleProjectProperties.getForProject(getProject(), gradleBuildLogger);
String mainClass = gradleProjectProperties.getMainClass(jibExtension);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2018 Google LLC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.cloud.tools.jib.gradle;

import com.google.cloud.tools.jib.image.ImageFormat;
import com.google.cloud.tools.jib.image.json.BuildableManifestTemplate;
import com.google.common.base.Preconditions;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;

/**
* A bean that configures properties of the container run from the image. This is configurable with
* Groovy closures and can be validated when used as a task input.
*/
public class ContainerParameters {

private List<String> jvmFlags = Collections.emptyList();
@Nullable private String mainClass;
private List<String> args = Collections.emptyList();
private ImageFormat format = ImageFormat.Docker;

@Input
@Optional
public List<String> getJvmFlags() {
return jvmFlags;
}

public void setJvmFlags(List<String> jvmFlags) {
this.jvmFlags = jvmFlags;
}

@Input
@Nullable
@Optional
public String getMainClass() {
return mainClass;
}

public void setMainClass(String mainClass) {
this.mainClass = mainClass;
}

@Input
@Optional
public List<String> getArgs() {
return args;
}

public void setArgs(List<String> args) {
this.args = args;
}

@Input
@Optional
public Class<? extends BuildableManifestTemplate> getFormat() {
return Preconditions.checkNotNull(format).getManifestTemplateClass();
}

public void setFormat(ImageFormat format) {
this.format = format;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public void generateDockerContext() {
Preconditions.checkNotNull(jibExtension);

GradleBuildLogger gradleBuildLogger = new GradleBuildLogger(getLogger());
jibExtension.handleDeprecatedParameters(gradleBuildLogger);

GradleProjectProperties gradleProjectProperties =
GradleProjectProperties.getForProject(getProject(), gradleBuildLogger);
String mainClass = gradleProjectProperties.getMainClass(jibExtension);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

package com.google.cloud.tools.jib.gradle;

import com.google.cloud.tools.jib.builder.BuildLogger;
import com.google.cloud.tools.jib.image.ImageFormat;
import com.google.cloud.tools.jib.image.json.BuildableManifestTemplate;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -47,50 +49,92 @@
* image = ‘gcr.io/gcp-project/my-app:built-with-jib’
* credHelper = ‘ecr-login’
* }
* jvmFlags = [‘-Xms512m’, ‘-Xdebug’]
* mainClass = ‘com.mycompany.myproject.Main’
* args = ['arg1', 'arg2']
* format = OCI
* container {
* jvmFlags = [‘-Xms512m’, ‘-Xdebug’]
* mainClass = ‘com.mycompany.myproject.Main’
* args = ['arg1', 'arg2']
* format = OCI
* }
* }
* }</pre>
*/
public class JibExtension {

// Defines default configuration values.
private static final String DEFAULT_FROM_IMAGE = "gcr.io/distroless/java";
private static final List<String> DEFAULT_JVM_FLAGS = Collections.emptyList();
private static final List<String> DEFAULT_ARGS = Collections.emptyList();
private static final ImageFormat DEFAULT_FORMAT = ImageFormat.Docker;
private static final boolean DEFAULT_USE_ONLY_PROJECT_CACHE = false;

private final ImageConfiguration from;
private final ImageConfiguration to;
private final ContainerParameters container;
private final Property<Boolean> useOnlyProjectCache;

// TODO: Deprecated parameters; remove these 4
private final ListProperty<String> jvmFlags;
private final Property<String> mainClass;
private final ListProperty<String> args;
private final Property<ImageFormat> format;
private final Property<Boolean> useOnlyProjectCache;

public JibExtension(Project project) {
ObjectFactory objectFactory = project.getObjects();

from = objectFactory.newInstance(ImageConfiguration.class);
to = objectFactory.newInstance(ImageConfiguration.class);
container = objectFactory.newInstance(ContainerParameters.class);

jvmFlags = objectFactory.listProperty(String.class);
mainClass = objectFactory.property(String.class);
args = objectFactory.listProperty(String.class);
format = objectFactory.property(ImageFormat.class);

useOnlyProjectCache = objectFactory.property(Boolean.class);

// Sets defaults.
from.setImage(DEFAULT_FROM_IMAGE);
jvmFlags.set(DEFAULT_JVM_FLAGS);
args.set(DEFAULT_ARGS);
format.set(DEFAULT_FORMAT);
jvmFlags.set(Collections.emptyList());
args.set(Collections.emptyList());
useOnlyProjectCache.set(DEFAULT_USE_ONLY_PROJECT_CACHE);
}

/**
* Warns about deprecated parameters in use.
*
* @param logger The logger used to print the warnings
*/
void handleDeprecatedParameters(BuildLogger logger) {
StringBuilder deprecatedParams = new StringBuilder();
if (!jvmFlags.get().isEmpty()) {
deprecatedParams.append(" jvmFlags -> container.jvmFlags\n");
if (container.getJvmFlags().isEmpty()) {
container.setJvmFlags(jvmFlags.get());
}
}
if (!Strings.isNullOrEmpty(mainClass.getOrNull())) {
deprecatedParams.append(" mainClass -> container.mainClass\n");
if (Strings.isNullOrEmpty(container.getMainClass())) {
container.setMainClass(mainClass.getOrNull());
}
}
if (!args.get().isEmpty()) {
deprecatedParams.append(" args -> container.args\n");
if (container.getArgs().isEmpty()) {
container.setArgs(args.get());
}
}
if (format.getOrNull() != null) {
deprecatedParams.append(" format -> container.format\n");
container.setFormat(format.get());
}

if (deprecatedParams.length() > 0) {
logger.warn(
"There are deprecated parameters used in the build configuration. Please make the "
+ "following changes to your build.gradle to avoid issues in the future:\n"
+ deprecatedParams
+ "You may also wrap the parameters in a container{} block.");
}
}

public void from(Action<? super ImageConfiguration> action) {
action.execute(from);
}
Expand All @@ -99,6 +143,10 @@ public void to(Action<? super ImageConfiguration> action) {
action.execute(to);
}

public void container(Action<? super ContainerParameters> action) {
action.execute(container);
}

public void setJvmFlags(List<String> jvmFlags) {
this.jvmFlags.set(jvmFlags);
}
Expand Down Expand Up @@ -142,28 +190,39 @@ ImageConfiguration getTo() {
return to;
}

@Nested
@Optional
ContainerParameters getContainer() {
return container;
}

// TODO: Make @Internal (deprecated)
@Input
@Optional
List<String> getJvmFlags() {
return jvmFlags.get();
return container.getJvmFlags();
}

// TODO: Make @Internal (deprecated)
@Input
@Nullable
@Optional
String getMainClass() {
return mainClass.getOrNull();
return container.getMainClass();
}

// TODO: Make @Internal (deprecated)
@Input
@Optional
List<String> getArgs() {
return args.get();
return container.getArgs();
}

// TODO: Make @Internal (deprecated)
@Input
@Optional
Class<? extends BuildableManifestTemplate> getFormat() {
return format.get().getManifestTemplateClass();
return container.getFormat();
}

@Input
Expand Down
Loading

0 comments on commit 4077a22

Please sign in to comment.