Skip to content

Commit

Permalink
chore(pd): add UT; update data path
Browse files Browse the repository at this point in the history
  • Loading branch information
csuzhangxc committed Dec 20, 2024
1 parent 4e07af2 commit c508b2f
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 28 deletions.
1 change: 0 additions & 1 deletion apis/core/v1alpha1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ type VolumeUsage struct {
// A volume can be defined for multiple usages.
Type VolumeUsageType `json:"type"`
// SubPath is the relative path of the volume's mount path.
// The default value of sub path is determined by the usage type.
SubPath string `json:"subPath,omitempty"`
}

Expand Down
5 changes: 2 additions & 3 deletions manifests/crd/core.pingcap.com_pdgroups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,8 @@ spec:
items:
properties:
subPath:
description: |-
SubPath is the relative path of the volume's mount path.
The default value of sub path is determined by the usage type.
description: SubPath is the relative path of the
volume's mount path.
type: string
type:
description: |-
Expand Down
5 changes: 2 additions & 3 deletions manifests/crd/core.pingcap.com_pds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ spec:
items:
properties:
subPath:
description: |-
SubPath is the relative path of the volume's mount path.
The default value of sub path is determined by the usage type.
description: SubPath is the relative path of the volume's
mount path.
type: string
type:
description: |-
Expand Down
5 changes: 2 additions & 3 deletions manifests/crd/core.pingcap.com_tidbgroups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,8 @@ spec:
items:
properties:
subPath:
description: |-
SubPath is the relative path of the volume's mount path.
The default value of sub path is determined by the usage type.
description: SubPath is the relative path of the
volume's mount path.
type: string
type:
description: |-
Expand Down
5 changes: 2 additions & 3 deletions manifests/crd/core.pingcap.com_tidbs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,8 @@ spec:
items:
properties:
subPath:
description: |-
SubPath is the relative path of the volume's mount path.
The default value of sub path is determined by the usage type.
description: SubPath is the relative path of the volume's
mount path.
type: string
type:
description: |-
Expand Down
5 changes: 2 additions & 3 deletions manifests/crd/core.pingcap.com_tiflashes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,8 @@ spec:
items:
properties:
subPath:
description: |-
SubPath is the relative path of the volume's mount path.
The default value of sub path is determined by the usage type.
description: SubPath is the relative path of the volume's
mount path.
type: string
type:
description: |-
Expand Down
5 changes: 2 additions & 3 deletions manifests/crd/core.pingcap.com_tiflashgroups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,8 @@ spec:
items:
properties:
subPath:
description: |-
SubPath is the relative path of the volume's mount path.
The default value of sub path is determined by the usage type.
description: SubPath is the relative path of the
volume's mount path.
type: string
type:
description: |-
Expand Down
5 changes: 2 additions & 3 deletions manifests/crd/core.pingcap.com_tikvgroups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,8 @@ spec:
items:
properties:
subPath:
description: |-
SubPath is the relative path of the volume's mount path.
The default value of sub path is determined by the usage type.
description: SubPath is the relative path of the
volume's mount path.
type: string
type:
description: |-
Expand Down
5 changes: 2 additions & 3 deletions manifests/crd/core.pingcap.com_tikvs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,8 @@ spec:
items:
properties:
subPath:
description: |-
SubPath is the relative path of the volume's mount path.
The default value of sub path is determined by the usage type.
description: SubPath is the relative path of the volume's
mount path.
type: string
type:
description: |-
Expand Down
16 changes: 13 additions & 3 deletions pkg/configs/pd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,10 @@ func (c *Config) Overlay(cluster *v1alpha1.Cluster, pd *v1alpha1.PD, peers []*v1
vol := &pd.Spec.Volumes[i]
for _, usage := range vol.For {
if usage.Type == v1alpha1.VolumeUsageTypePDData {
p := string(usage.Type)
c.DataDir = vol.Path
if usage.SubPath != "" {
p = usage.SubPath
c.DataDir = path.Join(vol.Path, usage.SubPath)
}
c.DataDir = path.Join(vol.Path, p)
}
}
}
Expand Down Expand Up @@ -146,6 +145,17 @@ func (c *Config) Validate() error {
fields = append(fields, "join")
}

// valid security fields
if c.Security.CAPath != "" {
fields = append(fields, "security.cacert-path")
}
if c.Security.CertPath != "" {
fields = append(fields, "security.cert-path")
}
if c.Security.KeyPath != "" {
fields = append(fields, "security.key-path")
}

