Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds to.tags configuration for Gradle. #978

Merged
merged 10 commits into from
Sep 21, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ private Void afterAllPushed() throws IOException, RegistryException, ExecutionEx
// Pushes to all target image tags.
// TODO: Parallelize.
for (String tag : buildConfiguration.getAllTargetImageTags()) {
buildConfiguration.getBuildLogger().info("Tagging with " + tag + "...");
registryClient.pushManifest(manifestTemplate, tag);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ public void testBuild_empty() throws IOException, InterruptedException {
new Command("docker", "inspect", "-f", "{{.Created}}", targetImage).run().trim());
}

@Test
public void testBuild_multipleTags() throws IOException, InterruptedException {
// TODO: Implement
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is to be implemented in a later PR, I'd remove it from here. It's just a test method, and I don't think we need a placeholder.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yea, I will implement this in this PR. I wanted to solicit some comments about the way BaseImageParameters and TargetImageParameters are laid out first.

}

@Test
public void testBuild_simple() throws IOException, InterruptedException {
String targetImage =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2018 Google LLC.
*
* 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 javax.annotation.Nullable;
import javax.inject.Inject;
import org.gradle.api.Action;
import org.gradle.api.model.ObjectFactory;

/** {@link ImageParameters} that configure the base image. */
public class BaseImageParameters implements ImageParameters {

private AuthParameters auth;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this can be final.


@Nullable private String image;
@Nullable private String credHelper;

@Inject
public BaseImageParameters(ObjectFactory objectFactory, String imageDescriptor) {
auth = objectFactory.newInstance(AuthParameters.class, imageDescriptor + ".auth");
}

@Nullable
@Override
public String getImage() {
return image;
}

@Override
public void setImage(String image) {
this.image = image;
}

@Nullable
@Override
public String getCredHelper() {
return credHelper;
}

@Override
public void setCredHelper(String credHelper) {
this.credHelper = credHelper;
}

@Override
public AuthParameters getAuth() {
return auth;
}

@Override
public void auth(Action<? super AuthParameters> action) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does Action<AuthParameters> work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that would work - is there a preference for using the specific type over allowing super types?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a public facing API, and I was wondering if there is anyone calling auth() with a type Action<AuthProperty> for example. Maybe I'm not familiar with Gradle idioms.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for like the jib-gradle-plugin jib.to.auth configuration so that users can configure the auth for a base or target image. The actual action that Groovy generates for this may be just Action<AuthParameters>, but I guess someone could decide to use like an Action<Object> that did some arbitrary thing on the auth object.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might have something to do with gradle decorating things.

action.execute(auth);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public void buildImage() throws InvalidImageReferenceException {
.setBaseImageConfiguration(
pluginConfigurationProcessor.getBaseImageConfigurationBuilder().build())
.setTargetImageConfiguration(targetImageConfiguration)
.setAdditionalTargetImageTags(jibExtension.getTo().getTags())
.setContainerConfiguration(
pluginConfigurationProcessor.getContainerConfigurationBuilder().build())
.setTargetFormat(jibExtension.getFormat())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
package com.google.cloud.tools.jib.gradle;

import javax.annotation.Nullable;
import javax.inject.Inject;
import org.gradle.api.Action;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.Optional;
Expand All @@ -31,47 +29,25 @@
* <p>{@code image} (required) is the image reference and {@code credHelper} (optional) is the name
* (after {@code docker-credential} of the credential helper for accessing the {@code image}.
*/
public class ImageParameters {

private AuthParameters auth;

@Nullable private String image;
@Nullable private String credHelper;

@Inject
public ImageParameters(ObjectFactory objectFactory, String imageDescriptor) {
auth = objectFactory.newInstance(AuthParameters.class, imageDescriptor + ".auth");
}
interface ImageParameters {

@Input
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this @Input in effect in implementation classes? Should this be on implementation classes? (Not sure.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea I believe so, since it does not work without @Optional here (Gradle fails with jib.to.image not set), so that indicates that the annotations are inherited from the interface.

@Nullable
@Optional
public String getImage() {
return image;
}
String getImage();

public void setImage(String image) {
this.image = image;
}
void setImage(String image);

@Input
@Nullable
@Optional
public String getCredHelper() {
return credHelper;
}
String getCredHelper();

public void setCredHelper(String credHelper) {
this.credHelper = credHelper;
}
void setCredHelper(String credHelper);

@Nested
@Optional
AuthParameters getAuth() {
return auth;
}
AuthParameters getAuth();

public void auth(Action<? super AuthParameters> action) {
action.execute(auth);
}
void auth(Action<? super AuthParameters> action);
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,24 @@ private static Path resolveDefaultExtraDirectory(Path projectDirectory) {
return projectDirectory.resolve("src").resolve("main").resolve("jib");
}

private final ImageParameters from;
private final ImageParameters to;
private final BaseImageParameters from;
private final TargetImageParameters to;
private final ContainerParameters container;
private final Property<Boolean> useOnlyProjectCache;
private final Property<Boolean> allowInsecureRegistries;
private final Property<Path> extraDirectory;

private final Path projectDir;

// 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;

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

from = objectFactory.newInstance(ImageParameters.class, "jib.from");
to = objectFactory.newInstance(ImageParameters.class, "jib.to");
from = objectFactory.newInstance(BaseImageParameters.class, "jib.from");
to = objectFactory.newInstance(TargetImageParameters.class, "jib.to");
container = objectFactory.newInstance(ContainerParameters.class);

jvmFlags = objectFactory.listProperty(String.class);
Expand All @@ -111,7 +108,7 @@ public JibExtension(Project project) {
args.set(Collections.emptyList());
useOnlyProjectCache.set(DEFAULT_USE_ONLY_PROJECT_CACHE);
allowInsecureRegistries.set(DEFAULT_ALLOW_INSECURE_REGISTIRIES);
extraDirectory.set(resolveDefaultExtraDirectory(projectDir));
extraDirectory.set(resolveDefaultExtraDirectory(project.getProjectDir().toPath()));
}

/**
Expand Down Expand Up @@ -206,13 +203,13 @@ String getTargetImage() {

@Nested
@Optional
public ImageParameters getFrom() {
public BaseImageParameters getFrom() {
return from;
}

@Nested
@Optional
public ImageParameters getTo() {
public TargetImageParameters getTo() {
return to;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2018 Google LLC.
*
* 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 java.util.Collections;
import java.util.Set;
import javax.annotation.Nullable;
import javax.inject.Inject;
import org.gradle.api.Action;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;

/** {@link ImageParameters} that configure the target image. */
public class TargetImageParameters implements ImageParameters {

private AuthParameters auth;

@Nullable private String image;
private Set<String> tags = Collections.emptySet();
@Nullable private String credHelper;

@Inject
public TargetImageParameters(ObjectFactory objectFactory, String imageDescriptor) {
auth = objectFactory.newInstance(AuthParameters.class, imageDescriptor + ".auth");
}

@Nullable
@Override
public String getImage() {
return image;
}

@Override
public void setImage(String image) {
this.image = image;
}

@Input
@Optional
public Set<String> getTags() {
return tags;
}

public void setTags(Set<String> tags) {
this.tags = tags;
}

@Nullable
@Override
public String getCredHelper() {
return credHelper;
}

@Override
public void setCredHelper(String credHelper) {
this.credHelper = credHelper;
}

@Override
public AuthParameters getAuth() {
return auth;
}

@Override
public void auth(Action<? super AuthParameters> action) {
action.execute(auth);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public class PluginConfigurationProcessorTest {

@Mock GradleJibLogger mockGradleJibLogger;
@Mock JibExtension mockJibExtension;
@Mock ImageParameters mockImageParameters;
@Mock BaseImageParameters mockBaseImageParameters;
@Mock ContainerParameters mockContainerParameters;
@Mock GradleProjectProperties mockProjectProperties;

@Before
public void setUp() throws Exception {
Mockito.doReturn("gcr.io/distroless/java").when(mockJibExtension).getBaseImage();
Mockito.doReturn(mockImageParameters).when(mockJibExtension).getFrom();
Mockito.doReturn(new AuthParameters("mock")).when(mockImageParameters).getAuth();
Mockito.doReturn(mockBaseImageParameters).when(mockJibExtension).getFrom();
Mockito.doReturn(new AuthParameters("mock")).when(mockBaseImageParameters).getAuth();
Mockito.doReturn(mockContainerParameters).when(mockJibExtension).getContainer();
Mockito.doReturn(Collections.emptyList()).when(mockContainerParameters).getEntrypoint();

Expand Down