mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-07-17 17:52:12 +00:00
Merge pull request #5463 from chansuke/feat/show-version
Fix version tag management
This commit is contained in:
4
Makefile
4
Makefile
@@ -75,7 +75,9 @@ $(MYGOBIN)/pluginator:
|
|||||||
# Build from local source.
|
# Build from local source.
|
||||||
$(MYGOBIN)/kustomize: build-kustomize-api
|
$(MYGOBIN)/kustomize: build-kustomize-api
|
||||||
cd kustomize; \
|
cd kustomize; \
|
||||||
go install -ldflags "-X sigs.k8s.io/kustomize/api/provenance.buildDate=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')" \
|
go install -ldflags \
|
||||||
|
"-X sigs.k8s.io/kustomize/api/provenance.buildDate=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ') \
|
||||||
|
-X sigs.k8s.io/kustomize/api/provenance.version=$(shell git describe --tags --always --dirty)" \
|
||||||
.
|
.
|
||||||
|
|
||||||
kustomize: $(MYGOBIN)/kustomize
|
kustomize: $(MYGOBIN)/kustomize
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/blang/semver/v4 v4.0.0 // indirect
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/go-openapi/jsonpointer v0.19.6 // indirect
|
github.com/go-openapi/jsonpointer v0.19.6 // indirect
|
||||||
github.com/go-openapi/jsonreference v0.20.2 // indirect
|
github.com/go-openapi/jsonreference v0.20.2 // indirect
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
|
||||||
|
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
|
||||||
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
|
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
|
||||||
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
|
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
|
||||||
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
|
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/blang/semver/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
// These variables are set at build time using ldflags.
|
// These variables are set at build time using ldflags.
|
||||||
@@ -62,9 +64,34 @@ func GetProvenance() Provenance {
|
|||||||
p.GitCommit = setting.Value
|
p.GitCommit = setting.Value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, dep := range info.Deps {
|
||||||
|
if dep != nil && dep.Path == "sigs.k8s.io/kustomize/kustomize/v5" {
|
||||||
|
p.Version = GetMostRecentTag(*dep)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetMostRecentTag(m debug.Module) string {
|
||||||
|
for m.Replace != nil {
|
||||||
|
m = *m.Replace
|
||||||
|
}
|
||||||
|
|
||||||
|
split := strings.Split(m.Version, "-")
|
||||||
|
sv, err := semver.Parse(strings.TrimPrefix(split[0], "v"))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(split) > 1 && sv.Patch > 0 {
|
||||||
|
sv.Patch -= 1
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("v%s", sv.FinalizeVersion())
|
||||||
|
}
|
||||||
|
|
||||||
// Short returns the shortened provenance stamp.
|
// Short returns the shortened provenance stamp.
|
||||||
func (v Provenance) Short() string {
|
func (v Provenance) Short() string {
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ package provenance_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"runtime/debug"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -45,3 +46,52 @@ func TestProvenance_Semver(t *testing.T) {
|
|||||||
p.Version = "kustomize/v4.11.12"
|
p.Version = "kustomize/v4.11.12"
|
||||||
assert.Equal(t, "v4.11.12", p.Semver())
|
assert.Equal(t, "v4.11.12", p.Semver())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mockModule(version string) debug.Module {
|
||||||
|
return debug.Module{
|
||||||
|
Path: "sigs.k8s.io/kustomize/kustomize/v5",
|
||||||
|
Version: version,
|
||||||
|
Replace: nil,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetMostRecentTag(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
module debug.Module
|
||||||
|
expectedTag string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Standard version",
|
||||||
|
module: mockModule("v1.2.3"),
|
||||||
|
expectedTag: "v1.2.3",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Pseudo-version with patch increment",
|
||||||
|
module: mockModule("v0.0.0-20210101010101-abcdefabcdef"),
|
||||||
|
expectedTag: "v0.0.0",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Invalid semver string",
|
||||||
|
module: mockModule("invalid-version"),
|
||||||
|
expectedTag: "unknown",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Valid semver with patch increment and pre-release info",
|
||||||
|
module: mockModule("v1.2.3-0.20210101010101-abcdefabcdef"),
|
||||||
|
expectedTag: "v1.2.2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Valid semver no patch increment",
|
||||||
|
module: mockModule("v1.2.0"),
|
||||||
|
expectedTag: "v1.2.0",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
tag := provenance.GetMostRecentTag(tt.module)
|
||||||
|
assert.Equal(t, tt.expectedTag, tag)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user