Skip to content

Commit

Permalink
Allow users to configure skaffold data exported from jib to skaffold (#…
Browse files Browse the repository at this point in the history
…2292)

* Allow gradle users to configure skaffold data
  • Loading branch information
loosebazooka authored Feb 21, 2020
1 parent a6aa257 commit d6bcf77
Show file tree
Hide file tree
Showing 29 changed files with 746 additions and 22 deletions.
5 changes: 5 additions & 0 deletions jib-gradle-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.

### Added
- Additionally reads credentials from `~/.docker/.dockerconfigjson` and legacy Docker config (`~/.docker/.dockercfg`). Also searches for `$HOME/.docker/*` (in addition to current `System.get("user.home")/.docker/*`). This may help retrieve credentials, for example, on Kubernetes. ([#2260](https://github.com/GoogleContainerTools/jib/issues/2260))
- New skaffold configuration options that modify how jib's build config is presented to skaffold ([#2292](https://github.com/GoogleContainerTools/jib/pull/2292)):
- `jib.skaffold.watch.buildIncludes`: a list of build files to watch
- `jib.skaffold.watch.includes`: a list of project files to watch
- `jib.skaffold.watch.excludes`: a list of files to exclude from watching
- `jib.skaffold.sync.excludes`: a list of files to exclude from sync'ing

### Changed

Expand Down
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 @@ -94,6 +96,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 @@ -127,6 +130,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 @@ -171,6 +178,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 @@ -109,6 +109,14 @@ 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);
// we don't do any special pre-processing for ignore (input and ignore can overlap with exact
// matches)
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,59 @@
/*
* 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;

/** Skaffold specific JibExtension parameters. */
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,65 @@
/*
* 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;

/** Skaffold specific JibExtension parameters for configuring files to sync. */
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() {
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,124 @@
/*
* 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;

/** Skaffold specific JibExtension parameters for configuring files to watch. */
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() {
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() {
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 @@ -78,7 +78,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 @@ -20,6 +20,8 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -240,6 +242,35 @@ public void testOutputFiles() {
testJibExtension.getOutputPaths().getTarPath());
}

@Test
public void testSkaffold() {
testJibExtension.skaffold(
skaffold -> {
skaffold.sync(sync -> sync.setExcludes(fakeProject.files("sync1", "sync2")));
skaffold.watch(
watch -> {
watch.setBuildIncludes(ImmutableList.of("watch1", "watch2"));
watch.setIncludes("watch3");
watch.setExcludes(ImmutableList.of(new File("watch4")));
});
});
Path root = fakeProject.getRootDir().toPath();
Assert.assertEquals(
ImmutableSet.of(
root.resolve("sync1").toAbsolutePath(), root.resolve("sync2").toAbsolutePath()),
testJibExtension.getSkaffold().getSync().getExcludes());
Assert.assertEquals(
ImmutableSet.of(
root.resolve("watch1").toAbsolutePath(), root.resolve("watch2").toAbsolutePath()),
testJibExtension.getSkaffold().getWatch().getBuildIncludes());
Assert.assertEquals(
ImmutableSet.of(root.resolve("watch3").toAbsolutePath()),
testJibExtension.getSkaffold().getWatch().getIncludes());
Assert.assertEquals(
ImmutableSet.of(root.resolve("watch4").toAbsolutePath()),
testJibExtension.getSkaffold().getWatch().getExcludes());
}

@Test
public void testProperties() {
System.setProperty("jib.from.image", "fromImage");
Expand Down
Loading

0 comments on commit d6bcf77

Please sign in to comment.