Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pd): rename underlying resource names #5983

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions apis/core/v1alpha1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,14 @@ func (c *Cluster) IsTLSClusterEnabled() bool {
return c.Spec.TLSCluster != nil && c.Spec.TLSCluster.Enabled
}

// TLSClusterSecretName returns the mTLS secret name for a component group.
func (c *Cluster) TLSClusterSecretName(groupName string) string {
return fmt.Sprintf("%s-%s-cluster-secret", c.Name, groupName)
}

// ClusterClientTLSSecretName returns the mTLS secret name for the cluster client.
// TODO: move it to namer pkg
func (c *Cluster) ClusterClientTLSSecretName() string {
return TLSClusterClientSecretName(c.Name)
}

// TLSClusterClientSecretName returns the mTLS secret name for the cluster client.
// TODO: move it to namer pkg
func TLSClusterClientSecretName(clusterName string) string {
return fmt.Sprintf("%s-cluster-client-secret", clusterName)
}
Expand Down
26 changes: 26 additions & 0 deletions apis/core/v1alpha1/pd_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package v1alpha1

import (
"strings"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -249,6 +251,30 @@ func (in *PD) GetPeerPort() int32 {
return DefaultPDPortPeer
}

// NOTE: name prefix is used to generate all names of underlying resources of this instance
func (in *PD) NamePrefixAndSuffix() (prefix, suffix string) {
index := strings.LastIndexByte(in.Name, '-')
// TODO(liubo02): validate name to avoid '-' is not found
if index == -1 {
panic("cannot get name prefix")
}
return in.Name[:index], in.Name[index+1:]
}

// This name is not only for pod, but also configMap, hostname and almost all underlying resources
// TODO(liubo02): rename to more reasonable one
func (in *PD) PodName() string {
prefix, suffix := in.NamePrefixAndSuffix()
return prefix + "-pd-" + suffix
}

// TLSClusterSecretName returns the mTLS secret name for a component.
// TODO(liubo02): move to namer
func (in *PD) TLSClusterSecretName() string {
prefix, _ := in.NamePrefixAndSuffix()
return prefix + "-pd-cluster-secret"
}

// PDGroupSpec describes the common attributes of a PDGroup
type PDGroupSpec struct {
Cluster ClusterReference `json:"cluster"`
Expand Down
29 changes: 27 additions & 2 deletions apis/core/v1alpha1/tidb_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package v1alpha1

import (
"fmt"
"strings"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -279,6 +280,30 @@ func (in *TiDB) GetStatusPort() int32 {
return DefaultTiDBPortStatus
}

// NOTE: name prefix is used to generate all names of underlying resources of this instance
func (in *TiDB) NamePrefixAndSuffix() (prefix, suffix string) {
index := strings.LastIndexByte(in.Name, '-')
// TODO(liubo02): validate name to avoid '-' is not found
if index == -1 {
panic("cannot get name prefix")
}
return in.Name[:index], in.Name[index+1:]
}

// This name is not only for pod, but also configMap, hostname and almost all underlying resources
// TODO(liubo02): rename to more reasonable one
func (in *TiDB) PodName() string {
prefix, suffix := in.NamePrefixAndSuffix()
return prefix + "-tidb-" + suffix
}

// TLSClusterSecretName returns the mTLS secret name for a component.
// TODO(liubo02): move to namer
func (in *TiDB) TLSClusterSecretName() string {
prefix, _ := in.NamePrefixAndSuffix()
return prefix + "-tidb-cluster-secret"
}

// TiDBGroupSpec describes the common attributes of a TiDBGroup.
type TiDBGroupSpec struct {
Cluster ClusterReference `json:"cluster"`
Expand Down Expand Up @@ -481,12 +506,12 @@ func (in *TiDBGroup) IsTLSClientEnabled() bool {

// TiDBServerTLSSecretName returns the secret name used in TiDB server for the TLS between TiDB server and MySQL client.
func (in *TiDBGroup) TiDBServerTLSSecretName() string {
return fmt.Sprintf("%s-%s-server-secret", in.Spec.Cluster.Name, in.Name)
return fmt.Sprintf("%s-tidb-server-secret", in.Name)
}

// TiDBClientTLSSecretName returns the secret name used in MySQL client for the TLS between TiDB server and MySQL client.
func (in *TiDBGroup) TiDBClientTLSSecretName() string {
return fmt.Sprintf("%s-%s-client-secret", in.Spec.Cluster.Name, in.Name)
return fmt.Sprintf("%s-tidb-client-secret", in.Name)
}

func (in *TiDBGroup) IsBootstrapSQLEnabled() bool {
Expand Down
26 changes: 26 additions & 0 deletions apis/core/v1alpha1/tiflash_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package v1alpha1

import (
"strings"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -269,6 +271,30 @@ func (in *TiFlash) GetProxyStatusPort() int32 {
return DefaultTiFlashPortProxyStatus
}

// NOTE: name prefix is used to generate all names of underlying resources of this instance
func (in *TiFlash) NamePrefixAndSuffix() (prefix, suffix string) {
index := strings.LastIndexByte(in.Name, '-')
// TODO(liubo02): validate name to avoid '-' is not found
if index == -1 {
panic("cannot get name prefix")
}
return in.Name[:index], in.Name[index+1:]
}

// This name is not only for pod, but also configMap, hostname and almost all underlying resources
// TODO(liubo02): rename to more reasonable one
func (in *TiFlash) PodName() string {
prefix, suffix := in.NamePrefixAndSuffix()
return prefix + "-tiflash-" + suffix
}

// TLSClusterSecretName returns the mTLS secret name for a component.
// TODO(liubo02): move to namer
func (in *TiFlash) TLSClusterSecretName() string {
prefix, _ := in.NamePrefixAndSuffix()
return prefix + "-tiflash-cluster-secret"
}

type TiFlashGroupSpec struct {
Cluster ClusterReference `json:"cluster"`
Replicas *int32 `json:"replicas"`
Expand Down
26 changes: 26 additions & 0 deletions apis/core/v1alpha1/tikv_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package v1alpha1

import (
"strings"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -259,6 +261,30 @@ func (in *TiKV) GetStatusPort() int32 {
return DefaultTiKVPortStatus
}

// NOTE: name prefix is used to generate all names of underlying resources of this instance
func (in *TiKV) NamePrefixAndSuffix() (prefix, suffix string) {
index := strings.LastIndexByte(in.Name, '-')
// TODO(liubo02): validate name to avoid '-' is not found
if index == -1 {
panic("cannot get name prefix")
}
return in.Name[:index], in.Name[index+1:]
}

// This name is not only for pod, but also configMap, hostname and almost all underlying resources
// TODO(liubo02): rename to more reasonable one
func (in *TiKV) PodName() string {
prefix, suffix := in.NamePrefixAndSuffix()
return prefix + "-tikv-" + suffix
}

// TLSClusterSecretName returns the mTLS secret name for a component.
// TODO(liubo02): move to namer
func (in *TiKV) TLSClusterSecretName() string {
prefix, _ := in.NamePrefixAndSuffix()
return prefix + "-tikv-cluster-secret"
}

// TiKVGroupSpec describes the common attributes of a TiKVGroup
type TiKVGroupSpec struct {
Cluster ClusterReference `json:"cluster"`
Expand Down
4 changes: 2 additions & 2 deletions pkg/configs/pd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func getAdvertiseClientURLs(pd *v1alpha1.PD, scheme string) string {
if ns == "" {
ns = corev1.NamespaceDefault
}
host := pd.Name + "." + pd.Spec.Subdomain + "." + ns
host := pd.PodName() + "." + pd.Spec.Subdomain + "." + ns
return fmt.Sprintf("%s://%s:%d", scheme, host, pd.GetClientPort())
}

Expand All @@ -175,7 +175,7 @@ func getAdvertisePeerURLs(pd *v1alpha1.PD, scheme string) string {
if ns == "" {
ns = corev1.NamespaceDefault
}
host := pd.Name + "." + pd.Spec.Subdomain + "." + ns
host := pd.PodName() + "." + pd.Spec.Subdomain + "." + ns
return fmt.Sprintf("%s://%s:%d", scheme, host, pd.GetPeerPort())
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/configs/tidb/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func getAdvertiseAddress(tidb *v1alpha1.TiDB) string {
if ns == "" {
ns = corev1.NamespaceDefault
}
return tidb.Name + "." + tidb.Spec.Subdomain + "." + ns + ".svc"
return tidb.PodName() + "." + tidb.Spec.Subdomain + "." + ns + ".svc"
}

func removeHTTPPrefix(url string) string {
Expand Down
6 changes: 3 additions & 3 deletions pkg/configs/tiflash/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func GetServiceAddr(tiflash *v1alpha1.TiFlash) string {
if ns == "" {
ns = corev1.NamespaceDefault
}
return fmt.Sprintf("%s.%s.%s:%d", tiflash.Name, tiflash.Spec.Subdomain, ns, tiflash.GetFlashPort())
return fmt.Sprintf("%s.%s.%s:%d", tiflash.PodName(), tiflash.Spec.Subdomain, ns, tiflash.GetFlashPort())
}

func getProxyAddr(tiflash *v1alpha1.TiFlash) string {
Expand All @@ -200,15 +200,15 @@ func getProxyAdvertiseAddr(tiflash *v1alpha1.TiFlash) string {
if ns == "" {
ns = corev1.NamespaceDefault
}
return fmt.Sprintf("%s.%s.%s:%d", tiflash.Name, tiflash.Spec.Subdomain, ns, tiflash.GetProxyPort())
return fmt.Sprintf("%s.%s.%s:%d", tiflash.PodName(), tiflash.Spec.Subdomain, ns, tiflash.GetProxyPort())
}

func getProxyAdvertiseStatusAddr(tiflash *v1alpha1.TiFlash) string {
ns := tiflash.Namespace
if ns == "" {
ns = corev1.NamespaceDefault
}
return fmt.Sprintf("%s.%s.%s:%d", tiflash.Name, tiflash.Spec.Subdomain, ns, tiflash.GetProxyStatusPort())
return fmt.Sprintf("%s.%s.%s:%d", tiflash.PodName(), tiflash.Spec.Subdomain, ns, tiflash.GetProxyStatusPort())
}

func GetServerLogPath(tiflash *v1alpha1.TiFlash) string {
Expand Down
4 changes: 2 additions & 2 deletions pkg/configs/tikv/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func GetAdvertiseClientURLs(tikv *v1alpha1.TiKV) string {
if ns == "" {
ns = corev1.NamespaceDefault
}
return fmt.Sprintf("%s.%s.%s:%d", tikv.Name, tikv.Spec.Subdomain, ns, tikv.GetClientPort())
return fmt.Sprintf("%s.%s.%s:%d", tikv.PodName(), tikv.Spec.Subdomain, ns, tikv.GetClientPort())
}

func getStatusURLs(tikv *v1alpha1.TiKV) string {
Expand All @@ -169,5 +169,5 @@ func getAdvertiseStatusURLs(tikv *v1alpha1.TiKV) string {
if ns == "" {
ns = corev1.NamespaceDefault
}
return fmt.Sprintf("%s.%s.%s:%d", tikv.Name, tikv.Spec.Subdomain, ns, tikv.GetStatusPort())
return fmt.Sprintf("%s.%s.%s:%d", tikv.PodName(), tikv.Spec.Subdomain, ns, tikv.GetStatusPort())
}
4 changes: 2 additions & 2 deletions pkg/controllers/cluster/tasks/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func (t *TaskStatus) Sync(ctx task.Context[ReconcileContext]) task.Result {
if rtx.Cluster.IsTLSClusterEnabled() {
scheme = "https"
}
pdAddr := fmt.Sprintf("%s://%s-%s.%s:%d", scheme, rtx.Cluster.Name,
rtx.PDGroup.Name, rtx.PDGroup.Namespace, rtx.PDGroup.GetClientPort())
// TODO(liubo02): extract a common util to get pd addr
pdAddr := fmt.Sprintf("%s://%s-pd.%s:%d", scheme, rtx.PDGroup.Name, rtx.PDGroup.Namespace, rtx.PDGroup.GetClientPort())
if rtx.Cluster.Status.PD != pdAddr { // TODO(csuzhangxc): verify switch between TLS and non-TLS
rtx.Cluster.Status.PD = pdAddr
needUpdate = true
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/pd/tasks/cm.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TaskConfigMap(ctx *ReconcileContext, _ logr.Logger, c client.Client) task.T
func newConfigMap(pd *v1alpha1.PD, data []byte, hash string) *corev1.ConfigMap {
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: ConfigMapName(pd.Name),
Name: pd.PodName(),
Namespace: pd.Namespace,
Labels: maputil.Merge(pd.Labels, map[string]string{
v1alpha1.LabelKeyInstance: pd.Name,
Expand Down
6 changes: 4 additions & 2 deletions pkg/controllers/pd/tasks/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ func (ctx *ReconcileContext) SetCluster(c *v1alpha1.Cluster) {
ctx.Cluster = c
}

// Pod always uses same namespace and name of PD
func (ctx *ReconcileContext) PodKey() types.NamespacedName {
return ctx.Key
return types.NamespacedName{
Namespace: ctx.PD.Namespace,
Name: ctx.PD.PodName(),
}
}

func (ctx *ReconcileContext) GetPod() *corev1.Pod {
Expand Down
38 changes: 34 additions & 4 deletions pkg/controllers/pd/tasks/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
package tasks

import (
"context"

corev1 "k8s.io/api/core/v1"

"github.com/pingcap/tidb-operator/apis/core/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/client"
"github.com/pingcap/tidb-operator/pkg/runtime"
"github.com/pingcap/tidb-operator/pkg/utils/k8s"
"github.com/pingcap/tidb-operator/pkg/utils/task/v3"
)
Expand All @@ -30,19 +36,26 @@ func TaskFinalizerDel(ctx *ReconcileContext, c client.Client) task.Task {
return task.Fail().With("cannot delete member: %v", err)
}

if err := k8s.EnsureInstanceSubResourceDeleted(ctx, c,
ctx.PD.Namespace, ctx.PD.Name); err != nil {
wait, err := EnsureSubResourcesDeleted(ctx, c, ctx.PD)
if err != nil {
return task.Fail().With("cannot delete subresources: %v", err)
}

if wait {
return task.Wait().With("wait all subresources deleted")
}

if err := k8s.RemoveFinalizer(ctx, c, ctx.PD); err != nil {
return task.Fail().With("cannot remove finalizer: %v", err)
}
case ctx.IsAvailable:
if err := k8s.EnsureInstanceSubResourceDeleted(ctx, c,
ctx.PD.Namespace, ctx.PD.Name); err != nil {
wait, err := EnsureSubResourcesDeleted(ctx, c, ctx.PD)
if err != nil {
return task.Fail().With("cannot delete subresources: %v", err)
}
if wait {
return task.Wait().With("wait all subresources deleted")
}

if err := k8s.RemoveFinalizer(ctx, c, ctx.PD); err != nil {
return task.Fail().With("cannot remove finalizer: %v", err)
Expand All @@ -64,3 +77,20 @@ func TaskFinalizerAdd(ctx *ReconcileContext, c client.Client) task.Task {
return task.Complete().With("finalizer is added")
})
}

func EnsureSubResourcesDeleted(ctx context.Context, c client.Client, pd *v1alpha1.PD) (wait bool, _ error) {
wait1, err := k8s.DeleteInstanceSubresource(ctx, c, runtime.FromPD(pd), &corev1.PodList{})
if err != nil {
return false, err
}
wait2, err := k8s.DeleteInstanceSubresource(ctx, c, runtime.FromPD(pd), &corev1.ConfigMapList{})
if err != nil {
return false, err
}
wait3, err := k8s.DeleteInstanceSubresource(ctx, c, runtime.FromPD(pd), &corev1.PersistentVolumeClaimList{})
if err != nil {
return false, err
}

return wait1 || wait2 || wait3, nil
}
Loading
Loading