Skip to content

Commit 14934b4

Browse files
committed
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
1 parent a3094cc commit 14934b4

File tree

8 files changed

+106
-93
lines changed

8 files changed

+106
-93
lines changed

api/openapi-spec/swagger.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/openapi-spec/v3/version_openapi.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/apimachinery/pkg/util/version/version.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ type Version struct {
3333
semver bool
3434
preRelease string
3535
buildMetadata string
36-
info apimachineryversion.Info
3736
}
3837

3938
var (
@@ -456,24 +455,25 @@ func (v *Version) Compare(other string) (int, error) {
456455
return v.compareInternal(ov), nil
457456
}
458457

459-
// WithInfo returns copy of the version object with requested info
458+
// WithInfo returns copy of the version object.
459+
// Deprecated: The Info field has been removed from the Version struct. This method no longer modifies the Version object.
460460
func (v *Version) WithInfo(info apimachineryversion.Info) *Version {
461461
result := *v
462-
result.info = info
463462
return &result
464463
}
465464

465+
// Info returns the version information of a component.
466+
// Deprecated: Use Info() from effective version instead.
466467
func (v *Version) Info() *apimachineryversion.Info {
467468
if v == nil {
468469
return nil
469470
}
470471
// in case info is empty, or the major and minor in info is different from the actual major and minor
471-
v.info.Major = Itoa(v.Major())
472-
v.info.Minor = Itoa(v.Minor())
473-
if v.info.GitVersion == "" {
474-
v.info.GitVersion = v.String()
472+
return &apimachineryversion.Info{
473+
Major: Itoa(v.Major()),
474+
Minor: Itoa(v.Minor()),
475+
GitVersion: v.String(),
475476
}
476-
return &v.info
477477
}
478478

479479
func Itoa(i uint) string {

staging/src/k8s.io/apiserver/pkg/server/options/serving_test.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import (
4747
restclient "k8s.io/client-go/rest"
4848
cliflag "k8s.io/component-base/cli/flag"
4949
basecompatibility "k8s.io/component-base/compatibility"
50+
baseversion "k8s.io/component-base/version"
5051
"k8s.io/klog/v2/ktesting"
5152
netutils "k8s.io/utils/net"
5253
)
@@ -277,9 +278,8 @@ func TestServerRunWithSNI(t *testing.T) {
277278

278279
// launch server
279280
config := setUp(t)
280-
v := fakeVersion()
281-
config.EffectiveVersion = basecompatibility.NewEffectiveVersionFromString(v.String(), "", "")
282-
281+
info := fakeVersionInfo()
282+
config.EffectiveVersion = basecompatibility.NewEffectiveVersionFromString(fmt.Sprintf("%s.%s", info.Major, info.Minor), "", "")
283283
config.EnableIndex = true
284284
secureOptions := (&SecureServingOptions{
285285
BindAddress: netutils.ParseIPSloppy("127.0.0.1"),
@@ -371,7 +371,7 @@ func TestServerRunWithSNI(t *testing.T) {
371371
if err != nil {
372372
t.Fatalf("failed to connect with loopback client: %v", err)
373373
}
374-
if expected := &v; !reflect.DeepEqual(got, expected) {
374+
if expected := &info; !reflect.DeepEqual(got, expected) {
375375
t.Errorf("loopback client didn't get correct version info: expected=%v got=%v", *expected, *got)
376376
}
377377

@@ -461,16 +461,15 @@ func certSignature(cert tls.Certificate) (string, error) {
461461
return x509CertSignature(x509Certs[0]), nil
462462
}
463463

464-
func fakeVersion() version.Info {
465-
return version.Info{
466-
Major: "42",
467-
Minor: "42",
468-
EmulationMajor: "42",
469-
EmulationMinor: "42",
470-
MinCompatibilityMajor: "42",
471-
MinCompatibilityMinor: "41",
472-
GitVersion: "42.42",
473-
}
464+
func fakeVersionInfo() version.Info {
465+
baseVer := baseversion.Get()
466+
baseVer.Major = "42"
467+
baseVer.Minor = "42"
468+
baseVer.EmulationMajor = "42"
469+
baseVer.EmulationMinor = "42"
470+
baseVer.MinCompatibilityMajor = "42"
471+
baseVer.MinCompatibilityMinor = "41"
472+
return baseVer
474473
}
475474

476475
// generateSelfSignedCertKey creates a self-signed certificate and key for the given host.

staging/src/k8s.io/apiserver/pkg/server/routes/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (v Version) Install(c *restful.Container) {
4343
versionWS.Route(
4444
versionWS.GET("/").To(v.handleVersion).
4545
Doc("get the version information for this server").
46-
Operation("getVersion").
46+
Operation("getCodeVersion").
4747
Produces(restful.MIME_JSON).
4848
Consumes(restful.MIME_JSON).
4949
Writes(version.Info{}))

staging/src/k8s.io/apiserver/pkg/util/compatibility/version.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ func kubeEffectiveVersionFloors(binaryVersion *version.Version) *version.Version
5555
// 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.
5656
// Only used in tests.
5757
func DefaultKubeEffectiveVersionForTest() basecompatibility.MutableEffectiveVersion {
58-
binaryVersion := version.MustParse(baseversion.DefaultKubeBinaryVersion).WithInfo(baseversion.Get())
58+
binaryVersion := version.MustParse(baseversion.DefaultKubeBinaryVersion)
5959
return basecompatibility.NewEffectiveVersion(binaryVersion, false, version.MustParse("0.0"), version.MustParse("0.0"))
6060
}
6161

6262
func defaultBuildBinaryVersion() *version.Version {
6363
verInfo := baseversion.Get()
64-
return version.MustParse(verInfo.String()).WithInfo(verInfo)
64+
return version.MustParse(verInfo.String())
6565
}

staging/src/k8s.io/component-base/compatibility/version.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,12 @@ func (m *effectiveVersion) Info() *apimachineryversion.Info {
185185
return nil
186186
}
187187

188-
info := binVer.Info()
188+
info := baseversion.Get()
189+
info.Major = version.Itoa(binVer.Major())
190+
info.Minor = version.Itoa(binVer.Minor())
191+
if info.GitVersion == "" {
192+
info.GitVersion = binVer.String()
193+
}
189194

190195
if ev := m.EmulationVersion(); ev != nil {
191196
info.EmulationMajor = version.Itoa(ev.Major())
@@ -197,7 +202,7 @@ func (m *effectiveVersion) Info() *apimachineryversion.Info {
197202
info.MinCompatibilityMinor = version.Itoa(mcv.Minor())
198203
}
199204

200-
return info
205+
return &info
201206
}
202207

203208
// NewEffectiveVersion creates a MutableEffectiveVersion from the binaryVersion.
@@ -236,5 +241,5 @@ func NewEffectiveVersionFromString(binaryVer, emulationVerFloor, minCompatibilit
236241

237242
func defaultBuildBinaryVersion() *version.Version {
238243
verInfo := baseversion.Get()
239-
return version.MustParse(verInfo.String()).WithInfo(verInfo)
244+
return version.MustParse(verInfo.String())
240245
}

staging/src/k8s.io/component-base/compatibility/version_test.go

Lines changed: 72 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ limitations under the License.
1717
package compatibility
1818

1919
import (
20-
"reflect"
2120
"testing"
2221

2322
"k8s.io/apimachinery/pkg/util/version"
24-
apimachineryversion "k8s.io/apimachinery/pkg/version"
2523
)
2624

2725
func TestValidate(t *testing.T) {
@@ -151,91 +149,80 @@ func TestSetEmulationVersion(t *testing.T) {
151149

152150
func TestInfo(t *testing.T) {
153151
tests := []struct {
154-
name string
155-
binaryVersion string
156-
emulationVersion string
157-
minCompatibilityVersion string
158-
expectedInfo *apimachineryversion.Info
152+
name string
153+
binaryVersion string
154+
emulationVersion string
155+
minCompatibilityVersion string
156+
expectedMajor string
157+
expectedMinor string
158+
expectedEmulationMajor string
159+
expectedEmulationMinor string
160+
expectedMinCompatibilityMajor string
161+
expectedMinCompatibilityMinor string
159162
}{
160163
{
161-
name: "normal case",
162-
binaryVersion: "v1.34.0",
163-
emulationVersion: "v1.32.0",
164-
minCompatibilityVersion: "v1.31.0",
165-
expectedInfo: &apimachineryversion.Info{
166-
Major: "1",
167-
Minor: "34",
168-
EmulationMajor: "1",
169-
EmulationMinor: "32",
170-
MinCompatibilityMajor: "1",
171-
MinCompatibilityMinor: "31",
172-
GitVersion: "1.34.0",
173-
},
164+
name: "normal case",
165+
binaryVersion: "v1.34.0",
166+
emulationVersion: "v1.32.0",
167+
minCompatibilityVersion: "v1.31.0",
168+
expectedMajor: "1",
169+
expectedMinor: "34",
170+
expectedEmulationMajor: "1",
171+
expectedEmulationMinor: "32",
172+
expectedMinCompatibilityMajor: "1",
173+
expectedMinCompatibilityMinor: "31",
174174
},
175175
{
176176
name: "default min compatibility version is emulation version - 1",
177177
binaryVersion: "v1.34.0",
178178
emulationVersion: "v1.32.0",
179179
// minCompatibilityVersion not set, should default to v1.31.0
180-
expectedInfo: &apimachineryversion.Info{
181-
Major: "1",
182-
Minor: "34",
183-
EmulationMajor: "1",
184-
EmulationMinor: "32",
185-
MinCompatibilityMajor: "1",
186-
MinCompatibilityMinor: "31",
187-
GitVersion: "1.34.0",
188-
},
180+
expectedMajor: "1",
181+
expectedMinor: "34",
182+
expectedEmulationMajor: "1",
183+
expectedEmulationMinor: "32",
184+
expectedMinCompatibilityMajor: "1",
185+
expectedMinCompatibilityMinor: "31",
189186
},
190187
{
191188
name: "emulation version same as binary version",
192189
binaryVersion: "v1.34.0",
193190
emulationVersion: "v1.34.0",
194191
// minCompatibilityVersion not set, should default to v1.33.0
195-
expectedInfo: &apimachineryversion.Info{
196-
Major: "1",
197-
Minor: "34",
198-
EmulationMajor: "1",
199-
EmulationMinor: "34",
200-
MinCompatibilityMajor: "1",
201-
MinCompatibilityMinor: "33",
202-
GitVersion: "1.34.0",
203-
},
192+
expectedMajor: "1",
193+
expectedMinor: "34",
194+
expectedEmulationMajor: "1",
195+
expectedEmulationMinor: "34",
196+
expectedMinCompatibilityMajor: "1",
197+
expectedMinCompatibilityMinor: "33",
204198
},
205199
{
206200
name: "empty binary version",
207201
binaryVersion: "",
208-
expectedInfo: nil,
209202
},
210203
{
211204
name: "with pre-release and build metadata",
212205
binaryVersion: "v1.34.0-alpha.1+abc123",
213206
emulationVersion: "v1.32.0",
214207
// minCompatibilityVersion not set, should default to v1.31.0
215-
expectedInfo: &apimachineryversion.Info{
216-
Major: "1",
217-
Minor: "34",
218-
EmulationMajor: "1",
219-
EmulationMinor: "32",
220-
MinCompatibilityMajor: "1",
221-
MinCompatibilityMinor: "31",
222-
GitVersion: "1.34.0-alpha.1+abc123",
223-
},
208+
expectedMajor: "1",
209+
expectedMinor: "34",
210+
expectedEmulationMajor: "1",
211+
expectedEmulationMinor: "32",
212+
expectedMinCompatibilityMajor: "1",
213+
expectedMinCompatibilityMinor: "31",
224214
},
225215
{
226-
name: "override default min compatibility version",
227-
binaryVersion: "v1.34.0",
228-
emulationVersion: "v1.32.0",
229-
minCompatibilityVersion: "v1.32.0", // explicitly set to same as emulation version
230-
expectedInfo: &apimachineryversion.Info{
231-
Major: "1",
232-
Minor: "34",
233-
EmulationMajor: "1",
234-
EmulationMinor: "32",
235-
MinCompatibilityMajor: "1",
236-
MinCompatibilityMinor: "32",
237-
GitVersion: "1.34.0",
238-
},
216+
name: "override default min compatibility version",
217+
binaryVersion: "v1.34.0",
218+
emulationVersion: "v1.32.0",
219+
minCompatibilityVersion: "v1.32.0", // explicitly set to same as emulation version
220+
expectedMajor: "1",
221+
expectedMinor: "34",
222+
expectedEmulationMajor: "1",
223+
expectedEmulationMinor: "32",
224+
expectedMinCompatibilityMajor: "1",
225+
expectedMinCompatibilityMinor: "32",
239226
},
240227
}
241228

@@ -254,8 +241,30 @@ func TestInfo(t *testing.T) {
254241
}
255242
}
256243
info := effective.Info()
257-
if !reflect.DeepEqual(test.expectedInfo, info) {
258-
t.Errorf("Expected %#v, Got %#v", test.expectedInfo, *info)
244+
if info == nil {
245+
if test.expectedMajor != "" {
246+
t.Fatalf("expected info, got nil")
247+
}
248+
return
249+
}
250+
251+
if info.Major != test.expectedMajor {
252+
t.Errorf("expected major %s, got %s", test.expectedMajor, info.Major)
253+
}
254+
if info.Minor != test.expectedMinor {
255+
t.Errorf("expected minor %s, got %s", test.expectedMinor, info.Minor)
256+
}
257+
if info.EmulationMajor != test.expectedEmulationMajor {
258+
t.Errorf("expected emulation major %s, got %s", test.expectedEmulationMajor, info.EmulationMajor)
259+
}
260+
if info.EmulationMinor != test.expectedEmulationMinor {
261+
t.Errorf("expected emulation minor %s, got %s", test.expectedEmulationMinor, info.EmulationMinor)
262+
}
263+
if info.MinCompatibilityMajor != test.expectedMinCompatibilityMajor {
264+
t.Errorf("expected min compatibility major %s, got %s", test.expectedMinCompatibilityMajor, info.MinCompatibilityMajor)
265+
}
266+
if info.MinCompatibilityMinor != test.expectedMinCompatibilityMinor {
267+
t.Errorf("expected min compatibility minor %s, got %s", test.expectedMinCompatibilityMinor, info.MinCompatibilityMinor)
259268
}
260269
})
261270
}

0 commit comments

Comments
 (0)