diff --git a/Makefile b/Makefile index 06004ffd19..e8482cec71 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,18 @@ VTAG := $(shell [ -z $(VTAG) ] && echo $(ETAG) || echo $(VTAG)) VERS ?= $(shell git describe --tags --match 'v*' 2>/dev/null) KVER ?= $(shell git describe --tags --match 'knative-*' 2>/dev/null) -LDFLAGS := -X knative.dev/func/pkg/version.Vers=$(VERS) -X knative.dev/func/pkg/version.Kver=$(KVER) -X knative.dev/func/pkg/version.Hash=$(HASH) +# -X pkg.Var= clears the Go initializer, so only stamp values that are +# actually set (e.g. VERS/KVER are empty outside of a tagged checkout). +LDFLAGS := +ifneq ($(VERS),) +LDFLAGS += -X knative.dev/func/pkg/version.Vers=$(VERS) +endif +ifneq ($(KVER),) +LDFLAGS += -X knative.dev/func/pkg/version.Kver=$(KVER) +endif +ifneq ($(HASH),) +LDFLAGS += -X knative.dev/func/pkg/version.Hash=$(HASH) +endif # Overrides the func-utils image; the default is version.FuncUtilsImage. FUNC_UTILS_IMG ?= diff --git a/cmd/root.go b/cmd/root.go index ec1e2e680f..b1ec2a05cf 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -20,10 +20,12 @@ import ( "knative.dev/func/pkg/config" fn "knative.dev/func/pkg/functions" "knative.dev/func/pkg/k8s" + "knative.dev/func/pkg/version" ) -// DefaultVersion when building source directly (bypassing the Makefile) -const DefaultVersion = "v0.0.0+source" +// DefaultVersion when building source directly (bypassing the Makefile). +// Delegates to version.Default so the fallback is defined in one place. +const DefaultVersion = version.Default // DefaultNamespace is the global static default namespace, and is equivalent // to the Kubernetes default namespace. diff --git a/cmd/version.go b/cmd/version.go index da82b99b58..cfb4e65933 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -76,13 +76,6 @@ func runVersion(cmd *cobra.Command, v Version) error { // Set verbose flag v.Verbose = verbose - // Initialize the default value to the zero semver with a descriptive - // metadata tag indicating this must have been built from source if - // undefined: - if v.Vers == "" { - v.Vers = DefaultVersion - } - // Kver and Hash are already set from build via ldflags, // injected into the Version struct at startup (see pkg/app/app.go). // Populate image fields from k8s package constants diff --git a/pkg/mcp/mcp.go b/pkg/mcp/mcp.go index a4c89f9d3f..85d8798f5a 100644 --- a/pkg/mcp/mcp.go +++ b/pkg/mcp/mcp.go @@ -9,12 +9,12 @@ import ( "sync/atomic" "github.com/modelcontextprotocol/go-sdk/mcp" + "knative.dev/func/pkg/version" ) const ( - name = "func" - title = "func" - version = "0.1.0" + name = "func" + title = "func" // EnvMCPWrite is the environment variable that enables write operations // (deploy, delete, and pushing built images) on the MCP server. Set to @@ -30,6 +30,7 @@ const ( type Server struct { OnInit func(context.Context) // Invoked when the server is initialized prefix string // Command prefix ("func" or "kn func") + version string // Computed once at construction; used by healthcheck readonly atomic.Bool // disables mutation tools when true executor executor transport mcp.Transport // Transport to use (defaults to StdioTransport) @@ -103,6 +104,7 @@ func WithReadonly(readonly bool) Option { func New(options ...Option) *Server { s := &Server{ prefix: "func", + version: version.Vers, transport: &mcp.StdioTransport{}, OnInit: func(_ context.Context) {}, } @@ -117,7 +119,7 @@ func New(options ...Option) *Server { &mcp.Implementation{ Name: name, Title: title, - Version: version}, + Version: s.version}, &mcp.ServerOptions{ Instructions: instructions(s.readonly.Load()), HasPrompts: true, diff --git a/pkg/mcp/tools_healthcheck.go b/pkg/mcp/tools_healthcheck.go index af16fe25c5..3e2b22147f 100644 --- a/pkg/mcp/tools_healthcheck.go +++ b/pkg/mcp/tools_healthcheck.go @@ -21,7 +21,7 @@ func (s *Server) healthcheckHandler(ctx context.Context, r *mcp.CallToolRequest, output = HealthcheckOutput{ Status: "ok", Message: "The MCP server is running!", - Version: version, + Version: s.version, } return } diff --git a/pkg/mcp/tools_healthcheck_test.go b/pkg/mcp/tools_healthcheck_test.go index b0c7d117db..1c313ea471 100644 --- a/pkg/mcp/tools_healthcheck_test.go +++ b/pkg/mcp/tools_healthcheck_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/modelcontextprotocol/go-sdk/mcp" + "knative.dev/func/pkg/version" ) // TestTool_Healthcheck verifies the healthcheck tool returns expected output @@ -64,7 +65,7 @@ func TestTool_Healthcheck(t *testing.T) { t.Errorf("expected message to contain 'running', got %q", output.Message) } - if output.Version == "" { - t.Error("expected non-empty version") + if output.Version != version.Vers { + t.Errorf("healthcheck version = %q, want build stamp %q", output.Version, version.Vers) } } diff --git a/pkg/version/version.go b/pkg/version/version.go index 46a9aa0ee6..68944841ff 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -1,6 +1,14 @@ package version -var Vers, Kver, Hash string +// Default is the fallback version used when no build-time version was +// injected (e.g. source builds that bypass the Makefile). +const Default = "v0.0.0+source" + +var ( + Vers = Default // overwritten by ldflags when make actually has a describe + Kver string + Hash string +) // FuncUtilsImage is the func-utils image that func runs in the cluster: the // minor in development on main, X.Y on release-X.Y. Builds can override it