diff --git a/Makefile b/Makefile index bf822f03b..50e5a8d60 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ VTAG := $(shell [ -z $(VTAG) ] && echo $(ETAG) || echo $(VTAG)) VERS ?= $(shell git describe --tags --match 'v*') KVER ?= $(shell git describe --tags --match 'knative-*') -LDFLAGS := -X knative.dev/func/pkg/app.vers=$(VERS) -X knative.dev/func/pkg/app.kver=$(KVER) -X knative.dev/func/pkg/app.hash=$(HASH) +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) FUNC_UTILS_IMG ?= ghcr.io/knative/func-utils:v2 LDFLAGS += -X knative.dev/func/pkg/k8s.SocatImage=$(FUNC_UTILS_IMG) diff --git a/go.mod b/go.mod index 2501f76f3..7662bb26a 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/Microsoft/go-winio v0.6.2 github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 github.com/alecthomas/jsonschema v0.0.0-20220216202328-9eeeec9d044b + github.com/blang/semver/v4 v4.0.0 github.com/buildpacks/pack v0.36.4 github.com/chainguard-dev/git-urls v1.0.2 github.com/cloudevents/sdk-go/v2 v2.15.2 @@ -111,7 +112,6 @@ require ( github.com/aws/smithy-go v1.22.2 // indirect github.com/awslabs/amazon-ecr-credential-helper/ecr-login v0.0.0-20250205235911-d2398ba46815 // indirect github.com/beorn7/perks v1.0.1 // indirect - github.com/blang/semver/v4 v4.0.0 // indirect github.com/blendle/zapdriver v1.3.1 // indirect github.com/buildpacks/imgutil v0.0.0-20240605145725-186f89b2d168 // indirect github.com/buildpacks/libcnb v1.30.3 // indirect diff --git a/pkg/app/app.go b/pkg/app/app.go index d0eec3195..eae55e63a 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -10,12 +10,12 @@ import ( "syscall" "github.com/AlecAivazis/survey/v2/terminal" + "knative.dev/func/cmd" "knative.dev/func/pkg/docker" + "knative.dev/func/pkg/version" ) -var vers, kver, hash string - func Main() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -32,9 +32,9 @@ func Main() { cfg := cmd.RootCommandConfig{ Name: "func", Version: cmd.Version{ - Vers: vers, - Kver: kver, - Hash: hash, + Vers: version.Vers, + Kver: version.Kver, + Hash: version.Hash, }} if err := cmd.NewRootCmd(cfg).ExecuteContext(ctx); err != nil { diff --git a/pkg/functions/function.go b/pkg/functions/function.go index a3cf6dc72..0104f8ce4 100644 --- a/pkg/functions/function.go +++ b/pkg/functions/function.go @@ -10,9 +10,12 @@ import ( "strings" "time" + "github.com/blang/semver/v4" "github.com/google/go-containerregistry/pkg/name" "gopkg.in/yaml.v2" + fnlabels "knative.dev/func/pkg/k8s/labels" + "knative.dev/func/pkg/version" "knative.dev/pkg/ptr" ) @@ -444,12 +447,12 @@ func (f Function) Write() (err error) { } defer rwFile.Close() - tagVersion := f.SpecVersion + schemaURI := funcYamlSchemaURI() // Write schema header - schemaHeader := fmt.Sprintf(`# $schema: https://raw.githubusercontent.com/knative/func/refs/tags/v%s/schema/func_yaml-schema.json -# yaml-language-server: $schema=https://raw.githubusercontent.com/knative/func/refs/tags/v%s/schema/func_yaml-schema.json -`, tagVersion, tagVersion) + schemaHeader := fmt.Sprintf(`# $schema: %s +# yaml-language-server: $schema=%s +`, schemaURI, schemaURI) if _, err = rwFile.WriteString(schemaHeader); err != nil { return err @@ -479,6 +482,25 @@ func (f Function) Write() (err error) { return } +func funcYamlSchemaURI() string { + var ( + ref = "main" + kver semver.Version + err error + ) + kver, err = semver.Parse(strings.TrimPrefix(version.Kver, "knative-v")) + if err == nil { + ref = fmt.Sprintf("release-%d.%d", kver.Major, kver.Minor) + } else { + if version.Hash != "" { + ref = version.Hash + } + } + + return fmt.Sprintf("https://raw.githubusercontent.com/knative/func"+ + "/%s/schema/func_yaml-schema.json", ref) +} + type stampOptions struct{ journal bool } type stampOption func(o *stampOptions) diff --git a/pkg/version/version.go b/pkg/version/version.go new file mode 100644 index 000000000..114c9658b --- /dev/null +++ b/pkg/version/version.go @@ -0,0 +1,3 @@ +package version + +var Vers, Kver, Hash string