From b04eac668af82e416c1b7980200ab9104c2bbcea Mon Sep 17 00:00:00 2001 From: Pablo Chacin Date: Tue, 25 Jul 2023 18:09:39 +0200 Subject: [PATCH 1/2] Use runtime build info for version Signed-off-by: Pablo Chacin --- pkg/disruptors/controller.go | 4 ++-- pkg/internal/consts/const.go | 18 --------------- pkg/internal/version/version.go | 39 +++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 20 deletions(-) delete mode 100644 pkg/internal/consts/const.go create mode 100644 pkg/internal/version/version.go diff --git a/pkg/disruptors/controller.go b/pkg/disruptors/controller.go index f0066f1a..e76b94b9 100644 --- a/pkg/disruptors/controller.go +++ b/pkg/disruptors/controller.go @@ -6,7 +6,7 @@ import ( "sync" "time" - "github.com/grafana/xk6-disruptor/pkg/internal/consts" + "github.com/grafana/xk6-disruptor/pkg/internal/version" "github.com/grafana/xk6-disruptor/pkg/kubernetes/helpers" @@ -44,7 +44,7 @@ func (c *agentController) InjectDisruptorAgent(ctx context.Context) error { agentContainer := corev1.EphemeralContainer{ EphemeralContainerCommon: corev1.EphemeralContainerCommon{ Name: "xk6-agent", - Image: consts.AgentImage(), + Image: version.AgentImage(), ImagePullPolicy: corev1.PullIfNotPresent, SecurityContext: &corev1.SecurityContext{ Capabilities: &corev1.Capabilities{ diff --git a/pkg/internal/consts/const.go b/pkg/internal/consts/const.go deleted file mode 100644 index 0e8b7f1f..00000000 --- a/pkg/internal/consts/const.go +++ /dev/null @@ -1,18 +0,0 @@ -// Package consts provide constants used by other packages to ensure consistency. -// Some of these constants are defined as variables to allow the value to be set at build time -package consts - -// Version contains the current semantic version of k6. -// The value is set when building the binary -var Version = "" //nolint:gochecknoglobals - -// AgentImage returns the name of the agent image that corresponds to -// this version of the extension. -func AgentImage() string { - tag := "latest" - if Version != "" { - tag = Version - } - - return "ghcr.io/grafana/xk6-disruptor-agent:" + tag -} diff --git a/pkg/internal/version/version.go b/pkg/internal/version/version.go new file mode 100644 index 00000000..48f4c446 --- /dev/null +++ b/pkg/internal/version/version.go @@ -0,0 +1,39 @@ +// Package version provide information about the build version +package version + +import ( + "runtime/debug" +) + +const xk6DisruptorPath = "github.com/grafana/xk6-disruptor" + +// DisruptorVersion returns the version of the currently executed disruptor +func DisruptorVersion() string { + if bi, ok := debug.ReadBuildInfo(); ok { + for _, d := range bi.Deps { + if d.Path == xk6DisruptorPath { + if d.Replace != nil { + return d.Replace.Version + } + return d.Version + } + } + } + + return "" +} + +// AgentImage returns the name of the agent image that corresponds to +// this version of the extension. +func AgentImage() string { + tag := "latest" + + // if a specific version of the disruptor was built, use it for agent's tag + // (go test sets version to "") + dv := DisruptorVersion() + if dv != "" && dv != "(devel)" { + tag = dv + } + + return "ghcr.io/grafana/xk6-disruptor-agent:" + tag +} From 9e1211b5a9f6307e10996cd20d7dbc79eeb8799d Mon Sep 17 00:00:00 2001 From: Pablo Chacin Date: Tue, 25 Jul 2023 18:10:54 +0200 Subject: [PATCH 2/2] Allow specification of build version Signed-off-by: Pablo Chacin --- build-package.sh | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/build-package.sh b/build-package.sh index 631e7086..694ef1df 100755 --- a/build-package.sh +++ b/build-package.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/bash ARCH=$(go env GOARCH) BUILD="build" @@ -7,7 +7,7 @@ DIST="dist" PKG="" NAME="xk6-disruptor" OS=$(go env GOOS) -VERSION="latest" +VERSION="" function usage() { cat << EOF @@ -33,7 +33,6 @@ options: -v, --version: package version in semver formatf -y, --binary: name of the binary (default is name-os-arch) - EOF } @@ -59,6 +58,13 @@ function build() { binary="$NAME-$os-$arch" fi + # set disruptor version to use for build + mod=$(go list -m) + replace="." + if [[ ! -z $version ]]; then + replace=${mod}@${version} + fi + #start sub shell to create its own environment ( if [[ $os == "linux" ]]; then # disable cross-compiling for linux @@ -67,8 +73,8 @@ function build() { export GOARCH=$arch export GOOS=$os - export XK6_BUILD_FLAGS='-ldflags "-w -s -X github.com/grafana/xk6-disruptor/pkg/internal/consts.Version='${version}'"' - xk6 build --with $(go list -m)=. --output $BUILD/$binary + export XK6_BUILD_FLAGS='-ldflags "-w -s' + xk6 build --with $mod=${replace} --output $BUILD/$binary ) }