Skip to content

Commit

Permalink
Cosmos - parse go.mod go version (#85)
Browse files Browse the repository at this point in the history
* Cleanup refactor, rename confusing config properties and flags while retaining backwards compatibility

* rename to builder

* Update docs and chains.yaml to non-deprecated

* Improve platforms flag description

* Update stale references to git-ref flag

* Add logs for github release fetches

* Remove github basic auth

* Parse go.mod version and use for cosmos builds

* Migrate from ioutil to io

* Remove redundant log
  • Loading branch information
agouin committed Feb 1, 2023
1 parent 9406f6b commit 7ebae1e
Show file tree
Hide file tree
Showing 6 changed files with 1,408 additions and 24 deletions.
95 changes: 95 additions & 0 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package builder
import (
"context"
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
Expand All @@ -12,10 +13,25 @@ import (
"syscall"
"time"

"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/storage/memory"
"golang.org/x/mod/modfile"
"golang.org/x/mod/semver"

"github.com/strangelove-ventures/heighliner/docker"
"github.com/strangelove-ventures/heighliner/dockerfile"
)

const (
// golang official dockerhub images to use for cosmos builds
Go118Image = "1.18.10-alpine3.17"
Go119Image = "1.19.5-alpine3.17"

GoDefaultImage = Go119Image // default image for cosmos go builds if go.mod parse fails
)

type HeighlinerBuilder struct {
buildConfig HeighlinerDockerBuildConfig
queue []HeighlinerQueuedChainBuilds
Expand Down Expand Up @@ -133,6 +149,70 @@ func rawDockerfile(
}
}

// baseImageForGoVersion will determine the go version in go.mod and return the base image
func baseImageForGoVersion(
repoHost string,
organization string,
repoName string,
ref string,
buildDir string,
) (string, error) {

// single branch depth 1 clone to only fetch most recent state of files
cloneOpts := &git.CloneOptions{
URL: fmt.Sprintf("https://%s/%s/%s", repoHost, organization, repoName),
SingleBranch: true,
Depth: 1,
}
// Try as tag ref first
cloneOpts.ReferenceName = plumbing.NewTagReferenceName(ref)

// Clone into memory
fs := memfs.New()

_, err := git.Clone(memory.NewStorage(), fs, cloneOpts)
if err != nil {
// In error case, try as branch ref
cloneOpts.ReferenceName = plumbing.NewBranchReferenceName(ref)

_, err := git.Clone(memory.NewStorage(), fs, cloneOpts)
if err != nil {
return "", fmt.Errorf("failed to clone go.mod file to determine go version: %w", err)
}
}

goModPath := "go.mod"
if buildDir != "" {
goModPath = buildDir + "/" + goModPath
}

goModFile, err := fs.Open(goModPath)
if err != nil {
return "", fmt.Errorf("failed to open go.mod file: %w", err)
}

goModBz, err := io.ReadAll(goModFile)
if err != nil {
return "", fmt.Errorf("failed to read go.mod file: %w", err)
}

goMod, err := modfile.Parse("go.mod", goModBz, nil)
if err != nil {
return "", fmt.Errorf("failed to parse go.mod file: %w", err)
}

var baseImageVersion string
if semver.Compare("v"+goMod.Go.Version, "v1.19") >= 0 {
baseImageVersion = Go119Image
} else {
baseImageVersion = Go118Image
}

fmt.Printf("Go version: %s, using image: golang:%s\n", goMod.Go.Version, baseImageVersion)

return baseImageVersion, nil
}

// buildChainNodeDockerImage builds the requested chain node docker image
// based on the input configuration.
func (h *HeighlinerBuilder) buildChainNodeDockerImage(
Expand Down Expand Up @@ -228,8 +308,23 @@ func (h *HeighlinerBuilder) buildChainNodeDockerImage(
buildTimestamp = strconv.FormatInt(time.Now().Unix(), 10)
}

var baseVersion string
if dockerfile == DockerfileTypeCosmos {
baseVersion = GoDefaultImage // default, and fallback if go.mod parse fails

baseVer, err := baseImageForGoVersion(repoHost, chainConfig.Build.GithubOrganization, chainConfig.Build.GithubRepo, chainConfig.Ref, chainConfig.Build.BuildDir)

// In error case, fallback to default image
if err != nil {
fmt.Println(err)
} else {
baseVersion = baseVer
}
}

buildArgs := map[string]string{
"VERSION": chainConfig.Ref,
"BASE_VERSION": baseVersion,
"NAME": chainConfig.Build.Name,
"BASE_IMAGE": chainConfig.Build.BaseImage,
"REPO_HOST": repoHost,
Expand Down
3 changes: 2 additions & 1 deletion dockerfile/cosmos/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM --platform=$BUILDPLATFORM golang:1.19-alpine3.16 AS build-env
ARG BASE_VERSION
FROM --platform=$BUILDPLATFORM golang:${BASE_VERSION} AS build-env

RUN apk add --update --no-cache curl make git libc-dev bash gcc linux-headers eudev-dev

Expand Down
3 changes: 2 additions & 1 deletion dockerfile/cosmos/local.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM golang:1.19-alpine3.16 AS build-env
ARG BASE_VERSION
FROM golang:${BASE_VERSION} AS build-env

RUN apk add --update --no-cache curl make git libc-dev bash gcc linux-headers eudev-dev ncurses-dev

Expand Down
3 changes: 2 additions & 1 deletion dockerfile/cosmos/native.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM golang:1.19-alpine3.16 AS build-env
ARG BASE_VERSION
FROM golang:${BASE_VERSION} AS build-env

RUN apk add --update --no-cache curl make git libc-dev bash gcc linux-headers eudev-dev ncurses-dev

Expand Down
31 changes: 22 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ go 1.17

require (
github.com/docker/docker v20.10.12+incompatible
github.com/go-git/go-billy/v5 v5.4.0
github.com/go-git/go-git/v5 v5.5.2
github.com/moby/buildkit v0.10.1
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.3.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/mod v0.7.0
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20221026131551-cf6655e29de4 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/cloudflare/circl v1.1.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/containerd/containerd v1.6.3-0.20220401172941-5ff8fce1fcc6 // indirect
github.com/containerd/continuity v0.2.3-0.20220330195504-d132b287edc8 // indirect
Expand All @@ -23,6 +29,8 @@ require (
github.com/docker/docker-credential-helpers v0.6.4 // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-logr/logr v1.2.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gofrs/flock v0.7.3 // indirect
Expand All @@ -32,35 +40,40 @@ require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.15.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/moby/sys/signal v0.6.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 // indirect
github.com/opencontainers/runc v1.1.1 // indirect
github.com/pjbgf/sha1cd v0.2.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/skeema/knownhosts v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tonistiigi/fsutil v0.0.0-20220115021204-b19f7f9cb274 // indirect
github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea // indirect
github.com/tonistiigi/vt100 v0.0.0-20210615222946-8066bb97264f // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.29.0 // indirect
go.opentelemetry.io/otel v1.4.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.4.1 // indirect
go.opentelemetry.io/otel/sdk v1.4.1 // indirect
go.opentelemetry.io/otel/trace v1.4.1 // indirect
go.opentelemetry.io/proto/otlp v0.12.0 // indirect
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e // indirect
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f // indirect
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/crypto v0.3.0 // indirect
golang.org/x/net v0.2.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
google.golang.org/grpc v1.45.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gotest.tools/v3 v3.1.0 // indirect
)

Expand Down
Loading

0 comments on commit 7ebae1e

Please sign in to comment.