Skip to content

Commit dca49fd

Browse files
authored
fix(tidb): support keyspace in config file if spec.keyspace is unset and remove unnecessary validation (#6451)
Signed-off-by: liubo02 <[email protected]>
1 parent e9395ab commit dca49fd

File tree

5 files changed

+41
-46
lines changed

5 files changed

+41
-46
lines changed

api/core/v1alpha1/tidb_types.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,6 @@ type TiDBSpec struct {
325325

326326
// TiDBTemplateSpec embedded some fields managed by TiDBGroup.
327327
// +kubebuilder:validation:XValidation:rule="!has(self.mode) || self.mode == 'Normal' || !has(self.keyspace) || self.keyspace.size() == 0",message="keyspace cannot be set if mode is StandBy"
328-
// +kubebuilder:validation:XValidation:rule="(has(oldSelf.mode) && oldSelf.mode == 'StandBy') || ((!has(oldSelf.keyspace) && !has(self.keyspace)) || (has(oldSelf.keyspace) && has(self.keyspace) && oldSelf.keyspace == self.keyspace))",message="keyspace can only be set once when mode is changed from StandBy to Normal"
329-
// +kubebuilder:validation:XValidation:rule="!has(self.mode) || self.mode != 'StandBy' || oldSelf.mode == 'StandBy'",message="mode can only be set from StandBy to Normal once"
330328
TiDBTemplateSpec `json:",inline"`
331329
}
332330

manifests/crd/core.pingcap.com_tidbs.yaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8709,14 +8709,6 @@ spec:
87098709
- message: keyspace cannot be set if mode is StandBy
87108710
rule: '!has(self.mode) || self.mode == ''Normal'' || !has(self.keyspace)
87118711
|| self.keyspace.size() == 0'
8712-
- message: keyspace can only be set once when mode is changed from StandBy
8713-
to Normal
8714-
rule: (has(oldSelf.mode) && oldSelf.mode == 'StandBy') || ((!has(oldSelf.keyspace)
8715-
&& !has(self.keyspace)) || (has(oldSelf.keyspace) && has(self.keyspace)
8716-
&& oldSelf.keyspace == self.keyspace))
8717-
- message: mode can only be set from StandBy to Normal once
8718-
rule: '!has(self.mode) || self.mode != ''StandBy'' || oldSelf.mode ==
8719-
''StandBy'''
87208712
- message: overlay volumeClaims names must exist in volumes
87218713
rule: '!has(self.overlay) || !has(self.overlay.volumeClaims) || (has(self.volumes)
87228714
&& self.overlay.volumeClaims.all(vc, vc.name in self.volumes.map(v,

pkg/configs/tidb/config.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ type StandBy struct {
8383
}
8484

8585
func (c *Config) Overlay(cluster *v1alpha1.Cluster, tidb *v1alpha1.TiDB, fg features.Gates) error {
86-
if err := c.Validate(coreutil.IsSeparateSlowLogEnabled(tidb)); err != nil {
86+
if err := c.Validate(tidb); err != nil {
8787
return err
8888
}
8989

@@ -144,7 +144,8 @@ func (c *Config) Overlay(cluster *v1alpha1.Cluster, tidb *v1alpha1.TiDB, fg feat
144144
}
145145

146146
//nolint:gocyclo // refactor if possible
147-
func (c *Config) Validate(separateSlowLog bool) error {
147+
func (c *Config) Validate(tidb *v1alpha1.TiDB) error {
148+
separateSlowLog := coreutil.IsSeparateSlowLogEnabled(tidb)
148149
var fields []string
149150

150151
if c.Store != "" {
@@ -197,8 +198,10 @@ func (c *Config) Validate(separateSlowLog bool) error {
197198
fields = append(fields, "labels")
198199
}
199200

200-
if c.KeyspaceName != "" {
201-
fields = append(fields, "keyspace")
201+
if tidb.Spec.Keyspace != "" {
202+
if c.KeyspaceName != "" {
203+
fields = append(fields, "keyspace")
204+
}
202205
}
203206

204207
if c.StandBy != nil && c.StandBy.StandByMode != nil {

pkg/configs/tidb/config_test.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,39 @@ import (
3030

3131
func TestValidate(t *testing.T) {
3232
cfgValid := &Config{}
33-
err := cfgValid.Validate(true)
33+
tidb := &v1alpha1.TiDB{
34+
ObjectMeta: metav1.ObjectMeta{
35+
Namespace: "ns1",
36+
Name: "basic-0",
37+
},
38+
Spec: v1alpha1.TiDBSpec{
39+
Cluster: v1alpha1.ClusterReference{
40+
Name: "cluster-1",
41+
},
42+
Subdomain: "basic-tidb-peer",
43+
TiDBTemplateSpec: v1alpha1.TiDBTemplateSpec{
44+
SlowLog: &v1alpha1.TiDBSlowLog{},
45+
Security: &v1alpha1.TiDBSecurity{
46+
AuthToken: &v1alpha1.TiDBAuthToken{
47+
JWKs: corev1.LocalObjectReference{
48+
Name: "auth-token-jwks",
49+
},
50+
},
51+
SEM: &v1alpha1.SEM{
52+
Config: corev1.LocalObjectReference{
53+
Name: "sem-config",
54+
},
55+
},
56+
TLS: &v1alpha1.TiDBTLS{
57+
MySQL: &v1alpha1.TLS{
58+
Enabled: true,
59+
},
60+
},
61+
},
62+
},
63+
},
64+
}
65+
err := cfgValid.Validate(tidb)
3466
require.NoError(t, err)
3567

3668
cfgInvalid := &Config{
@@ -56,7 +88,7 @@ func TestValidate(t *testing.T) {
5688
ServerLabels: map[string]string{"foo": "bar"},
5789
}
5890

59-
err = cfgInvalid.Validate(true)
91+
err = cfgInvalid.Validate(tidb)
6092
require.Error(t, err)
6193
assert.Contains(t, err.Error(), "store")
6294
assert.Contains(t, err.Error(), "advertise-address")

tests/validation/tidb_test.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ func mysqlTLS() []Case {
107107
}
108108

109109
func keyspace() []Case {
110-
err0 := `spec: Invalid value: "object": keyspace can only be set once when mode is changed from StandBy to Normal`
111110
err1 := `spec: Invalid value: "object": keyspace cannot be set if mode is StandBy`
112-
err2 := `spec: Invalid value: "object": mode can only be set from StandBy to Normal once`
113111
return []Case{
114112
{
115113
desc: "no keyspace and no mode",
@@ -156,24 +154,6 @@ func keyspace() []Case {
156154
current: map[string]any{"mode": "Normal", "keyspace": "xxx"},
157155
mode: PatchModeMerge,
158156
},
159-
{
160-
desc: "update: mode is Normal and try to change keyspace",
161-
old: map[string]any{"mode": "Normal", "keyspace": "xxx"},
162-
current: map[string]any{"mode": "Normal", "keyspace": "yyy"},
163-
mode: PatchModeMerge,
164-
wantErrs: []string{
165-
err0,
166-
},
167-
},
168-
{
169-
desc: "update: mode is Normal and try to unset keyspace",
170-
old: map[string]any{"mode": "Normal", "keyspace": "xxx"},
171-
current: map[string]any{"mode": "Normal"},
172-
mode: PatchModeMerge,
173-
wantErrs: []string{
174-
err0,
175-
},
176-
},
177157
{
178158
desc: "mode is StandBy and no keyspace",
179159
isCreate: true,
@@ -228,15 +208,5 @@ func keyspace() []Case {
228208
current: map[string]any{"mode": "Normal", "keyspace": "yyy"},
229209
mode: PatchModeMerge,
230210
},
231-
{
232-
desc: "update: mode is Normal and try to change to StandBy back",
233-
old: map[string]any{"mode": "Normal", "keyspace": "xxx"},
234-
current: map[string]any{"mode": "StandBy"},
235-
mode: PatchModeMerge,
236-
wantErrs: []string{
237-
err0,
238-
err2,
239-
},
240-
},
241211
}
242212
}

0 commit comments

Comments
 (0)