-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inject feature gate instance into client-go for kube components.
In order to avoid a dependency cycle between component-base and client-go, client-go maintains parallel definitions of component-base's feature types and constants. Passing kube's default feature gate instance to client-go requires an adapter.
- Loading branch information
1 parent
f4aeeb1
commit f0298b3
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package features | ||
|
||
import ( | ||
"fmt" | ||
|
||
clientfeatures "k8s.io/client-go/features" | ||
"k8s.io/component-base/featuregate" | ||
) | ||
|
||
type clientFeatureGateAdapter struct { | ||
mfg featuregate.MutableFeatureGate | ||
} | ||
|
||
func (a *clientFeatureGateAdapter) Enabled(name clientfeatures.Feature) bool { | ||
return a.mfg.Enabled(featuregate.Feature(name)) | ||
} | ||
|
||
func (a *clientFeatureGateAdapter) Add(in map[clientfeatures.Feature]clientfeatures.FeatureSpec) error { | ||
out := map[featuregate.Feature]featuregate.FeatureSpec{} | ||
for name, spec := range in { | ||
converted := featuregate.FeatureSpec{ | ||
Default: spec.Default, | ||
LockToDefault: spec.LockToDefault, | ||
} | ||
switch spec.PreRelease { | ||
case clientfeatures.Alpha: | ||
converted.PreRelease = featuregate.Alpha | ||
case clientfeatures.Beta: | ||
converted.PreRelease = featuregate.Beta | ||
case clientfeatures.GA: | ||
converted.PreRelease = featuregate.GA | ||
case clientfeatures.Deprecated: | ||
converted.PreRelease = featuregate.Deprecated | ||
default: | ||
// The default case implies programmer error. The same set of prerelease | ||
// constants must exist in both component-base and client-go, and each one | ||
// must have a case here. | ||
panic(fmt.Sprintf("unrecognized prerelease %q of feature %q", spec.PreRelease, name)) | ||
} | ||
out[featuregate.Feature(name)] = converted | ||
} | ||
return a.mfg.Add(out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package features | ||
|
||
import ( | ||
"testing" | ||
|
||
clientfeatures "k8s.io/client-go/features" | ||
"k8s.io/component-base/featuregate" | ||
) | ||
|
||
func TestClientAdapterEnabled(t *testing.T) { | ||
fg := featuregate.NewFeatureGate() | ||
if err := fg.Add(map[featuregate.Feature]featuregate.FeatureSpec{ | ||
"Foo": {Default: true}, | ||
}); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
a := clientFeatureGateAdapter{fg} | ||
if !a.Enabled("Foo") { | ||
t.Error("expected Enabled(\"Foo\") to return true") | ||
} | ||
var r interface{} | ||
func() { | ||
defer func() { | ||
r = recover() | ||
}() | ||
a.Enabled("Bar") | ||
}() | ||
if r == nil { | ||
t.Error("expected Enabled(\"Bar\") to panic due to unknown feature name") | ||
} | ||
} | ||
|
||
func TestClientAdapterAdd(t *testing.T) { | ||
fg := featuregate.NewFeatureGate() | ||
a := clientFeatureGateAdapter{fg} | ||
defaults := fg.GetAll() | ||
if err := a.Add(map[clientfeatures.Feature]clientfeatures.FeatureSpec{ | ||
"FeatureAlpha": {PreRelease: clientfeatures.Alpha, Default: true}, | ||
"FeatureBeta": {PreRelease: clientfeatures.Beta, Default: false}, | ||
"FeatureGA": {PreRelease: clientfeatures.GA, Default: true, LockToDefault: true}, | ||
"FeatureDeprecated": {PreRelease: clientfeatures.Deprecated, Default: false, LockToDefault: true}, | ||
}); err != nil { | ||
t.Fatal(err) | ||
} | ||
all := fg.GetAll() | ||
allexpected := map[featuregate.Feature]featuregate.FeatureSpec{ | ||
"FeatureAlpha": {PreRelease: featuregate.Alpha, Default: true}, | ||
"FeatureBeta": {PreRelease: featuregate.Beta, Default: false}, | ||
"FeatureGA": {PreRelease: featuregate.GA, Default: true, LockToDefault: true}, | ||
"FeatureDeprecated": {PreRelease: featuregate.Deprecated, Default: false, LockToDefault: true}, | ||
} | ||
for name, spec := range defaults { | ||
allexpected[name] = spec | ||
} | ||
if len(all) != len(allexpected) { | ||
t.Errorf("expected %d registered features, got %d", len(allexpected), len(all)) | ||
} | ||
for name, expected := range allexpected { | ||
actual, ok := all[name] | ||
if !ok { | ||
t.Errorf("expected feature %q not found", name) | ||
continue | ||
} | ||
|
||
if actual != expected { | ||
t.Errorf("expected feature %q spec %#v, got spec %#v", name, expected, actual) | ||
} | ||
} | ||
|
||
var r interface{} | ||
func() { | ||
defer func() { | ||
r = recover() | ||
}() | ||
_ = a.Add(map[clientfeatures.Feature]clientfeatures.FeatureSpec{ | ||
"FeatureAlpha": {PreRelease: "foobar"}, | ||
}) | ||
}() | ||
if r == nil { | ||
t.Error("expected panic when adding feature with unknown prerelease") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters