Skip to content

Commit

Permalink
Make go-version configurable
Browse files Browse the repository at this point in the history
This also affects the version used within viceroy-based builds.
  • Loading branch information
zerok committed Aug 23, 2023
1 parent 49898f6 commit 1bf879f
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 20 deletions.
8 changes: 8 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/urfave/cli/v2"
)

const defaultGoVersion = "1.21.0"

var FlagPackage = &cli.StringSliceFlag{
Name: "package",
Usage: "Path to a grafana.tar.gz package used as input. This command will process each package provided separately and produce an equal number of applicable outputs",
Expand Down Expand Up @@ -114,6 +116,12 @@ var GrafanaFlags = []cli.Flag{
Name: "go-tags",
Usage: "Sets the go `-tags` flag when compiling the backend",
},
&cli.StringFlag{
Name: "go-version",
Usage: "The version of Go to be used for building the Grafana backend",
Required: false,
Value: defaultGoVersion,
},
&cli.StringFlag{
Name: "yarn-cache",
Usage: "If there is a yarn cache directory, then mount that when running 'yarn install' instead of creating a cache directory",
Expand Down
3 changes: 3 additions & 0 deletions containers/base_golang.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package containers

import (
"log"

"dagger.io/dagger"
)

// GolangContainer returns a dagger container with everything set up that is needed to build Grafana's Go backend or run the Golang tests.
func GolangContainer(d *dagger.Client, platform dagger.Platform, base string) *dagger.Container {
log.Printf("Retrieving Go container based on `%s`", base)
opts := dagger.ContainerOpts{
Platform: platform,
}
Expand Down
6 changes: 3 additions & 3 deletions containers/base_viceroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
)

const (
GoURL = "https://go.dev/dl/go1.20.2.linux-%s.tar.gz"
GoURL = "https://go.dev/dl/go%s.linux-%s.tar.gz"
ViceroyImage = "rfratto/viceroy:v0.3.0"
)

// ViceroyContainer returns a dagger container with everything set up that is needed to build Grafana's Go backend
// with CGO using Viceroy, which makes setting up the C compiler toolchain easier.
func ViceroyContainer(d *dagger.Client, distro executil.Distribution, base string) *dagger.Container {
func ViceroyContainer(d *dagger.Client, distro executil.Distribution, base string, goVersion string) *dagger.Container {
opts := dagger.ContainerOpts{
Platform: "linux/amd64",
}
Expand All @@ -25,7 +25,7 @@ func ViceroyContainer(d *dagger.Client, distro executil.Distribution, base strin
// * amd64
// * armv6l
// * arm64
goURL := fmt.Sprintf(GoURL, "amd64")
goURL := fmt.Sprintf(GoURL, goVersion, "amd64")
container := d.Container(opts).From(base)

// Install Go manually, and install make, git, and curl from the package manager.
Expand Down
17 changes: 9 additions & 8 deletions containers/build_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/grafana/grafana-build/versions"
)

const (
GoImageAlpine = "golang:1.21-alpine"
)
func GetGoImageAlpine(version string) string {
return fmt.Sprintf("golang:%s-alpine", version)
}

var GrafanaCommands = []string{
"grafana",
Expand All @@ -38,18 +38,19 @@ type CompileBackendOpts struct {
BuildInfo *BuildInfo
Env map[string]string
GoTags []string
GoVersion string

CombinedExecutables bool
}

func goBuildImage(distro executil.Distribution, opts *executil.GoBuildOpts) string {
func goBuildImage(distro executil.Distribution, opts *executil.GoBuildOpts, goVersion string) string {
os, _ := executil.OSAndArch(distro)

if os != "linux" {
return ViceroyImage
}

return GoImageAlpine
return GetGoImageAlpine(goVersion)
}

// This function will return the platform equivalent to the distribution.
Expand Down Expand Up @@ -92,15 +93,15 @@ func CompileBackendBuilder(d *dagger.Client, opts *CompileBackendOpts) *dagger.C
var (
goBuildOpts = goBuildOptsFunc(distro, buildinfo)
env = executil.GoBuildEnv(goBuildOpts)
image = goBuildImage(distro, goBuildOpts)
image = goBuildImage(distro, goBuildOpts, opts.GoVersion)
)

log.Println("Creating Grafana backend build container for", distro, "on platform", platform)
log.Println("Creating Grafana backend build container for", distro, "on platform", platform, "using", opts.GoVersion)

builder := GolangContainer(d, platform, image)

if image == ViceroyImage {
builder = ViceroyContainer(d, distro, ViceroyImage)
builder = ViceroyContainer(d, distro, ViceroyImage, opts.GoVersion)
}

genGoArgs := []string{"make", "gen-go"}
Expand Down
4 changes: 2 additions & 2 deletions containers/dependencies_golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ func WithCachedGoDependencies(container *dagger.Container, dir *dagger.Directory
})
}

func DownloadGolangDependencies(d *dagger.Client, platform dagger.Platform, gomod, gosum *dagger.File) *dagger.Directory {
container := GolangContainer(d, platform, GoImageAlpine).
func DownloadGolangDependencies(d *dagger.Client, platform dagger.Platform, gomod, gosum *dagger.File, goVersion string) *dagger.Directory {
container := GolangContainer(d, platform, GetGoImageAlpine(goVersion)).
WithWorkdir("/src").
WithMountedFile("/src/go.mod", gomod).
WithMountedFile("/src/go.sum", gosum).
Expand Down
3 changes: 3 additions & 0 deletions containers/opts_grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type GrafanaOpts struct {
GitHubToken string
Env map[string]string
GoTags []string
GoVersion string

// Version will be set by the '--version' flag if provided, and returned in the 'Version' function.
// If not set, then the version function will attempt to retrieve the version from Grafana's package.json or some other method.
Expand All @@ -52,6 +53,7 @@ func GrafanaOptsFromFlags(ctx context.Context, c cliutil.CLIContext) (*GrafanaOp
buildID = c.String("build-id")
gitHubToken = c.String("github-token")
goTags = c.StringSlice("go-tags")
goVersion = c.String("go-version")
)

if buildID == "" {
Expand Down Expand Up @@ -98,6 +100,7 @@ func GrafanaOptsFromFlags(ctx context.Context, c cliutil.CLIContext) (*GrafanaOp
GitHubToken: gitHubToken,
Env: env,
GoTags: goTags,
GoVersion: goVersion,
YarnCacheHostDir: c.String("yarn-cache"),
}, nil
}
Expand Down
4 changes: 2 additions & 2 deletions containers/test_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
)

func BackendTestShort(d *dagger.Client, platform dagger.Platform, dir *dagger.Directory) *dagger.Container {
return GrafanaContainer(d, platform, GoImageAlpine, dir).
return GrafanaContainer(d, platform, GetGoImageAlpine("1.21.0"), dir).
WithExec([]string{"go", "test", "-tags", "requires_buildifer", "-short", "-covermode", "atomic", "-timeout", "5m", "./pkg/..."})
}

func BackendTestIntegration(d *dagger.Client, platform dagger.Platform, dir *dagger.Directory) *dagger.Container {
return GrafanaContainer(d, platform, GoImageAlpine, dir).
return GrafanaContainer(d, platform, GetGoImageAlpine("1.21.0"), dir).
WithExec([]string{"go", "test", "-run", "Integration", "-covermode", "atomic", "-timeout", "5m", "./pkg/..."})
}
13 changes: 8 additions & 5 deletions pipelines/backend_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ type GrafanaCompileOpts struct {
Version string

// Env is an optional map of environment variables (keyed by literal variable name to value) to set in the build container(s).
Env map[string]string
GoTags []string
Env map[string]string
GoTags []string
GoVersion string

// Edition is just used for logging / visualization purposes
Edition string
Expand All @@ -38,7 +39,7 @@ func (o *GrafanaCompileOpts) BuildInfo(ctx context.Context, d *dagger.Client) (*
return containers.GetBuildInfo(ctx, d, o.Source, o.Version)
}

func (o *GrafanaCompileOpts) BackendCompileOpts(ctx context.Context, d *dagger.Client) (*containers.CompileBackendOpts, error) {
func (o *GrafanaCompileOpts) BackendCompileOpts(ctx context.Context, d *dagger.Client, gops *GrafanaCompileOpts) (*containers.CompileBackendOpts, error) {
buildinfo, err := o.BuildInfo(ctx, d)
if err != nil {
return nil, err
Expand All @@ -49,6 +50,7 @@ func (o *GrafanaCompileOpts) BackendCompileOpts(ctx context.Context, d *dagger.C
Distribution: o.Distribution,
Platform: o.Platform,
BuildInfo: buildinfo,
GoVersion: gops.GoVersion,
}, nil
}

Expand All @@ -66,11 +68,11 @@ func GrafanaBackendBuildDirectory(ctx context.Context, d *dagger.Client, opts *G

var (
cacheKey = "go-mod-" + version
cacheDir = containers.DownloadGolangDependencies(d, platform, src.File("go.mod"), src.File("go.sum"))
cacheDir = containers.DownloadGolangDependencies(d, platform, src.File("go.mod"), src.File("go.sum"), opts.GoVersion)
cache = d.CacheVolume(cacheKey)
)

backendCompileOpts, err := opts.BackendCompileOpts(ctx, d)
backendCompileOpts, err := opts.BackendCompileOpts(ctx, d, opts)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -139,6 +141,7 @@ func GrafanaBackendBuild(ctx context.Context, d *dagger.Client, src *dagger.Dire
Version: args.GrafanaOpts.Version,
Env: args.GrafanaOpts.Env,
GoTags: args.GrafanaOpts.GoTags,
GoVersion: args.GrafanaOpts.GoVersion,
YarnCacheHostDir: args.GrafanaOpts.YarnCacheHostDir,
}
container, err := GrafanaBackendBuildDirectory(ctx, d, opts)
Expand Down

0 comments on commit 1bf879f

Please sign in to comment.