Skip to content

Commit

Permalink
Support building with Go 1.17 (#66)
Browse files Browse the repository at this point in the history
* Support building with Go 1.17
Fixes #65

Signed-off-by: Juraci Paixão Kröhling <juraci@kroehling.de>

* Update workflows to use Go 1.17

Signed-off-by: Juraci Paixão Kröhling <juraci@kroehling.de>

* Add gosec exceptions for exec.Command

Signed-off-by: Juraci Paixão Kröhling <juraci@kroehling.de>
  • Loading branch information
jpkrohling authored Sep 1, 2021
1 parent 08d2c20 commit 5bb4137
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.17

- name: Check out code into the Go module directory
uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/integration-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.17

- name: Check out code into the Go module directory
uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.17
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func Execute() error {
cmd.Flags().BoolVar(&cfg.Distribution.IncludeCore, "include-core", true, "Whether the core components should be included in the distribution")
cmd.Flags().StringVar(&cfg.Distribution.OtelColVersion, "otelcol-version", cfg.Distribution.OtelColVersion, "Which version of OpenTelemetry Collector to use as base")
cmd.Flags().StringVar(&cfg.Distribution.OutputPath, "output-path", cfg.Distribution.OutputPath, "Where to write the resulting files")
cmd.Flags().StringVar(&cfg.Distribution.Go, "go", "/usr/bin/go", "The Go binary to use during the compilation phase")
cmd.Flags().StringVar(&cfg.Distribution.Go, "go", "", "The Go binary to use during the compilation phase. Default: go from the PATH")
cmd.Flags().StringVar(&cfg.Distribution.Module, "module", "github.com/open-telemetry/opentelemetry-collector-builder", "The Go module for the new distribution")

// version of this binary
Expand Down
25 changes: 24 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

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

go 1.16
go 1.17

require (
github.com/go-logr/logr v0.2.1
Expand All @@ -24,3 +24,26 @@ require (
github.com/stretchr/testify v1.6.1
go.uber.org/zap v1.10.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/magiconair/properties v1.8.1 // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.1.2 // indirect
github.com/spf13/cast v1.3.0 // indirect
github.com/spf13/jwalterweatherman v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/multierr v1.1.0 // indirect
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0 // indirect
golang.org/x/text v0.3.2 // indirect
gopkg.in/ini.v1 v1.51.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
13 changes: 12 additions & 1 deletion internal/builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -89,7 +90,17 @@ func DefaultConfig() Config {
}

// Validate checks whether the current configuration is valid
func (c Config) Validate() error {
func (c *Config) Validate() error {
// #nosec G204
if _, err := exec.Command(c.Distribution.Go, "env").CombinedOutput(); err != nil {
path, err := exec.LookPath("go")
if err != nil {
return ErrGoNotFound
}
c.Distribution.Go = path
}
c.Logger.Info("Using go", "Go executable", c.Distribution.Go)

return nil
}

Expand Down
37 changes: 9 additions & 28 deletions internal/builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,9 @@ func Compile(cfg Config) error {
return nil
}

// first, we test to check if we have Go at all
goBinary, err := getGoPath(cfg)
if err != nil {
return err
}

cfg.Logger.Info("Compiling")
cmd := exec.Command(goBinary, "build", "-ldflags=-s -w", "-trimpath", "-o", cfg.Distribution.ExeName)
// #nosec G204
cmd := exec.Command(cfg.Distribution.Go, "build", "-ldflags=-s -w", "-trimpath", "-o", cfg.Distribution.ExeName)
cmd.Dir = cfg.Distribution.OutputPath
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("failed to compile the OpenTelemetry Collector distribution: %w. Output: %q", err, out)
Expand All @@ -114,20 +109,21 @@ func Compile(cfg Config) error {

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

cfg.Logger.Info("Getting go modules")

// basic retry if error from go mod command (in case of transient network error). This could be improved
// retry 3 times with 5 second spacing interval
retries := 3
failReason := "unknown"
for i := 1; i <= retries; i++ {
cmd := exec.Command(goBinary, "mod", "download", "all")
// #nosec G204
cmd := exec.Command(cfg.Distribution.Go, "mod", "download", "all")
cmd.Dir = cfg.Distribution.OutputPath
if out, err := cmd.CombinedOutput(); err != nil {
failReason = fmt.Sprintf("%s. Output: %q", err, out)
Expand All @@ -140,21 +136,6 @@ func GetModules(cfg Config) error {
return fmt.Errorf("failed to download go modules: %s", failReason)
}

// getGoPath checks if go is present and correct, and returns a useable go bin location
func getGoPath(cfg Config) (string, error) {
goBinary := cfg.Distribution.Go
if _, err := exec.Command(goBinary, "env").CombinedOutput(); err != nil {
path, err := exec.LookPath("go")
if err != nil {
return "", ErrGoNotFound
}
goBinary = path
cfg.Logger.Info("Using go from PATH", "Go executable", path)
}

return goBinary, nil
}

func processAndWrite(cfg Config, tmpl string, outFile string, tmplParams interface{}) error {
t, err := template.New("template").Parse(tmpl)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/scaffold/gomod.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package scaffold
const Gomod = `
module {{.Distribution.Module}}
go 1.16
go 1.17
require (
{{- range .Extensions}}
Expand Down

0 comments on commit 5bb4137

Please sign in to comment.