feat: extend version information with more detailed version fields

- Add new version fields to version.Info struct:
  * EmulationMajor and EmulationMinor to track emulated version
  * MinCompatibilityMajor and MinCompatibilityMinor for compatibility tracking
- Update related code to populate and use these new fields
- Improve version information documentation and OpenAPI generation
- Modify version routes and documentation to reflect new version information structure
This commit is contained in:
yongruilin
2025-02-06 16:11:12 -08:00
parent cd451c6a36
commit a3094ccbe6
18 changed files with 410 additions and 55 deletions

View File

@@ -19474,6 +19474,14 @@
"compiler": {
"type": "string"
},
"emulationMajor": {
"description": "EmulationMajor is the major version of the emulation version",
"type": "string"
},
"emulationMinor": {
"description": "EmulationMinor is the minor version of the emulation version",
"type": "string"
},
"gitCommit": {
"type": "string"
},
@@ -19487,9 +19495,19 @@
"type": "string"
},
"major": {
"description": "Major is the major version of the binary version",
"type": "string"
},
"minCompatibilityMajor": {
"description": "MinCompatibilityMajor is the major version of the minimum compatibility version",
"type": "string"
},
"minCompatibilityMinor": {
"description": "MinCompatibilityMinor is the minor version of the minimum compatibility version",
"type": "string"
},
"minor": {
"description": "Minor is the minor version of the binary version",
"type": "string"
},
"platform": {
@@ -85995,8 +86013,8 @@
"consumes": [
"application/json"
],
"description": "get the code version",
"operationId": "getCodeVersion",
"description": "get the version information for this server",
"operationId": "getVersion",
"produces": [
"application/json"
],

View File

@@ -12,6 +12,14 @@
"default": "",
"type": "string"
},
"emulationMajor": {
"description": "EmulationMajor is the major version of the emulation version",
"type": "string"
},
"emulationMinor": {
"description": "EmulationMinor is the minor version of the emulation version",
"type": "string"
},
"gitCommit": {
"default": "",
"type": "string"
@@ -30,10 +38,20 @@
},
"major": {
"default": "",
"description": "Major is the major version of the binary version",
"type": "string"
},
"minCompatibilityMajor": {
"description": "MinCompatibilityMajor is the major version of the minimum compatibility version",
"type": "string"
},
"minCompatibilityMinor": {
"description": "MinCompatibilityMinor is the minor version of the minimum compatibility version",
"type": "string"
},
"minor": {
"default": "",
"description": "Minor is the minor version of the binary version",
"type": "string"
},
"platform": {
@@ -72,8 +90,8 @@
"paths": {
"/version/": {
"get": {
"description": "get the code version",
"operationId": "getCodeVersion",
"description": "get the version information for this server",
"operationId": "getVersion",
"responses": {
"200": {
"content": {

View File

@@ -243,8 +243,14 @@ func TestVersion(t *testing.T) {
}
expectedInfo := utilversion.Get()
kubeVersion := compatibility.DefaultKubeEffectiveVersionForTest().BinaryVersion()
emulationVersion := compatibility.DefaultKubeEffectiveVersionForTest().EmulationVersion()
minCompatibilityVersion := compatibility.DefaultKubeEffectiveVersionForTest().MinCompatibilityVersion()
expectedInfo.Major = fmt.Sprintf("%d", kubeVersion.Major())
expectedInfo.Minor = fmt.Sprintf("%d", kubeVersion.Minor())
expectedInfo.EmulationMajor = fmt.Sprintf("%d", emulationVersion.Major())
expectedInfo.EmulationMinor = fmt.Sprintf("%d", emulationVersion.Minor())
expectedInfo.MinCompatibilityMajor = fmt.Sprintf("%d", minCompatibilityVersion.Major())
expectedInfo.MinCompatibilityMinor = fmt.Sprintf("%d", minCompatibilityVersion.Minor())
if !reflect.DeepEqual(expectedInfo, info) {
t.Errorf("Expected %#v, Got %#v", expectedInfo, info)

View File

@@ -58457,16 +58457,46 @@ func schema_k8sio_apimachinery_pkg_version_Info(ref common.ReferenceCallback) co
Properties: map[string]spec.Schema{
"major": {
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
Description: "Major is the major version of the binary version",
Default: "",
Type: []string{"string"},
Format: "",
},
},
"minor": {
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
Description: "Minor is the minor version of the binary version",
Default: "",
Type: []string{"string"},
Format: "",
},
},
"emulationMajor": {
SchemaProps: spec.SchemaProps{
Description: "EmulationMajor is the major version of the emulation version",
Type: []string{"string"},
Format: "",
},
},
"emulationMinor": {
SchemaProps: spec.SchemaProps{
Description: "EmulationMinor is the minor version of the emulation version",
Type: []string{"string"},
Format: "",
},
},
"minCompatibilityMajor": {
SchemaProps: spec.SchemaProps{
Description: "MinCompatibilityMajor is the major version of the minimum compatibility version",
Type: []string{"string"},
Format: "",
},
},
"minCompatibilityMinor": {
SchemaProps: spec.SchemaProps{
Description: "MinCompatibilityMinor is the minor version of the minimum compatibility version",
Type: []string{"string"},
Format: "",
},
},
"gitVersion": {

View File

@@ -6599,16 +6599,46 @@ func schema_k8sio_apimachinery_pkg_version_Info(ref common.ReferenceCallback) co
Properties: map[string]spec.Schema{
"major": {
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
Description: "Major is the major version of the binary version",
Default: "",
Type: []string{"string"},
Format: "",
},
},
"minor": {
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
Description: "Minor is the minor version of the binary version",
Default: "",
Type: []string{"string"},
Format: "",
},
},
"emulationMajor": {
SchemaProps: spec.SchemaProps{
Description: "EmulationMajor is the major version of the emulation version",
Type: []string{"string"},
Format: "",
},
},
"emulationMinor": {
SchemaProps: spec.SchemaProps{
Description: "EmulationMinor is the minor version of the emulation version",
Type: []string{"string"},
Format: "",
},
},
"minCompatibilityMajor": {
SchemaProps: spec.SchemaProps{
Description: "MinCompatibilityMajor is the major version of the minimum compatibility version",
Type: []string{"string"},
Format: "",
},
},
"minCompatibilityMinor": {
SchemaProps: spec.SchemaProps{
Description: "MinCompatibilityMinor is the minor version of the minimum compatibility version",
Type: []string{"string"},
Format: "",
},
},
"gitVersion": {

View File

@@ -468,15 +468,15 @@ func (v *Version) Info() *apimachineryversion.Info {
return nil
}
// in case info is empty, or the major and minor in info is different from the actual major and minor
v.info.Major = itoa(v.Major())
v.info.Minor = itoa(v.Minor())
v.info.Major = Itoa(v.Major())
v.info.Minor = Itoa(v.Minor())
if v.info.GitVersion == "" {
v.info.GitVersion = v.String()
}
return &v.info
}
func itoa(i uint) string {
func Itoa(i uint) string {
if i == 0 {
return ""
}

View File

@@ -16,5 +16,5 @@ limitations under the License.
// +k8s:openapi-gen=true
// Package version supplies the type for version information collected at build time.
// Package version supplies the type for version information.
package version

View File

@@ -20,15 +20,25 @@ package version
// TODO: Add []string of api versions supported? It's still unclear
// how we'll want to distribute that information.
type Info struct {
Major string `json:"major"`
Minor string `json:"minor"`
GitVersion string `json:"gitVersion"`
GitCommit string `json:"gitCommit"`
GitTreeState string `json:"gitTreeState"`
BuildDate string `json:"buildDate"`
GoVersion string `json:"goVersion"`
Compiler string `json:"compiler"`
Platform string `json:"platform"`
// Major is the major version of the binary version
Major string `json:"major"`
// Minor is the minor version of the binary version
Minor string `json:"minor"`
// EmulationMajor is the major version of the emulation version
EmulationMajor string `json:"emulationMajor,omitempty"`
// EmulationMinor is the minor version of the emulation version
EmulationMinor string `json:"emulationMinor,omitempty"`
// MinCompatibilityMajor is the major version of the minimum compatibility version
MinCompatibilityMajor string `json:"minCompatibilityMajor,omitempty"`
// MinCompatibilityMinor is the minor version of the minimum compatibility version
MinCompatibilityMinor string `json:"minCompatibilityMinor,omitempty"`
GitVersion string `json:"gitVersion"`
GitCommit string `json:"gitCommit"`
GitTreeState string `json:"gitTreeState"`
BuildDate string `json:"buildDate"`
GoVersion string `json:"goVersion"`
Compiler string `json:"compiler"`
Platform string `json:"platform"`
}
// String returns info as a human-friendly version string.

View File

@@ -1108,7 +1108,7 @@ func installAPI(name string, s *GenericAPIServer, c *Config) {
}
}
routes.Version{Version: c.EffectiveVersion.BinaryVersion().Info()}.Install(s.Handler.GoRestfulContainer)
routes.Version{Version: c.EffectiveVersion.Info()}.Install(s.Handler.GoRestfulContainer)
if c.EnableDiscovery {
if c.FeatureGate.Enabled(genericfeatures.AggregatedDiscoveryEndpoint) {

View File

@@ -372,7 +372,7 @@ func TestServerRunWithSNI(t *testing.T) {
t.Fatalf("failed to connect with loopback client: %v", err)
}
if expected := &v; !reflect.DeepEqual(got, expected) {
t.Errorf("loopback client didn't get correct version info: expected=%v got=%v", expected, got)
t.Errorf("loopback client didn't get correct version info: expected=%v got=%v", *expected, *got)
}
select {
@@ -463,9 +463,13 @@ func certSignature(cert tls.Certificate) (string, error) {
func fakeVersion() version.Info {
return version.Info{
Major: "42",
Minor: "42",
GitVersion: "42.42",
Major: "42",
Minor: "42",
EmulationMajor: "42",
EmulationMinor: "42",
MinCompatibilityMajor: "42",
MinCompatibilityMinor: "41",
GitVersion: "42.42",
}
}

View File

@@ -39,11 +39,11 @@ func (v Version) Install(c *restful.Container) {
// Set up a service to return the git code version.
versionWS := new(restful.WebService)
versionWS.Path("/version")
versionWS.Doc("git code version from which this is built")
versionWS.Doc("get the version information for this server.")
versionWS.Route(
versionWS.GET("/").To(v.handleVersion).
Doc("get the code version").
Operation("getCodeVersion").
Doc("get the version information for this server").
Operation("getVersion").
Produces(restful.MIME_JSON).
Consumes(restful.MIME_JSON).
Writes(version.Info{}))

View File

@@ -2625,16 +2625,46 @@ func schema_k8sio_apimachinery_pkg_version_Info(ref common.ReferenceCallback) co
Properties: map[string]spec.Schema{
"major": {
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
Description: "Major is the major version of the binary version",
Default: "",
Type: []string{"string"},
Format: "",
},
},
"minor": {
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
Description: "Minor is the minor version of the binary version",
Default: "",
Type: []string{"string"},
Format: "",
},
},
"emulationMajor": {
SchemaProps: spec.SchemaProps{
Description: "EmulationMajor is the major version of the emulation version",
Type: []string{"string"},
Format: "",
},
},
"emulationMinor": {
SchemaProps: spec.SchemaProps{
Description: "EmulationMinor is the minor version of the emulation version",
Type: []string{"string"},
Format: "",
},
},
"minCompatibilityMajor": {
SchemaProps: spec.SchemaProps{
Description: "MinCompatibilityMajor is the major version of the minimum compatibility version",
Type: []string{"string"},
Format: "",
},
},
"minCompatibilityMinor": {
SchemaProps: spec.SchemaProps{
Description: "MinCompatibilityMinor is the minor version of the minimum compatibility version",
Type: []string{"string"},
Format: "",
},
},
"gitVersion": {

View File

@@ -21,6 +21,7 @@ import (
"sync/atomic"
"k8s.io/apimachinery/pkg/util/version"
apimachineryversion "k8s.io/apimachinery/pkg/version"
baseversion "k8s.io/component-base/version"
)
@@ -42,6 +43,9 @@ type EffectiveVersion interface {
// AllowedMinCompatibilityVersionRange returns the string of the allowed range of min compatibility version.
// Used only for docs/help.
AllowedMinCompatibilityVersionRange() string
// Info returns the version information of a component.
Info() *apimachineryversion.Info
}
type MutableEffectiveVersion interface {
@@ -173,6 +177,29 @@ func (m *effectiveVersion) Validate() []error {
return errs
}
// Info returns the version information of a component.
// If the binary version is nil, it returns nil.
func (m *effectiveVersion) Info() *apimachineryversion.Info {
binVer := m.BinaryVersion()
if binVer == nil {
return nil
}
info := binVer.Info()
if ev := m.EmulationVersion(); ev != nil {
info.EmulationMajor = version.Itoa(ev.Major())
info.EmulationMinor = version.Itoa(ev.Minor())
}
if mcv := m.MinCompatibilityVersion(); mcv != nil {
info.MinCompatibilityMajor = version.Itoa(mcv.Major())
info.MinCompatibilityMinor = version.Itoa(mcv.Minor())
}
return info
}
// NewEffectiveVersion creates a MutableEffectiveVersion from the binaryVersion.
// If useDefaultBuildBinaryVersion is true, the call of BinaryVersion() will always return the current binary version.
// NewEffectiveVersion(binaryVersion, true) should only be used if the binary version is dynamic.

View File

@@ -17,9 +17,11 @@ limitations under the License.
package compatibility
import (
"reflect"
"testing"
"k8s.io/apimachinery/pkg/util/version"
apimachineryversion "k8s.io/apimachinery/pkg/version"
)
func TestValidate(t *testing.T) {
@@ -146,3 +148,115 @@ func TestSetEmulationVersion(t *testing.T) {
})
}
}
func TestInfo(t *testing.T) {
tests := []struct {
name string
binaryVersion string
emulationVersion string
minCompatibilityVersion string
expectedInfo *apimachineryversion.Info
}{
{
name: "normal case",
binaryVersion: "v1.34.0",
emulationVersion: "v1.32.0",
minCompatibilityVersion: "v1.31.0",
expectedInfo: &apimachineryversion.Info{
Major: "1",
Minor: "34",
EmulationMajor: "1",
EmulationMinor: "32",
MinCompatibilityMajor: "1",
MinCompatibilityMinor: "31",
GitVersion: "1.34.0",
},
},
{
name: "default min compatibility version is emulation version - 1",
binaryVersion: "v1.34.0",
emulationVersion: "v1.32.0",
// minCompatibilityVersion not set, should default to v1.31.0
expectedInfo: &apimachineryversion.Info{
Major: "1",
Minor: "34",
EmulationMajor: "1",
EmulationMinor: "32",
MinCompatibilityMajor: "1",
MinCompatibilityMinor: "31",
GitVersion: "1.34.0",
},
},
{
name: "emulation version same as binary version",
binaryVersion: "v1.34.0",
emulationVersion: "v1.34.0",
// minCompatibilityVersion not set, should default to v1.33.0
expectedInfo: &apimachineryversion.Info{
Major: "1",
Minor: "34",
EmulationMajor: "1",
EmulationMinor: "34",
MinCompatibilityMajor: "1",
MinCompatibilityMinor: "33",
GitVersion: "1.34.0",
},
},
{
name: "empty binary version",
binaryVersion: "",
expectedInfo: nil,
},
{
name: "with pre-release and build metadata",
binaryVersion: "v1.34.0-alpha.1+abc123",
emulationVersion: "v1.32.0",
// minCompatibilityVersion not set, should default to v1.31.0
expectedInfo: &apimachineryversion.Info{
Major: "1",
Minor: "34",
EmulationMajor: "1",
EmulationMinor: "32",
MinCompatibilityMajor: "1",
MinCompatibilityMinor: "31",
GitVersion: "1.34.0-alpha.1+abc123",
},
},
{
name: "override default min compatibility version",
binaryVersion: "v1.34.0",
emulationVersion: "v1.32.0",
minCompatibilityVersion: "v1.32.0", // explicitly set to same as emulation version
expectedInfo: &apimachineryversion.Info{
Major: "1",
Minor: "34",
EmulationMajor: "1",
EmulationMinor: "32",
MinCompatibilityMajor: "1",
MinCompatibilityMinor: "32",
GitVersion: "1.34.0",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var effective MutableEffectiveVersion
if test.binaryVersion == "" {
effective = &effectiveVersion{}
} else {
effective = NewEffectiveVersionFromString(test.binaryVersion, "", "")
if test.emulationVersion != "" {
effective.SetEmulationVersion(version.MustParse(test.emulationVersion))
}
if test.minCompatibilityVersion != "" {
effective.SetMinCompatibilityVersion(version.MustParse(test.minCompatibilityVersion))
}
}
info := effective.Info()
if !reflect.DeepEqual(test.expectedInfo, info) {
t.Errorf("Expected %#v, Got %#v", test.expectedInfo, *info)
}
})
}
}

View File

@@ -25,6 +25,8 @@ import (
// Get returns the overall codebase version. It's for detecting
// what code a binary was built from.
// The caller should use BinaryMajor and BinaryMinor to determine
// the binary version. The Major and Minor fields are still set by git version for backwards compatibility.
func Get() apimachineryversion.Info {
// These variables typically come from -ldflags settings and in
// their absence fallback to the settings in ./base.go

View File

@@ -2625,16 +2625,46 @@ func schema_k8sio_apimachinery_pkg_version_Info(ref common.ReferenceCallback) co
Properties: map[string]spec.Schema{
"major": {
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
Description: "Major is the major version of the binary version",
Default: "",
Type: []string{"string"},
Format: "",
},
},
"minor": {
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
Description: "Minor is the minor version of the binary version",
Default: "",
Type: []string{"string"},
Format: "",
},
},
"emulationMajor": {
SchemaProps: spec.SchemaProps{
Description: "EmulationMajor is the major version of the emulation version",
Type: []string{"string"},
Format: "",
},
},
"emulationMinor": {
SchemaProps: spec.SchemaProps{
Description: "EmulationMinor is the minor version of the emulation version",
Type: []string{"string"},
Format: "",
},
},
"minCompatibilityMajor": {
SchemaProps: spec.SchemaProps{
Description: "MinCompatibilityMajor is the major version of the minimum compatibility version",
Type: []string{"string"},
Format: "",
},
},
"minCompatibilityMinor": {
SchemaProps: spec.SchemaProps{
Description: "MinCompatibilityMinor is the minor version of the minimum compatibility version",
Type: []string{"string"},
Format: "",
},
},
"gitVersion": {

View File

@@ -2623,16 +2623,46 @@ func schema_k8sio_apimachinery_pkg_version_Info(ref common.ReferenceCallback) co
Properties: map[string]spec.Schema{
"major": {
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
Description: "Major is the major version of the binary version",
Default: "",
Type: []string{"string"},
Format: "",
},
},
"minor": {
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
Description: "Minor is the minor version of the binary version",
Default: "",
Type: []string{"string"},
Format: "",
},
},
"emulationMajor": {
SchemaProps: spec.SchemaProps{
Description: "EmulationMajor is the major version of the emulation version",
Type: []string{"string"},
Format: "",
},
},
"emulationMinor": {
SchemaProps: spec.SchemaProps{
Description: "EmulationMinor is the minor version of the emulation version",
Type: []string{"string"},
Format: "",
},
},
"minCompatibilityMajor": {
SchemaProps: spec.SchemaProps{
Description: "MinCompatibilityMajor is the major version of the minimum compatibility version",
Type: []string{"string"},
Format: "",
},
},
"minCompatibilityMinor": {
SchemaProps: spec.SchemaProps{
Description: "MinCompatibilityMinor is the minor version of the minimum compatibility version",
Type: []string{"string"},
Format: "",
},
},
"gitVersion": {

View File

@@ -85,8 +85,14 @@ func TestClient(t *testing.T) {
}
expectedInfo := utilversion.Get()
kubeVersion := compatibility.DefaultKubeEffectiveVersionForTest().BinaryVersion()
emulationVersion := compatibility.DefaultKubeEffectiveVersionForTest().EmulationVersion()
minCompatibilityVersion := compatibility.DefaultKubeEffectiveVersionForTest().MinCompatibilityVersion()
expectedInfo.Major = fmt.Sprintf("%d", kubeVersion.Major())
expectedInfo.Minor = fmt.Sprintf("%d", kubeVersion.Minor())
expectedInfo.EmulationMajor = fmt.Sprintf("%d", emulationVersion.Major())
expectedInfo.EmulationMinor = fmt.Sprintf("%d", emulationVersion.Minor())
expectedInfo.MinCompatibilityMajor = fmt.Sprintf("%d", minCompatibilityVersion.Major())
expectedInfo.MinCompatibilityMinor = fmt.Sprintf("%d", minCompatibilityVersion.Minor())
if e, a := expectedInfo, *info; !reflect.DeepEqual(e, a) {
t.Errorf("expected %#v, got %#v", e, a)