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

Remove bazel plugin & revert back to original #1989

Merged
merged 2 commits into from
Apr 20, 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
4 changes: 0 additions & 4 deletions integration/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ func TestRun(t *testing.T) {
dir: "testdata/plugin/gcb",
deployments: []string{"leeroy-app", "leeroy-web"},
remoteOnly: true,
}, {
description: "bazel plugin in local exec environment",
dir: "testdata/plugin/local/bazel",
pods: []string{"bazel"},
}, {
description: "docker plugin in local exec environment",
dir: "testdata/plugin/local/docker",
Expand Down
9 changes: 0 additions & 9 deletions integration/testdata/plugin/local/bazel/BUILD

This file was deleted.

30 changes: 0 additions & 30 deletions integration/testdata/plugin/local/bazel/WORKSPACE

This file was deleted.

8 changes: 0 additions & 8 deletions integration/testdata/plugin/local/bazel/k8s/k8s-pod.yaml

This file was deleted.

13 changes: 0 additions & 13 deletions integration/testdata/plugin/local/bazel/main.go

This file was deleted.

10 changes: 0 additions & 10 deletions integration/testdata/plugin/local/bazel/skaffold.yaml

This file was deleted.

153 changes: 131 additions & 22 deletions pkg/skaffold/build/local/bazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,140 @@ package local

import (
"context"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/plugin/builders/bazel"
runcontext "github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/context"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/pkg/errors"
)

func (b *Builder) buildBazel(ctx context.Context, out io.Writer, a *latest.Artifact, tag string) (string, error) {
builder := bazel.NewBuilder()
builder.LocalBuild = b.cfg
builder.LocalDocker = b.localDocker
builder.KubeContext = b.kubeContext
builder.PushImages = b.pushImages
builder.PluginMode = false

builder.Init(&runcontext.RunContext{
Opts: &config.SkaffoldOptions{
SkipTests: b.skipTests,
},
Cfg: &latest.Pipeline{
Build: latest.BuildConfig{
ExecutionEnvironment: &latest.ExecutionEnvironment{},
},
},
})
return builder.BuildArtifact(ctx, out, a, tag)
func (b *Builder) buildBazel(ctx context.Context, out io.Writer, artifact *latest.Artifact, tag string) (string, error) {
args := []string{"build"}
a := artifact.ArtifactType.BazelArtifact
workspace := artifact.Workspace
args = append(args, a.BuildArgs...)
args = append(args, a.BuildTarget)

// FIXME: is it possible to apply b.skipTests?
cmd := exec.CommandContext(ctx, "bazel", args...)
cmd.Dir = workspace
cmd.Stdout = out
cmd.Stderr = out
if err := util.RunCmd(cmd); err != nil {
return "", errors.Wrap(err, "running command")
}

bazelBin, err := bazelBin(ctx, workspace, a)
if err != nil {
return "", errors.Wrap(err, "getting path of bazel-bin")
}

tarPath := filepath.Join(bazelBin, buildTarPath(a.BuildTarget))

if b.pushImages {
return pushImage(tarPath, tag, b.insecureRegistries)
}

return b.loadImage(ctx, out, tarPath, a, tag)
}

// pushImage pushes the tarball image created by bazel
func pushImage(tarPath, tag string, insecureRegistries map[string]bool) (string, error) {
t, err := name.NewTag(tag, name.WeakValidation)
if err != nil {
return "", errors.Wrapf(err, "parsing tag %q", tag)
}

auth, err := authn.DefaultKeychain.Resolve(t.Registry)
if err != nil {
return "", errors.Wrapf(err, "getting creds for %q", t)
}

i, err := tarball.ImageFromPath(tarPath, nil)
if err != nil {
return "", errors.Wrapf(err, "reading image %q", tarPath)
}

if err := remote.Write(t, i, auth, http.DefaultTransport); err != nil {
return "", errors.Wrapf(err, "writing image %q", t)
}

return docker.RemoteDigest(tag, insecureRegistries)
}

func (b *Builder) loadImage(ctx context.Context, out io.Writer, tarPath string, a *latest.BazelArtifact, tag string) (string, error) {
imageTar, err := os.Open(tarPath)
if err != nil {
return "", errors.Wrap(err, "opening image tarball")
}
defer imageTar.Close()

bazelTag := buildImageTag(a.BuildTarget)
imageID, err := b.localDocker.Load(ctx, out, imageTar, bazelTag)
if err != nil {
return "", errors.Wrap(err, "loading image into docker daemon")
}

if err := b.localDocker.Tag(ctx, imageID, tag); err != nil {
return "", errors.Wrap(err, "tagging the image")
}

b.builtImages = append(b.builtImages, imageID)
return imageID, nil
}

func bazelBin(ctx context.Context, workspace string, a *latest.BazelArtifact) (string, error) {
args := []string{"info", "bazel-bin"}
args = append(args, a.BuildArgs...)

cmd := exec.CommandContext(ctx, "bazel", args...)
cmd.Dir = workspace

buf, err := util.RunCmdOut(cmd)
if err != nil {
return "", err
}

return strings.TrimSpace(string(buf)), nil
}

func trimTarget(buildTarget string) string {
//TODO(r2d4): strip off leading //:, bad
trimmedTarget := strings.TrimPrefix(buildTarget, "//")
// Useful if root target "//:target"
trimmedTarget = strings.TrimPrefix(trimmedTarget, ":")

return trimmedTarget
}

func buildTarPath(buildTarget string) string {
tarPath := trimTarget(buildTarget)
tarPath = strings.Replace(tarPath, ":", string(os.PathSeparator), 1)

return tarPath
}

func buildImageTag(buildTarget string) string {
imageTag := trimTarget(buildTarget)
imageTag = strings.TrimPrefix(imageTag, ":")

//TODO(r2d4): strip off trailing .tar, even worse
imageTag = strings.TrimSuffix(imageTag, ".tar")

if strings.Contains(imageTag, ":") {
return fmt.Sprintf("bazel/%s", imageTag)
}

return fmt.Sprintf("bazel:%s", imageTag)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package bazel
package local

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/build/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import (
"strings"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/bazel"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/tag"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/jib"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/plugin/builders/bazel"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/pkg/errors"
Expand Down
2 changes: 0 additions & 2 deletions pkg/skaffold/build/plugin/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/hashicorp/go-hclog"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/plugin/builders/bazel"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/plugin/builders/docker"
)

Expand All @@ -33,7 +32,6 @@ const DefaultPluginLogLevel = hclog.Info
// SkaffoldCorePluginExecutionMap maps the core plugin name to the execution function
var SkaffoldCorePluginExecutionMap = map[string]func() error{
"docker": docker.Execute(DefaultPluginLogLevel),
"bazel": bazel.Execute(DefaultPluginLogLevel),
}

// GetCorePluginFromEnv returns the core plugin name if env variables for plugins are set properly
Expand Down
43 changes: 0 additions & 43 deletions pkg/skaffold/plugin/builders/bazel/bazel.go

This file was deleted.

Loading