From 5bb41375483cd28c2f4d4bdad37c6644db432dd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraci=20Paix=C3=A3o=20Kr=C3=B6hling?= Date: Wed, 1 Sep 2021 20:51:45 +0200 Subject: [PATCH] Support building with Go 1.17 (#66) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Support building with Go 1.17 Fixes #65 Signed-off-by: Juraci Paixão Kröhling * Update workflows to use Go 1.17 Signed-off-by: Juraci Paixão Kröhling * Add gosec exceptions for exec.Command Signed-off-by: Juraci Paixão Kröhling --- .github/workflows/go.yaml | 2 +- .github/workflows/integration-test.yaml | 2 +- .github/workflows/release.yml | 2 +- cmd/root.go | 2 +- go.mod | 25 ++++++++++++++++- internal/builder/config.go | 13 ++++++++- internal/builder/main.go | 37 ++++++------------------- internal/scaffold/gomod.go | 2 +- 8 files changed, 50 insertions(+), 35 deletions(-) diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml index 05844862b99..f7893d6fb4f 100644 --- a/.github/workflows/go.yaml +++ b/.github/workflows/go.yaml @@ -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 diff --git a/.github/workflows/integration-test.yaml b/.github/workflows/integration-test.yaml index f0116cd3a6c..7fbe81bcbac 100644 --- a/.github/workflows/integration-test.yaml +++ b/.github/workflows/integration-test.yaml @@ -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 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a54664e95e9..8cb55ebb47a 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 diff --git a/cmd/root.go b/cmd/root.go index 626db0e702a..229853ff798 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 diff --git a/go.mod b/go.mod index ac7af80d741..a6374f62af0 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 +) diff --git a/internal/builder/config.go b/internal/builder/config.go index 06da30cc6bc..f8af3a42d26 100644 --- a/internal/builder/config.go +++ b/internal/builder/config.go @@ -19,6 +19,7 @@ import ( "fmt" "io/ioutil" "os" + "os/exec" "strings" "github.com/go-logr/logr" @@ -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 } diff --git a/internal/builder/main.go b/internal/builder/main.go index 142b6273bba..2b8ab7773c1 100644 --- a/internal/builder/main.go +++ b/internal/builder/main.go @@ -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) @@ -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) @@ -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 { diff --git a/internal/scaffold/gomod.go b/internal/scaffold/gomod.go index 2f49578647e..275f5c92a15 100644 --- a/internal/scaffold/gomod.go +++ b/internal/scaffold/gomod.go @@ -17,7 +17,7 @@ package scaffold const Gomod = ` module {{.Distribution.Module}} -go 1.16 +go 1.17 require ( {{- range .Extensions}}