Skip to content

Commit 41dd2d0

Browse files
committed
refactor: extract OpenShift version detection to a method
1 parent f2ccca3 commit 41dd2d0

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

pkg/featuregates/featuregates.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import (
55
"github.com/openshift/api/features"
66
)
77

8+
// StubOpenShiftVersion is the default OpenShift version placeholder for the purpose of determining
9+
// enabled and disabled CVO feature gates. It is assumed to never conflict with a real OpenShift
10+
// version. Both DefaultCvoGates and CvoGatesFromFeatureGate should return a safe conservative
11+
// default set of enabled and disabled gates, typically with unknownVersion set to true.
12+
const StubOpenShiftVersion = "0.0.1-snapshot"
13+
814
// CvoGateChecker allows CVO code to check which feature gates are enabled
915
type CvoGateChecker interface {
1016
// UnknownVersion flag is set to true if CVO did not find a matching version in the FeatureGate

pkg/start/start.go

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -189,31 +189,7 @@ func (o *Options) Run(ctx context.Context) error {
189189
return err
190190
}
191191

192-
payloadRoot := payload.DefaultRootPath
193-
if o.PayloadOverride != "" {
194-
payloadRoot = payload.RootPath(o.PayloadOverride)
195-
}
196-
197-
cvoOpenShiftVersion := "0.0.1-snapshot"
198-
// Peek at the local release metadata to determine the version of OpenShift this CVO belongs to. This assumes the CVO is
199-
// executing in a container from the payload image. Full payload content is only read later once leader lease is
200-
// acquired, and here we should only read as little data as possible to determine the version so we can establish
201-
// enabled feature gate checker for all following code.
202-
//
203-
// We cannot refuse to start CVO if for some reason we cannot determine the OpenShift version on startup from the local
204-
// release metadata. The only consequence is we fail to determine enabled/disabled feature gates and will have to use
205-
// some defaults.
206-
releaseMetadata, err := payloadRoot.LoadReleaseMetadata()
207-
switch {
208-
case err != nil:
209-
klog.Warningf("Failed to read release metadata to determine OpenShift version for this CVO (will use placeholder version %q): %v", cvoOpenShiftVersion, err)
210-
case releaseMetadata.Version == "":
211-
klog.Warningf("Version missing from release metadata, cannot determine OpenShift version for this CVO (will use placeholder version %q)", cvoOpenShiftVersion)
212-
default:
213-
cvoOpenShiftVersion = releaseMetadata.Version
214-
klog.Infof("Determined OpenShift version for this CVO: %q", cvoOpenShiftVersion)
215-
}
216-
192+
_ = o.getOpenShiftVersion()
217193
clusterVersionConfigInformerFactory, configInformerFactory := o.prepareConfigInformerFactories(cb)
218194

219195
// initialize the controllers and attempt to load the payload information
@@ -237,6 +213,35 @@ func (o *Options) prepareConfigInformerFactories(cb *ClientBuilder) (configinfor
237213
return clusterVersionConfigInformerFactory, configInformerFactory
238214
}
239215

216+
// getOpenShiftVersion peeks at the local release metadata to determine the version of OpenShift this CVO belongs to.
217+
// This assumes the CVO is executing in a container from the payload image. This does not and should not fully load
218+
// whole payload content, that is only loaded later once leader lease is acquired. Here we should only read as little
219+
// data as possible to determine the version so we can establish enabled feature gate checker for all following code.
220+
func (o *Options) getOpenShiftVersion() string {
221+
payloadRoot := payload.DefaultRootPath
222+
if o.PayloadOverride != "" {
223+
payloadRoot = payload.RootPath(o.PayloadOverride)
224+
}
225+
226+
// We cannot refuse to start CVO if for some reason we cannot determine the OpenShift version on startup from the local
227+
// release metadata. The only consequence is we fail to determine enabled/disabled feature gates and will have to use
228+
// some defaults.
229+
releaseMetadata, err := payloadRoot.LoadReleaseMetadata()
230+
if err != nil {
231+
klog.Warningf("Failed to read release metadata to determine OpenShift version for this CVO (will use placeholder version %q): %v", featuregates.StubOpenShiftVersion, err)
232+
return featuregates.StubOpenShiftVersion
233+
}
234+
235+
if releaseMetadata.Version == "" {
236+
klog.Warningf("Version missing from release metadata, cannot determine OpenShift version for this CVO (will use placeholder version %q)", featuregates.StubOpenShiftVersion)
237+
return featuregates.StubOpenShiftVersion
238+
}
239+
240+
klog.Infof("Determined OpenShift version for this CVO: %q", releaseMetadata.Version)
241+
return releaseMetadata.Version
242+
243+
}
244+
240245
// run launches a number of goroutines to handle manifest application,
241246
// metrics serving, etc. It continues operating until ctx.Done(),
242247
// and then attempts a clean shutdown limited by an internal context

0 commit comments

Comments
 (0)