Skip to content

Commit 4352c4a

Browse files
Add version mapping in ComponentGlobalsRegistry.
Signed-off-by: Siyuan Zhang <[email protected]>
1 parent 701e5fc commit 4352c4a

File tree

32 files changed

+853
-409
lines changed

32 files changed

+853
-409
lines changed

cmd/kube-apiserver/app/options/options_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestAddFlags(t *testing.T) {
5353
featureGate := featuregate.NewFeatureGate()
5454
componentRegistry := utilversion.NewComponentGlobalsRegistry()
5555
effectiveVersion := utilversion.NewEffectiveVersion("1.32")
56-
_ = componentRegistry.Register("test", effectiveVersion, featureGate, true)
56+
utilruntime.Must(componentRegistry.Register("test", effectiveVersion, featureGate))
5757
s := NewServerRunOptions(featureGate, effectiveVersion)
5858
for _, f := range s.Flags().FlagSets {
5959
fs.AddFlagSet(f)

cmd/kube-apiserver/app/testing/testserver.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"k8s.io/apimachinery/pkg/api/errors"
4444
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4545
utilerrors "k8s.io/apimachinery/pkg/util/errors"
46+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
4647
"k8s.io/apimachinery/pkg/util/wait"
4748
serveroptions "k8s.io/apiserver/pkg/server/options"
4849
"k8s.io/apiserver/pkg/storage/storagebackend"
@@ -182,12 +183,12 @@ func StartTestServer(t ktesting.TB, instanceOptions *TestServerInstanceOptions,
182183
fs := pflag.NewFlagSet("test", pflag.PanicOnError)
183184

184185
featureGate := utilfeature.DefaultMutableFeatureGate
185-
binaryVersion := utilversion.DefaultKubeEffectiveVersion().BinaryVersion().String()
186+
effectiveVersion := utilversion.DefaultKubeEffectiveVersion()
186187
if instanceOptions.BinaryVersion != "" {
187-
binaryVersion = instanceOptions.BinaryVersion
188+
effectiveVersion = utilversion.NewEffectiveVersion(instanceOptions.BinaryVersion)
188189
}
189-
effectiveVersion := utilversion.NewEffectiveVersion(binaryVersion)
190-
_ = utilversion.DefaultComponentGlobalsRegistry.Register(utilversion.DefaultKubeComponent, effectiveVersion, featureGate, true)
190+
utilversion.DefaultComponentGlobalsRegistry.Reset()
191+
utilruntime.Must(utilversion.DefaultComponentGlobalsRegistry.Register(utilversion.DefaultKubeComponent, effectiveVersion, featureGate))
191192

192193
s := options.NewServerRunOptions(featureGate, effectiveVersion)
193194

pkg/controlplane/apiserver/config.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import (
4646
clientgoinformers "k8s.io/client-go/informers"
4747
clientgoclientset "k8s.io/client-go/kubernetes"
4848
"k8s.io/client-go/util/keyutil"
49-
"k8s.io/component-base/version"
5049
aggregatorapiserver "k8s.io/kube-aggregator/pkg/apiserver"
5150
openapicommon "k8s.io/kube-openapi/pkg/common"
5251

@@ -172,9 +171,6 @@ func BuildGenericConfig(
172171
sets.NewString("attach", "exec", "proxy", "log", "portforward"),
173172
)
174173

175-
kubeVersion := version.Get()
176-
genericConfig.Version = &kubeVersion
177-
178174
if genericConfig.EgressSelector != nil {
179175
s.Etcd.StorageConfig.Transport.EgressLookup = genericConfig.EgressSelector.Lookup
180176
}

pkg/controlplane/apiserver/options/options_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestAddFlags(t *testing.T) {
4848
featureGate := featuregate.NewFeatureGate()
4949
effectiveVersion := utilversion.NewEffectiveVersion("1.32")
5050
componentRegistry := utilversion.NewComponentGlobalsRegistry()
51-
_ = componentRegistry.Register("test", effectiveVersion, featureGate, true)
51+
utilruntime.Must(componentRegistry.Register("test", effectiveVersion, featureGate))
5252
s := NewOptions(featureGate, effectiveVersion)
5353
var fss cliflag.NamedFlagSets
5454
s.AddFlags(&fss)

pkg/controlplane/instance_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"crypto/tls"
2222
"encoding/json"
23+
"fmt"
2324
"io"
2425
"net"
2526
"net/http"
@@ -118,9 +119,7 @@ func setUp(t *testing.T) (*etcd3testing.EtcdTestServer, Config, *assert.Assertio
118119
t.Fatal(err)
119120
}
120121

121-
kubeVersion := kubeversion.Get()
122122
config.ControlPlane.Generic.Authorization.Authorizer = authorizerfactory.NewAlwaysAllowAuthorizer()
123-
config.ControlPlane.Generic.Version = &kubeVersion
124123
config.ControlPlane.StorageFactory = storageFactory
125124
config.ControlPlane.Generic.LoopbackClientConfig = &restclient.Config{APIPath: "/api", ContentConfig: restclient.ContentConfig{NegotiatedSerializer: legacyscheme.Codecs}}
126125
config.ControlPlane.Generic.PublicAddress = netutils.ParseIPSloppy("192.168.10.4")
@@ -243,9 +242,13 @@ func TestVersion(t *testing.T) {
243242
if err != nil {
244243
t.Errorf("unexpected error: %v", err)
245244
}
245+
expectedInfo := kubeversion.Get()
246+
kubeVersion := utilversion.DefaultKubeEffectiveVersion().BinaryVersion()
247+
expectedInfo.Major = fmt.Sprintf("%d", kubeVersion.Major())
248+
expectedInfo.Minor = fmt.Sprintf("%d", kubeVersion.Minor())
246249

247-
if !reflect.DeepEqual(kubeversion.Get(), info) {
248-
t.Errorf("Expected %#v, Got %#v", kubeversion.Get(), info)
250+
if !reflect.DeepEqual(expectedInfo, info) {
251+
t.Errorf("Expected %#v, Got %#v", expectedInfo, info)
249252
}
250253
}
251254

staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/apiserver.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import (
4040
"k8s.io/apimachinery/pkg/runtime/schema"
4141
"k8s.io/apimachinery/pkg/runtime/serializer"
4242
"k8s.io/apimachinery/pkg/util/wait"
43-
"k8s.io/apimachinery/pkg/version"
4443
"k8s.io/apiserver/pkg/endpoints/discovery"
4544
"k8s.io/apiserver/pkg/endpoints/discovery/aggregated"
4645
genericregistry "k8s.io/apiserver/pkg/registry/generic"
@@ -118,12 +117,6 @@ func (cfg *Config) Complete() CompletedConfig {
118117
}
119118

120119
c.GenericConfig.EnableDiscovery = false
121-
if c.GenericConfig.Version == nil {
122-
c.GenericConfig.Version = &version.Info{
123-
Major: "0",
124-
Minor: "1",
125-
}
126-
}
127120

128121
return CompletedConfig{&c}
129122
}

staging/src/k8s.io/apiextensions-apiserver/pkg/cmd/server/testing/testserver.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
extensionsapiserver "k8s.io/apiextensions-apiserver/pkg/apiserver"
3232
"k8s.io/apiextensions-apiserver/pkg/cmd/server/options"
3333
generatedopenapi "k8s.io/apiextensions-apiserver/pkg/generated/openapi"
34+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3435
"k8s.io/apimachinery/pkg/util/wait"
3536
openapinamer "k8s.io/apiserver/pkg/endpoints/openapi"
3637
genericapiserver "k8s.io/apiserver/pkg/server"
@@ -124,7 +125,8 @@ func StartTestServer(t Logger, _ *TestServerInstanceOptions, customFlags []strin
124125

125126
featureGate := utilfeature.DefaultMutableFeatureGate
126127
effectiveVersion := utilversion.DefaultKubeEffectiveVersion()
127-
_ = utilversion.DefaultComponentGlobalsRegistry.Register(utilversion.DefaultKubeComponent, effectiveVersion, featureGate, true)
128+
utilversion.DefaultComponentGlobalsRegistry.Reset()
129+
utilruntime.Must(utilversion.DefaultComponentGlobalsRegistry.Register(utilversion.DefaultKubeComponent, effectiveVersion, featureGate))
128130
s := options.NewCustomResourceDefinitionsServerOptions(os.Stdout, os.Stderr, featureGate, effectiveVersion)
129131

130132
utilversion.DefaultComponentGlobalsRegistry.AddFlags(fs)

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

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"regexp"
2424
"strconv"
2525
"strings"
26+
27+
apimachineryversion "k8s.io/apimachinery/pkg/version"
2628
)
2729

2830
// Version is an opaque representation of a version number
@@ -31,6 +33,7 @@ type Version struct {
3133
semver bool
3234
preRelease string
3335
buildMetadata string
36+
info apimachineryversion.Info
3437
}
3538

3639
var (
@@ -252,19 +255,30 @@ func (v *Version) WithMinor(minor uint) *Version {
252255
return &result
253256
}
254257

255-
// SubtractMinor returns the version diff minor versions back, with the same major and no patch.
256-
// If diff >= current minor, the minor would be 0.
257-
func (v *Version) SubtractMinor(diff uint) *Version {
258+
// SubtractMinor returns the version with offset from the original minor, with the same major and no patch.
259+
// If -offset >= current minor, the minor would be 0.
260+
func (v *Version) OffsetMinor(offset int) *Version {
258261
var minor uint
259-
if diff < v.Minor() {
260-
minor = v.Minor() - diff
262+
if offset >= 0 {
263+
minor = v.Minor() + uint(offset)
264+
} else {
265+
diff := uint(-offset)
266+
if diff < v.Minor() {
267+
minor = v.Minor() - diff
268+
}
261269
}
262270
return MajorMinor(v.Major(), minor)
263271
}
264272

273+
// SubtractMinor returns the version diff minor versions back, with the same major and no patch.
274+
// If diff >= current minor, the minor would be 0.
275+
func (v *Version) SubtractMinor(diff uint) *Version {
276+
return v.OffsetMinor(-int(diff))
277+
}
278+
265279
// AddMinor returns the version diff minor versions forward, with the same major and no patch.
266280
func (v *Version) AddMinor(diff uint) *Version {
267-
return MajorMinor(v.Major(), v.Minor()+diff)
281+
return v.OffsetMinor(int(diff))
268282
}
269283

270284
// WithPatch returns copy of the version object with requested patch number
@@ -441,3 +455,30 @@ func (v *Version) Compare(other string) (int, error) {
441455
}
442456
return v.compareInternal(ov), nil
443457
}
458+
459+
// WithInfo returns copy of the version object with requested info
460+
func (v *Version) WithInfo(info apimachineryversion.Info) *Version {
461+
result := *v
462+
result.info = info
463+
return &result
464+
}
465+
466+
func (v *Version) Info() *apimachineryversion.Info {
467+
if v == nil {
468+
return nil
469+
}
470+
// 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()
475+
}
476+
return &v.info
477+
}
478+
479+
func itoa(i uint) string {
480+
if i == 0 {
481+
return ""
482+
}
483+
return strconv.Itoa(int(i))
484+
}

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -453,37 +453,42 @@ func TestHighestSupportedVersion(t *testing.T) {
453453
}
454454
}
455455

456-
func TestSubtractMinor(t *testing.T) {
456+
func TestOffsetMinor(t *testing.T) {
457457
var tests = []struct {
458458
version string
459-
diff uint
459+
diff int
460460
expectedComponents []uint
461461
}{
462462
{
463463
version: "1.0.2",
464-
diff: 3,
464+
diff: -3,
465465
expectedComponents: []uint{1, 0},
466466
},
467467
{
468468
version: "1.3.2-alpha+001",
469-
diff: 2,
469+
diff: -2,
470470
expectedComponents: []uint{1, 1},
471471
},
472472
{
473473
version: "1.3.2-alpha+001",
474-
diff: 3,
474+
diff: -3,
475475
expectedComponents: []uint{1, 0},
476476
},
477477
{
478478
version: "1.20",
479-
diff: 5,
479+
diff: -5,
480480
expectedComponents: []uint{1, 15},
481481
},
482+
{
483+
version: "1.20",
484+
diff: 5,
485+
expectedComponents: []uint{1, 25},
486+
},
482487
}
483488

484489
for _, test := range tests {
485490
version, _ := ParseGeneric(test.version)
486-
if !reflect.DeepEqual(test.expectedComponents, version.SubtractMinor(test.diff).Components()) {
491+
if !reflect.DeepEqual(test.expectedComponents, version.OffsetMinor(test.diff).Components()) {
487492
t.Error("parse returned un'expected components")
488493
}
489494
}

staging/src/k8s.io/apiserver/pkg/server/config.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import (
4444
"k8s.io/apimachinery/pkg/util/sets"
4545
"k8s.io/apimachinery/pkg/util/version"
4646
utilwaitgroup "k8s.io/apimachinery/pkg/util/waitgroup"
47-
apimachineryversion "k8s.io/apimachinery/pkg/version"
4847
"k8s.io/apiserver/pkg/admission"
4948
"k8s.io/apiserver/pkg/audit"
5049
"k8s.io/apiserver/pkg/authentication/authenticator"
@@ -150,8 +149,6 @@ type Config struct {
150149
// done values in this values for this map are ignored.
151150
PostStartHooks map[string]PostStartHookConfigEntry
152151

153-
// Version will enable the /version endpoint if non-nil
154-
Version *apimachineryversion.Info
155152
// EffectiveVersion determines which apis and features are available
156153
// based on when the api/feature lifecyle.
157154
EffectiveVersion utilversion.EffectiveVersion
@@ -702,12 +699,8 @@ func (c *Config) Complete(informers informers.SharedInformerFactory) CompletedCo
702699
}
703700
c.ExternalAddress = net.JoinHostPort(c.ExternalAddress, strconv.Itoa(port))
704701
}
705-
var ver *version.Version
706-
if c.EffectiveVersion != nil {
707-
ver = c.EffectiveVersion.EmulationVersion()
708-
}
709-
completeOpenAPI(c.OpenAPIConfig, ver)
710-
completeOpenAPIV3(c.OpenAPIV3Config, ver)
702+
completeOpenAPI(c.OpenAPIConfig, c.EffectiveVersion.EmulationVersion())
703+
completeOpenAPIV3(c.OpenAPIV3Config, c.EffectiveVersion.EmulationVersion())
711704

712705
if c.DiscoveryAddresses == nil {
713706
c.DiscoveryAddresses = discovery.DefaultAddresses{DefaultAddress: c.ExternalAddress}
@@ -834,7 +827,6 @@ func (c completedConfig) New(name string, delegationTarget DelegationTarget) (*G
834827
StorageVersionManager: c.StorageVersionManager,
835828

836829
EffectiveVersion: c.EffectiveVersion,
837-
Version: c.Version,
838830
FeatureGate: c.FeatureGate,
839831

840832
muxAndDiscoveryCompleteSignals: map[string]<-chan struct{}{},
@@ -1103,7 +1095,7 @@ func installAPI(s *GenericAPIServer, c *Config) {
11031095
}
11041096
}
11051097

1106-
routes.Version{Version: c.Version}.Install(s.Handler.GoRestfulContainer)
1098+
routes.Version{Version: c.EffectiveVersion.BinaryVersion().Info()}.Install(s.Handler.GoRestfulContainer)
11071099

11081100
if c.EnableDiscovery {
11091101
if utilfeature.DefaultFeatureGate.Enabled(genericfeatures.AggregatedDiscoveryEndpoint) {

0 commit comments

Comments
 (0)