| 1 |
/* |
| 2 |
Copyright 2025 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package compatibility |
| 18 |
|
| 19 |
import ( |
| 20 |
"k8s.io/apimachinery/pkg/util/version" |
| 21 |
basecompatibility "k8s.io/component-base/compatibility" |
| 22 |
baseversion "k8s.io/component-base/version" |
| 23 |
) |
| 24 |
|
| 25 |
// minimumKubeEmulationVersion is the first release emulation version is introduced, |
| 26 |
// so the emulation version cannot go lower than that. |
| 27 |
var minimumKubeEmulationVersion *version.Version = version.MajorMinor(1, 31) |
| 28 |
|
| 29 |
// DefaultBuildEffectiveVersion returns the MutableEffectiveVersion based on the |
| 30 |
// current build information. |
| 31 |
func DefaultBuildEffectiveVersion() basecompatibility.MutableEffectiveVersion { |
| 32 |
binaryVersion := defaultBuildBinaryVersion() |
| 33 |
useDefaultBuildBinaryVersion := true |
| 34 |
// fall back to the hard coded kube version only when the git tag is not available for local unit tests. |
| 35 |
if binaryVersion.Major() == 0 && binaryVersion.Minor() == 0 { |
| 36 |
useDefaultBuildBinaryVersion = false |
| 37 |
binaryVersion = version.MustParse(baseversion.DefaultKubeBinaryVersion) |
| 38 |
} |
| 39 |
versionFloor := kubeEffectiveVersionFloors(binaryVersion) |
| 40 |
return basecompatibility.NewEffectiveVersion(binaryVersion, useDefaultBuildBinaryVersion, versionFloor, versionFloor) |
| 41 |
} |
| 42 |
|
| 43 |
func kubeEffectiveVersionFloors(binaryVersion *version.Version) *version.Version { |
| 44 |
// both emulationVersion and minCompatibilityVersion can be set to binaryVersion - 3 |
| 45 |
versionFloor := binaryVersion.WithPatch(0).SubtractMinor(3) |
| 46 |
if versionFloor.LessThan(minimumKubeEmulationVersion) { |
| 47 |
versionFloor = minimumKubeEmulationVersion |
| 48 |
} |
| 49 |
return versionFloor |
| 50 |
} |
| 51 |
|
| 52 |
// DefaultKubeEffectiveVersionForTest returns the MutableEffectiveVersion based on the |
| 53 |
// latest K8s release hardcoded in DefaultKubeBinaryVersion. |
| 54 |
// DefaultKubeBinaryVersion is hard coded because defaultBuildBinaryVersion would return 0.0 when test is run without a git tag. |
| 55 |
// 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. |
| 56 |
// Only used in tests. |
| 57 |
func DefaultKubeEffectiveVersionForTest() basecompatibility.MutableEffectiveVersion { |
| 58 |
binaryVersion := version.MustParse(baseversion.DefaultKubeBinaryVersion) |
| 59 |
return basecompatibility.NewEffectiveVersion(binaryVersion, false, version.MustParse("0.0"), version.MustParse("0.0")) |
| 60 |
} |
| 61 |
|
| 62 |
func defaultBuildBinaryVersion() *version.Version { |
| 63 |
verInfo := baseversion.Get() |
| 64 |
return version.MustParse(verInfo.String()) |
| 65 |
} |