mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 14:37:00 +00:00
refactor: detach Info from apimachinery util version
- Remove `info` field from `Version` struct - Modify `WithInfo` and `Info` methods to be deprecated - Update version information retrieval to use base version info - Simplify version information generation in compatibility tests - Remove unnecessary version info passing in build and test scenarios
This commit is contained in:
parent
a3094ccbe6
commit
14934b481e
2
api/openapi-spec/swagger.json
generated
2
api/openapi-spec/swagger.json
generated
@ -86014,7 +86014,7 @@
|
|||||||
"application/json"
|
"application/json"
|
||||||
],
|
],
|
||||||
"description": "get the version information for this server",
|
"description": "get the version information for this server",
|
||||||
"operationId": "getVersion",
|
"operationId": "getCodeVersion",
|
||||||
"produces": [
|
"produces": [
|
||||||
"application/json"
|
"application/json"
|
||||||
],
|
],
|
||||||
|
2
api/openapi-spec/v3/version_openapi.json
generated
2
api/openapi-spec/v3/version_openapi.json
generated
@ -91,7 +91,7 @@
|
|||||||
"/version/": {
|
"/version/": {
|
||||||
"get": {
|
"get": {
|
||||||
"description": "get the version information for this server",
|
"description": "get the version information for this server",
|
||||||
"operationId": "getVersion",
|
"operationId": "getCodeVersion",
|
||||||
"responses": {
|
"responses": {
|
||||||
"200": {
|
"200": {
|
||||||
"content": {
|
"content": {
|
||||||
|
@ -33,7 +33,6 @@ type Version struct {
|
|||||||
semver bool
|
semver bool
|
||||||
preRelease string
|
preRelease string
|
||||||
buildMetadata string
|
buildMetadata string
|
||||||
info apimachineryversion.Info
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -456,24 +455,25 @@ func (v *Version) Compare(other string) (int, error) {
|
|||||||
return v.compareInternal(ov), nil
|
return v.compareInternal(ov), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithInfo returns copy of the version object with requested info
|
// WithInfo returns copy of the version object.
|
||||||
|
// Deprecated: The Info field has been removed from the Version struct. This method no longer modifies the Version object.
|
||||||
func (v *Version) WithInfo(info apimachineryversion.Info) *Version {
|
func (v *Version) WithInfo(info apimachineryversion.Info) *Version {
|
||||||
result := *v
|
result := *v
|
||||||
result.info = info
|
|
||||||
return &result
|
return &result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Info returns the version information of a component.
|
||||||
|
// Deprecated: Use Info() from effective version instead.
|
||||||
func (v *Version) Info() *apimachineryversion.Info {
|
func (v *Version) Info() *apimachineryversion.Info {
|
||||||
if v == nil {
|
if v == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// in case info is empty, or the major and minor in info is different from the actual major and minor
|
// 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())
|
return &apimachineryversion.Info{
|
||||||
v.info.Minor = Itoa(v.Minor())
|
Major: Itoa(v.Major()),
|
||||||
if v.info.GitVersion == "" {
|
Minor: Itoa(v.Minor()),
|
||||||
v.info.GitVersion = v.String()
|
GitVersion: v.String(),
|
||||||
}
|
}
|
||||||
return &v.info
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Itoa(i uint) string {
|
func Itoa(i uint) string {
|
||||||
|
@ -47,6 +47,7 @@ import (
|
|||||||
restclient "k8s.io/client-go/rest"
|
restclient "k8s.io/client-go/rest"
|
||||||
cliflag "k8s.io/component-base/cli/flag"
|
cliflag "k8s.io/component-base/cli/flag"
|
||||||
basecompatibility "k8s.io/component-base/compatibility"
|
basecompatibility "k8s.io/component-base/compatibility"
|
||||||
|
baseversion "k8s.io/component-base/version"
|
||||||
"k8s.io/klog/v2/ktesting"
|
"k8s.io/klog/v2/ktesting"
|
||||||
netutils "k8s.io/utils/net"
|
netutils "k8s.io/utils/net"
|
||||||
)
|
)
|
||||||
@ -277,9 +278,8 @@ func TestServerRunWithSNI(t *testing.T) {
|
|||||||
|
|
||||||
// launch server
|
// launch server
|
||||||
config := setUp(t)
|
config := setUp(t)
|
||||||
v := fakeVersion()
|
info := fakeVersionInfo()
|
||||||
config.EffectiveVersion = basecompatibility.NewEffectiveVersionFromString(v.String(), "", "")
|
config.EffectiveVersion = basecompatibility.NewEffectiveVersionFromString(fmt.Sprintf("%s.%s", info.Major, info.Minor), "", "")
|
||||||
|
|
||||||
config.EnableIndex = true
|
config.EnableIndex = true
|
||||||
secureOptions := (&SecureServingOptions{
|
secureOptions := (&SecureServingOptions{
|
||||||
BindAddress: netutils.ParseIPSloppy("127.0.0.1"),
|
BindAddress: netutils.ParseIPSloppy("127.0.0.1"),
|
||||||
@ -371,7 +371,7 @@ func TestServerRunWithSNI(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to connect with loopback client: %v", err)
|
t.Fatalf("failed to connect with loopback client: %v", err)
|
||||||
}
|
}
|
||||||
if expected := &v; !reflect.DeepEqual(got, expected) {
|
if expected := &info; !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)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -461,16 +461,15 @@ func certSignature(cert tls.Certificate) (string, error) {
|
|||||||
return x509CertSignature(x509Certs[0]), nil
|
return x509CertSignature(x509Certs[0]), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func fakeVersion() version.Info {
|
func fakeVersionInfo() version.Info {
|
||||||
return version.Info{
|
baseVer := baseversion.Get()
|
||||||
Major: "42",
|
baseVer.Major = "42"
|
||||||
Minor: "42",
|
baseVer.Minor = "42"
|
||||||
EmulationMajor: "42",
|
baseVer.EmulationMajor = "42"
|
||||||
EmulationMinor: "42",
|
baseVer.EmulationMinor = "42"
|
||||||
MinCompatibilityMajor: "42",
|
baseVer.MinCompatibilityMajor = "42"
|
||||||
MinCompatibilityMinor: "41",
|
baseVer.MinCompatibilityMinor = "41"
|
||||||
GitVersion: "42.42",
|
return baseVer
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// generateSelfSignedCertKey creates a self-signed certificate and key for the given host.
|
// generateSelfSignedCertKey creates a self-signed certificate and key for the given host.
|
||||||
|
@ -43,7 +43,7 @@ func (v Version) Install(c *restful.Container) {
|
|||||||
versionWS.Route(
|
versionWS.Route(
|
||||||
versionWS.GET("/").To(v.handleVersion).
|
versionWS.GET("/").To(v.handleVersion).
|
||||||
Doc("get the version information for this server").
|
Doc("get the version information for this server").
|
||||||
Operation("getVersion").
|
Operation("getCodeVersion").
|
||||||
Produces(restful.MIME_JSON).
|
Produces(restful.MIME_JSON).
|
||||||
Consumes(restful.MIME_JSON).
|
Consumes(restful.MIME_JSON).
|
||||||
Writes(version.Info{}))
|
Writes(version.Info{}))
|
||||||
|
@ -55,11 +55,11 @@ func kubeEffectiveVersionFloors(binaryVersion *version.Version) *version.Version
|
|||||||
// We do not enforce the N-3..N emulation version range in tests so that the tests would not automatically fail when there is a version bump.
|
// We do not enforce the N-3..N emulation version range in tests so that the tests would not automatically fail when there is a version bump.
|
||||||
// Only used in tests.
|
// Only used in tests.
|
||||||
func DefaultKubeEffectiveVersionForTest() basecompatibility.MutableEffectiveVersion {
|
func DefaultKubeEffectiveVersionForTest() basecompatibility.MutableEffectiveVersion {
|
||||||
binaryVersion := version.MustParse(baseversion.DefaultKubeBinaryVersion).WithInfo(baseversion.Get())
|
binaryVersion := version.MustParse(baseversion.DefaultKubeBinaryVersion)
|
||||||
return basecompatibility.NewEffectiveVersion(binaryVersion, false, version.MustParse("0.0"), version.MustParse("0.0"))
|
return basecompatibility.NewEffectiveVersion(binaryVersion, false, version.MustParse("0.0"), version.MustParse("0.0"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultBuildBinaryVersion() *version.Version {
|
func defaultBuildBinaryVersion() *version.Version {
|
||||||
verInfo := baseversion.Get()
|
verInfo := baseversion.Get()
|
||||||
return version.MustParse(verInfo.String()).WithInfo(verInfo)
|
return version.MustParse(verInfo.String())
|
||||||
}
|
}
|
||||||
|
@ -185,7 +185,12 @@ func (m *effectiveVersion) Info() *apimachineryversion.Info {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
info := binVer.Info()
|
info := baseversion.Get()
|
||||||
|
info.Major = version.Itoa(binVer.Major())
|
||||||
|
info.Minor = version.Itoa(binVer.Minor())
|
||||||
|
if info.GitVersion == "" {
|
||||||
|
info.GitVersion = binVer.String()
|
||||||
|
}
|
||||||
|
|
||||||
if ev := m.EmulationVersion(); ev != nil {
|
if ev := m.EmulationVersion(); ev != nil {
|
||||||
info.EmulationMajor = version.Itoa(ev.Major())
|
info.EmulationMajor = version.Itoa(ev.Major())
|
||||||
@ -197,7 +202,7 @@ func (m *effectiveVersion) Info() *apimachineryversion.Info {
|
|||||||
info.MinCompatibilityMinor = version.Itoa(mcv.Minor())
|
info.MinCompatibilityMinor = version.Itoa(mcv.Minor())
|
||||||
}
|
}
|
||||||
|
|
||||||
return info
|
return &info
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewEffectiveVersion creates a MutableEffectiveVersion from the binaryVersion.
|
// NewEffectiveVersion creates a MutableEffectiveVersion from the binaryVersion.
|
||||||
@ -236,5 +241,5 @@ func NewEffectiveVersionFromString(binaryVer, emulationVerFloor, minCompatibilit
|
|||||||
|
|
||||||
func defaultBuildBinaryVersion() *version.Version {
|
func defaultBuildBinaryVersion() *version.Version {
|
||||||
verInfo := baseversion.Get()
|
verInfo := baseversion.Get()
|
||||||
return version.MustParse(verInfo.String()).WithInfo(verInfo)
|
return version.MustParse(verInfo.String())
|
||||||
}
|
}
|
||||||
|
@ -17,11 +17,9 @@ limitations under the License.
|
|||||||
package compatibility
|
package compatibility
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/util/version"
|
"k8s.io/apimachinery/pkg/util/version"
|
||||||
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestValidate(t *testing.T) {
|
func TestValidate(t *testing.T) {
|
||||||
@ -151,91 +149,80 @@ func TestSetEmulationVersion(t *testing.T) {
|
|||||||
|
|
||||||
func TestInfo(t *testing.T) {
|
func TestInfo(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
binaryVersion string
|
binaryVersion string
|
||||||
emulationVersion string
|
emulationVersion string
|
||||||
minCompatibilityVersion string
|
minCompatibilityVersion string
|
||||||
expectedInfo *apimachineryversion.Info
|
expectedMajor string
|
||||||
|
expectedMinor string
|
||||||
|
expectedEmulationMajor string
|
||||||
|
expectedEmulationMinor string
|
||||||
|
expectedMinCompatibilityMajor string
|
||||||
|
expectedMinCompatibilityMinor string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "normal case",
|
name: "normal case",
|
||||||
binaryVersion: "v1.34.0",
|
binaryVersion: "v1.34.0",
|
||||||
emulationVersion: "v1.32.0",
|
emulationVersion: "v1.32.0",
|
||||||
minCompatibilityVersion: "v1.31.0",
|
minCompatibilityVersion: "v1.31.0",
|
||||||
expectedInfo: &apimachineryversion.Info{
|
expectedMajor: "1",
|
||||||
Major: "1",
|
expectedMinor: "34",
|
||||||
Minor: "34",
|
expectedEmulationMajor: "1",
|
||||||
EmulationMajor: "1",
|
expectedEmulationMinor: "32",
|
||||||
EmulationMinor: "32",
|
expectedMinCompatibilityMajor: "1",
|
||||||
MinCompatibilityMajor: "1",
|
expectedMinCompatibilityMinor: "31",
|
||||||
MinCompatibilityMinor: "31",
|
|
||||||
GitVersion: "1.34.0",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "default min compatibility version is emulation version - 1",
|
name: "default min compatibility version is emulation version - 1",
|
||||||
binaryVersion: "v1.34.0",
|
binaryVersion: "v1.34.0",
|
||||||
emulationVersion: "v1.32.0",
|
emulationVersion: "v1.32.0",
|
||||||
// minCompatibilityVersion not set, should default to v1.31.0
|
// minCompatibilityVersion not set, should default to v1.31.0
|
||||||
expectedInfo: &apimachineryversion.Info{
|
expectedMajor: "1",
|
||||||
Major: "1",
|
expectedMinor: "34",
|
||||||
Minor: "34",
|
expectedEmulationMajor: "1",
|
||||||
EmulationMajor: "1",
|
expectedEmulationMinor: "32",
|
||||||
EmulationMinor: "32",
|
expectedMinCompatibilityMajor: "1",
|
||||||
MinCompatibilityMajor: "1",
|
expectedMinCompatibilityMinor: "31",
|
||||||
MinCompatibilityMinor: "31",
|
|
||||||
GitVersion: "1.34.0",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "emulation version same as binary version",
|
name: "emulation version same as binary version",
|
||||||
binaryVersion: "v1.34.0",
|
binaryVersion: "v1.34.0",
|
||||||
emulationVersion: "v1.34.0",
|
emulationVersion: "v1.34.0",
|
||||||
// minCompatibilityVersion not set, should default to v1.33.0
|
// minCompatibilityVersion not set, should default to v1.33.0
|
||||||
expectedInfo: &apimachineryversion.Info{
|
expectedMajor: "1",
|
||||||
Major: "1",
|
expectedMinor: "34",
|
||||||
Minor: "34",
|
expectedEmulationMajor: "1",
|
||||||
EmulationMajor: "1",
|
expectedEmulationMinor: "34",
|
||||||
EmulationMinor: "34",
|
expectedMinCompatibilityMajor: "1",
|
||||||
MinCompatibilityMajor: "1",
|
expectedMinCompatibilityMinor: "33",
|
||||||
MinCompatibilityMinor: "33",
|
|
||||||
GitVersion: "1.34.0",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "empty binary version",
|
name: "empty binary version",
|
||||||
binaryVersion: "",
|
binaryVersion: "",
|
||||||
expectedInfo: nil,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with pre-release and build metadata",
|
name: "with pre-release and build metadata",
|
||||||
binaryVersion: "v1.34.0-alpha.1+abc123",
|
binaryVersion: "v1.34.0-alpha.1+abc123",
|
||||||
emulationVersion: "v1.32.0",
|
emulationVersion: "v1.32.0",
|
||||||
// minCompatibilityVersion not set, should default to v1.31.0
|
// minCompatibilityVersion not set, should default to v1.31.0
|
||||||
expectedInfo: &apimachineryversion.Info{
|
expectedMajor: "1",
|
||||||
Major: "1",
|
expectedMinor: "34",
|
||||||
Minor: "34",
|
expectedEmulationMajor: "1",
|
||||||
EmulationMajor: "1",
|
expectedEmulationMinor: "32",
|
||||||
EmulationMinor: "32",
|
expectedMinCompatibilityMajor: "1",
|
||||||
MinCompatibilityMajor: "1",
|
expectedMinCompatibilityMinor: "31",
|
||||||
MinCompatibilityMinor: "31",
|
|
||||||
GitVersion: "1.34.0-alpha.1+abc123",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "override default min compatibility version",
|
name: "override default min compatibility version",
|
||||||
binaryVersion: "v1.34.0",
|
binaryVersion: "v1.34.0",
|
||||||
emulationVersion: "v1.32.0",
|
emulationVersion: "v1.32.0",
|
||||||
minCompatibilityVersion: "v1.32.0", // explicitly set to same as emulation version
|
minCompatibilityVersion: "v1.32.0", // explicitly set to same as emulation version
|
||||||
expectedInfo: &apimachineryversion.Info{
|
expectedMajor: "1",
|
||||||
Major: "1",
|
expectedMinor: "34",
|
||||||
Minor: "34",
|
expectedEmulationMajor: "1",
|
||||||
EmulationMajor: "1",
|
expectedEmulationMinor: "32",
|
||||||
EmulationMinor: "32",
|
expectedMinCompatibilityMajor: "1",
|
||||||
MinCompatibilityMajor: "1",
|
expectedMinCompatibilityMinor: "32",
|
||||||
MinCompatibilityMinor: "32",
|
|
||||||
GitVersion: "1.34.0",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -254,8 +241,30 @@ func TestInfo(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
info := effective.Info()
|
info := effective.Info()
|
||||||
if !reflect.DeepEqual(test.expectedInfo, info) {
|
if info == nil {
|
||||||
t.Errorf("Expected %#v, Got %#v", test.expectedInfo, *info)
|
if test.expectedMajor != "" {
|
||||||
|
t.Fatalf("expected info, got nil")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if info.Major != test.expectedMajor {
|
||||||
|
t.Errorf("expected major %s, got %s", test.expectedMajor, info.Major)
|
||||||
|
}
|
||||||
|
if info.Minor != test.expectedMinor {
|
||||||
|
t.Errorf("expected minor %s, got %s", test.expectedMinor, info.Minor)
|
||||||
|
}
|
||||||
|
if info.EmulationMajor != test.expectedEmulationMajor {
|
||||||
|
t.Errorf("expected emulation major %s, got %s", test.expectedEmulationMajor, info.EmulationMajor)
|
||||||
|
}
|
||||||
|
if info.EmulationMinor != test.expectedEmulationMinor {
|
||||||
|
t.Errorf("expected emulation minor %s, got %s", test.expectedEmulationMinor, info.EmulationMinor)
|
||||||
|
}
|
||||||
|
if info.MinCompatibilityMajor != test.expectedMinCompatibilityMajor {
|
||||||
|
t.Errorf("expected min compatibility major %s, got %s", test.expectedMinCompatibilityMajor, info.MinCompatibilityMajor)
|
||||||
|
}
|
||||||
|
if info.MinCompatibilityMinor != test.expectedMinCompatibilityMinor {
|
||||||
|
t.Errorf("expected min compatibility minor %s, got %s", test.expectedMinCompatibilityMinor, info.MinCompatibilityMinor)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user