mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +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"
|
||||
],
|
||||
"description": "get the version information for this server",
|
||||
"operationId": "getVersion",
|
||||
"operationId": "getCodeVersion",
|
||||
"produces": [
|
||||
"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/": {
|
||||
"get": {
|
||||
"description": "get the version information for this server",
|
||||
"operationId": "getVersion",
|
||||
"operationId": "getCodeVersion",
|
||||
"responses": {
|
||||
"200": {
|
||||
"content": {
|
||||
|
@ -33,7 +33,6 @@ type Version struct {
|
||||
semver bool
|
||||
preRelease string
|
||||
buildMetadata string
|
||||
info apimachineryversion.Info
|
||||
}
|
||||
|
||||
var (
|
||||
@ -456,24 +455,25 @@ func (v *Version) Compare(other string) (int, error) {
|
||||
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 {
|
||||
result := *v
|
||||
result.info = info
|
||||
return &result
|
||||
}
|
||||
|
||||
// Info returns the version information of a component.
|
||||
// Deprecated: Use Info() from effective version instead.
|
||||
func (v *Version) Info() *apimachineryversion.Info {
|
||||
if v == nil {
|
||||
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())
|
||||
if v.info.GitVersion == "" {
|
||||
v.info.GitVersion = v.String()
|
||||
return &apimachineryversion.Info{
|
||||
Major: Itoa(v.Major()),
|
||||
Minor: Itoa(v.Minor()),
|
||||
GitVersion: v.String(),
|
||||
}
|
||||
return &v.info
|
||||
}
|
||||
|
||||
func Itoa(i uint) string {
|
||||
|
@ -47,6 +47,7 @@ import (
|
||||
restclient "k8s.io/client-go/rest"
|
||||
cliflag "k8s.io/component-base/cli/flag"
|
||||
basecompatibility "k8s.io/component-base/compatibility"
|
||||
baseversion "k8s.io/component-base/version"
|
||||
"k8s.io/klog/v2/ktesting"
|
||||
netutils "k8s.io/utils/net"
|
||||
)
|
||||
@ -277,9 +278,8 @@ func TestServerRunWithSNI(t *testing.T) {
|
||||
|
||||
// launch server
|
||||
config := setUp(t)
|
||||
v := fakeVersion()
|
||||
config.EffectiveVersion = basecompatibility.NewEffectiveVersionFromString(v.String(), "", "")
|
||||
|
||||
info := fakeVersionInfo()
|
||||
config.EffectiveVersion = basecompatibility.NewEffectiveVersionFromString(fmt.Sprintf("%s.%s", info.Major, info.Minor), "", "")
|
||||
config.EnableIndex = true
|
||||
secureOptions := (&SecureServingOptions{
|
||||
BindAddress: netutils.ParseIPSloppy("127.0.0.1"),
|
||||
@ -371,7 +371,7 @@ func TestServerRunWithSNI(t *testing.T) {
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
|
||||
@ -461,16 +461,15 @@ func certSignature(cert tls.Certificate) (string, error) {
|
||||
return x509CertSignature(x509Certs[0]), nil
|
||||
}
|
||||
|
||||
func fakeVersion() version.Info {
|
||||
return version.Info{
|
||||
Major: "42",
|
||||
Minor: "42",
|
||||
EmulationMajor: "42",
|
||||
EmulationMinor: "42",
|
||||
MinCompatibilityMajor: "42",
|
||||
MinCompatibilityMinor: "41",
|
||||
GitVersion: "42.42",
|
||||
}
|
||||
func fakeVersionInfo() version.Info {
|
||||
baseVer := baseversion.Get()
|
||||
baseVer.Major = "42"
|
||||
baseVer.Minor = "42"
|
||||
baseVer.EmulationMajor = "42"
|
||||
baseVer.EmulationMinor = "42"
|
||||
baseVer.MinCompatibilityMajor = "42"
|
||||
baseVer.MinCompatibilityMinor = "41"
|
||||
return baseVer
|
||||
}
|
||||
|
||||
// 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.GET("/").To(v.handleVersion).
|
||||
Doc("get the version information for this server").
|
||||
Operation("getVersion").
|
||||
Operation("getCodeVersion").
|
||||
Produces(restful.MIME_JSON).
|
||||
Consumes(restful.MIME_JSON).
|
||||
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.
|
||||
// Only used in tests.
|
||||
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"))
|
||||
}
|
||||
|
||||
func defaultBuildBinaryVersion() *version.Version {
|
||||
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
|
||||
}
|
||||
|
||||
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 {
|
||||
info.EmulationMajor = version.Itoa(ev.Major())
|
||||
@ -197,7 +202,7 @@ func (m *effectiveVersion) Info() *apimachineryversion.Info {
|
||||
info.MinCompatibilityMinor = version.Itoa(mcv.Minor())
|
||||
}
|
||||
|
||||
return info
|
||||
return &info
|
||||
}
|
||||
|
||||
// NewEffectiveVersion creates a MutableEffectiveVersion from the binaryVersion.
|
||||
@ -236,5 +241,5 @@ func NewEffectiveVersionFromString(binaryVer, emulationVerFloor, minCompatibilit
|
||||
|
||||
func defaultBuildBinaryVersion() *version.Version {
|
||||
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
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/version"
|
||||
apimachineryversion "k8s.io/apimachinery/pkg/version"
|
||||
)
|
||||
|
||||
func TestValidate(t *testing.T) {
|
||||
@ -151,91 +149,80 @@ func TestSetEmulationVersion(t *testing.T) {
|
||||
|
||||
func TestInfo(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
binaryVersion string
|
||||
emulationVersion string
|
||||
minCompatibilityVersion string
|
||||
expectedInfo *apimachineryversion.Info
|
||||
name string
|
||||
binaryVersion string
|
||||
emulationVersion string
|
||||
minCompatibilityVersion string
|
||||
expectedMajor string
|
||||
expectedMinor string
|
||||
expectedEmulationMajor string
|
||||
expectedEmulationMinor string
|
||||
expectedMinCompatibilityMajor string
|
||||
expectedMinCompatibilityMinor string
|
||||
}{
|
||||
{
|
||||
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: "normal case",
|
||||
binaryVersion: "v1.34.0",
|
||||
emulationVersion: "v1.32.0",
|
||||
minCompatibilityVersion: "v1.31.0",
|
||||
expectedMajor: "1",
|
||||
expectedMinor: "34",
|
||||
expectedEmulationMajor: "1",
|
||||
expectedEmulationMinor: "32",
|
||||
expectedMinCompatibilityMajor: "1",
|
||||
expectedMinCompatibilityMinor: "31",
|
||||
},
|
||||
{
|
||||
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",
|
||||
},
|
||||
expectedMajor: "1",
|
||||
expectedMinor: "34",
|
||||
expectedEmulationMajor: "1",
|
||||
expectedEmulationMinor: "32",
|
||||
expectedMinCompatibilityMajor: "1",
|
||||
expectedMinCompatibilityMinor: "31",
|
||||
},
|
||||
{
|
||||
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",
|
||||
},
|
||||
expectedMajor: "1",
|
||||
expectedMinor: "34",
|
||||
expectedEmulationMajor: "1",
|
||||
expectedEmulationMinor: "34",
|
||||
expectedMinCompatibilityMajor: "1",
|
||||
expectedMinCompatibilityMinor: "33",
|
||||
},
|
||||
{
|
||||
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",
|
||||
},
|
||||
expectedMajor: "1",
|
||||
expectedMinor: "34",
|
||||
expectedEmulationMajor: "1",
|
||||
expectedEmulationMinor: "32",
|
||||
expectedMinCompatibilityMajor: "1",
|
||||
expectedMinCompatibilityMinor: "31",
|
||||
},
|
||||
{
|
||||
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",
|
||||
},
|
||||
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
|
||||
expectedMajor: "1",
|
||||
expectedMinor: "34",
|
||||
expectedEmulationMajor: "1",
|
||||
expectedEmulationMinor: "32",
|
||||
expectedMinCompatibilityMajor: "1",
|
||||
expectedMinCompatibilityMinor: "32",
|
||||
},
|
||||
}
|
||||
|
||||
@ -254,8 +241,30 @@ func TestInfo(t *testing.T) {
|
||||
}
|
||||
}
|
||||
info := effective.Info()
|
||||
if !reflect.DeepEqual(test.expectedInfo, info) {
|
||||
t.Errorf("Expected %#v, Got %#v", test.expectedInfo, *info)
|
||||
if info == nil {
|
||||
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