From 0ec6566c68c4e10b0379518bb91fdc4edfce7adc Mon Sep 17 00:00:00 2001 From: Siyuan Zhang Date: Wed, 19 Mar 2025 10:29:42 -0700 Subject: [PATCH] chore: update emulation version help msg. Signed-off-by: Siyuan Zhang --- pkg/controlplane/apiserver/apis.go | 1 + .../apiserver/pkg/server/deleted_kinds.go | 10 +++++++--- .../component-base/compatibility/registry.go | 4 ++-- .../compatibility/registry_test.go | 18 +++++++++--------- .../component-base/compatibility/version.go | 12 +++--------- 5 files changed, 22 insertions(+), 23 deletions(-) diff --git a/pkg/controlplane/apiserver/apis.go b/pkg/controlplane/apiserver/apis.go index 241caa88e51..b164feffa59 100644 --- a/pkg/controlplane/apiserver/apis.go +++ b/pkg/controlplane/apiserver/apis.go @@ -91,6 +91,7 @@ func (s *Server) InstallAPIs(restStorageProviders ...RESTStorageProvider) error // used later in the loop to filter the served resource by those that have expired. resourceExpirationEvaluatorOpts := genericapiserver.ResourceExpirationEvaluatorOptions{ CurrentVersion: s.GenericAPIServer.EffectiveVersion.EmulationVersion(), + Prerelease: s.GenericAPIServer.EffectiveVersion.BinaryVersion().PreRelease(), EmulationForwardCompatible: s.GenericAPIServer.EmulationForwardCompatible, RuntimeConfigEmulationForwardCompatible: s.GenericAPIServer.RuntimeConfigEmulationForwardCompatible, } diff --git a/staging/src/k8s.io/apiserver/pkg/server/deleted_kinds.go b/staging/src/k8s.io/apiserver/pkg/server/deleted_kinds.go index fb48ddc29b2..dd2368f91c1 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/deleted_kinds.go +++ b/staging/src/k8s.io/apiserver/pkg/server/deleted_kinds.go @@ -66,6 +66,9 @@ type ResourceExpirationEvaluator interface { type ResourceExpirationEvaluatorOptions struct { // CurrentVersion is the current version of the apiserver. CurrentVersion *apimachineryversion.Version + // Prerelease holds an optional prerelease portion of the version. + // This is used to determine if the current binary is an alpha. + Prerelease string // EmulationForwardCompatible indicates whether the apiserver should serve resources that are introduced after the current version, // when resources of the same group and resource name but with lower priority are served. EmulationForwardCompatible bool @@ -76,7 +79,8 @@ type ResourceExpirationEvaluatorOptions struct { func NewResourceExpirationEvaluator(currentVersion *apimachineryversion.Version) (ResourceExpirationEvaluator, error) { opts := ResourceExpirationEvaluatorOptions{ - CurrentVersion: currentVersion, + CurrentVersion: apimachineryversion.MajorMinor(currentVersion.Major(), currentVersion.Minor()), + Prerelease: currentVersion.PreRelease(), } return NewResourceExpirationEvaluatorFromOptions(opts) } @@ -94,8 +98,8 @@ func NewResourceExpirationEvaluatorFromOptions(opts ResourceExpirationEvaluatorO } // Only keeps the major and minor versions from input version. ret.currentVersion = apimachineryversion.MajorMinor(currentVersion.Major(), currentVersion.Minor()) - ret.isAlpha = strings.Contains(currentVersion.PreRelease(), "alpha") - ret.isAlphaZero = strings.Contains(currentVersion.PreRelease(), "alpha.0") + ret.isAlpha = strings.Contains(opts.Prerelease, "alpha") + ret.isAlphaZero = strings.Contains(opts.Prerelease, "alpha.0") if envString, ok := os.LookupEnv("KUBE_APISERVER_STRICT_REMOVED_API_HANDLING_IN_ALPHA"); !ok { // do nothing diff --git a/staging/src/k8s.io/component-base/compatibility/registry.go b/staging/src/k8s.io/component-base/compatibility/registry.go index 24d620efd00..f6ac962312b 100644 --- a/staging/src/k8s.io/component-base/compatibility/registry.go +++ b/staging/src/k8s.io/component-base/compatibility/registry.go @@ -235,8 +235,8 @@ func (r *componentGlobalsRegistry) AddFlags(fs *pflag.FlagSet) { fs.StringSliceVar(&r.emulationVersionConfig, "emulated-version", r.emulationVersionConfig, ""+ "The versions different components emulate their capabilities (APIs, features, ...) of.\n"+ "If set, the component will emulate the behavior of this version instead of the underlying binary version.\n"+ - "Version format could only be major.minor, for example: '--emulated-version=wardle=1.2,kube=1.31'. Options are:\n"+strings.Join(r.unsafeVersionFlagOptions(true), "\n")+ - "If the component is not specified, defaults to \"kube\"") + "Version format could only be major.minor, for example: '--emulated-version=wardle=1.2,kube=1.31'.\nOptions are: "+strings.Join(r.unsafeVersionFlagOptions(true), ",")+ + "\nIf the component is not specified, defaults to \"kube\"") fs.Var(cliflag.NewColonSeparatedMultimapStringStringAllowDefaultEmptyKey(&r.featureGatesConfig), "feature-gates", "Comma-separated list of component:key=value pairs that describe feature gates for alpha/experimental features of different components.\n"+ "If the component is not specified, defaults to \"kube\". This flag can be repeatedly invoked. For example: --feature-gates 'wardle:featureA=true,wardle:featureB=false' --feature-gates 'kube:featureC=true'"+ diff --git a/staging/src/k8s.io/component-base/compatibility/registry_test.go b/staging/src/k8s.io/component-base/compatibility/registry_test.go index 28a456e6227..dc073f1df43 100644 --- a/staging/src/k8s.io/component-base/compatibility/registry_test.go +++ b/staging/src/k8s.io/component-base/compatibility/registry_test.go @@ -57,7 +57,7 @@ func TestEffectiveVersionRegistry(t *testing.T) { func testRegistry(t *testing.T) *componentGlobalsRegistry { r := NewComponentGlobalsRegistry() - verKube := NewEffectiveVersionFromString("1.31", "1.31", "1.30") + verKube := NewEffectiveVersionFromString("1.31.1-beta.0.353", "1.31", "1.30") fgKube := featuregate.NewVersionedFeatureGate(version.MustParse("0.0")) err := fgKube.AddVersioned(map[featuregate.Feature]featuregate.VersionedSpecs{ "kubeA": { @@ -103,13 +103,13 @@ func testRegistry(t *testing.T) *componentGlobalsRegistry { func TestVersionFlagOptions(t *testing.T) { r := testRegistry(t) - emuVers := strings.Join(r.unsafeVersionFlagOptions(true), "\n") - expectedEmuVers := "kube=1.31..1.31 (default=1.31)\ntest=2.8..2.8 (default=2.8)" + emuVers := strings.Join(r.unsafeVersionFlagOptions(true), ",") + expectedEmuVers := "kube=1.31..1.31(default:1.31),test=2.8..2.8(default:2.8)" if emuVers != expectedEmuVers { t.Errorf("wanted emulation version flag options to be: %s, got %s", expectedEmuVers, emuVers) } - minCompVers := strings.Join(r.unsafeVersionFlagOptions(false), "\n") - expectedMinCompVers := "kube=1.30..1.31 (default=1.30)\ntest=2.7..2.8 (default=2.7)" + minCompVers := strings.Join(r.unsafeVersionFlagOptions(false), ",") + expectedMinCompVers := "kube=1.30..1.31(default:1.30),test=2.7..2.8(default:2.7)" if minCompVers != expectedMinCompVers { t.Errorf("wanted min compatibility version flag options to be: %s, got %s", expectedMinCompVers, minCompVers) } @@ -119,13 +119,13 @@ func TestVersionFlagOptionsWithMapping(t *testing.T) { r := testRegistry(t) utilruntime.Must(r.SetEmulationVersionMapping(testComponent, DefaultKubeComponent, func(from *version.Version) *version.Version { return version.MajorMinor(1, from.Minor()+23) })) - emuVers := strings.Join(r.unsafeVersionFlagOptions(true), "\n") - expectedEmuVers := "test=2.8..2.8 (default=2.8)" + emuVers := strings.Join(r.unsafeVersionFlagOptions(true), ",") + expectedEmuVers := "test=2.8..2.8(default:2.8)" if emuVers != expectedEmuVers { t.Errorf("wanted emulation version flag options to be: %s, got %s", expectedEmuVers, emuVers) } - minCompVers := strings.Join(r.unsafeVersionFlagOptions(false), "\n") - expectedMinCompVers := "kube=1.30..1.31 (default=1.30)\ntest=2.7..2.8 (default=2.7)" + minCompVers := strings.Join(r.unsafeVersionFlagOptions(false), ",") + expectedMinCompVers := "kube=1.30..1.31(default:1.30),test=2.7..2.8(default:2.7)" if minCompVers != expectedMinCompVers { t.Errorf("wanted min compatibility version flag options to be: %s, got %s", expectedMinCompVers, minCompVers) } diff --git a/staging/src/k8s.io/component-base/compatibility/version.go b/staging/src/k8s.io/component-base/compatibility/version.go index 29e33ce37a9..331375ff4c2 100644 --- a/staging/src/k8s.io/component-base/compatibility/version.go +++ b/staging/src/k8s.io/component-base/compatibility/version.go @@ -79,13 +79,7 @@ func (m *effectiveVersion) BinaryVersion() *version.Version { } func (m *effectiveVersion) EmulationVersion() *version.Version { - ver := m.emulationVersion.Load() - if ver != nil { - // Emulation version can have "alpha" as pre-release to continue serving expired apis while we clean up the test. - // The pre-release should not be accessible to the users. - return ver.WithPreRelease(m.BinaryVersion().PreRelease()) - } - return ver + return m.emulationVersion.Load() } func (m *effectiveVersion) MinCompatibilityVersion() *version.Version { @@ -140,7 +134,7 @@ func (m *effectiveVersion) AllowedEmulationVersionRange() string { floor = version.MajorMinor(0, 0) } - return fmt.Sprintf("%s..%s (default=%s)", floor.String(), binaryVersion.String(), m.EmulationVersion().String()) + return fmt.Sprintf("%s..%s(default:%s)", floor.String(), binaryVersion.String(), m.EmulationVersion().String()) } func (m *effectiveVersion) AllowedMinCompatibilityVersionRange() string { @@ -157,7 +151,7 @@ func (m *effectiveVersion) AllowedMinCompatibilityVersionRange() string { floor = version.MajorMinor(0, 0) } - return fmt.Sprintf("%s..%s (default=%s)", floor.String(), binaryVersion.String(), m.MinCompatibilityVersion().String()) + return fmt.Sprintf("%s..%s(default:%s)", floor.String(), binaryVersion.String(), m.MinCompatibilityVersion().String()) } func (m *effectiveVersion) Validate() []error {