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

Build debuggable containers #4606

Merged
merged 11 commits into from
Aug 23, 2020
9 changes: 5 additions & 4 deletions integration/testdata/debug/go/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
FROM golang:1.12 as builder

COPY app.go .
# Must use eval to handle GOGCFLAGS with spaces like `-gcflags='all=-N -l'`
ARG GOGCFLAGS
RUN eval go build "${GOGCFLAGS}" -o /app .
ARG SKAFFOLD_GO_GCFLAGS
ARG SKAFFOLD_GO_LDFLAGS
# Must use eval to handle gcflags with spaces like `all=-N -l`
RUN eval go build -gcflags="${SKAFFOLD_GO_GCFLAGS}" -ldflags="${SKAFFOLD_GO_LDFLAGS}" -o /app .

FROM gcr.io/distroless/base
# `skaffold debug` uses GOTRACEBACK as an indicator of the Go runtime
ENV GOTRACEBACK=all
WORKDIR /
EXPOSE 8080
COPY --from=builder /app .
CMD ["/app"]
CMD ["/app"]
5 changes: 0 additions & 5 deletions integration/testdata/debug/skaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ build:
context: python3
- image: skaffold-debug-go
context: go
docker:
buildArgs:
GOGCFLAGS: "-gcflags='all=-N -l'"

deploy:
kubectl:
Expand Down Expand Up @@ -57,5 +54,3 @@ profiles:
context: go
buildpacks:
builder: "gcr.io/buildpacks/builder:v1"
env:
- GOOGLE_GCFLAGS="-gcflags='all=-N -l'"
69 changes: 53 additions & 16 deletions pkg/skaffold/build/buildpacks/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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: buildArgsForDebug,
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: buildArgsForDev,
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: buildArgsForDebug,
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: buildArgsForDev,
Image: "img:latest",
},
},
Expand All @@ -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"
Expand All @@ -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: AppendDefaultArgs(config.RunModes.Build, map[string]string{
"GOOGLE_RUNTIME_VERSION": "14.3.0",
},
}),
Image: "img:latest",
},
},
Expand All @@ -120,14 +156,15 @@ id = "my/ignored"
Builder: "my/builder3",
RunImage: "my/run3",
Buildpacks: []string{"my/buildpack", "my/otherBuildpack"},
Env: map[string]string{},
Env: buildArgsForDev,
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]]
Expand All @@ -139,10 +176,10 @@ value = "VALUE2"
AppPath: ".",
Builder: "my/builder4",
RunImage: "my/run4",
Env: map[string]string{
Env: AppendDefaultArgs(config.RunModes.Build, map[string]string{
"KEY1": "VALUE1",
"KEY2": "VALUE2",
},
}),
Image: "img:latest",
},
},
Expand All @@ -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: AppendDefaultArgs(config.RunModes.Build, map[string]string{
"GOOGLE_DEVMODE": "1",
},
}),
Image: "img:latest",
},
},
Expand All @@ -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: buildArgsForDev,
Image: "img:latest",
},
},
Expand Down Expand Up @@ -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)
Expand Down
46 changes: 46 additions & 0 deletions pkg/skaffold/build/buildpacks/default_args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
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 buildArgsForDebug = map[string]string{
"GOOGLE_GOGCFLAGS": "'all=-N -l'", // disable build optimization for Golang
// TODO: Add for other languages
}

var buildArgsForDev = map[string]string{
"GOOGLE_GOLDFLAGS": "-w", // omit debug information in build output for Golang
gsquared94 marked this conversation as resolved.
Show resolved Hide resolved
// TODO: Add for other languages
}

func AppendDefaultArgs(mode config.RunMode, existing map[string]string) map[string]string {
gsquared94 marked this conversation as resolved.
Show resolved Hide resolved
var args map[string]string
switch mode {
case config.RunModes.Debug:
args = buildArgsForDebug
default:
args = buildArgsForDev
}

for k, v := range args {
if _, found := existing[k]; !found {
existing[k] = v
}
}
return existing
}
6 changes: 4 additions & 2 deletions pkg/skaffold/build/buildpacks/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"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/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
)
Expand Down Expand Up @@ -65,20 +66,21 @@ func (b *Builder) build(ctx context.Context, out io.Writer, a *latest.Artifact,
}
latest := parsed.BaseName + ":latest"

// Eveluate Env Vars.
// Evaluate Env Vars.
envVars, err := misc.EvaluateEnv(artifact.Env)
if err != nil {
return "", fmt.Errorf("unable to evaluate env variables: %w", err)
}

if b.devMode && a.Sync != nil && a.Sync.Auto != nil {
if b.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
}
gsquared94 marked this conversation as resolved.
Show resolved Hide resolved
env = AppendDefaultArgs(b.mode, env)

// List buildpacks to be used for the build.
// Those specified in the skaffold.yaml replace those in the project.toml.
Expand Down
11 changes: 7 additions & 4 deletions pkg/skaffold/build/buildpacks/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,23 @@ limitations under the License.

package buildpacks

import "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
)

// Builder is an artifact builder that uses buildpacks
type Builder struct {
localDocker docker.LocalDaemon
pushImages bool
devMode bool
mode config.RunMode
}

// NewArtifactBuilder returns a new buildpack artifact builder
func NewArtifactBuilder(localDocker docker.LocalDaemon, pushImages, devMode bool) *Builder {
func NewArtifactBuilder(localDocker docker.LocalDaemon, pushImages bool, mode config.RunMode) *Builder {
return &Builder{
localDocker: localDocker,
pushImages: pushImages,
devMode: devMode,
mode: mode,
}
}
2 changes: 1 addition & 1 deletion pkg/skaffold/build/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func NewCache(runCtx *runcontext.RunContext, imagesAreLocal bool, dependencies D
cacheFile: cacheFile,
imagesAreLocal: imagesAreLocal,
hashForArtifact: func(ctx context.Context, a *latest.Artifact) (string, error) {
return getHashForArtifact(ctx, dependencies, a, runCtx.DevMode())
return getHashForArtifact(ctx, dependencies, a, runCtx.Mode())
},
}, nil
}
Expand Down
17 changes: 12 additions & 5 deletions pkg/skaffold/build/cache/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/misc"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
)
Expand All @@ -40,11 +41,11 @@ var (
artifactConfigFunction = artifactConfig
)

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

// Append the artifact's configuration
config, err := artifactConfigFunction(a, devMode)
config, err := artifactConfigFunction(a, mode)
if err != nil {
return "", fmt.Errorf("getting artifact's configuration for %q: %w", a.ImageName, err)
}
Expand Down Expand Up @@ -72,7 +73,9 @@ func getHashForArtifact(ctx context.Context, depLister DependencyLister, a *late

// add build args for the artifact if specified
if buildArgs := retrieveBuildArgs(a); buildArgs != nil {
buildArgs, err := docker.EvaluateBuildArgs(buildArgs)
buildArgs, err := docker.EvaluateBuildArgs(buildArgs, func(m map[string]*string) map[string]*string {
return docker.AppendDefaultArgs(mode, buildArgs)
})
if err != nil {
return "", fmt.Errorf("evaluating build args: %w", err)
}
Expand Down Expand Up @@ -100,16 +103,20 @@ func getHashForArtifact(ctx context.Context, depLister DependencyLister, a *late
}

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

if devMode && a.BuildpackArtifact != nil && a.Sync != nil && a.Sync.Auto != nil {
if mode == config.RunModes.Dev && a.BuildpackArtifact != nil && a.Sync != nil && a.Sync.Auto != nil {
return string(buf) + ".DEV", nil
}

if mode == config.RunModes.Debug {
return string(buf) + ".DEBUG", nil
}

return string(buf), nil
}

Expand Down
Loading