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 heuristics to speed up Jib check in skaffold init #3120

Merged
merged 2 commits into from
Oct 25, 2019
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
10 changes: 9 additions & 1 deletion pkg/skaffold/jib/jib_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os/exec"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -106,22 +107,29 @@ type jibJSON struct {
func ValidateJibConfig(path string) []Jib {
// Determine whether maven or gradle
var builderType PluginType
var executable, wrapper, taskName string
var executable, wrapper, taskName, searchString string
switch {
case strings.HasSuffix(path, "pom.xml"):
builderType = JibMaven
executable = "mvn"
wrapper = "mvnw"
searchString = "<artifactId>jib-maven-plugin</artifactId>"
taskName = "jib:_skaffold-init"
case strings.HasSuffix(path, "build.gradle"), strings.HasSuffix(path, "build.gradle.kts"):
builderType = JibGradle
executable = "gradle"
wrapper = "gradlew"
searchString = "com.google.cloud.tools.jib"
taskName = "_jibSkaffoldInit"
default:
return nil
}

// Search for indication of Jib in build file before proceeding
if content, err := ioutil.ReadFile(path); err != nil || !strings.Contains(string(content), searchString) {
return nil
}

// Run Jib's skaffold init task/goal to check if Jib is configured
if wrapperExecutable, err := util.AbsFile(filepath.Dir(path), wrapper); err == nil {
executable = wrapperExecutable
Expand Down
52 changes: 35 additions & 17 deletions pkg/skaffold/jib/jib_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestValidateJibConfig(t *testing.T) {
var tests = []struct {
description string
path string
fileContents string
command string
stdout string
expectedConfig []Jib
Expand All @@ -39,16 +40,24 @@ func TestValidateJibConfig(t *testing.T) {
expectedConfig: nil,
},
{
description: "jib not configured",
description: "jib string not found",
path: "path/to/build.gradle",
fileContents: "not a useful string",
expectedConfig: nil,
},
{
description: "jib string found but not configured",
path: "path/to/build.gradle",
fileContents: "com.google.cloud.tools.jib",
command: "gradle _jibSkaffoldInit -q",
stdout: "error",
expectedConfig: nil,
},
{
description: "jib gradle single project",
path: "path/to/build.gradle",
command: "gradle _jibSkaffoldInit -q",
description: "jib gradle single project",
path: "path/to/build.gradle",
fileContents: "com.google.cloud.tools.jib",
command: "gradle _jibSkaffoldInit -q",
stdout: `BEGIN JIB JSON
{"image":"image","project":"project"}
`,
Expand All @@ -57,9 +66,10 @@ func TestValidateJibConfig(t *testing.T) {
},
},
{
description: "jib gradle-kotlin single project",
path: "path/to/build.gradle.kts",
command: "gradle _jibSkaffoldInit -q",
description: "jib gradle-kotlin single project",
path: "path/to/build.gradle.kts",
fileContents: "com.google.cloud.tools.jib",
command: "gradle _jibSkaffoldInit -q",
stdout: `BEGIN JIB JSON
{"image":"image","project":"project"}
`,
Expand All @@ -68,9 +78,10 @@ func TestValidateJibConfig(t *testing.T) {
},
},
{
description: "jib gradle multi-project",
path: "path/to/build.gradle",
command: "gradle _jibSkaffoldInit -q",
description: "jib gradle multi-project",
path: "path/to/build.gradle",
fileContents: "com.google.cloud.tools.jib",
command: "gradle _jibSkaffoldInit -q",
stdout: `BEGIN JIB JSON
{"image":"image","project":"project1"}

Expand All @@ -83,19 +94,21 @@ BEGIN JIB JSON
},
},
{
description: "jib maven single module",
path: "path/to/pom.xml",
command: "mvn jib:_skaffold-init -q",
description: "jib maven single module",
path: "path/to/pom.xml",
fileContents: "<artifactId>jib-maven-plugin</artifactId>",
command: "mvn jib:_skaffold-init -q",
stdout: `BEGIN JIB JSON
{"image":"image","project":"project"}`,
expectedConfig: []Jib{
{BuilderName: PluginName(JibMaven), FilePath: "path/to/pom.xml", Image: "image", Project: "project"},
},
},
{
description: "jib maven multi-module",
path: "path/to/pom.xml",
command: "mvn jib:_skaffold-init -q",
description: "jib maven multi-module",
path: "path/to/pom.xml",
fileContents: "<artifactId>jib-maven-plugin</artifactId>",
command: "mvn jib:_skaffold-init -q",
stdout: `BEGIN JIB JSON
{"image":"image","project":"project1"}

Expand All @@ -110,12 +123,17 @@ BEGIN JIB JSON
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
tmpDir := t.NewTempDir().Write(test.path, test.fileContents)
for i := range test.expectedConfig {
test.expectedConfig[i].FilePath = tmpDir.Path(test.expectedConfig[i].FilePath)
}

t.Override(&util.DefaultExecCommand, testutil.CmdRunOut(
test.command,
test.stdout,
))

validated := ValidateJibConfig(test.path)
validated := ValidateJibConfig(tmpDir.Path(test.path))

t.CheckDeepEqual(test.expectedConfig, validated)
})
Expand Down