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

Auto sync with Buildpacks #3555

Merged
merged 1 commit into from
Jan 27, 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
117 changes: 72 additions & 45 deletions pkg/skaffold/build/buildpacks/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,19 @@ func (f *fakePack) runPack(_ context.Context, _ io.Writer, opts pack.BuildOption
func TestBuild(t *testing.T) {
tests := []struct {
description string
artifact *latest.BuildpackArtifact
artifact *latest.Artifact
tag string
api *testutil.FakeAPIClient
pushImages bool
noPull bool
devMode bool
shouldErr bool
expectedOptions *pack.BuildOptions
}{
{
description: "success",
artifact: &latest.BuildpackArtifact{
Builder: "my/builder",
RunImage: "my/run",
Dependencies: defaultBuildpackDependencies(),
},
tag: "img:tag",
api: &testutil.FakeAPIClient{},
artifact: buildpacksArtifact("my/builder", "my/run"),
tag: "img:tag",
api: &testutil.FakeAPIClient{},
expectedOptions: &pack.BuildOptions{
AppPath: ".",
Builder: "my/builder",
Expand All @@ -67,41 +63,58 @@ func TestBuild(t *testing.T) {
},
},
{
description: "invalid ref",
artifact: &latest.BuildpackArtifact{
Builder: "my/builder",
RunImage: "my/run",
Dependencies: defaultBuildpackDependencies(),
description: "dev mode",
artifact: withSync(&latest.Sync{Infer: []string{"**/*"}}, buildpacksArtifact("another/builder", "another/run")),
tag: "img:tag",
api: &testutil.FakeAPIClient{},
devMode: true,
expectedOptions: &pack.BuildOptions{
AppPath: ".",
Builder: "another/builder",
RunImage: "another/run",
Env: map[string]string{
"GOOGLE_DEVMODE": "1",
},
Image: "img:latest",
},
tag: "in valid ref",
api: &testutil.FakeAPIClient{},
shouldErr: true,
},
{
description: "push error",
artifact: &latest.BuildpackArtifact{
Builder: "my/builder",
RunImage: "my/run",
Dependencies: defaultBuildpackDependencies(),
description: "dev mode but no sync",
artifact: buildpacksArtifact("my/other-builder", "my/run"),
tag: "img:tag",
api: &testutil.FakeAPIClient{},
devMode: true,
expectedOptions: &pack.BuildOptions{
AppPath: ".",
Builder: "my/other-builder",
RunImage: "my/run",
Env: map[string]string{},
Image: "img:latest",
},
tag: "img:tag",
pushImages: true,
},
{
description: "invalid ref",
artifact: buildpacksArtifact("my/builder", "my/run"),
tag: "in valid ref",
api: &testutil.FakeAPIClient{},
shouldErr: true,
},
{
description: "push error",
artifact: buildpacksArtifact("my/builder", "my/run"),
tag: "img:tag",
pushImages: true,
api: &testutil.FakeAPIClient{
ErrImagePush: true,
},
shouldErr: true,
},
{
description: "invalid env",
artifact: &latest.BuildpackArtifact{
Builder: "my/builder",
RunImage: "my/run",
Env: []string{"INVALID"},
Dependencies: defaultBuildpackDependencies(),
},
tag: "img:tag",
api: &testutil.FakeAPIClient{},
shouldErr: true,
artifact: withEnv([]string{"INVALID"}, buildpacksArtifact("my/builder", "my/run")),
tag: "img:tag",
api: &testutil.FakeAPIClient{},
shouldErr: true,
},
}
for _, test := range tests {
Expand All @@ -111,18 +124,13 @@ func TestBuild(t *testing.T) {
t.Override(&runPackBuildFunc, pack.runPack)

test.api.
Add(test.artifact.Builder, "builderImageID").
Add(test.artifact.RunImage, "runImageID").
Add(test.artifact.BuildpackArtifact.Builder, "builderImageID").
Add(test.artifact.BuildpackArtifact.RunImage, "runImageID").
Add("img:latest", "builtImageID")
localDocker := docker.NewLocalDaemon(test.api, nil, false, nil)

builder := NewArtifactBuilder(localDocker, test.pushImages)
_, err := builder.Build(context.Background(), ioutil.Discard, &latest.Artifact{
Workspace: ".",
ArtifactType: latest.ArtifactType{
BuildpackArtifact: test.artifact,
},
}, test.tag)
builder := NewArtifactBuilder(localDocker, test.pushImages, test.devMode)
_, err := builder.Build(context.Background(), ioutil.Discard, test.artifact, test.tag)

t.CheckError(test.shouldErr, err)
if test.expectedOptions != nil {
Expand All @@ -132,8 +140,27 @@ func TestBuild(t *testing.T) {
}
}

func defaultBuildpackDependencies() *latest.BuildpackDependencies {
return &latest.BuildpackDependencies{
Paths: []string{"."},
func buildpacksArtifact(builder, runImage string) *latest.Artifact {
return &latest.Artifact{
Workspace: ".",
ArtifactType: latest.ArtifactType{
BuildpackArtifact: &latest.BuildpackArtifact{
Builder: builder,
RunImage: runImage,
Dependencies: &latest.BuildpackDependencies{
Paths: []string{"."},
},
},
},
}
}

func withEnv(env []string, artifact *latest.Artifact) *latest.Artifact {
artifact.BuildpackArtifact.Env = env
return artifact
}

func withSync(sync *latest.Sync, artifact *latest.Artifact) *latest.Artifact {
artifact.Sync = sync
return artifact
}
4 changes: 4 additions & 0 deletions pkg/skaffold/build/buildpacks/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func (b *Builder) build(ctx context.Context, out io.Writer, a *latest.Artifact,
return "", errors.Wrap(err, "unable to evaluate env variables")
}

if b.devMode && a.Sync != nil && len(a.Sync.Infer) > 0 {
env = append(env, "GOOGLE_DEVMODE=1")
}

alreadyPulled := images.AreAlreadyPulled(artifact.Builder, artifact.RunImage)

if err := runPackBuildFunc(ctx, out, pack.BuildOptions{
Expand Down
66 changes: 66 additions & 0 deletions pkg/skaffold/build/buildpacks/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2019 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 buildpacks

import (
"encoding/json"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
)

type buildMetadata struct {
Bom []bom `json:"bom"`
}

type bom struct {
Metadata bomMetadata `json:"metadata"`
}

type bomMetadata struct {
Sync []syncRule `json:"devmode.sync"`
}

type syncRule struct {
Src string `json:"src"`
Dest string `json:"dest"`
}

// $ docker inspect demo/buildpacks | jq -r '.[].Config.Labels["io.buildpacks.build.metadata"] | fromjson.bom[].metadata["devmode.sync"]'
func SyncRules(labels map[string]string) ([]*latest.SyncRule, error) {
metadataJSON, present := labels["io.buildpacks.build.metadata"]
if !present {
return nil, nil
}

m := buildMetadata{}
if err := json.Unmarshal([]byte(metadataJSON), &m); err != nil {
return nil, err
}

var rules []*latest.SyncRule

for _, b := range m.Bom {
for _, sync := range b.Metadata.Sync {
rules = append(rules, &latest.SyncRule{
Src: sync.Src,
Dest: sync.Dest,
})
}
}

return rules, nil
}
71 changes: 71 additions & 0 deletions pkg/skaffold/build/buildpacks/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright 2019 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 buildpacks

import (
"testing"

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

func TestSyncRules(t *testing.T) {
tests := []struct {
description string
labels map[string]string
expectedRules []*latest.SyncRule
shouldErr bool
}{
{
description: "missing labels",
labels: map[string]string{},
},
{
description: "invalid labels",
labels: map[string]string{
"io.buildpacks.build.metadata": "invalid",
},
shouldErr: true,
},
{
description: "valid labels",
labels: map[string]string{
"io.buildpacks.build.metadata": `{
"bom":[{
"metadata":{
"devmode.sync": [
{"src":"src-value1","dest":"dest-value1"},
{"src":"src-value2","dest":"dest-value2"}
]
}
}]
}`,
},
expectedRules: []*latest.SyncRule{
{Src: "src-value1", Dest: "dest-value1"},
{Src: "src-value2", Dest: "dest-value2"},
},
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
rules, err := SyncRules(test.labels)

t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expectedRules, rules)
})
}
}
4 changes: 3 additions & 1 deletion pkg/skaffold/build/buildpacks/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
type Builder struct {
localDocker docker.LocalDaemon
pushImages bool
devMode bool
balopat marked this conversation as resolved.
Show resolved Hide resolved
}

// NewArtifactBuilder returns a new buildpack artifact builder
func NewArtifactBuilder(localDocker docker.LocalDaemon, pushImages bool) *Builder {
func NewArtifactBuilder(localDocker docker.LocalDaemon, pushImages, devMode bool) *Builder {
return &Builder{
localDocker: localDocker,
pushImages: pushImages,
devMode: devMode,
}
}
2 changes: 2 additions & 0 deletions pkg/skaffold/build/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type cache struct {
insecureRegistries map[string]bool
cacheFile string
imagesAreLocal bool
devMode bool
}

// DependencyLister fetches a list of dependencies for an artifact
Expand Down Expand Up @@ -85,6 +86,7 @@ func NewCache(runCtx *runcontext.RunContext, imagesAreLocal bool, dependencies D
insecureRegistries: runCtx.InsecureRegistries,
cacheFile: cacheFile,
imagesAreLocal: imagesAreLocal,
devMode: runCtx.DevMode,
}, nil
}

Expand Down
11 changes: 8 additions & 3 deletions pkg/skaffold/build/cache/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ var (
artifactConfigFunction = artifactConfig
)

func getHashForArtifact(ctx context.Context, depLister DependencyLister, a *latest.Artifact) (string, error) {
func getHashForArtifact(ctx context.Context, depLister DependencyLister, a *latest.Artifact, devMode bool) (string, error) {
var inputs []string

// Append the artifact's configuration
config, err := artifactConfigFunction(a)
config, err := artifactConfigFunction(a, devMode)
if err != nil {
return "", errors.Wrapf(err, "getting artifact's configuration for %s", a.ImageName)
}
Expand Down Expand Up @@ -100,12 +100,17 @@ func getHashForArtifact(ctx context.Context, depLister DependencyLister, a *late
return hex.EncodeToString(hasher.Sum(nil)), nil
}

func artifactConfig(a *latest.Artifact) (string, error) {
// TODO(dgageot): when the buildpacks builder image digest changes, we need to change the hash
func artifactConfig(a *latest.Artifact, devMode bool) (string, error) {
buf, err := json.Marshal(a.ArtifactType)
if err != nil {
return "", errors.Wrapf(err, "marshalling the artifact's configuration for %s", a.ImageName)
}

if devMode && a.BuildpackArtifact != nil && a.Sync != nil && len(a.Sync.Infer) > 0 {
return string(buf) + ".DEV", nil
}

return string(buf), nil
}

Expand Down
Loading