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 support for building projects using jib #1073

Merged
merged 21 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from 19 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
37 changes: 31 additions & 6 deletions pkg/skaffold/build/local/jib.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,41 @@ package local

import (
"context"
"crypto/sha1"
"encoding/hex"
"io"
"os/exec"
"regexp"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha3"
"github.com/pkg/errors"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/sirupsen/logrus"
)

func (b *Builder) buildJibMaven(_ /*ctx*/ context.Context, _ /*out*/ io.Writer, _ /*workspace*/ string, _ /*a*/ *v1alpha3.JibMavenArtifact) (string, error) {
return "", errors.New("buildJibMaven is unimplemented")
// executeBuildCommand executes the command-line with the working directory set to `workspace`.
func executeBuildCommand(ctx context.Context, out io.Writer, workspace string, commandLine []string) error {
logrus.Infof("Building %v: %v", workspace, commandLine)
briandealwis marked this conversation as resolved.
Show resolved Hide resolved
cmd := exec.CommandContext(ctx, commandLine[0], commandLine[1:]...)
cmd.Dir = workspace
cmd.Stdout = out
cmd.Stderr = out
return util.RunCmd(cmd)
}

func (b *Builder) buildJibGradle(_ /*ctx*/ context.Context, _ /*out*/ io.Writer, _ /*workspace*/ string, _ /*a*/ *v1alpha3.JibGradleArtifact) (string, error) {
return "", errors.New("buildJibGradle is unimplemented")
// jibBuildImageRef generates a valid image name for the workspace and project.
briandealwis marked this conversation as resolved.
Show resolved Hide resolved
// The image name is always prefixed with `jib`.
func generateJibImageRef(workspace string, project string) string {
imageName := "jib" + workspace
briandealwis marked this conversation as resolved.
Show resolved Hide resolved
if project != "" {
imageName += "_" + project
}
// if the workspace + project is a valid image name then use it
match := regexp.MustCompile(constants.RepositoryComponentRegex).MatchString(imageName)
if match {
briandealwis marked this conversation as resolved.
Show resolved Hide resolved
return imageName
}
// otherwise use a hash for a deterministic name
hasher := sha1.New()
io.WriteString(hasher, imageName)
return "jib__" + hex.EncodeToString(hasher.Sum(nil))
}
57 changes: 57 additions & 0 deletions pkg/skaffold/build/local/jib_gradle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright 2018 The Skaffold Authors

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 local

import (
"context"
"fmt"
"io"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha3"
"github.com/pkg/errors"
)

func (b *Builder) buildJibGradle(ctx context.Context, out io.Writer, workspace string, a *v1alpha3.JibGradleArtifact) (string, error) {
skaffoldImage := generateJibImageRef(workspace, a.Project)
gradle, err := findBuilder("gradle", "gradlew", workspace)
if err != nil {
return "", errors.Wrap(err, "Unable to find gradle executable")
}
gradleCommand := generateGradleCommand(workspace, skaffoldImage, a)
commandLine := append(gradle, gradleCommand...)

err = executeBuildCommand(ctx, out, workspace, commandLine)
if err != nil {
return "", errors.Wrap(err, "gradle build failed")
}
return skaffoldImage, nil
}

// generateGradleCommand generates the command-line to pass to gradle for building an
// project in `workspace`. The resulting image is added to the local docker daemon
// and called `skaffoldImage`.
func generateGradleCommand(_ /*workspace*/ string, skaffoldImage string, a *v1alpha3.JibGradleArtifact) []string {
var command []string
if a.Project == "" {
command = []string{":jibDockerBuild"}
} else {
// multi-module
command = []string{fmt.Sprintf(":%s:jibDockerBuild", a.Project)}
}
command = append(command, "--image="+skaffoldImage)
return command
}
61 changes: 61 additions & 0 deletions pkg/skaffold/build/local/jib_maven.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2018 The Skaffold Authors

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 local

import (
"context"
"io"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha3"
"github.com/pkg/errors"
)

func (b *Builder) buildJibMaven(ctx context.Context, out io.Writer, workspace string, a *v1alpha3.JibMavenArtifact) (string, error) {
skaffoldImage := generateJibImageRef(workspace, a.Module)

maven, err := findBuilder("mvn", "mvnw", workspace)
if err != nil {
return "", errors.Wrap(err, "Unable to find maven executable")
}
mavenCommand, err := generateMavenCommand(workspace, skaffoldImage, a)
if err != nil {
return "", err
}
commandLine := append(maven, mavenCommand...)

err = executeBuildCommand(ctx, out, workspace, commandLine)
if err != nil {
return "", errors.Wrap(err, "maven build failed")
}
return skaffoldImage, nil
}

// generateMavenCommand generates the command-line to pass to maven for building a
// project found in `workspace`. The resulting image is added to the local docker daemon
// and called `skaffoldImage`.
func generateMavenCommand(_ /*workspace*/ string, skaffoldImage string, a *v1alpha3.JibMavenArtifact) ([]string, error) {
if a.Module != "" {
// TODO: multi-module
return nil, errors.New("Maven multi-modules not supported yet")
}
// use mostly-qualified plugin ID in case jib is not a configured plugin
commandLine := []string{"prepare-package", "com.google.cloud.tools:jib-maven-plugin::dockerBuild", "-Dimage=" + skaffoldImage}
if a.Profile != "" {
commandLine = append(commandLine, "-P"+a.Profile)
}
return commandLine, nil
}
84 changes: 84 additions & 0 deletions pkg/skaffold/build/local/jib_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2018 The Skaffold Authors

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 local

import (
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha3"
"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestGenerateMavenCommand(t *testing.T) {
briandealwis marked this conversation as resolved.
Show resolved Hide resolved
var testCases = []struct {
in v1alpha3.JibMavenArtifact
out []string
}{
{v1alpha3.JibMavenArtifact{}, []string{"prepare-package", "com.google.cloud.tools:jib-maven-plugin::dockerBuild", "-Dimage=image"}},
{v1alpha3.JibMavenArtifact{Profile: "profile"}, []string{"prepare-package", "com.google.cloud.tools:jib-maven-plugin::dockerBuild", "-Dimage=image", "-Pprofile"}},
}

for _, tt := range testCases {
commandLine, err := generateMavenCommand(".", "image", &tt.in)

testutil.CheckError(t, false, err)
testutil.CheckDeepEqual(t, tt.out, commandLine)
}
}

func TestGenerateMavenCommand_errorWithModule(t *testing.T) {
a := v1alpha3.JibMavenArtifact{Module: "module"}
_, err := generateMavenCommand(".", "image", &a)

testutil.CheckError(t, true, err)
}

func TestGenerateGradleCommand(t *testing.T) {
var testCases = []struct {
in v1alpha3.JibGradleArtifact
out []string
}{
{v1alpha3.JibGradleArtifact{}, []string{":jibDockerBuild", "--image=image"}},
{v1alpha3.JibGradleArtifact{Project: "project"}, []string{":project:jibDockerBuild", "--image=image"}},
}

for _, tt := range testCases {
commandLine := generateGradleCommand(".", "image", &tt.in)

testutil.CheckDeepEqual(t, tt.out, commandLine)
}
}

func TestGenerateJibImageRef(t *testing.T) {
var testCases = []struct {
workspace string
project string
out string
}{
{"simple", "", "jibsimple"},
{"simple", "project", "jibsimple_project"},
{".", "project", "jib__d8c7cbe8892fe8442b7f6ef42026769ee6a01e67"},
{"complex/workspace", "project", "jib__965ec099f720d3ccc9c038c21ea4a598c9632883"},
}

for _, tt := range testCases {
computed := generateJibImageRef(tt.workspace, tt.project)
if tt.out != computed {
t.Errorf("Expected '%s' for '%s'/'%s': '%s'", tt.out, tt.workspace, tt.project, computed)
}
}
}
45 changes: 45 additions & 0 deletions pkg/skaffold/build/local/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// +build !windows

/*
Copyright 2018 The Skaffold Authors

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 local

import (
"os/exec"
"path/filepath"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)

// Maven and Gradle projects often provide a wrapper to ensure a particular
// builder version is used. This function tries to resolve a wrapper
// or otherwise resolves the builder executable.
func findBuilder(builderExecutable string, wrapperScriptName string, workspace string) ([]string, error) {
wrapperFile := filepath.Join(workspace, wrapperScriptName)
if util.IsFile(wrapperFile) {
briandealwis marked this conversation as resolved.
Show resolved Hide resolved
absolute, err := filepath.Abs(wrapperFile)
if err != nil {
return nil, err
}
return []string{absolute}, nil
}
path, err := exec.LookPath(builderExecutable)
if err != nil {
return nil, err
}
return []string{path}, nil
}
57 changes: 57 additions & 0 deletions pkg/skaffold/build/local/util_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright 2018 The Skaffold Authors

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 local

import (
"os/exec"
"path/filepath"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)

// Maven and Gradle projects often provide a wrapper to ensure a particular
// builder version is used. This function tries to resolve a wrapper
// or otherwise resolves the builder executable.
func findBuilder(builderExecutable string, wrapperScriptName string, workspace string) ([]string, error) {
wrapperFile := filepath.Join(workspace, wrapperScriptName)
if util.IsFile(wrapperFile) {
path, err := filepath.Abs(wrapperFile)
if err != nil {
return nil, err
}
return []string{path}, nil
}
if cmdFile := wrapperFile + ".cmd"; util.IsFile(cmdFile) {
path, err := filepath.Abs(cmdFile)
if err != nil {
return nil, err
}
return []string{"cmd", "/c", path}, nil
}
if batFile := wrapperFile + ".bat"; util.IsFile(batFile) {
briandealwis marked this conversation as resolved.
Show resolved Hide resolved
path, err := filepath.Abs(batFile)
if err != nil {
return nil, err
}
return []string{"cmd", "/c", path}, nil
}
path, err := exec.LookPath(builderExecutable)
if err != nil {
return nil, err
}
return []string{path}, nil
}
3 changes: 3 additions & 0 deletions pkg/skaffold/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ const (
UpdateCheckEnvironmentVariable = "SKAFFOLD_UPDATE_CHECK"

DefaultCloudBuildDockerImage = "gcr.io/cloud-builders/docker"

// A regex matching valid repository names (https://github.com/docker/distribution/blob/master/reference/reference.go)
RepositoryComponentRegex string = `^[a-z\d]+(?:(?:[_.]|__|-+)[a-z\d]+)*$`
)

var DefaultKubectlManifests = []string{"k8s/*.yaml"}
Expand Down
8 changes: 8 additions & 0 deletions pkg/skaffold/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ func StrSliceContains(sl []string, s string) bool {
return false
}

// IsFile returns true if the provided `flePath` refers to a fail, and
// false otherwise.
// TODO merge with AbsFile
func IsFile(filePath string) bool {
info, err := os.Stat(filePath)
return err == nil && !info.IsDir()
}

// ExpandPathsGlob expands paths according to filepath.Glob patterns
// Returns a list of unique files that match the glob patterns passed in.
func ExpandPathsGlob(workingDir string, paths []string) ([]string, error) {
Expand Down
Loading