Skip to content

Commit 5bb4137

Browse files
authored
Support building with Go 1.17 (open-telemetry#66)
* Support building with Go 1.17 Fixes open-telemetry#65 Signed-off-by: Juraci Paixão Kröhling <[email protected]> * Update workflows to use Go 1.17 Signed-off-by: Juraci Paixão Kröhling <[email protected]> * Add gosec exceptions for exec.Command Signed-off-by: Juraci Paixão Kröhling <[email protected]>
1 parent 08d2c20 commit 5bb4137

File tree

8 files changed

+50
-35
lines changed

8 files changed

+50
-35
lines changed

.github/workflows/go.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- name: Set up Go
1616
uses: actions/setup-go@v2
1717
with:
18-
go-version: 1.16
18+
go-version: 1.17
1919

2020
- name: Check out code into the Go module directory
2121
uses: actions/checkout@v2

.github/workflows/integration-test.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- name: Set up Go
1616
uses: actions/setup-go@v2
1717
with:
18-
go-version: 1.16
18+
go-version: 1.17
1919

2020
- name: Check out code into the Go module directory
2121
uses: actions/checkout@v2

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
name: Set up Go
1919
uses: actions/setup-go@v2
2020
with:
21-
go-version: 1.16
21+
go-version: 1.17
2222
-
2323
name: Run GoReleaser
2424
uses: goreleaser/goreleaser-action@v2

cmd/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func Execute() error {
7575
cmd.Flags().BoolVar(&cfg.Distribution.IncludeCore, "include-core", true, "Whether the core components should be included in the distribution")
7676
cmd.Flags().StringVar(&cfg.Distribution.OtelColVersion, "otelcol-version", cfg.Distribution.OtelColVersion, "Which version of OpenTelemetry Collector to use as base")
7777
cmd.Flags().StringVar(&cfg.Distribution.OutputPath, "output-path", cfg.Distribution.OutputPath, "Where to write the resulting files")
78-
cmd.Flags().StringVar(&cfg.Distribution.Go, "go", "/usr/bin/go", "The Go binary to use during the compilation phase")
78+
cmd.Flags().StringVar(&cfg.Distribution.Go, "go", "", "The Go binary to use during the compilation phase. Default: go from the PATH")
7979
cmd.Flags().StringVar(&cfg.Distribution.Module, "module", "github.com/open-telemetry/opentelemetry-collector-builder", "The Go module for the new distribution")
8080

8181
// version of this binary

go.mod

+24-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
module github.com/open-telemetry/opentelemetry-collector-builder
1616

17-
go 1.16
17+
go 1.17
1818

1919
require (
2020
github.com/go-logr/logr v0.2.1
@@ -24,3 +24,26 @@ require (
2424
github.com/stretchr/testify v1.6.1
2525
go.uber.org/zap v1.10.0
2626
)
27+
28+
require (
29+
github.com/davecgh/go-spew v1.1.1 // indirect
30+
github.com/fsnotify/fsnotify v1.4.7 // indirect
31+
github.com/hashicorp/hcl v1.0.0 // indirect
32+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
33+
github.com/magiconair/properties v1.8.1 // indirect
34+
github.com/mitchellh/mapstructure v1.1.2 // indirect
35+
github.com/pelletier/go-toml v1.2.0 // indirect
36+
github.com/pmezard/go-difflib v1.0.0 // indirect
37+
github.com/spf13/afero v1.1.2 // indirect
38+
github.com/spf13/cast v1.3.0 // indirect
39+
github.com/spf13/jwalterweatherman v1.0.0 // indirect
40+
github.com/spf13/pflag v1.0.5 // indirect
41+
github.com/subosito/gotenv v1.2.0 // indirect
42+
go.uber.org/atomic v1.4.0 // indirect
43+
go.uber.org/multierr v1.1.0 // indirect
44+
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0 // indirect
45+
golang.org/x/text v0.3.2 // indirect
46+
gopkg.in/ini.v1 v1.51.0 // indirect
47+
gopkg.in/yaml.v2 v2.4.0 // indirect
48+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
49+
)

internal/builder/config.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020
"io/ioutil"
2121
"os"
22+
"os/exec"
2223
"strings"
2324

2425
"github.com/go-logr/logr"
@@ -89,7 +90,17 @@ func DefaultConfig() Config {
8990
}
9091

9192
// Validate checks whether the current configuration is valid
92-
func (c Config) Validate() error {
93+
func (c *Config) Validate() error {
94+
// #nosec G204
95+
if _, err := exec.Command(c.Distribution.Go, "env").CombinedOutput(); err != nil {
96+
path, err := exec.LookPath("go")
97+
if err != nil {
98+
return ErrGoNotFound
99+
}
100+
c.Distribution.Go = path
101+
}
102+
c.Logger.Info("Using go", "Go executable", c.Distribution.Go)
103+
93104
return nil
94105
}
95106

internal/builder/main.go

+9-28
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,9 @@ func Compile(cfg Config) error {
9595
return nil
9696
}
9797

98-
// first, we test to check if we have Go at all
99-
goBinary, err := getGoPath(cfg)
100-
if err != nil {
101-
return err
102-
}
103-
10498
cfg.Logger.Info("Compiling")
105-
cmd := exec.Command(goBinary, "build", "-ldflags=-s -w", "-trimpath", "-o", cfg.Distribution.ExeName)
99+
// #nosec G204
100+
cmd := exec.Command(cfg.Distribution.Go, "build", "-ldflags=-s -w", "-trimpath", "-o", cfg.Distribution.ExeName)
106101
cmd.Dir = cfg.Distribution.OutputPath
107102
if out, err := cmd.CombinedOutput(); err != nil {
108103
return fmt.Errorf("failed to compile the OpenTelemetry Collector distribution: %w. Output: %q", err, out)
@@ -114,20 +109,21 @@ func Compile(cfg Config) error {
114109

115110
// GetModules retrieves the go modules, updating go.mod and go.sum in the process
116111
func GetModules(cfg Config) error {
117-
// first, we test to check if we have Go at all
118-
goBinary, err := getGoPath(cfg)
119-
if err != nil {
120-
return err
112+
// #nosec G204
113+
cmd := exec.Command(cfg.Distribution.Go, "mod", "tidy")
114+
cmd.Dir = cfg.Distribution.OutputPath
115+
if out, err := cmd.CombinedOutput(); err != nil {
116+
return fmt.Errorf("failed to update go.mod: %w. Output: %q", err, out)
121117
}
122118

123119
cfg.Logger.Info("Getting go modules")
124-
125120
// basic retry if error from go mod command (in case of transient network error). This could be improved
126121
// retry 3 times with 5 second spacing interval
127122
retries := 3
128123
failReason := "unknown"
129124
for i := 1; i <= retries; i++ {
130-
cmd := exec.Command(goBinary, "mod", "download", "all")
125+
// #nosec G204
126+
cmd := exec.Command(cfg.Distribution.Go, "mod", "download", "all")
131127
cmd.Dir = cfg.Distribution.OutputPath
132128
if out, err := cmd.CombinedOutput(); err != nil {
133129
failReason = fmt.Sprintf("%s. Output: %q", err, out)
@@ -140,21 +136,6 @@ func GetModules(cfg Config) error {
140136
return fmt.Errorf("failed to download go modules: %s", failReason)
141137
}
142138

143-
// getGoPath checks if go is present and correct, and returns a useable go bin location
144-
func getGoPath(cfg Config) (string, error) {
145-
goBinary := cfg.Distribution.Go
146-
if _, err := exec.Command(goBinary, "env").CombinedOutput(); err != nil {
147-
path, err := exec.LookPath("go")
148-
if err != nil {
149-
return "", ErrGoNotFound
150-
}
151-
goBinary = path
152-
cfg.Logger.Info("Using go from PATH", "Go executable", path)
153-
}
154-
155-
return goBinary, nil
156-
}
157-
158139
func processAndWrite(cfg Config, tmpl string, outFile string, tmplParams interface{}) error {
159140
t, err := template.New("template").Parse(tmpl)
160141
if err != nil {

internal/scaffold/gomod.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package scaffold
1717
const Gomod = `
1818
module {{.Distribution.Module}}
1919
20-
go 1.16
20+
go 1.17
2121
2222
require (
2323
{{- range .Extensions}}

0 commit comments

Comments
 (0)