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

[buildpacks] Refactor code to simplify #3395 #3441

Merged
merged 2 commits into from
Jan 7, 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
4 changes: 2 additions & 2 deletions pkg/skaffold/build/buildpacks/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (

// Build builds an artifact with Cloud Native Buildpacks:
// https://buildpacks.io/
func (b *Builder) Build(ctx context.Context, out io.Writer, a *latest.Artifact, tag string) (string, error) {
built, err := b.build(ctx, out, a.Workspace, a.BuildpackArtifact, tag)
func (b *Builder) Build(ctx context.Context, out io.Writer, artifact *latest.Artifact, tag string) (string, error) {
built, err := b.build(ctx, out, artifact, tag)
if err != nil {
return "", err
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/skaffold/build/buildpacks/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)

func (b *Builder) build(ctx context.Context, out io.Writer, workspace string, artifact *latest.BuildpackArtifact, tag string) (string, error) {
func (b *Builder) build(ctx context.Context, out io.Writer, a *latest.Artifact, tag string) (string, error) {
artifact := a.BuildpackArtifact
workspace := a.Workspace

// To improve caching, we always build the image with [:latest] tag
// This way, the lifecycle is able to "bootstrap" from the previously built image.
// The image will then be tagged as usual with the tag provided by the tag policy.
Expand Down
27 changes: 19 additions & 8 deletions pkg/skaffold/docker/image_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
)

func RetrieveWorkingDir(tagged string, insecureRegistries map[string]bool) (string, error) {
var cf *v1.ConfigFile
var err error

func RetrieveConfigFile(tagged string, insecureRegistries map[string]bool) (*v1.ConfigFile, error) {
if strings.ToLower(tagged) == "scratch" {
return "/", nil
return nil, nil
}

var cf *v1.ConfigFile
var err error

// TODO: use the proper RunContext
localDocker, err := NewAPIClient(&runcontext.RunContext{})
if err == nil {
Expand All @@ -45,12 +45,23 @@ func RetrieveWorkingDir(tagged string, insecureRegistries map[string]bool) (stri
cf, err = RetrieveRemoteConfig(tagged, insecureRegistries)
}
if err != nil {
return "", errors.Wrap(err, "retrieving image config")
return nil, errors.Wrap(err, "retrieving image config")
}

if cf.Config.WorkingDir == "" {
return cf, err
}

func RetrieveWorkingDir(tagged string, insecureRegistries map[string]bool) (string, error) {
cf, err := RetrieveConfigFile(tagged, insecureRegistries)
switch {
case err != nil:
return "", err
case cf == nil:
return "/", nil
case cf.Config.WorkingDir == "":
logrus.Debugf("Using default workdir '/' for %s", tagged)
return "/", nil
default:
return cf.Config.WorkingDir, nil
}
return cf.Config.WorkingDir, nil
}
2 changes: 1 addition & 1 deletion pkg/skaffold/runner/dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,10 @@ func TestDevSync(t *testing.T) {
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
var actualFileSyncEventCalls fileSyncEventCalls
t.SetupFakeKubernetesContext(api.Config{CurrentContext: "cluster1"})
t.Override(&fileSyncInProgress, func(int, string) { actualFileSyncEventCalls.InProgress++ })
t.Override(&fileSyncFailed, func(int, string, error) { actualFileSyncEventCalls.Failed++ })
t.Override(&fileSyncSucceeded, func(int, string) { actualFileSyncEventCalls.Succeeded++ })
t.SetupFakeKubernetesContext(api.Config{CurrentContext: "cluster1"})
t.Override(&sync.WorkingDir, func(string, map[string]bool) (string, error) { return "/", nil })
test.testBench.cycles = len(test.watchEvents)

Expand Down
14 changes: 7 additions & 7 deletions pkg/skaffold/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ var (
)

func NewItem(a *latest.Artifact, e filemon.Events, builds []build.Artifact, insecureRegistries map[string]bool, destProvider DestinationProvider) (*Item, error) {
if !e.HasChanged() || a.Sync == nil {
switch {
case !e.HasChanged():
return nil, nil
}

if len(a.Sync.Manual) > 0 {
case a.Sync != nil && len(a.Sync.Manual) > 0:
return manualSyncItem(a, e, builds, insecureRegistries)
}

if len(a.Sync.Infer) > 0 {
case a.Sync != nil && len(a.Sync.Infer) > 0:
return inferredSyncItem(a, e, builds, destProvider)
}

return nil, nil
default:
return nil, nil
}
}

func manualSyncItem(a *latest.Artifact, e filemon.Events, builds []build.Artifact, insecureRegistries map[string]bool) (*Item, error) {
Expand Down
4 changes: 1 addition & 3 deletions pkg/skaffold/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,9 +637,7 @@ func TestNewSyncItem(t *testing.T) {
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.Override(&WorkingDir, func(string, map[string]bool) (string, error) {
return test.workingDir, nil
})
t.Override(&WorkingDir, func(string, map[string]bool) (string, error) { return test.workingDir, nil })

provider := func() (map[string][]string, error) { return test.dependencies, nil }
actual, err := NewItem(test.artifact, test.evt, test.builds, nil, provider)
Expand Down