fix version handling and injection

Signed-off-by: Matthias Bertschy <matthias.bertschy@gmail.com>
This commit is contained in:
Matthias Bertschy
2026-02-04 10:42:12 +01:00
parent 0841d1d483
commit b97f50ffb5
8 changed files with 55 additions and 50 deletions
+2 -2
View File
@@ -36,8 +36,8 @@ builds:
- amd64
- arm64
ldflags:
- -s -w
- -X "github.com/kubescape/kubescape/v3/core/cautils.Client={{.Env.CLIENT}}"
- "{{.Ldflags}}"
- -X "github.com/kubescape/backend/pkg/versioncheck.Client={{.Env.CLIENT}}"
hooks:
post:
- cmd: >
+7 -7
View File
@@ -44,12 +44,12 @@ var ksExamples = fmt.Sprintf(`
%[1]s config view
`, cautils.ExecName())
func NewDefaultKubescapeCommand(ctx context.Context) *cobra.Command {
func NewDefaultKubescapeCommand(ctx context.Context, ksVersion, ksCommit, ksDate string) *cobra.Command {
ks := core.NewKubescape(ctx)
return getRootCmd(ks)
return getRootCmd(ks, ksVersion, ksCommit, ksDate)
}
func getRootCmd(ks meta.IKubescape) *cobra.Command {
func getRootCmd(ks meta.IKubescape, ksVersion, ksCommit, ksDate string) *cobra.Command {
rootCmd := &cobra.Command{
Use: "kubescape",
@@ -93,7 +93,7 @@ func getRootCmd(ks meta.IKubescape) *cobra.Command {
rootCmd.AddCommand(download.GetDownloadCmd(ks))
rootCmd.AddCommand(list.GetListCmd(ks))
rootCmd.AddCommand(completion.GetCompletionCmd())
rootCmd.AddCommand(version.GetVersionCmd(ks))
rootCmd.AddCommand(version.GetVersionCmd(ks, ksVersion, ksCommit, ksDate))
rootCmd.AddCommand(config.GetConfigCmd(ks))
rootCmd.AddCommand(update.GetUpdateCmd(ks))
rootCmd.AddCommand(fix.GetFixCmd(ks))
@@ -116,7 +116,7 @@ func getRootCmd(ks meta.IKubescape) *cobra.Command {
return rootCmd
}
func Execute(ctx context.Context) error {
ks := NewDefaultKubescapeCommand(ctx)
func Execute(ctx context.Context, ksVersion, ksCommit, ksDate string) error {
ks := NewDefaultKubescapeCommand(ctx, ksVersion, ksCommit, ksDate)
return ks.Execute()
}
}
+3 -3
View File
@@ -9,16 +9,16 @@ import (
func TestNewDefaultKubescapeCommand(t *testing.T) {
t.Run("NewDefaultKubescapeCommand", func(t *testing.T) {
cmd := NewDefaultKubescapeCommand(context.Background())
cmd := NewDefaultKubescapeCommand(context.Background(), "", "", "")
assert.NotNil(t, cmd)
})
}
func TestExecute(t *testing.T) {
t.Run("Execute", func(t *testing.T) {
err := Execute(context.Background())
err := Execute(context.Background(), "", "", "")
if err != nil {
assert.EqualErrorf(t, err, "unknown command \"^\\\\QTestExecute\\\\E$\" for \"kubescape\"", err.Error())
}
})
}
}
+12 -4
View File
@@ -9,21 +9,29 @@ import (
"github.com/spf13/cobra"
)
func GetVersionCmd(ks meta.IKubescape) *cobra.Command {
func GetVersionCmd(ks meta.IKubescape, version, commit, date string) *cobra.Command {
versionCmd := &cobra.Command{
Use: "version",
Short: "Get current version",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
v := versioncheck.NewIVersionCheckHandler(ks.Context())
_ = v.CheckLatestVersion(ks.Context(), versioncheck.NewVersionCheckRequest("", versioncheck.BuildNumber, "", "", "version", nil))
_ = v.CheckLatestVersion(ks.Context(), versioncheck.NewVersionCheckRequest("", version, "", "", "version", nil))
_, _ = fmt.Fprintf(cmd.OutOrStdout(),
"Your current version is: %s\n",
versioncheck.BuildNumber,
version,
)
_, _ = fmt.Fprintf(cmd.OutOrStdout(),
"Build commit: %s\n",
commit,
)
_, _ = fmt.Fprintf(cmd.OutOrStdout(),
"Build date: %s\n",
date,
)
return nil
},
}
return versionCmd
}
}
+4 -4
View File
@@ -21,12 +21,12 @@ func TestGetVersionCmd(t *testing.T) {
{
name: "Undefined Build Number",
buildNumber: "unknown",
want: "Your current version is: unknown\n",
want: "Your current version is: unknown\nBuild commit: \nBuild date: \n",
},
{
name: "Defined Build Number: v3.0.1",
buildNumber: "v3.0.1",
want: "Your current version is: v3.0.1\n",
want: "Your current version is: v3.0.1\nBuild commit: \nBuild date: \n",
},
}
for _, tt := range tests {
@@ -34,7 +34,7 @@ func TestGetVersionCmd(t *testing.T) {
versioncheck.BuildNumber = tt.buildNumber
ks := core.NewKubescape(context.TODO())
if cmd := GetVersionCmd(ks); cmd != nil {
if cmd := GetVersionCmd(ks, tt.buildNumber, "", ""); cmd != nil {
buf := bytes.NewBufferString("")
cmd.SetOut(buf)
cmd.Execute()
@@ -46,4 +46,4 @@ func TestGetVersionCmd(t *testing.T) {
}
})
}
}
}
-27
View File
@@ -1,27 +0,0 @@
package cautils
import (
"os"
"runtime/debug"
"github.com/kubescape/backend/pkg/versioncheck"
)
var Client string
func init() {
// Try to get version from build info (Go 1.24+ automatically populates this from VCS tags)
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "" && info.Main.Version != "(devel)" {
versioncheck.BuildNumber = info.Main.Version
}
// Fallback to RELEASE environment variable
if versioncheck.BuildNumber == "" {
versioncheck.BuildNumber = os.Getenv("RELEASE")
}
// Client is typically set via ldflags: -X "github.com/kubescape/kubescape/v3/core/cautils.Client=..."
if Client != "" {
versioncheck.Client = Client
}
}
+14 -1
View File
@@ -22,12 +22,25 @@ import (
"github.com/kubescape/kubescape/v3/pkg/ksinit"
)
// GoReleaser will fill these at build time
var (
version = "dev"
commit = "none"
date = "unknown"
)
const (
defaultNamespace = "kubescape"
)
func main() {
ctx := context.Background()
versioncheck.BuildNumber = version
logger.L().Info("Starting Kubescape server",
helpers.String("version", version),
helpers.String("commit", commit),
helpers.String("date", date))
cfg, err := config.LoadConfig("/etc/config")
if err != nil {
@@ -41,7 +54,7 @@ func main() {
// to enable otel, set OTEL_COLLECTOR_SVC=otel-collector:4317
if otelHost, present := os.LookupEnv("OTEL_COLLECTOR_SVC"); present {
ctx = logger.InitOtel("kubescape",
os.Getenv(versioncheck.BuildNumber),
version,
config.GetAccount(),
clusterName,
url.URL{Host: otelHost})
+13 -2
View File
@@ -6,11 +6,22 @@ import (
"os/signal"
"syscall"
"github.com/kubescape/backend/pkg/versioncheck"
"github.com/kubescape/go-logger"
"github.com/kubescape/kubescape/v3/cmd"
)
// GoReleaser will fill these at build time
var (
version = "dev"
commit = "none"
date = "unknown"
)
func main() {
// Set the global build number for version checking
versioncheck.BuildNumber = version
// Capture interrupt signal
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
@@ -24,8 +35,8 @@ func main() {
stop()
}()
if err := cmd.Execute(ctx); err != nil {
if err := cmd.Execute(ctx, version, commit, date); err != nil {
stop()
logger.L().Fatal(err.Error())
}
}
}