-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add default build arguments for docker and buildpacks builder to optimize images for dev vs debug. * simplify method * Fix cache computation * fix linters * Filter out unused default docker build args. Introduce SKAFFOLD_RUN_MODE arg. * Modify debug test * missing new line * PR feedback * Simplify Dockerfile
- Loading branch information
1 parent
43023aa
commit 4652950
Showing
31 changed files
with
943 additions
and
301 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ import ( | |
|
||
"github.com/buildpacks/pack" | ||
|
||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config" | ||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker" | ||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" | ||
"github.com/GoogleContainerTools/skaffold/testutil" | ||
|
@@ -46,35 +47,69 @@ func TestBuild(t *testing.T) { | |
api *testutil.FakeAPIClient | ||
files map[string]string | ||
pushImages bool | ||
devMode bool | ||
shouldErr bool | ||
mode config.RunMode | ||
expectedOptions *pack.BuildOptions | ||
}{ | ||
{ | ||
description: "success", | ||
description: "success for debug", | ||
artifact: buildpacksArtifact("my/builder", "my/run"), | ||
tag: "img:tag", | ||
mode: config.RunModes.Debug, | ||
api: &testutil.FakeAPIClient{}, | ||
expectedOptions: &pack.BuildOptions{ | ||
AppPath: ".", | ||
Builder: "my/builder", | ||
RunImage: "my/run", | ||
Env: map[string]string{}, | ||
Env: debugModeArgs, | ||
Image: "img:latest", | ||
}, | ||
}, | ||
{ | ||
description: "success with buildpacks", | ||
description: "success for build", | ||
artifact: buildpacksArtifact("my/builder", "my/run"), | ||
tag: "img:tag", | ||
mode: config.RunModes.Build, | ||
api: &testutil.FakeAPIClient{}, | ||
expectedOptions: &pack.BuildOptions{ | ||
AppPath: ".", | ||
Builder: "my/builder", | ||
RunImage: "my/run", | ||
NoPull: true, | ||
Env: nonDebugModeArgs, | ||
Image: "img:latest", | ||
}, | ||
}, | ||
{ | ||
description: "success with buildpacks for debug", | ||
artifact: withTrustedBuilder(withBuildpacks([]string{"my/buildpack", "my/otherBuildpack"}, buildpacksArtifact("my/otherBuilder", "my/otherRun"))), | ||
tag: "img:tag", | ||
api: &testutil.FakeAPIClient{}, | ||
mode: config.RunModes.Debug, | ||
expectedOptions: &pack.BuildOptions{ | ||
AppPath: ".", | ||
Builder: "my/otherBuilder", | ||
RunImage: "my/otherRun", | ||
Buildpacks: []string{"my/buildpack", "my/otherBuildpack"}, | ||
TrustBuilder: true, | ||
Env: map[string]string{}, | ||
Env: debugModeArgs, | ||
Image: "img:latest", | ||
}, | ||
}, | ||
{ | ||
description: "success with buildpacks for build", | ||
artifact: withTrustedBuilder(withBuildpacks([]string{"my/buildpack", "my/otherBuildpack"}, buildpacksArtifact("my/otherBuilder", "my/otherRun"))), | ||
tag: "img:tag", | ||
api: &testutil.FakeAPIClient{}, | ||
mode: config.RunModes.Build, | ||
expectedOptions: &pack.BuildOptions{ | ||
AppPath: ".", | ||
Builder: "my/otherBuilder", | ||
RunImage: "my/otherRun", | ||
Buildpacks: []string{"my/buildpack", "my/otherBuildpack"}, | ||
TrustBuilder: true, | ||
NoPull: true, | ||
Env: nonDebugModeArgs, | ||
Image: "img:latest", | ||
}, | ||
}, | ||
|
@@ -83,6 +118,7 @@ func TestBuild(t *testing.T) { | |
artifact: buildpacksArtifact("my/builder2", "my/run2"), | ||
tag: "img:tag", | ||
api: &testutil.FakeAPIClient{}, | ||
mode: config.RunModes.Build, | ||
files: map[string]string{ | ||
"project.toml": `[[build.env]] | ||
name = "GOOGLE_RUNTIME_VERSION" | ||
|
@@ -99,9 +135,9 @@ version = "1.0" | |
Builder: "my/builder2", | ||
RunImage: "my/run2", | ||
Buildpacks: []string{"my/buildpack", "my/[email protected]"}, | ||
Env: map[string]string{ | ||
Env: addDefaultArgs(config.RunModes.Build, map[string]string{ | ||
"GOOGLE_RUNTIME_VERSION": "14.3.0", | ||
}, | ||
}), | ||
Image: "img:latest", | ||
}, | ||
}, | ||
|
@@ -120,14 +156,15 @@ id = "my/ignored" | |
Builder: "my/builder3", | ||
RunImage: "my/run3", | ||
Buildpacks: []string{"my/buildpack", "my/otherBuildpack"}, | ||
Env: map[string]string{}, | ||
Env: nonDebugModeArgs, | ||
Image: "img:latest", | ||
}, | ||
}, | ||
{ | ||
description: "Combine env from skaffold.yaml and project.toml", | ||
artifact: withEnv([]string{"KEY1=VALUE1"}, buildpacksArtifact("my/builder4", "my/run4")), | ||
tag: "img:tag", | ||
mode: config.RunModes.Build, | ||
api: &testutil.FakeAPIClient{}, | ||
files: map[string]string{ | ||
"project.toml": `[[build.env]] | ||
|
@@ -139,10 +176,10 @@ value = "VALUE2" | |
AppPath: ".", | ||
Builder: "my/builder4", | ||
RunImage: "my/run4", | ||
Env: map[string]string{ | ||
Env: addDefaultArgs(config.RunModes.Build, map[string]string{ | ||
"KEY1": "VALUE1", | ||
"KEY2": "VALUE2", | ||
}, | ||
}), | ||
Image: "img:latest", | ||
}, | ||
}, | ||
|
@@ -151,14 +188,14 @@ value = "VALUE2" | |
artifact: withSync(&latest.Sync{Auto: &latest.Auto{}}, buildpacksArtifact("another/builder", "another/run")), | ||
tag: "img:tag", | ||
api: &testutil.FakeAPIClient{}, | ||
devMode: true, | ||
mode: config.RunModes.Dev, | ||
expectedOptions: &pack.BuildOptions{ | ||
AppPath: ".", | ||
Builder: "another/builder", | ||
RunImage: "another/run", | ||
Env: map[string]string{ | ||
Env: addDefaultArgs(config.RunModes.Build, map[string]string{ | ||
"GOOGLE_DEVMODE": "1", | ||
}, | ||
}), | ||
Image: "img:latest", | ||
}, | ||
}, | ||
|
@@ -167,12 +204,12 @@ value = "VALUE2" | |
artifact: buildpacksArtifact("my/other-builder", "my/run"), | ||
tag: "img:tag", | ||
api: &testutil.FakeAPIClient{}, | ||
devMode: true, | ||
mode: config.RunModes.Dev, | ||
expectedOptions: &pack.BuildOptions{ | ||
AppPath: ".", | ||
Builder: "my/other-builder", | ||
RunImage: "my/run", | ||
Env: map[string]string{}, | ||
Env: nonDebugModeArgs, | ||
Image: "img:latest", | ||
}, | ||
}, | ||
|
@@ -223,7 +260,7 @@ value = "VALUE2" | |
Add("img:latest", "builtImageID") | ||
localDocker := docker.NewLocalDaemon(test.api, nil, false, nil) | ||
|
||
builder := NewArtifactBuilder(localDocker, test.pushImages, test.devMode) | ||
builder := NewArtifactBuilder(localDocker, test.pushImages, test.mode) | ||
_, err := builder.Build(context.Background(), ioutil.Discard, test.artifact, test.tag) | ||
|
||
t.CheckError(test.shouldErr, err) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
Copyright 2020 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 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/config" | ||
|
||
var debugModeArgs = map[string]string{ | ||
"GOOGLE_GOGCFLAGS": "'all=-N -l'", // disable build optimization for Golang | ||
// TODO: Add for other languages | ||
} | ||
|
||
var nonDebugModeArgs = map[string]string{} | ||
|
||
func addDefaultArgs(mode config.RunMode, existing map[string]string) map[string]string { | ||
var args map[string]string | ||
switch mode { | ||
case config.RunModes.Debug: | ||
args = debugModeArgs | ||
default: | ||
args = nonDebugModeArgs | ||
} | ||
|
||
for k, v := range args { | ||
if _, found := existing[k]; !found { | ||
existing[k] = v | ||
} | ||
} | ||
return existing | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Copyright 2020 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 ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/buildpacks/pack/project" | ||
|
||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/misc" | ||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config" | ||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" | ||
) | ||
|
||
func GetEnv(a *latest.Artifact, mode config.RunMode) (map[string]string, error) { | ||
artifact := a.BuildpackArtifact | ||
workspace := a.Workspace | ||
|
||
path := filepath.Join(workspace, artifact.ProjectDescriptor) | ||
projectDescriptor, err := project.ReadProjectDescriptor(path) | ||
if err != nil && !os.IsNotExist(err) { | ||
return nil, fmt.Errorf("failed to read project descriptor %q: %w", path, err) | ||
} | ||
return env(a, mode, projectDescriptor) | ||
} | ||
|
||
func env(a *latest.Artifact, mode config.RunMode, projectDescriptor project.Descriptor) (map[string]string, error) { | ||
envVars, err := misc.EvaluateEnv(a.BuildpackArtifact.Env) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to evaluate env variables: %w", err) | ||
} | ||
|
||
if mode == config.RunModes.Dev && a.Sync != nil && a.Sync.Auto != nil { | ||
envVars = append(envVars, "GOOGLE_DEVMODE=1") | ||
} | ||
|
||
env := envMap(envVars) | ||
for _, kv := range projectDescriptor.Build.Env { | ||
env[kv.Name] = kv.Value | ||
} | ||
env = addDefaultArgs(mode, env) | ||
return env, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.