Skip to content

Commit

Permalink
Build debuggable containers (#4606)
Browse files Browse the repository at this point in the history
* 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
gsquared94 authored Aug 23, 2020
1 parent 43023aa commit 4652950
Show file tree
Hide file tree
Showing 31 changed files with 943 additions and 301 deletions.
5 changes: 2 additions & 3 deletions integration/testdata/debug/go/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
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
RUN go build -gcflags="${SKAFFOLD_GO_GCFLAGS}" -o /app .

FROM gcr.io/distroless/base
# `skaffold debug` uses GOTRACEBACK as an indicator of the Go runtime
Expand Down
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: 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",
},
},
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: addDefaultArgs(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: 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]]
Expand All @@ -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",
},
},
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: addDefaultArgs(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: nonDebugModeArgs,
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
43 changes: 43 additions & 0 deletions pkg/skaffold/build/buildpacks/default_args.go
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
}
59 changes: 59 additions & 0 deletions pkg/skaffold/build/buildpacks/env.go
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
}
15 changes: 2 additions & 13 deletions pkg/skaffold/build/buildpacks/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/buildpacks/pack"
"github.com/buildpacks/pack/project"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/misc"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
)
Expand Down Expand Up @@ -65,21 +64,11 @@ func (b *Builder) build(ctx context.Context, out io.Writer, a *latest.Artifact,
}
latest := parsed.BaseName + ":latest"

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

if b.devMode && 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
}

// List buildpacks to be used for the build.
// Those specified in the skaffold.yaml replace those in the project.toml.
buildpacks := artifact.Buildpacks
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
Loading

0 comments on commit 4652950

Please sign in to comment.