Skip to content

Commit 74f20e4

Browse files
authored
fix(client): fix new client error if VolumeAttributeClass is not enabled (#6077)
1 parent 3c1e3dc commit 74f20e4

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

cmd/operator/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func BuildCacheByObject() map[client.Object]cache.ByObject {
283283
Label: labels.Everything(),
284284
},
285285
}
286-
if kubefeat.FeatureGates.Stage(kubefeat.VolumeAttributesClass).Enabled(kubefeat.BETA) {
286+
if kubefeat.Stage(kubefeat.VolumeAttributesClass).Enabled(kubefeat.BETA) {
287287
byObj[&storagev1beta1.VolumeAttributesClass{}] = cache.ByObject{
288288
Label: labels.Everything(),
289289
}

pkg/client/client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"sigs.k8s.io/structured-merge-diff/v4/typed"
3636

3737
"github.com/pingcap/tidb-operator/pkg/scheme"
38+
"github.com/pingcap/tidb-operator/pkg/utils/kubefeat"
3839
forkedproto "github.com/pingcap/tidb-operator/third_party/kube-openapi/pkg/util/proto"
3940
)
4041

@@ -189,6 +190,8 @@ func gvToAPIPath(gv schema.GroupVersion) string {
189190
}
190191

191192
func New(cfg *rest.Config, opts client.Options) (Client, error) {
193+
kubefeat.MustInitFeatureGates(cfg)
194+
192195
dc, err := discovery.NewDiscoveryClientForConfig(cfg)
193196
if err != nil {
194197
return nil, fmt.Errorf("cannot new discovery client: %w", err)
@@ -199,7 +202,7 @@ func New(cfg *rest.Config, opts client.Options) (Client, error) {
199202
if err != nil {
200203
return nil, err
201204
}
202-
gvs := scheme.GroupVersions
205+
gvs := scheme.GroupVersions()
203206

204207
parser, err := NewGVKParser(gvs, paths)
205208
if err != nil {

pkg/scheme/scheme.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
2626

2727
"github.com/pingcap/tidb-operator/api/v2/core/v1alpha1"
28+
"github.com/pingcap/tidb-operator/pkg/utils/kubefeat"
2829
)
2930

3031
// Scheme is used by client to visit kubernetes API.
@@ -39,9 +40,15 @@ func init() {
3940
utilruntime.Must(v1alpha1.Install(Scheme))
4041
}
4142

42-
var GroupVersions = []schema.GroupVersion{
43-
corev1.SchemeGroupVersion,
44-
storagev1.SchemeGroupVersion,
45-
v1alpha1.SchemeGroupVersion,
46-
storagev1beta1.SchemeGroupVersion,
43+
func GroupVersions() []schema.GroupVersion {
44+
gvs := []schema.GroupVersion{
45+
corev1.SchemeGroupVersion,
46+
storagev1.SchemeGroupVersion,
47+
v1alpha1.SchemeGroupVersion,
48+
}
49+
if kubefeat.Stage(kubefeat.VolumeAttributesClass).Enabled(kubefeat.BETA) {
50+
gvs = append(gvs, storagev1beta1.SchemeGroupVersion)
51+
}
52+
53+
return gvs
4754
}

pkg/utils/kubefeat/gates.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,40 @@ import (
2525
"k8s.io/client-go/rest"
2626
)
2727

28-
var FeatureGates Gates
28+
var defaultFeatureGates Gates
29+
30+
func Stage(key Feature) StagedFeature {
31+
if defaultFeatureGates == nil {
32+
panic("please init featureGates before use it")
33+
}
34+
35+
return defaultFeatureGates.Stage(key)
36+
}
2937

3038
type Gates interface {
3139
Stage(key Feature) StagedFeature
3240
}
3341

3442
type StagedFeature interface {
35-
Enabled(Stage) bool
43+
Enabled(FeatureStage) bool
3644
}
3745

3846
type featureGates struct {
3947
feats map[Feature]spec
4048
}
4149

42-
type Stage int
50+
type FeatureStage int
4351

4452
const (
45-
INVAILD Stage = 0
53+
INVAILD FeatureStage = 0
4654

47-
ALPHA Stage = 1 << iota
55+
ALPHA FeatureStage = 1 << iota
4856
BETA
4957
STABLE
5058
ANY = ALPHA | BETA | STABLE
5159
)
5260

53-
func stageFromString(s string) Stage {
61+
func stageFromString(s string) FeatureStage {
5462
switch s {
5563
case "ALPHA":
5664
return ALPHA
@@ -65,10 +73,10 @@ func stageFromString(s string) Stage {
6573

6674
type spec struct {
6775
enabled bool
68-
stage Stage
76+
stage FeatureStage
6977
}
7078

71-
func (s spec) Enabled(stage Stage) bool {
79+
func (s spec) Enabled(stage FeatureStage) bool {
7280
if s.stage&stage == 0 {
7381
return false
7482
}
@@ -80,6 +88,9 @@ func (g *featureGates) Stage(key Feature) StagedFeature {
8088
}
8189

8290
func MustInitFeatureGates(cfg *rest.Config) {
91+
if defaultFeatureGates != nil {
92+
return
93+
}
8394
gates, err := NewFeatureGates(cfg)
8495
if err != nil {
8596
// TODO: use a common panic util to panic
@@ -88,7 +99,7 @@ func MustInitFeatureGates(cfg *rest.Config) {
8899

89100
fmt.Println("init feature gates")
90101

91-
FeatureGates = gates
102+
defaultFeatureGates = gates
92103
}
93104

94105
func NewFeatureGates(cfg *rest.Config) (Gates, error) {

0 commit comments

Comments
 (0)