Skip to content

Commit a0fc39a

Browse files
committed
Removes dependency on other k8s packages in featuregate pkg.
The removed packages are: * k8s.io/apimachinery/pkg/util/naming * k8s.io/klog/v2 Do note that removing naming package necessitates adding feature gate name argument to featuregate.New().
1 parent c27c5af commit a0fc39a

File tree

4 files changed

+32
-37
lines changed

4 files changed

+32
-37
lines changed

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module go.etcd.io/etcd/v3
22

3-
go 1.22.0
3+
go 1.22
44

55
toolchain go1.22.4
66

@@ -95,6 +95,6 @@ require (
9595
google.golang.org/genproto/googleapis/rpc v0.0.0-20240520151616-dc85e6b867a5 // indirect
9696
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
9797
gopkg.in/yaml.v3 v3.0.1 // indirect
98-
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
98+
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 // indirect
9999
sigs.k8s.io/yaml v1.4.0 // indirect
100100
)

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
250250
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
251251
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
252252
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
253-
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
254-
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
253+
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 h1:fD1pz4yfdADVNfFmcP2aBEtudwUQ1AlLnRBALr33v3s=
254+
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6/go.mod h1:p4QtZmO4uMYipTQNzagwnNoseA6OxSUutVw05NhYDRs=
255255
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
256256
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=

pkg/featuregate/feature_gate.go

+13-19
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
// featuregate package is copied from k8s.io/[email protected] to avoid any potential circular dependency between k8s and etcd.
15+
// Package featuregate is copied from k8s.io/[email protected] to avoid any potential circular dependency between k8s and etcd.
1616
package featuregate
1717

