Skip to content

Commit 6a3ef46

Browse files
authored
fix: refer correct version of schema in func.yaml (knative#2924)
Signed-off-by: Matej Vašek <[email protected]>
1 parent 630064c commit 6a3ef46

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ VTAG := $(shell [ -z $(VTAG) ] && echo $(ETAG) || echo $(VTAG))
2828
VERS ?= $(shell git describe --tags --match 'v*')
2929
KVER ?= $(shell git describe --tags --match 'knative-*')
3030

31-
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)
31+
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)
3232

3333
FUNC_UTILS_IMG ?= ghcr.io/knative/func-utils:v2
3434
LDFLAGS += -X knative.dev/func/pkg/k8s.SocatImage=$(FUNC_UTILS_IMG)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/Microsoft/go-winio v0.6.2
1212
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2
1313
github.com/alecthomas/jsonschema v0.0.0-20220216202328-9eeeec9d044b
14+
github.com/blang/semver/v4 v4.0.0
1415
github.com/buildpacks/pack v0.36.4
1516
github.com/chainguard-dev/git-urls v1.0.2
1617
github.com/cloudevents/sdk-go/v2 v2.15.2
@@ -111,7 +112,6 @@ require (
111112
github.com/aws/smithy-go v1.22.2 // indirect
112113
github.com/awslabs/amazon-ecr-credential-helper/ecr-login v0.0.0-20250205235911-d2398ba46815 // indirect
113114
github.com/beorn7/perks v1.0.1 // indirect
114-
github.com/blang/semver/v4 v4.0.0 // indirect
115115
github.com/blendle/zapdriver v1.3.1 // indirect
116116
github.com/buildpacks/imgutil v0.0.0-20240605145725-186f89b2d168 // indirect
117117
github.com/buildpacks/libcnb v1.30.3 // indirect

pkg/app/app.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import (
1010
"syscall"
1111

1212
"github.com/AlecAivazis/survey/v2/terminal"
13+
1314
"knative.dev/func/cmd"
1415
"knative.dev/func/pkg/docker"
16+
"knative.dev/func/pkg/version"
1517
)
1618

17-
var vers, kver, hash string
18-
1919
func Main() {
2020
ctx, cancel := context.WithCancel(context.Background())
2121
defer cancel()
@@ -32,9 +32,9 @@ func Main() {
3232
cfg := cmd.RootCommandConfig{
3333
Name: "func",
3434
Version: cmd.Version{
35-
Vers: vers,
36-
Kver: kver,
37-
Hash: hash,
35+
Vers: version.Vers,
36+
Kver: version.Kver,
37+
Hash: version.Hash,
3838
}}
3939

4040
if err := cmd.NewRootCmd(cfg).ExecuteContext(ctx); err != nil {

pkg/functions/function.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ import (
1010
"strings"
1111
"time"
1212

13+
"github.com/blang/semver/v4"
1314
"github.com/google/go-containerregistry/pkg/name"
1415
"gopkg.in/yaml.v2"
16+
1517
fnlabels "knative.dev/func/pkg/k8s/labels"
18+
"knative.dev/func/pkg/version"
1619
"knative.dev/pkg/ptr"
1720
)
1821

@@ -444,12 +447,12 @@ func (f Function) Write() (err error) {
444447
}
445448
defer rwFile.Close()
446449

447-
tagVersion := f.SpecVersion
450+
schemaURI := funcYamlSchemaURI()
448451

449452
// Write schema header
450-
schemaHeader := fmt.Sprintf(`# $schema: https://raw.githubusercontent.com/knative/func/refs/tags/v%s/schema/func_yaml-schema.json
451-
# yaml-language-server: $schema=https://raw.githubusercontent.com/knative/func/refs/tags/v%s/schema/func_yaml-schema.json
452-
`, tagVersion, tagVersion)
453+
schemaHeader := fmt.Sprintf(`# $schema: %s
454+
# yaml-language-server: $schema=%s
455+
`, schemaURI, schemaURI)
453456

454457
if _, err = rwFile.WriteString(schemaHeader); err != nil {
455458
return err
@@ -479,6 +482,25 @@ func (f Function) Write() (err error) {
479482
return
480483
}
481484

485+
func funcYamlSchemaURI() string {
486+
var (
487+
ref = "main"
488+
kver semver.Version
489+
err error
490+
)
491+
kver, err = semver.Parse(strings.TrimPrefix(version.Kver, "knative-v"))
492+
if err == nil {
493+
ref = fmt.Sprintf("release-%d.%d", kver.Major, kver.Minor)
494+
} else {
495+
if version.Hash != "" {
496+
ref = version.Hash
497+
}
498+
}
499+
500+
return fmt.Sprintf("https://raw.githubusercontent.com/knative/func"+
501+
"/%s/schema/func_yaml-schema.json", ref)
502+
}
503+
482504
type stampOptions struct{ journal bool }
483505
type stampOption func(o *stampOptions)
484506

pkg/version/version.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package version
2+
3+
var Vers, Kver, Hash string

0 commit comments

Comments
 (0)