if len(fields) == 0 {
return nil
}
Expand Down
144 changes: 144 additions & 0 deletions pkg/configs/pd/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Copyright 2024 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package pd

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/pingcap/tidb-operator/apis/core/v1alpha1"
)

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

cfgInvalid := &Config{
Name: "pd-0",
DataDir: "/var/lib/pd",
ClientUrls: "https://[::]:2379",
PeerUrls: "https://[::]:2380",
AdvertiseClientUrls: "https://pd-0.default.svc:2379",
AdvertisePeerUrls: "https://pd-0.default.svc:2380",
InitialCluster: "pd-0=https://pd-0.default.svc:2380,pd-1=https://pd-1.default.svc:2380",
InitialClusterState: InitialClusterStateNew,
InitialClusterToken: "pd-cluster",
Join: "pd-2=https://pd-2.default.svc:2380",
Security: Security{
CAPath: "/path/to/ca",
CertPath: "/path/to/cert",
KeyPath: "/path/to/key",
},
}

err = cfgInvalid.Validate()
require.Error(t, err)
assert.Contains(t, err.Error(), "name")
assert.Contains(t, err.Error(), "data-dir")
assert.Contains(t, err.Error(), "client-urls")
assert.Contains(t, err.Error(), "peer-urls")
assert.Contains(t, err.Error(), "advertise-client-urls")
assert.Contains(t, err.Error(), "advertise-peer-urls")
assert.Contains(t, err.Error(), "initial-cluster")
assert.Contains(t, err.Error(), "initial-cluster-state")
assert.Contains(t, err.Error(), "initial-cluster-token")
assert.Contains(t, err.Error(), "join")
assert.Contains(t, err.Error(), "security.cacert-path")
assert.Contains(t, err.Error(), "security.cert-path")
assert.Contains(t, err.Error(), "security.key-path")
}

func TestOverlay(t *testing.T) {
cluster := &v1alpha1.Cluster{
Spec: v1alpha1.ClusterSpec{
TLSCluster: &v1alpha1.TLSCluster{Enabled: true},
},
Status: v1alpha1.ClusterStatus{
PD: "https://db-pd.default.svc:2379",
},
}
pd := &v1alpha1.PD{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "basic-0",
},
Spec: v1alpha1.PDSpec{
Cluster: v1alpha1.ClusterReference{
Name: "cluster-1",
},
Subdomain: "basic",
PDTemplateSpec: v1alpha1.PDTemplateSpec{
Volumes: []v1alpha1.Volume{
{
Name: "data",
Path: "/var/lib/pd",
For: []v1alpha1.VolumeUsage{
{
Type: v1alpha1.VolumeUsageTypePDData,
},
},
Storage: resource.Quantity{
Format: "1Gi",
},
},
},
Config: v1alpha1.ConfigFile(`[log]
level = "info"`),
},
},
}
peers := []*v1alpha1.PD{
{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "basic-1",
},
Spec: v1alpha1.PDSpec{
Subdomain: "default",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "basic-2",
},
Spec: v1alpha1.PDSpec{
Subdomain: "default",
},
},
}

// join to an existing cluster
cfg := &Config{}
err := cfg.Overlay(cluster, pd, peers)
require.NoError(t, err)
assert.Equal(t, "basic-0", cfg.Name)
assert.Equal(t, "/var/lib/pd", cfg.DataDir)
assert.Equal(t, "https://[::]:2379", cfg.ClientUrls)
assert.Equal(t, "https://[::]:2380", cfg.PeerUrls)
assert.Equal(t, "https://basic-pd-0.basic.default:2379", cfg.AdvertiseClientUrls)
assert.Equal(t, "https://basic-pd-0.basic.default:2380", cfg.AdvertisePeerUrls)
assert.Equal(t, "", cfg.InitialCluster)
assert.Equal(t, "", cfg.InitialClusterState)
assert.Equal(t, "cluster-1", cfg.InitialClusterToken)
assert.Equal(t, "/var/lib/pd-tls/ca.crt", cfg.Security.CAPath)
assert.Equal(t, "/var/lib/pd-tls/tls.crt", cfg.Security.CertPath)
assert.Equal(t, "/var/lib/pd-tls/tls.key", cfg.Security.KeyPath)
}

0 comments on commit c508b2f

Please sign in to comment.