Skip to content

Commit

Permalink
Allow gradle users to configure skaffold data
Browse files Browse the repository at this point in the history
  • Loading branch information
loosebazooka committed Feb 12, 2020
1 parent 84933d5 commit f4fa3fe
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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

import com.google.cloud.tools.jib.gradle.skaffold.SkaffoldParameters;
import com.google.cloud.tools.jib.plugins.common.PropertyNames;
import org.gradle.api.Action;
import org.gradle.api.Project;
Expand Down Expand Up @@ -77,6 +78,7 @@ public class JibExtension {
private final ExtraDirectoriesParameters extraDirectories;
private final DockerClientParameters dockerClient;
private final OutputPathsParameters outputPaths;
private final SkaffoldParameters skaffold;
private final Property<Boolean> allowInsecureRegistries;
private final Property<String> containerizingMode;

Expand All @@ -89,6 +91,7 @@ public JibExtension(Project project) {
extraDirectories = objectFactory.newInstance(ExtraDirectoriesParameters.class, project);
dockerClient = objectFactory.newInstance(DockerClientParameters.class);
outputPaths = objectFactory.newInstance(OutputPathsParameters.class, project);
skaffold = objectFactory.newInstance(SkaffoldParameters.class, project);

allowInsecureRegistries = objectFactory.property(Boolean.class);
containerizingMode = objectFactory.property(String.class);
Expand Down Expand Up @@ -122,6 +125,10 @@ public void outputPaths(Action<? super OutputPathsParameters> action) {
action.execute(outputPaths);
}

public void skaffold(Action<? super SkaffoldParameters> action) {
action.execute(skaffold);
}

public void setAllowInsecureRegistries(boolean allowInsecureRegistries) {
this.allowInsecureRegistries.set(allowInsecureRegistries);
}
Expand Down Expand Up @@ -166,6 +173,12 @@ public OutputPathsParameters getOutputPaths() {
return outputPaths;
}

@Nested
@Optional
public SkaffoldParameters getSkaffold() {
return skaffold;
}

@Input
boolean getAllowInsecureRegistries() {
if (System.getProperty(PropertyNames.ALLOW_INSECURE_REGISTRIES) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ public void listFiles() throws IOException {
}
}

// Configure other files from config
SkaffoldWatchParameters watch = jibExtension.getSkaffold().getWatch();
watch.getBuildIncludes().forEach(skaffoldFilesOutput::addBuild);
watch.getIncludes().forEach(skaffoldFilesOutput::addInput);
watch.getExcludes().forEach(skaffoldFilesOutput::addIgnore);

// Print files
System.out.println();
System.out.println("BEGIN JIB JSON");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2020 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.skaffold;

import com.google.common.base.Preconditions;
import javax.inject.Inject;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.tasks.Nested;

public class SkaffoldParameters {

private final SkaffoldWatchParameters watch;
private final SkaffoldSyncParameters sync;

@Inject
public SkaffoldParameters(Project project) {
ObjectFactory objectFactory = project.getObjects();

watch = objectFactory.newInstance(SkaffoldWatchParameters.class, project);
sync = objectFactory.newInstance(SkaffoldSyncParameters.class, project);

Preconditions.checkNotNull(watch);
}

public void watch(Action<? super SkaffoldWatchParameters> action) {
action.execute(watch);
}

public void sync(Action<? super SkaffoldSyncParameters> action) {
action.execute(sync);
}

@Nested
public SkaffoldWatchParameters getWatch() {
return watch;
}

@Nested
public SkaffoldSyncParameters getSync() {
return sync;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2020 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.skaffold;

import java.io.File;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
import javax.inject.Inject;
import org.gradle.api.Project;
import org.gradle.api.tasks.Internal;

public class SkaffoldSyncParameters {
private final Project project;

private Set<Path> excludes = Collections.emptySet();

@Inject
public SkaffoldSyncParameters(Project project) {
this.project = project;
}

/**
* Get the excludes directive for sync functionality in skaffold.
*
* @return a set of absolute paths
*/
@Internal
public Set<Path> getExcludes() {
// Gradle warns about @Input annotations on File objects, so we have to expose a getter for a
// String to make them go away.
return excludes;
}

/**
* Sets excludes. {@code excludes} can be any suitable object describing file paths convertible by
* {@link Project#files} (such as {@link File}, {@code List<File>}, or {@code List<String>}).
*
* @param paths paths to set on excludes
*/
public void setExcludes(Object paths) {
this.excludes =
project
.files(paths)
.getFiles()
.stream()
.map(File::toPath)
.map(Path::toAbsolutePath)
.collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright 2020 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.skaffold;

import java.io.File;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
import javax.inject.Inject;
import org.gradle.api.Project;
import org.gradle.api.tasks.Internal;

public class SkaffoldWatchParameters {

private final Project project;

private Set<Path> buildIncludes = Collections.emptySet();
private Set<Path> includes = Collections.emptySet();
private Set<Path> excludes = Collections.emptySet();

@Inject
public SkaffoldWatchParameters(Project project) {
this.project = project;
}

/**
* A set of absolute paths to include with skaffold watching.
*
* @return a set of absolute paths
*/
@Internal
public Set<Path> getBuildIncludes() {
// Gradle warns about @Input annotations on File objects, so we have to expose a getter for a
// String to make them go away.
return buildIncludes;
}

/**
* Sets includes. {@code includes} can be any suitable object describing file paths convertible by
* {@link Project#files} (such as {@link File}, {@code List<File>}, or {@code List<String>}).
*
* @param paths paths to set on includes
*/
public void setBuildIncludes(Object paths) {
this.buildIncludes =
project
.files(paths)
.getFiles()
.stream()
.map(File::toPath)
.map(Path::toAbsolutePath)
.collect(Collectors.toSet());
}

/**
* A set of absolute paths to include with skaffold watching.
*
* @return a set of absolute paths
*/
@Internal
public Set<Path> getIncludes() {
// Gradle warns about @Input annotations on File objects, so we have to expose a getter for a
// String to make them go away.
return includes;
}

/**
* Sets includes. {@code includes} can be any suitable object describing file paths convertible by
* {@link Project#files} (such as {@link File}, {@code List<File>}, or {@code List<String>}).
*
* @param paths paths to set on includes
*/
public void setIncludes(Object paths) {
this.includes =
project
.files(paths)
.getFiles()
.stream()
.map(File::toPath)
.map(Path::toAbsolutePath)
.collect(Collectors.toSet());
}

/**
* A set of absolute paths to exclude from skaffold watching.
*
* @return a set of absolute paths
*/
@Internal
public Set<Path> getExcludes() {
// Gradle warns about @Input annotations on File objects, so we have to expose a getter for a
// String to make them go away.
return excludes;
}

/**
* Sets excludes. {@code excludes} can be any suitable object describing file paths convertible by
* {@link Project#files} (such as {@link File}, {@code List<File>}, or {@code List<String>}).
*
* @param paths paths to set on excludes
*/
public void setExcludes(Object paths) {
this.excludes =
project
.files(paths)
.getFiles()
.stream()
.map(File::toPath)
.map(Path::toAbsolutePath)
.collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ public void listFilesAndTargets() {

try {
String syncMapJson =
PluginConfigurationProcessor.getSkaffoldSyncMap(configuration, projectProperties);
PluginConfigurationProcessor.getSkaffoldSyncMap(
configuration,
projectProperties,
jibExtension.getSkaffold().getSync().getExcludes());

System.out.println();
System.out.println("BEGIN JIB JSON: SYNCMAP/1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException;
import com.google.cloud.tools.jib.plugins.common.PluginConfigurationProcessor;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;
Expand Down Expand Up @@ -76,7 +77,8 @@ public void execute() throws MojoExecutionException {

try {
String syncMapJson =
PluginConfigurationProcessor.getSkaffoldSyncMap(configuration, projectProperties);
PluginConfigurationProcessor.getSkaffoldSyncMap(
configuration, projectProperties, ImmutableSet.of());

System.out.println();
System.out.println("BEGIN JIB JSON: SYNCMAP/1");
Expand Down
Loading

0 comments on commit f4fa3fe

Please sign in to comment.