Skip to content

Commit

Permalink
Restore honoring --version build ID overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
liggitt committed Aug 14, 2024
1 parent 8991b8e commit 906d157
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
14 changes: 14 additions & 0 deletions hack/make-rules/test-cmd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ function run_kube_apiserver() {
# Enable features
ENABLE_FEATURE_GATES=""

VERSION_OVERRIDE=""
if [[ "${CUSTOM_VERSION_SUFFIX:-}" != "" ]]; then
VERSION_OVERRIDE="--version=$("${THIS_PLATFORM_BIN}/kube-apiserver" --version | awk '{print $2}')${CUSTOM_VERSION_SUFFIX:-}"
fi

"${THIS_PLATFORM_BIN}/kube-apiserver" \
${VERSION_OVERRIDE:+"${VERSION_OVERRIDE}"} \
--bind-address="127.0.0.1" \
--authorization-mode="${AUTHORIZATION_MODE}" \
--secure-port="${SECURE_API_PORT}" \
Expand Down Expand Up @@ -185,6 +191,14 @@ fi
kube::log::status "Running kubectl tests for kube-apiserver"

setup

# Test custom version invocation
CUSTOM_VERSION_SUFFIX=-custom run_kube_apiserver
kube::test::if_has_string "$(kubectl get --raw /version)" "gitVersion.*-custom"
kill "${APISERVER_PID}" 1>&2 2>/dev/null
wait "${APISERVER_PID}" || true
unset APISERVER_PID

run_kube_apiserver
run_kube_controller_manager
create_node
Expand Down
26 changes: 19 additions & 7 deletions staging/src/k8s.io/apiserver/pkg/util/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type MutableEffectiveVersion interface {
}

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

func (m *effectiveVersion) BinaryVersion() *version.Version {
if m.useDefaultBuildBinaryVersion.Load() {
return defaultBuildBinaryVersion()
}
return m.binaryVersion.Load()
}

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

func (m *effectiveVersion) Set(binaryVersion, emulationVersion, minCompatibilityVersion *version.Version) {
m.binaryVersion.Store(binaryVersion)
m.useDefaultBuildBinaryVersion.Store(false)
m.emulationVersion.Store(majorMinor(emulationVersion))
m.minCompatibilityVersion.Store(majorMinor(minCompatibilityVersion))
}
Expand All @@ -104,7 +111,7 @@ func (m *effectiveVersion) SetMinCompatibilityVersion(minCompatibilityVersion *v
func (m *effectiveVersion) Validate() []error {
var errs []error
// Validate only checks the major and minor versions.
binaryVersion := m.binaryVersion.Load().WithPatch(0)
binaryVersion := m.BinaryVersion().WithPatch(0)
emulationVersion := m.emulationVersion.Load()
minCompatibilityVersion := m.minCompatibilityVersion.Load()

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

func newEffectiveVersion(binaryVersion *version.Version) MutableEffectiveVersion {
func newEffectiveVersion(binaryVersion *version.Version, useDefaultBuildBinaryVersion bool) MutableEffectiveVersion {
effective := &effectiveVersion{}
compatVersion := binaryVersion.SubtractMinor(1)
effective.Set(binaryVersion, binaryVersion, compatVersion)
effective.useDefaultBuildBinaryVersion.Store(useDefaultBuildBinaryVersion)
return effective
}

Expand All @@ -135,25 +143,29 @@ func NewEffectiveVersion(binaryVer string) MutableEffectiveVersion {
return &effectiveVersion{}
}
binaryVersion := version.MustParse(binaryVer)
return newEffectiveVersion(binaryVersion)
return newEffectiveVersion(binaryVersion, false)
}

func defaultBuildBinaryVersion() *version.Version {
verInfo := baseversion.Get()
return version.MustParse(verInfo.String()).WithInfo(verInfo)
}

// DefaultBuildEffectiveVersion returns the MutableEffectiveVersion based on the
// current build information.
func DefaultBuildEffectiveVersion() MutableEffectiveVersion {
verInfo := baseversion.Get()
binaryVersion := version.MustParse(verInfo.String()).WithInfo(verInfo)
binaryVersion := defaultBuildBinaryVersion()
if binaryVersion.Major() == 0 && binaryVersion.Minor() == 0 {
return DefaultKubeEffectiveVersion()
}
return newEffectiveVersion(binaryVersion)
return newEffectiveVersion(binaryVersion, true)
}

// DefaultKubeEffectiveVersion returns the MutableEffectiveVersion based on the
// latest K8s release.
func DefaultKubeEffectiveVersion() MutableEffectiveVersion {
binaryVersion := version.MustParse(baseversion.DefaultKubeBinaryVersion).WithInfo(baseversion.Get())
return newEffectiveVersion(binaryVersion)
return newEffectiveVersion(binaryVersion, false)
}

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

0 comments on commit 906d157

Please sign in to comment.