1818
import (
19-
"context"
2019
"fmt"
2120
"sort"
2221
"strconv"
@@ -25,9 +24,7 @@ import (
2524
"sync/atomic"
2625

2726
"github.com/spf13/pflag"
28-
29-
"k8s.io/apimachinery/pkg/util/naming"
30-
"k8s.io/klog/v2"
27+
"go.uber.org/zap"
3128
)
3229

3330
type Feature string
@@ -128,6 +125,8 @@ type MutableFeatureGate interface {
128125

129126
// featureGate implements FeatureGate as well as pflag.Value for flag parsing.
130127
type featureGate struct {
128+
lg *zap.Logger
129+
131130
featureGateName string
132131

133132
special map[Feature]func(map[Feature]FeatureSpec, map[Feature]bool, bool)
@@ -165,18 +164,15 @@ func setUnsetBetaGates(known map[Feature]FeatureSpec, enabled map[Feature]bool,
165164
// Set, String, and Type implement pflag.Value
166165
var _ pflag.Value = &featureGate{}
167166

168-
// internalPackages are packages that ignored when creating a name for featureGates. These packages are in the common
169-
// call chains, so they'd be unhelpful as names.
170-
var internalPackages = []string{"k8s.io/component-base/featuregate/feature_gate.go"}
171-
172-
func NewFeatureGate() *featureGate {
167+
func New(name string, lg *zap.Logger) *featureGate {
173168
known := map[Feature]FeatureSpec{}
174169
for k, v := range defaultFeatures {
175170
known[k] = v
176171
}
177172

178173
f := &featureGate{
179-
featureGateName: naming.GetNameFromCallsite(internalPackages...),
174+
lg: lg,
175+
featureGateName: name,
180176
special: specialFeatures,
181177
}
182178
f.known.Store(known)
@@ -239,17 +235,17 @@ func (f *featureGate) SetFromMap(m map[string]bool) error {
239235
}
240236

241237
if featureSpec.PreRelease == Deprecated {
242-
klog.Warningf("Setting deprecated feature gate %s=%t. It will be removed in a future release.", k, v)
238+
f.lg.Warn(fmt.Sprintf("Setting deprecated feature gate %s=%t. It will be removed in a future release.", k, v))
243239
} else if featureSpec.PreRelease == GA {
244-
klog.Warningf("Setting GA feature gate %s=%t. It will be removed in a future release.", k, v)
240+
f.lg.Warn(fmt.Sprintf("Setting GA feature gate %s=%t. It will be removed in a future release.", k, v))
245241
}
246242
}
247243

248244
// Persist changes
249245
f.known.Store(known)
250246
f.enabled.Store(enabled)
251247

252-
klog.V(1).Infof("feature gates: %v", f.enabled)
248+
f.lg.Info(fmt.Sprintf("feature gates: %v", f.enabled))
253249
return nil
254250
}
255251

@@ -319,9 +315,9 @@ func (f *featureGate) OverrideDefault(name Feature, override bool) error {
319315
case spec.LockToDefault:
320316
return fmt.Errorf("cannot override default: feature %q default is locked to %t", name, spec.Default)
321317
case spec.PreRelease == Deprecated:
322-
klog.Warningf("Overriding default of deprecated feature gate %s=%t. It will be removed in a future release.", name, override)
318+
f.lg.Warn(fmt.Sprintf("Overriding default of deprecated feature gate %s=%t. It will be removed in a future release.", name, override))
323319
case spec.PreRelease == GA:
324-
klog.Warningf("Overriding default of GA feature gate %s=%t. It will be removed in a future release.", name, override)
320+
f.lg.Warn(fmt.Sprintf("Overriding default of GA feature gate %s=%t. It will be removed in a future release.", name, override))
325321
}
326322

327323
spec.Default = override
@@ -369,9 +365,7 @@ func (f *featureGate) AddFlag(fs *pflag.FlagSet) {
369365
}
370366

371367
func (f *featureGate) AddMetrics() {
372-
for feature, featureSpec := range f.GetAll() {
373-
featuremetrics.RecordFeatureInfo(context.Background(), string(feature), string(featureSpec.PreRelease), f.Enabled(feature))
374-
}
368+
// TODO(henrybear327): implement this.
375369
}
376370

377371
// KnownFeatures returns a slice of strings describing the FeatureGate's known features.

pkg/featuregate/feature_gate_test.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/spf13/pflag"
2323
"github.com/stretchr/testify/assert"
24+
"go.uber.org/zap/zaptest"
2425
)
2526

2627
func TestFeatureGateFlag(t *testing.T) {
@@ -203,7 +204,7 @@ func TestFeatureGateFlag(t *testing.T) {
203204
for i, test := range tests {
204205
t.Run(test.arg, func(t *testing.T) {
205206
fs := pflag.NewFlagSet("testfeaturegateflag", pflag.ContinueOnError)
206-
f := NewFeatureGate()
207+
f := New("test", zaptest.NewLogger(t))
207208
f.Add(map[Feature]FeatureSpec{
208209
testAlphaGate: {Default: false, PreRelease: Alpha},
209210
testBetaGate: {Default: false, PreRelease: Beta},
@@ -232,7 +233,7 @@ func TestFeatureGateOverride(t *testing.T) {
232233
const testBetaGate Feature = "TestBeta"
233234

234235
// Don't parse the flag, assert defaults are used.
235-
var f *featureGate = NewFeatureGate()
236+
var f = New("test", zaptest.NewLogger(t))
236237
f.Add(map[Feature]FeatureSpec{
237238
testAlphaGate: {Default: false, PreRelease: Alpha},
238239
testBetaGate: {Default: false, PreRelease: Beta},
@@ -261,7 +262,7 @@ func TestFeatureGateFlagDefaults(t *testing.T) {
261262
const testBetaGate Feature = "TestBeta"
262263

263264
// Don't parse the flag, assert defaults are used.
264-
var f *featureGate = NewFeatureGate()
265+
var f = New("test", zaptest.NewLogger(t))
265266
f.Add(map[Feature]FeatureSpec{
266267
testAlphaGate: {Default: false, PreRelease: Alpha},
267268
testBetaGate: {Default: true, PreRelease: Beta},
@@ -285,7 +286,7 @@ func TestFeatureGateKnownFeatures(t *testing.T) {
285286
)
286287

287288
// Don't parse the flag, assert defaults are used.
288-
var f *featureGate = NewFeatureGate()
289+
var f = New("test", zaptest.NewLogger(t))
289290
f.Add(map[Feature]FeatureSpec{
290291
testAlphaGate: {Default: false, PreRelease: Alpha},
291292
testBetaGate: {Default: true, PreRelease: Beta},
@@ -392,7 +393,7 @@ func TestFeatureGateSetFromMap(t *testing.T) {
392393
}
393394
for i, test := range tests {
394395
t.Run(fmt.Sprintf("SetFromMap %s", test.name), func(t *testing.T) {
395-
f := NewFeatureGate()
396+
f := New("test", zaptest.NewLogger(t))
396397
f.Add(map[Feature]FeatureSpec{
397398
testAlphaGate: {Default: false, PreRelease: Alpha},
398399
testBetaGate: {Default: false, PreRelease: Beta},
@@ -462,7 +463,7 @@ func TestFeatureGateString(t *testing.T) {
462463
}
463464
for i, test := range tests {
464465
t.Run(fmt.Sprintf("SetFromMap %s", test.expect), func(t *testing.T) {
465-
f := NewFeatureGate()
466+
f := New("test", zaptest.NewLogger(t))
466467
f.Add(featuremap)
467468
f.SetFromMap(test.setmap)
468469
result := f.String()
@@ -475,7 +476,7 @@ func TestFeatureGateString(t *testing.T) {
475476

476477
func TestFeatureGateOverrideDefault(t *testing.T) {
477478
t.Run("overrides take effect", func(t *testing.T) {
478-
f := NewFeatureGate()
479+
f := New("test", zaptest.NewLogger(t))
479480
if err := f.Add(map[Feature]FeatureSpec{
480481
"TestFeature1": {Default: true},
481482
"TestFeature2": {Default: false},
@@ -497,7 +498,7 @@ func TestFeatureGateOverrideDefault(t *testing.T) {
497498
})
498499

499500
t.Run("overrides are preserved across deep copies", func(t *testing.T) {
500-
f := NewFeatureGate()
501+
f := New("test", zaptest.NewLogger(t))
501502
if err := f.Add(map[Feature]FeatureSpec{"TestFeature": {Default: false}}); err != nil {
502503
t.Fatal(err)
503504
}
@@ -511,7 +512,7 @@ func TestFeatureGateOverrideDefault(t *testing.T) {
511512
})
512513

513514
t.Run("reflected in known features", func(t *testing.T) {
514-
f := NewFeatureGate()
515+
f := New("test", zaptest.NewLogger(t))
515516
if err := f.Add(map[Feature]FeatureSpec{"TestFeature": {
516517
Default: false,
517518
PreRelease: Alpha,
@@ -537,7 +538,7 @@ func TestFeatureGateOverrideDefault(t *testing.T) {
537538
})
538539

539540
t.Run("may not change default for specs with locked defaults", func(t *testing.T) {
540-
f := NewFeatureGate()
541+
f := New("test", zaptest.NewLogger(t))
541542
if err := f.Add(map[Feature]FeatureSpec{
542543
"LockedFeature": {
543544
Default: true,
@@ -555,7 +556,7 @@ func TestFeatureGateOverrideDefault(t *testing.T) {
555556
})
556557

557558
t.Run("does not supersede explicitly-set value", func(t *testing.T) {
558-
f := NewFeatureGate()
559+
f := New("test", zaptest.NewLogger(t))
559560
if err := f.Add(map[Feature]FeatureSpec{"TestFeature": {Default: true}}); err != nil {
560561
t.Fatal(err)
561562
}
@@ -571,7 +572,7 @@ func TestFeatureGateOverrideDefault(t *testing.T) {
571572
})
572573

573574
t.Run("prevents re-registration of feature spec after overriding default", func(t *testing.T) {
574-
f := NewFeatureGate()
575+
f := New("test", zaptest.NewLogger(t))
575576
if err := f.Add(map[Feature]FeatureSpec{
576577
"TestFeature": {
577578
Default: true,
@@ -594,14 +595,14 @@ func TestFeatureGateOverrideDefault(t *testing.T) {
594595
})
595596

596597
t.Run("does not allow override for an unknown feature", func(t *testing.T) {
597-
f := NewFeatureGate()
598+
f := New("test", zaptest.NewLogger(t))
598599
if err := f.OverrideDefault("TestFeature", true); err == nil {
599600
t.Error("expected an error to be returned in attempt to override default for unregistered feature")
600601
}
601602
})
602603

603604
t.Run("returns error if already added to flag set", func(t *testing.T) {
604-
f := NewFeatureGate()
605+
f := New("test", zaptest.NewLogger(t))
605606
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
606607
f.AddFlag(fs)
607608

0 commit comments

Comments
 (0)