Skip to content

Commit

Permalink
simplify configuration of build service
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Chacin <[email protected]>
  • Loading branch information
pablochacin committed Jun 17, 2024
1 parent 37e4a91 commit 8f87592
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 44 deletions.
74 changes: 37 additions & 37 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
package cmd

import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"strings"

"github.com/grafana/k6build"
"github.com/grafana/k6catalog"
"github.com/grafana/k6foundry"
"github.com/spf13/cobra"
)

Expand All @@ -20,53 +17,53 @@ var ErrTargetPlatformUndefined = errors.New("target platform is required") //nol
const (
long = `
k6 build service returns artifacts that satisfies certain dependencies
`

example = `
# build k6 v0.50.0 with latest version of k6/x/kubernetes
k6build -k v0.50.0 -d k6/x/kubernetes
# build k6 v0.51.0 with k6/x/kubernetes v0.8.0 and k6/x/output-kafka v0.7.0
k6foundry build -k v0.51.0 \
-d k6/x/kubernetes:v0.8.0 \
-d k6/x/output-kafka:v0.7.0
# build latest version of k6 with a version of k6/x/kubernetes greater than v0.8.0
k6build -k v0.50.0 -d 'k6/x/kubernetes:>v0.8.0'
# build k6 v0.50.0 with latest version of k6/x/kubernetes using a custom catalog
k6build -k v0.50.0 -d k6/x/kubernetes \
-c /path/to/catalog.json
# build k6 v0.50.0 using a custom GOPROXY
k6build -k v0.50.0 -e GOPROXY=http://localhost:80
`
)

