Skip to content

Commit 69dbf2e

Browse files
authored
Merge pull request kubernetes#126665 from liggitt/version-build-id
Restore honoring --version build ID overrides
2 parents 03e8154 + c181912 commit 69dbf2e

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

hack/make-rules/test-cmd.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ function run_kube_apiserver() {
6464
# Enable features
6565
ENABLE_FEATURE_GATES=""
6666

67+
VERSION_OVERRIDE=""
68+
if [[ "${CUSTOM_VERSION_SUFFIX:-}" != "" ]]; then
69+
VERSION_OVERRIDE="--version=$("${THIS_PLATFORM_BIN}/kube-apiserver" --version | awk '{print $2}')${CUSTOM_VERSION_SUFFIX:-}"
70+
fi
71+
6772
"${THIS_PLATFORM_BIN}/kube-apiserver" \
73+
${VERSION_OVERRIDE:+"${VERSION_OVERRIDE}"} \
6874
--bind-address="127.0.0.1" \
6975
--authorization-mode="${AUTHORIZATION_MODE}" \
7076
--secure-port="${SECURE_API_PORT}" \
@@ -185,6 +191,14 @@ fi
185191
kube::log::status "Running kubectl tests for kube-apiserver"
186192

187193
setup
194+
195+
# Test custom version invocation
196+
CUSTOM_VERSION_SUFFIX=-custom run_kube_apiserver
197+
kube::test::if_has_string "$(kubectl get --raw /version)" "gitVersion.*-custom"
198+
kill "${APISERVER_PID}" 1>&2 2>/dev/null
199+
wait "${APISERVER_PID}" || true
200+
unset APISERVER_PID
201+
188202
run_kube_apiserver
189203
run_kube_controller_manager
190204
create_node

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ type MutableEffectiveVersion interface {
4141
}
4242

4343
type effectiveVersion struct {
44+
// When true, BinaryVersion() returns the current binary version
45+
useDefaultBuildBinaryVersion atomic.Bool
46+
// Holds the last binary version stored in Set()
4447
binaryVersion atomic.Pointer[version.Version]
4548
// If the emulationVersion is set by the users, it could only contain major and minor versions.
4649
// In tests, emulationVersion could be the same as the binary version, or set directly,
@@ -51,6 +54,9 @@ type effectiveVersion struct {
5154
}
5255

5356
func (m *effectiveVersion) BinaryVersion() *version.Version {
57+
if m.useDefaultBuildBinaryVersion.Load() {
58+
return defaultBuildBinaryVersion()
59+
}
5460
return m.binaryVersion.Load()
5561
}
5662

@@ -89,6 +95,7 @@ func majorMinor(ver *version.Version) *version.Version {
8995

9096
func (m *effectiveVersion) Set(binaryVersion, emulationVersion, minCompatibilityVersion *version.Version) {
9197
m.binaryVersion.Store(binaryVersion)
98+
m.useDefaultBuildBinaryVersion.Store(false)
9299
m.emulationVersion.Store(majorMinor(emulationVersion))
93100
m.minCompatibilityVersion.Store(majorMinor(minCompatibilityVersion))
94101
}
@@ -104,7 +111,7 @@ func (m *effectiveVersion) SetMinCompatibilityVersion(minCompatibilityVersion *v
104111
func (m *effectiveVersion) Validate() []error {
105112
var errs []error
106113
// Validate only checks the major and minor versions.
107-
binaryVersion := m.binaryVersion.Load().WithPatch(0)
114+
binaryVersion := m.BinaryVersion().WithPatch(0)
108115
emulationVersion := m.emulationVersion.Load()
109116
minCompatibilityVersion := m.minCompatibilityVersion.Load()
110117

@@ -123,10 +130,11 @@ func (m *effectiveVersion) Validate() []error {
123130
return errs
124131
}
125132

126-
func newEffectiveVersion(binaryVersion *version.Version) MutableEffectiveVersion {
133+
func newEffectiveVersion(binaryVersion *version.Version, useDefaultBuildBinaryVersion bool) MutableEffectiveVersion {
127134
effective := &effectiveVersion{}
128135
compatVersion := binaryVersion.SubtractMinor(1)
129136
effective.Set(binaryVersion, binaryVersion, compatVersion)
137+
effective.useDefaultBuildBinaryVersion.Store(useDefaultBuildBinaryVersion)
130138
return effective
131139
}
132140

@@ -135,25 +143,29 @@ func NewEffectiveVersion(binaryVer string) MutableEffectiveVersion {
135143
return &effectiveVersion{}
136144
}
137145
binaryVersion := version.MustParse(binaryVer)
138-
return newEffectiveVersion(binaryVersion)
146+
return newEffectiveVersion(binaryVersion, false)
147+
}
148+
149+
func defaultBuildBinaryVersion() *version.Version {
150+
verInfo := baseversion.Get()
151+
return version.MustParse(verInfo.String()).WithInfo(verInfo)
139152
}
140153

141154
// DefaultBuildEffectiveVersion returns the MutableEffectiveVersion based on the
142155
// current build information.
143156
func DefaultBuildEffectiveVersion() MutableEffectiveVersion {
144-
verInfo := baseversion.Get()
145-
binaryVersion := version.MustParse(verInfo.String()).WithInfo(verInfo)
157+
binaryVersion := defaultBuildBinaryVersion()
146158
if binaryVersion.Major() == 0 && binaryVersion.Minor() == 0 {
147159
return DefaultKubeEffectiveVersion()
148160
}
149-
return newEffectiveVersion(binaryVersion)
161+
return newEffectiveVersion(binaryVersion, true)
150162
}
151163

152164
// DefaultKubeEffectiveVersion returns the MutableEffectiveVersion based on the
153165
// latest K8s release.
154166
func DefaultKubeEffectiveVersion() MutableEffectiveVersion {
155167
binaryVersion := version.MustParse(baseversion.DefaultKubeBinaryVersion).WithInfo(baseversion.Get())
156-
return newEffectiveVersion(binaryVersion)
168+
return newEffectiveVersion(binaryVersion, false)
157169
}
158170

159171
// ValidateKubeEffectiveVersion validates the EmulationVersion is equal to the binary version at 1.31 for kube components.

0 commit comments

Comments
 (0)