Skip to content

Commit

Permalink
opt(tiflash): only report error if there are not equal to the managed…
Browse files Browse the repository at this point in the history
… fields for some dir config items
  • Loading branch information
csuzhangxc committed Jan 16, 2025
1 parent 6676ae2 commit e1d719f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
40 changes: 26 additions & 14 deletions pkg/configs/tiflash/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,6 @@ type Security struct {
}

func (c *Config) Overlay(cluster *v1alpha1.Cluster, tiflash *v1alpha1.TiFlash) error {
if err := c.Validate(); err != nil {
return err
}

if cluster.IsTLSClusterEnabled() {
c.Security.CAPath = path.Join(v1alpha1.TiFlashClusterTLSMountPath, corev1.ServiceAccountRootCAKey)
c.Security.CertPath = path.Join(v1alpha1.TiFlashClusterTLSMountPath, corev1.TLSCertKey)
c.Security.KeyPath = path.Join(v1alpha1.TiFlashClusterTLSMountPath, corev1.TLSPrivateKeyKey)
}

var dataDir string
for i := range tiflash.Spec.Volumes {
vol := &tiflash.Spec.Volumes[i]
Expand All @@ -113,6 +103,16 @@ func (c *Config) Overlay(cluster *v1alpha1.Cluster, tiflash *v1alpha1.TiFlash) e
}
}

if err := c.Validate(dataDir); err != nil {
return err
}

if cluster.IsTLSClusterEnabled() {
c.Security.CAPath = path.Join(v1alpha1.TiFlashClusterTLSMountPath, corev1.ServiceAccountRootCAKey)
c.Security.CertPath = path.Join(v1alpha1.TiFlashClusterTLSMountPath, corev1.TLSCertKey)
c.Security.KeyPath = path.Join(v1alpha1.TiFlashClusterTLSMountPath, corev1.TLSPrivateKeyKey)
}

c.TmpPath = getTmpPath(dataDir)
c.Storage.Main.Dir = []string{getMainStorageDir(dataDir)}
c.Storage.Raft.Dir = []string{getRaftStorageDir(dataDir)}
Expand All @@ -135,7 +135,7 @@ func (c *Config) Overlay(cluster *v1alpha1.Cluster, tiflash *v1alpha1.TiFlash) e
}

//nolint:gocyclo // refactor if possible
func (c *Config) Validate() error {
func (c *Config) Validate(dataDir string) error {
fields := []string{}

if c.Security.CAPath != "" {
Expand All @@ -152,10 +152,10 @@ func (c *Config) Validate() error {
fields = append(fields, "tmp_path")
}

if c.Storage.Main.Dir != nil {
if c.Storage.Main.Dir != nil && !areSlicesEqual(c.Storage.Main.Dir, []string{getMainStorageDir(dataDir)}) {
fields = append(fields, "storage.main.dir")
}
if c.Storage.Raft.Dir != nil {
if c.Storage.Raft.Dir != nil && !areSlicesEqual(c.Storage.Raft.Dir, []string{getRaftStorageDir(dataDir)}) {
fields = append(fields, "storage.raft.dir")
}

Expand All @@ -165,7 +165,7 @@ func (c *Config) Validate() error {
if c.Flash.Proxy.Config != "" {
fields = append(fields, "flash.proxy.config")
}
if c.Flash.Proxy.DataDir != "" {
if c.Flash.Proxy.DataDir != "" && c.Flash.Proxy.DataDir != getProxyDataDir(dataDir) {
fields = append(fields, "flash.proxy.data-dir")
}
if c.Flash.Proxy.Addr != "" {
Expand Down Expand Up @@ -264,3 +264,15 @@ func getProxyDataDir(dataDir string) string {
dataDir = GetDataDir(dataDir)
return fmt.Sprintf("%s/proxy", dataDir)
}

func areSlicesEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, sa := range a {
if sa != b[i] {
return false
}
}
return true
}
25 changes: 23 additions & 2 deletions pkg/configs/tiflash/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

func TestValidate(t *testing.T) {
cfgValid := &Config{}
err := cfgValid.Validate()
err := cfgValid.Validate("/data0")
require.NoError(t, err)

cfgInvalid := &Config{
Expand Down Expand Up @@ -66,7 +66,7 @@ func TestValidate(t *testing.T) {
},
}

err = cfgInvalid.Validate()
err = cfgInvalid.Validate("/data0")
require.Error(t, err)
require.Contains(t, err.Error(), "tmp_path")
require.Contains(t, err.Error(), "storage.main.dir")
Expand All @@ -86,6 +86,27 @@ func TestValidate(t *testing.T) {
require.Contains(t, err.Error(), "security.key_path")
}

func TestValidateEqual(t *testing.T) {
cfg := &Config{
Storage: Storage{
Main: StorageMain{
Dir: []string{"/data0/db"},
},
Raft: StorageRaft{
Dir: []string{"/data0/kvstore"},
},
},
Flash: Flash{
Proxy: Proxy{
DataDir: "/data0/proxy",
},
},
}

err := cfg.Validate("/data0")
require.NoError(t, err)
}

func TestOverlay(t *testing.T) {
cluster := &v1alpha1.Cluster{
Spec: v1alpha1.ClusterSpec{
Expand Down

0 comments on commit e1d719f

Please sign in to comment.