// New creates new cobra command for resolve command.
func New() *cobra.Command {
var (
deps []string
k6Constrains string
platform string
registry string
verbose bool
deps []string
k6 string
platform string
config k6build.LocalBuildServiceConfig
)

cmd := &cobra.Command{
Use: "k6build",
Short: "k6 build service",
Long: long,
Use: "k6build",
Short: "k6 build service",
Long: long,
Example: example,
// prevent the usage help to printed to stderr when an error is reported by a subcommand
SilenceUsage: true,
// this is needed to prevent cobra to print errors reported by subcommands in the stderr
SilenceErrors: true,
RunE: func(cmd *cobra.Command, _ []string) error {
catalog, err := k6catalog.NewCatalogFromJSON(registry)
srv, err := k6build.NewLocalBuildService(cmd.Context(), config)
if err != nil {
return fmt.Errorf("loading catalog %w", err)
return fmt.Errorf("configuring the build service %w", err)
}

opts := k6foundry.NativeBuilderOpts{}
// This is required to pass environment variables like the github access credential
opts.CopyGoEnv = true

if verbose {
opts.Verbose = true
opts.LogLevel = "DEBUG"
}
builder, err := k6foundry.NewNativeBuilder(context.TODO(), opts)
if err != nil {
return fmt.Errorf("setting up builder %w", err)
}

cache, err := k6build.NewTempFileCache()
if err != nil {
return fmt.Errorf("creating build cache %w", err)
}

srv := k6build.NewBuildService(catalog, builder, cache)

buildDeps := []k6build.Dependency{}
for _, d := range deps {
name, constrains, _ := strings.Cut(d, ":")
Expand All @@ -76,7 +73,7 @@ func New() *cobra.Command {
buildDeps = append(buildDeps, k6build.Dependency{Name: name, Constraints: constrains})
}

artifact, err := srv.Build(cmd.Context(), platform, k6Constrains, buildDeps)
artifact, err := srv.Build(cmd.Context(), platform, k6, buildDeps)
if err != nil {
return fmt.Errorf("building %w", err)
}
Expand All @@ -93,11 +90,14 @@ func New() *cobra.Command {
}

cmd.Flags().StringArrayVarP(&deps, "dependency", "d", nil, "list of dependencies in form package:constrains")
cmd.Flags().StringVarP(&k6Constrains, "k6-constrains", "k", "*", "k6 version constrains")
cmd.Flags().StringVarP(&k6, "k6", "k", "*", "k6 version constrains")
cmd.Flags().StringVarP(&platform, "platform", "p", "", "target platform (default GOOS/GOARCH)")
_ = cmd.MarkFlagRequired("platform")
cmd.Flags().StringVarP(&registry, "catalog", "c", "catalog.json", "dependencies catalog")
cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "print build process output")
cmd.Flags().StringVarP(&config.Catalog, "catalog", "c", "catalog.json", "dependencies catalog")
cmd.Flags().StringVarP(&config.CacheDir, "cache-dir", "f", "/tmp/buildservice", "cache dir")
cmd.Flags().BoolVarP(&config.Verbose, "verbose", "v", false, "print build process output")
cmd.Flags().BoolVarP(&config.CopyGoEnv, "copy-go-env", "g", true, "copy go environment")
cmd.Flags().StringToStringVarP(&config.BuildEnv, "env", "e", nil, "build environment variables")

return cmd
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22.2

require (
github.com/grafana/k6catalog v0.1.0
github.com/grafana/k6foundry v0.1.2
github.com/grafana/k6foundry v0.1.3
github.com/spf13/cobra v1.8.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/grafana/k6catalog v0.1.0 h1:jLmbmB3EUJ+zyQG3hWy6dWbtMjvTkvJNx1d4LX8it
github.com/grafana/k6catalog v0.1.0/go.mod h1:8R9eXAh2nb69+drkj0rZ4aemso0jcwCbPP6Q3E5LqCw=
github.com/grafana/k6foundry v0.1.2 h1:/vitrnaeVk86nh2mmaH+C2LUAvjMrT4I44xp5+cPQM0=
github.com/grafana/k6foundry v0.1.2/go.mod h1:b6n4InFgXl+3yPobmlyJfcJmLozU9CI9IIUuq8YqEiM=
github.com/grafana/k6foundry v0.1.3 h1:05sRM5ik+MsZr1tdJR/rTjI8trLpWFbG+vzmnpmsC5g=
github.com/grafana/k6foundry v0.1.3/go.mod h1:b6n4InFgXl+3yPobmlyJfcJmLozU9CI9IIUuq8YqEiM=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
46 changes: 46 additions & 0 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,52 @@ func NewBuildService(
}
}

// LocalBuildServiceConfig defines the configuration for a Local build service
type LocalBuildServiceConfig struct {
// Set build environment variables
// Can be used for setting (or overriding, if CopyGoEnv is true) go environment variables
BuildEnv map[string]string
// path to catalog's json file
Catalog string
// path to cache dir
CacheDir string
// Copy go environment. BuildEnv can override the variables copied from go environment.
CopyGoEnv bool
// ser verbose build mode
Verbose bool
}

// NewLocalBuildService creates a local build service using the given configuration
func NewLocalBuildService(ctx context.Context, config LocalBuildServiceConfig) (BuildService, error) {
catalog, err := k6catalog.NewCatalogFromJSON(config.Catalog)
if err != nil {
return nil, fmt.Errorf("creating catalog %w", err)
}

builderOpts := k6foundry.NativeBuilderOpts{
Verbose: config.Verbose,
GoOpts: k6foundry.GoOpts{
Env: config.BuildEnv,
CopyGoEnv: config.CopyGoEnv,
},
}
builder, err := k6foundry.NewNativeBuilder(ctx, builderOpts)
if err != nil {
return nil, fmt.Errorf("creating builder %w", err)
}

cache, err := NewFileCache(config.CacheDir)
if err != nil {
return nil, fmt.Errorf("creating cache %w", err)
}

return &buildsrv{
catalog: catalog,
builder: builder,
cache: cache,
}, nil
}

// DefaultLocalBuildService creates a Local Build service with default configuration
func DefaultLocalBuildService() (BuildService, error) {
catalog, err := k6catalog.DefaultCatalog()
Expand Down
14 changes: 8 additions & 6 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ func setupBuildService(cacheDir string) (BuildService, error) {

opts := k6foundry.NativeBuilderOpts{
GoOpts: k6foundry.GoOpts{
CopyGoEnv: true,
GoProxy: goproxySrv.URL,
GoNoProxy: "none",
GoPrivate: "go.k6.io",
GoNoSumDB: "go.k6.io",
EphemeralCache: true,
CopyGoEnv: true,
Env: map[string]string{
"GOPROXY": goproxySrv.URL,
"GONOPROXY": "none",
"GOPRIVATE": "go.k6.io",
"GONOSUMDB": "go.k6.io",
},
TmpCache: true,
},
}

Expand Down

0 comments on commit 8f87592

Please sign in to comment.