Skip to content

Commit b0a1702

Browse files
Harwaynejonjohnsonjr
authored andcommitted
Add --disable-optimizations flag to make debugging easier. (ko-build#24)
* Ignore GoLand files. * Add --disable-optimizations flag to make debugging easier. * Fix unit test (by ignoring the new functionality).
1 parent 3584e2d commit b0a1702

File tree

8 files changed

+76
-16
lines changed

8 files changed

+76
-16
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Ignore GoLand (IntelliJ) files.
2+
.idea/
3+

cmd/ko/commands.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func addKubeCommands(topLevel *cobra.Command) {
7070
no := &NameOptions{}
7171
fo := &FilenameOptions{}
7272
ta := &TagsOptions{}
73+
do := &DebugOptions{}
7374
apply := &cobra.Command{
7475
Use: "apply -f FILENAME",
7576
Short: "Apply the input files with image references resolved to built/pushed image digests.",
@@ -144,7 +145,7 @@ func addKubeCommands(topLevel *cobra.Command) {
144145
stdin.Write([]byte("---\n"))
145146
}
146147
// Once primed kick things off.
147-
resolveFilesToWriter(fo, no, lo, ta, stdin)
148+
resolveFilesToWriter(fo, no, lo, ta, do, stdin)
148149
}()
149150

150151
// Run it.
@@ -157,6 +158,7 @@ func addKubeCommands(topLevel *cobra.Command) {
157158
addNamingArgs(apply, no)
158159
addFileArg(apply, fo)
159160
addTagsArg(apply, ta)
161+
addDebugArg(apply, do)
160162

161163
// Collect the ko-specific apply flags before registering the kubectl global
162164
// flags so that we can ignore them when passing kubectl global flags through
@@ -197,13 +199,14 @@ func addKubeCommands(topLevel *cobra.Command) {
197199
ko resolve --local -f config/`,
198200
Args: cobra.NoArgs,
199201
Run: func(cmd *cobra.Command, args []string) {
200-
resolveFilesToWriter(fo, no, lo, ta, os.Stdout)
202+
resolveFilesToWriter(fo, no, lo, ta, do, os.Stdout)
201203
},
202204
}
203205
addLocalArg(resolve, lo)
204206
addNamingArgs(resolve, no)
205207
addFileArg(resolve, fo)
206208
addTagsArg(resolve, ta)
209+
addDebugArg(resolve, do)
207210
topLevel.AddCommand(resolve)
208211

209212
publish := &cobra.Command{
@@ -237,12 +240,13 @@ func addKubeCommands(topLevel *cobra.Command) {
237240
ko publish --local github.com/foo/bar/cmd/baz github.com/foo/bar/cmd/blah`,
238241
Args: cobra.MinimumNArgs(1),
239242
Run: func(_ *cobra.Command, args []string) {
240-
publishImages(args, no, lo, ta)
243+
publishImages(args, no, lo, ta, do)
241244
},
242245
}
243246
addLocalArg(publish, lo)
244247
addNamingArgs(publish, no)
245248
addTagsArg(publish, ta)
249+
addDebugArg(publish, do)
246250
topLevel.AddCommand(publish)
247251

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

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

297302
topLevel.AddCommand(run)
298303
}

cmd/ko/debug.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2019 Google LLC All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package main
16+
17+
import (
18+
"github.com/spf13/cobra"
19+
)
20+
21+
// DebugOptions holds options to improve debugging containers.
22+
type DebugOptions struct {
23+
DisableOptimizations bool
24+
}
25+
26+
func addDebugArg(cmd *cobra.Command, do *DebugOptions) {
27+
cmd.Flags().BoolVar(&do.DisableOptimizations, "disable-optimizations", do.DisableOptimizations,
28+
"Disable optimizations when building Go code. Useful when you want to interactively debug the created container.")
29+
}

cmd/ko/publish.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ func qualifyLocalImport(importpath, gopathsrc, pwd string) (string, error) {
3838
return filepath.Join(strings.TrimPrefix(pwd, gopathsrc+string(filepath.Separator)), importpath), nil
3939
}
4040

41-
func publishImages(importpaths []string, no *NameOptions, lo *LocalOptions, ta *TagsOptions) map[string]name.Reference {
42-
opt, err := gobuildOptions()
41+
func publishImages(importpaths []string, no *NameOptions, lo *LocalOptions, ta *TagsOptions, do *DebugOptions) map[string]name.Reference {
42+
opt, err := gobuildOptions(do)
4343
if err != nil {
4444
log.Fatalf("error setting up builder options: %v", err)
4545
}

cmd/ko/resolve.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
"github.com/mattmoor/dep-notify/pkg/graph"
3131
)
3232

33-
func gobuildOptions() ([]build.Option, error) {
33+
func gobuildOptions(do *DebugOptions) ([]build.Option, error) {
3434
creationTime, err := getCreationTime()
3535
if err != nil {
3636
return nil, err
@@ -41,11 +41,14 @@ func gobuildOptions() ([]build.Option, error) {
4141
if creationTime != nil {
4242
opts = append(opts, build.WithCreationTime(*creationTime))
4343
}
44+
if do.DisableOptimizations {
45+
opts = append(opts, build.WithDisabledOptimizations())
46+
}
4447
return opts, nil
4548
}
4649

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

107-
func resolveFilesToWriter(fo *FilenameOptions, no *NameOptions, lo *LocalOptions, ta *TagsOptions, out io.WriteCloser) {
110+
func resolveFilesToWriter(fo *FilenameOptions, no *NameOptions, lo *LocalOptions, ta *TagsOptions, do *DebugOptions, out io.WriteCloser) {
108111
defer out.Close()
109-
builder, err := makeBuilder()
112+
builder, err := makeBuilder(do)
110113
if err != nil {
111114
log.Fatalf("error creating builder: %v", err)
112115
}

pkg/build/gobuild.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ const (
3838

3939
// GetBase takes an importpath and returns a base v1.Image.
4040
type GetBase func(string) (v1.Image, error)
41-
type builder func(string) (string, error)
41+
type builder func(string, bool) (string, error)
4242

4343
type gobuild struct {
4444
getBase GetBase
4545
creationTime v1.Time
4646
build builder
47+
disableOptimizations bool
4748
}
4849

4950
// Option is a functional option for NewGo.
@@ -53,6 +54,7 @@ type gobuildOpener struct {
5354
getBase GetBase
5455
creationTime v1.Time
5556
build builder
57+
disableOptimizations bool
5658
}
5759

5860
func (gbo *gobuildOpener) Open() (Interface, error) {
@@ -63,6 +65,7 @@ func (gbo *gobuildOpener) Open() (Interface, error) {
6365
getBase: gbo.getBase,
6466
creationTime: gbo.creationTime,
6567
build: gbo.build,
68+
disableOptimizations: gbo.disableOptimizations,
6669
}, nil
6770
}
6871

@@ -94,14 +97,22 @@ func (*gobuild) IsSupportedReference(s string) bool {
9497
return p.IsCommand()
9598
}
9699

97-
func build(ip string) (string, error) {
100+
func build(ip string, disableOptimizations bool) (string, error) {
98101
tmpDir, err := ioutil.TempDir("", "ko")
99102
if err != nil {
100103
return "", err
101104
}
102105
file := filepath.Join(tmpDir, "out")
103106

104-
cmd := exec.Command("go", "build", "-o", file, ip)
107+
args := make([]string, 0, 6)
108+
args = append(args, "build")
109+
if disableOptimizations {
110+
// Disable optimizations (-N) and inlining (-l).
111+
args = append(args, "-gcflags", "all=-N -l")
112+
}
113+
args = append(args, "-o", file)
114+
args = append(args, ip)
115+
cmd := exec.Command("go", args...)
105116

106117
// Last one wins
107118
// TODO(mattmoor): GOARCH=amd64
@@ -273,7 +284,7 @@ func tarKoData(importpath string) (*bytes.Buffer, error) {
273284
// Build implements build.Interface
274285
func (gb *gobuild) Build(s string) (v1.Image, error) {
275286
// Do the build into a temporary file.
276-
file, err := gb.build(s)
287+
file, err := gb.build(s, gb.disableOptimizations)
277288
if err != nil {
278289
return nil, err
279290
}

pkg/build/gobuild_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestGoBuildIsSupportedRef(t *testing.T) {
6262
}
6363

6464
// A helper method we use to substitute for the default "build" method.
65-
func writeTempFile(s string) (string, error) {
65+
func writeTempFile(s string, _ bool) (string, error) {
6666
tmpDir, err := ioutil.TempDir("", "ko")
6767
if err != nil {
6868
return "", err

pkg/build/options.go

+9
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ func WithCreationTime(t v1.Time) Option {
3636
}
3737
}
3838

39+
// WithDisabledOptimizations is a functional option for disabling optimizations
40+
// when compiling.
41+
func WithDisabledOptimizations() Option {
42+
return func(gbo *gobuildOpener) error {
43+
gbo.disableOptimizations = true
44+
return nil
45+
}
46+
}
47+
3948
// withBuilder is a functional option for overriding the way go binaries
4049
// are built. This is exposed for testing.
4150
func withBuilder(b builder) Option {

0 commit comments

Comments
 (0)