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

Add new Auto sync option #3382

Merged
merged 10 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ jobs:
directories:
- $HOME/.cache/go-build
- $HOME/.gradle
- $HOME/.m2
- os: linux
name: "diag/Linux unit"
script:
Expand Down
12 changes: 11 additions & 1 deletion docs/content/en/schemas/v2beta1.json
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@
"description": "items that need to be built, along with the context in which they should be built.",
"x-intellij-html-description": "items that need to be built, along with the context in which they should be built."
},
"Auto": {
"description": "cannot be customized.",
"x-intellij-html-description": "cannot be customized."
},
"BazelArtifact": {
"required": [
"target"
Expand Down Expand Up @@ -1931,6 +1935,11 @@
},
"Sync": {
"properties": {
"auto": {
"$ref": "#/definitions/Auto",
"description": "delegates discovery of sync rules to the build system. Currently only available for jib.",
"x-intellij-html-description": "delegates discovery of sync rules to the build system. Currently only available for jib."
},
"infer": {
"items": {
"type": "string"
Expand All @@ -1951,7 +1960,8 @@
},
"preferredOrder": [
"manual",
"infer"
"infer",
"auto"
],
"additionalProperties": false,
"description": "*beta* specifies what files to sync into the container. This is a list of sync rules indicating the intent to sync for source files. If no files are listed, sync all the files and infer the destination.",
Expand Down
91 changes: 91 additions & 0 deletions integration/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ limitations under the License.
package integration

import (
"bufio"
"context"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -81,6 +83,95 @@ func TestDevSync(t *testing.T) {
}
}

func TestDevAutoSync(t *testing.T) {
if testing.Short() || RunOnGCP() {
t.Skip("skipping kind integration test")
}

dir := "testdata/jib-sync/"

tests := []struct {
description string
profiles []string
uniqueStr string
}{
{
description: "jib maven auto sync",
profiles: []string{"maven"},
uniqueStr: "maven-maven",
},
{
description: "jib gradle auto sync",
profiles: []string{"gradle"},
uniqueStr: "gradle-gradle",
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
// Run skaffold build first to fail quickly on a build failure
skaffold.Build().WithProfiles(test.profiles).InDir(dir).RunOrFail(t)

ns, client, deleteNs := SetupNamespace(t)
defer deleteNs()

output, cancel := skaffold.Dev("--trigger", "notify").WithProfiles(test.profiles).InDir(dir).InNs(ns.Name).RunBackgroundOutput(t)
defer cancel()

client.WaitForPodsReady("test-file-sync")

// give the server a chance to warm up, this integration test on slow environments (KIND on travis)
// fails because of a potential server race condition.
scanner := bufio.NewScanner(output)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "Started Application") {
err := output.Close()
if err != nil {
t.Fatal("failed to close skaffold dev output reader during test")
}
return
}
}

// direct file sync (this file is an existing file checked in for this testdata)
directFile := "direct-file"
directFilePath := dir + "src/main/jib/" + directFile
directFileData := "direct-data"
if err := ioutil.WriteFile(directFilePath, []byte(directFileData), 0644); err != nil {
t.Fatalf("Failed to write local file to sync %s", directFilePath)
}
defer func() { os.Truncate(directFilePath, 0) }()

err := wait.PollImmediate(time.Millisecond*500, 1*time.Minute, func() (bool, error) {
out, _ := exec.Command("kubectl", "exec", "test-file-sync", "-n", ns.Name, "--", "cat", directFile).Output()
return string(out) == directFileData, nil
})
failNowIfError(t, err)

// compile and sync
generatedFileSrc := dir + "src/main/java/hello/HelloController.java"
if oldContents, err := ioutil.ReadFile(generatedFileSrc); err != nil {
t.Fatalf("Failed to read file %s", generatedFileSrc)
} else {
newContents := strings.Replace(string(oldContents), "text-to-replace", test.uniqueStr, 1)
if err := ioutil.WriteFile(generatedFileSrc, []byte(newContents), 0644); err != nil {
t.Fatalf("Failed to write new contents to file %s", generatedFileSrc)
}
defer func() {
ioutil.WriteFile(generatedFileSrc, oldContents, 0644)
}()
}
err = wait.PollImmediate(time.Millisecond*500, 1*time.Minute, func() (bool, error) {
// distroless debug only has wget, not curl
out, _ := exec.Command("kubectl", "exec", "test-file-sync", "-n", ns.Name, "--", "wget", "localhost:8080/", "-q", "-O", "-").Output()
return string(out) == test.uniqueStr, nil
})
failNowIfError(t, err)
})
}
}

func TestDevSyncAPITrigger(t *testing.T) {
if testing.Short() || RunOnGCP() {
t.Skip("skipping kind integration test")
Expand Down
8 changes: 8 additions & 0 deletions integration/testdata/jib-sync/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
target
bin
build
.gradle
.classpath
.project
.settings
.vscode
110 changes: 110 additions & 0 deletions integration/testdata/jib-sync/.mvn/wrapper/MavenWrapperDownloader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.
*/

import java.net.*;
import java.io.*;
import java.nio.channels.*;
import java.util.Properties;

public class MavenWrapperDownloader {

/**
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
*/
private static final String DEFAULT_DOWNLOAD_URL =
"https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar";

/**
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
* use instead of the default one.
*/
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
".mvn/wrapper/maven-wrapper.properties";

/**
* Path where the maven-wrapper.jar will be saved to.
*/
private static final String MAVEN_WRAPPER_JAR_PATH =
".mvn/wrapper/maven-wrapper.jar";

/**
* Name of the property which should be used to override the default download url for the wrapper.
*/
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";

public static void main(String args[]) {
System.out.println("- Downloader started");
File baseDirectory = new File(args[0]);
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());

// If the maven-wrapper.properties exists, read it and check if it contains a custom
// wrapperUrl parameter.
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
String url = DEFAULT_DOWNLOAD_URL;
if(mavenWrapperPropertyFile.exists()) {
FileInputStream mavenWrapperPropertyFileInputStream = null;
try {
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
Properties mavenWrapperProperties = new Properties();
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
} catch (IOException e) {
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
} finally {
try {
if(mavenWrapperPropertyFileInputStream != null) {
mavenWrapperPropertyFileInputStream.close();
}
} catch (IOException e) {
// Ignore ...
}
}
}
System.out.println("- Downloading from: : " + url);

File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
if(!outputFile.getParentFile().exists()) {
if(!outputFile.getParentFile().mkdirs()) {
System.out.println(
"- ERROR creating output direcrory '" + outputFile.getParentFile().getAbsolutePath() + "'");
}
}
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
try {
downloadFileFromURL(url, outputFile);
System.out.println("Done");
System.exit(0);
} catch (Throwable e) {
System.out.println("- Error downloading");
e.printStackTrace();
System.exit(1);
}
}

private static void downloadFileFromURL(String urlString, File destination) throws Exception {
URL website = new URL(urlString);
ReadableByteChannel rbc;
rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}

}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.1/apache-maven-3.6.1-bin.zip
27 changes: 27 additions & 0 deletions integration/testdata/jib-sync/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
plugins {
id 'java'
id 'org.springframework.boot' version '2.0.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.7.RELEASE'
id 'com.google.cloud.tools.jib' version '2.1.0'
}

repositories {
mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
compile "org.springframework.boot:spring-boot-starter-web"
compile "org.springframework.boot:spring-boot-starter-actuator"
compile "org.springframework.boot:spring-boot-devtools"

testCompile("org.springframework.boot:spring-boot-starter-test")
}

jib {
from {
image = "gcr.io/distroless/java:debug"
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading