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

Add --disable-optimizations flag to make debugging easier. #24

Merged
merged 3 commits into from
Apr 18, 2019
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Ignore GoLand (IntelliJ) files.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

.idea/

13 changes: 9 additions & 4 deletions cmd/ko/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func addKubeCommands(topLevel *cobra.Command) {
no := &NameOptions{}
fo := &FilenameOptions{}
ta := &TagsOptions{}
do := &DebugOptions{}
apply := &cobra.Command{
Use: "apply -f FILENAME",
Short: "Apply the input files with image references resolved to built/pushed image digests.",
Expand Down Expand Up @@ -144,7 +145,7 @@ func addKubeCommands(topLevel *cobra.Command) {
stdin.Write([]byte("---\n"))
}
// Once primed kick things off.
resolveFilesToWriter(fo, no, lo, ta, stdin)
resolveFilesToWriter(fo, no, lo, ta, do, stdin)
}()

// Run it.
Expand All @@ -157,6 +158,7 @@ func addKubeCommands(topLevel *cobra.Command) {
addNamingArgs(apply, no)
addFileArg(apply, fo)
addTagsArg(apply, ta)
addDebugArg(apply, do)

// Collect the ko-specific apply flags before registering the kubectl global
// flags so that we can ignore them when passing kubectl global flags through
Expand Down Expand Up @@ -197,13 +199,14 @@ func addKubeCommands(topLevel *cobra.Command) {
ko resolve --local -f config/`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
resolveFilesToWriter(fo, no, lo, ta, os.Stdout)
resolveFilesToWriter(fo, no, lo, ta, do, os.Stdout)
},
}
addLocalArg(resolve, lo)
addNamingArgs(resolve, no)
addFileArg(resolve, fo)
addTagsArg(resolve, ta)
addDebugArg(resolve, do)
topLevel.AddCommand(resolve)

publish := &cobra.Command{
Expand Down Expand Up @@ -237,12 +240,13 @@ func addKubeCommands(topLevel *cobra.Command) {
ko publish --local github.com/foo/bar/cmd/baz github.com/foo/bar/cmd/blah`,
Args: cobra.MinimumNArgs(1),
Run: func(_ *cobra.Command, args []string) {
publishImages(args, no, lo, ta)
publishImages(args, no, lo, ta, do)
},
}
addLocalArg(publish, lo)
addNamingArgs(publish, no)
addTagsArg(publish, ta)
addDebugArg(publish, do)
topLevel.AddCommand(publish)

run := &cobra.Command{
Expand All @@ -259,7 +263,7 @@ func addKubeCommands(topLevel *cobra.Command) {
# This supports relative import paths as well.
ko run foo --image=./cmd/baz`,
Run: func(cmd *cobra.Command, args []string) {
imgs := publishImages([]string{bo.Path}, no, lo, ta)
imgs := publishImages([]string{bo.Path}, no, lo, ta, do)

// There's only one, but this is the simple way to access the
// reference since the import path may have been qualified.
Expand Down Expand Up @@ -293,6 +297,7 @@ func addKubeCommands(topLevel *cobra.Command) {
addNamingArgs(run, no)
addImageArg(run, bo)
addTagsArg(run, ta)
addDebugArg(run, do)

topLevel.AddCommand(run)
}
29 changes: 29 additions & 0 deletions cmd/ko/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2019 Google LLC All Rights Reserved.
//
// 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 main

import (
"github.com/spf13/cobra"
)

// DebugOptions holds options to improve debugging containers.
type DebugOptions struct {
DisableOptimizations bool
}

func addDebugArg(cmd *cobra.Command, do *DebugOptions) {
cmd.Flags().BoolVar(&do.DisableOptimizations, "disable-optimizations", do.DisableOptimizations,
"Disable optimizations when building Go code. Useful when you want to interactively debug the created container.")
}
4 changes: 2 additions & 2 deletions cmd/ko/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func qualifyLocalImport(importpath, gopathsrc, pwd string) (string, error) {
return filepath.Join(strings.TrimPrefix(pwd, gopathsrc+string(filepath.Separator)), importpath), nil
}

func publishImages(importpaths []string, no *NameOptions, lo *LocalOptions, ta *TagsOptions) map[string]name.Reference {
opt, err := gobuildOptions()
func publishImages(importpaths []string, no *NameOptions, lo *LocalOptions, ta *TagsOptions, do *DebugOptions) map[string]name.Reference {
opt, err := gobuildOptions(do)
if err != nil {
log.Fatalf("error setting up builder options: %v", err)
}
Expand Down
13 changes: 8 additions & 5 deletions cmd/ko/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/mattmoor/dep-notify/pkg/graph"
)

func gobuildOptions() ([]build.Option, error) {
func gobuildOptions(do *DebugOptions) ([]build.Option, error) {
creationTime, err := getCreationTime()
if err != nil {
return nil, err
Expand All @@ -41,11 +41,14 @@ func gobuildOptions() ([]build.Option, error) {
if creationTime != nil {
opts = append(opts, build.WithCreationTime(*creationTime))
}
if do.DisableOptimizations {
opts = append(opts, build.WithDisabledOptimizations())
}
return opts, nil
}

func makeBuilder() (*build.Caching, error) {
opt, err := gobuildOptions()
func makeBuilder(do *DebugOptions) (*build.Caching, error) {
opt, err := gobuildOptions(do)
if err != nil {
log.Fatalf("error setting up builder options: %v", err)
}
Expand Down Expand Up @@ -104,9 +107,9 @@ func makePublisher(no *NameOptions, lo *LocalOptions, ta *TagsOptions) (publish.
// resolvedFuture represents a "future" for the bytes of a resolved file.
type resolvedFuture chan []byte

func resolveFilesToWriter(fo *FilenameOptions, no *NameOptions, lo *LocalOptions, ta *TagsOptions, out io.WriteCloser) {
func resolveFilesToWriter(fo *FilenameOptions, no *NameOptions, lo *LocalOptions, ta *TagsOptions, do *DebugOptions, out io.WriteCloser) {
defer out.Close()
builder, err := makeBuilder()
builder, err := makeBuilder(do)
if err != nil {
log.Fatalf("error creating builder: %v", err)
}
Expand Down
19 changes: 15 additions & 4 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ const (

// GetBase takes an importpath and returns a base v1.Image.
type GetBase func(string) (v1.Image, error)
type builder func(string) (string, error)
type builder func(string, bool) (string, error)

type gobuild struct {
getBase GetBase
creationTime v1.Time
build builder
disableOptimizations bool
}

// Option is a functional option for NewGo.
Expand All @@ -53,6 +54,7 @@ type gobuildOpener struct {
getBase GetBase
creationTime v1.Time
build builder
disableOptimizations bool
}

func (gbo *gobuildOpener) Open() (Interface, error) {
Expand All @@ -63,6 +65,7 @@ func (gbo *gobuildOpener) Open() (Interface, error) {
getBase: gbo.getBase,
creationTime: gbo.creationTime,
build: gbo.build,
disableOptimizations: gbo.disableOptimizations,
}, nil
}

Expand Down Expand Up @@ -94,14 +97,22 @@ func (*gobuild) IsSupportedReference(s string) bool {
return p.IsCommand()
}

func build(ip string) (string, error) {
func build(ip string, disableOptimizations bool) (string, error) {
tmpDir, err := ioutil.TempDir("", "ko")
if err != nil {
return "", err
}
file := filepath.Join(tmpDir, "out")

cmd := exec.Command("go", "build", "-o", file, ip)
args := make([]string, 0, 6)
args = append(args, "build")
if disableOptimizations {
// Disable optimizations (-N) and inlining (-l).
args = append(args, "-gcflags", "all=-N -l")
}
args = append(args, "-o", file)
args = append(args, ip)
cmd := exec.Command("go", args...)

// Last one wins
// TODO(mattmoor): GOARCH=amd64
Expand Down Expand Up @@ -273,7 +284,7 @@ func tarKoData(importpath string) (*bytes.Buffer, error) {
// Build implements build.Interface
func (gb *gobuild) Build(s string) (v1.Image, error) {
// Do the build into a temporary file.
file, err := gb.build(s)
file, err := gb.build(s, gb.disableOptimizations)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/build/gobuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestGoBuildIsSupportedRef(t *testing.T) {
}

// A helper method we use to substitute for the default "build" method.
func writeTempFile(s string) (string, error) {
func writeTempFile(s string, _ bool) (string, error) {
tmpDir, err := ioutil.TempDir("", "ko")
if err != nil {
return "", err
Expand Down
9 changes: 9 additions & 0 deletions pkg/build/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ func WithCreationTime(t v1.Time) Option {
}
}

// WithDisabledOptimizations is a functional option for disabling optimizations
// when compiling.
func WithDisabledOptimizations() Option {
return func(gbo *gobuildOpener) error {
gbo.disableOptimizations = true
return nil
}
}

// withBuilder is a functional option for overriding the way go binaries
// are built. This is exposed for testing.
func withBuilder(b builder) Option {
Expand Down