Skip to content

Commit d2a0caa

Browse files
update feature gating changes section
Signed-off-by: Siyuan Zhang <[email protected]>
1 parent 219e3d6 commit d2a0caa

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

keps/sig-architecture/4330-compatibility-versions/README.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,9 @@ compatibility version support.
324324
#### Feature gating changes
325325
326326
In order to preserve the behavior of in-development features across multiple releases,
327-
feature implementations may also be gated by version number.
327+
feature implementation history should also be preserved in the code base.
328328
329+
Naively, the feature implementations can be gated by version number.
329330
For example, if `FeatureA` is partially implemented in 1.28 and additional functionality
330331
is added in 1.29, the feature developer is expected to gate the functionality by version.
331332
E.g.:
@@ -335,6 +336,74 @@ if feature_gate.Enabled(FeatureA) && feature_gate.CompatibilityVersion() <= "1.2
335336
if feature_gate.Enabled(FeatureA) && feature_gate.CompatibilityVersion() >= "1.29" {implementation 2}
336337
```
337338
339+
A better way might be to define a `featureOptions` struct constructed based on the the feature gate, and have the `featureOptions` control the main code flow, so that the main code is version agnostic.
340+
E.g.:
341+
```go
342+
// in kube_features.go
343+
const (
344+
FeatureA featuregate.Feature = "FeatureA"
345+
FeatureB featuregate.Feature = "FeatureB"
346+
)
347+
348+
func init() {
349+
utilfeature.DefaultMutableFeatureGate.AddVersioned(defaultKubernetesFeatureGates)
350+
}
351+
352+
var defaultKubernetesFeatureGates = map[Feature]VersionedSpecs{
353+
featureA: VersionedSpecs{
354+
{Version: mustParseVersion("1.27"), Default: false, PreRelease: Alpha},
355+
},
356+
featureB: VersionedSpecs{
357+
{Version: mustParseVersion("1.28"), Default: false, PreRelease: Alpha},
358+
{Version: mustParseVersion("1.30"), Default: true, PreRelease: Beta},
359+
},
360+
}
361+
362+
type featureOptions struct {
363+
AEnabled bool
364+
AHasCapabilityZ bool
365+
BEnabled bool
366+
BHandler func()
367+
}
368+
369+
func newFeatureOptions(featureGate FeatureGate) featureOptions {
370+
opts := featureOptions{}
371+
if featureGate.Enabled(FeatureA) {
372+
opts.AEnabled = true
373+
}
374+
if featureGate.CompatibilityVersion() > "1.29" {
375+
opts.AHasCapabilityZ = true
376+
}
377+
378+
if featureGate.Enabled(FeatureB) {
379+
opts.BEnabled = true
380+
}
381+
if featureGate.CompatibilityVersion() > "1.28" {
382+
opts.BHandler = newHandler
383+
} else {
384+
opts.BHandler = oldHandler
385+
}
386+
return opts
387+
}
388+
389+
// in client.go
390+
func ClientFunction() {
391+
// ...
392+
featureOpts := newFeatureOptions(utilfeature.DefaultFeatureGate)
393+
if featureOpts.AEnabled {
394+
// ...
395+
if featureOpts.AHasCapabilityZ {
396+
// run CapabilityZ
397+
}
398+
}
399+
if featureOpts.BEnabled {
400+
featureOpts.BHandler()
401+
}
402+
// ...
403+
}
404+
405+
```
406+
338407
### CEL Environment Compatibility Versioning
339408
340409
CEL environments already [support a compatibility

keps/sig-architecture/4330-compatibility-versions/kep.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ authors:
44
- "@logicalhan"
55
- "@jpbetz"
66
- "@liggitt"
7+
- "@siyuanfoundation"
78
owning-sig: sig-api-machinery
89
participating-sigs:
910
- sig-architecture

0 commit comments

Comments
 (0)