Skip to content

Commit

Permalink
refactor(api): add meta pkg and rename many names (#6063)
Browse files Browse the repository at this point in the history
  • Loading branch information
liubog2008 authored Feb 8, 2025
1 parent 39b6417 commit fa5d80e
Show file tree
Hide file tree
Showing 42 changed files with 404 additions and 331 deletions.
17 changes: 0 additions & 17 deletions api/core/v1alpha1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
PDClusterTLSVolumeName = NamePrefix + "pd-tls"
PDClusterTLSMountPath = "/var/lib/pd-tls"

TiKVClusterTLSVolumeName = NamePrefix + "tikv-tls"
TiKVClusterTLSMountPath = "/var/lib/tikv-tls"

TiDBClusterTLSVolumeName = NamePrefix + "tidb-tls"
TiDBClusterTLSMountPath = "/var/lib/tidb-tls"

TiFlashClusterTLSVolumeName = NamePrefix + "tiflash-tls"
TiFlashClusterTLSMountPath = "/var/lib/tiflash-tls"

ClusterTLSClientVolumeName = NamePrefix + "cluster-client-tls"
ClusterTLSClientMountPath = "/var/lib/cluster-client-tls"
)

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:object:root=true

Expand Down
105 changes: 26 additions & 79 deletions api/core/v1alpha1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,29 @@ const (
ReasonUnsuspended = "Unsuspended"
)

// TODO(liubo02): move to meta
const (
// Finalizer is the finalizer used by all resources managed by TiDB Operator.
Finalizer = "core.pingcap.com/finalizer"
)

const (
// KeyPrefix defines key prefix of well known labels and annotations
KeyPrefix = "pingcap.com/"
)

const (
// LabelKeyPrefix defines key prefix of well known labels
LabelKeyPrefix = KeyPrefix

// LabelKeyManagedBy means resources are managed by tidb operator
LabelKeyManagedBy = LabelKeyPrefix + "managed-by"
LabelKeyManagedBy = KeyPrefix + "managed-by"
LabelValManagedByOperator = "tidb-operator"

// LabelKeyCluster means which tidb cluster the resource belongs to
LabelKeyCluster = LabelKeyPrefix + "cluster"

LabelKeyCluster = KeyPrefix + "cluster"
// LabelKeyComponent means the component of the resource
LabelKeyComponent = LabelKeyPrefix + "component"
LabelValComponentPD = "pd"
LabelValComponentTiDB = "tidb"
LabelValComponentTiKV = "tikv"
LabelValComponentTiFlash = "tiflash"

LabelKeyComponent = KeyPrefix + "component"
// LabelKeyGroup means the component group of the resource
LabelKeyGroup = LabelKeyPrefix + "group"
LabelKeyGroup = KeyPrefix + "group"
// LabelKeyInstance means the instance of the resource
LabelKeyInstance = LabelKeyPrefix + "instance"
LabelKeyInstance = KeyPrefix + "instance"

// LabelKeyPodSpecHash is the hash of the pod spec.
LabelKeyPodSpecHash = LabelKeyPrefix + "pod-spec-hash"
LabelKeyPodSpecHash = KeyPrefix + "pod-spec-hash"

LabelKeyInstanceRevisionHash = LabelKeyPrefix + "instance-revision-hash"
// LabelKeyInstanceRevisionHash is the revision hash of the instance
LabelKeyInstanceRevisionHash = KeyPrefix + "instance-revision-hash"

// LabelKeyConfigHash is the hash of the user-specified config (i.e., `.Spec.Config`),
// which will be used to determine whether the config has changed.
Expand All @@ -76,7 +63,19 @@ const (
// potentially triggering an unexpected rolling update.
// Instead, we choose to hash the user-specified config,
// and the worst case is that users expect a reboot but it doesn't happen.
LabelKeyConfigHash = LabelKeyPrefix + "config-hash"
LabelKeyConfigHash = KeyPrefix + "config-hash"

// LabelKeyVolumeName is used to distinguish different volumes, e.g. data volumes, log volumes, etc.
// This label will be added to the PVCs created by the tidb operator.
LabelKeyVolumeName = KeyPrefix + "volume-name"
)

const (
// Label value for meta.LabelKeyComponent
LabelValComponentPD = "pd"
LabelValComponentTiDB = "tidb"
LabelValComponentTiKV = "tikv"
LabelValComponentTiFlash = "tiflash"

// LabelKeyClusterID is the unique identifier of the cluster.
// This label is used for backward compatibility with TiDB Operator v1, so it has a different prefix.
Expand All @@ -87,15 +86,11 @@ const (
// LabelKeyStoreID is the unique identifier of a TiKV or TiFlash store.
// This label is used for backward compatibility with TiDB Operator v1, so it has a different prefix.
LabelKeyStoreID = "tidb.pingcap.com/store-id"

// LabelKeyVolumeName is used to distinguish different volumes, e.g. data volumes, log volumes, etc.
// This label will be added to the PVCs created by the tidb operator.
LabelKeyVolumeName = LabelKeyPrefix + "volume-name"
)

const (
// AnnoKeyPrefix defines key prefix of well known annotations
AnnoKeyPrefix = KeyPrefix
AnnoKeyPrefix = "core.pingcap.com/"

// all bool anno will use this val as default
AnnoValTrue = "true"
Expand All @@ -104,56 +99,6 @@ const (
AnnoKeyDeferDelete = AnnoKeyPrefix + "defer-delete"
)

const (
// NamePrefix for "names" in k8s resources
// Users may overlay some fields in managed resource such as pods. Names with this
// prefix is preserved to avoid conflicts with fields defined by users.
NamePrefix = "ti-"

// VolNamePrefix is defined for custom persistent volume which may have name conflicts
// with the volumes managed by tidb operator
VolNamePrefix = NamePrefix + "vol-"
)

const (
// All volume names
//
// VolumeNameConfig defines volume name for main config file
VolumeNameConfig = NamePrefix + "config"
// VolumeNamePrestopChecker defines volume name for pre stop checker cmd
VolumeNamePrestopChecker = NamePrefix + "prestop-checker"

// All container names
//
// Main component containers of the tidb cluster
ContainerNamePD = "pd"
ContainerNameTiKV = "tikv"
ContainerNameTiDB = "tidb"
ContainerNameTiFlash = "tiflash"
// An init container to copy pre stop checker cmd to main container
ContainerNamePrestopChecker = NamePrefix + "prestop-checker"
)

const (
DirNameConfigPD = "/etc/pd"
DirNameConfigTiKV = "/etc/tikv"
DirNameConfigTiDB = "/etc/tidb"
DirNameConfigTiFlash = "/etc/tiflash"

// ConfigFileName defines default name of config file
ConfigFileName = "config.toml"

// ConfigFileTiFlashProxyName defines default name of tiflash proxy config file
ConfigFileTiFlashProxyName = "proxy.toml"

// PrestopDirName defines dir path of pre stop checker cmd
DirNamePrestop = "/prestop"
)

const (
DefaultHelperImage = "busybox:1.37.0"
)

// ConfigUpdateStrategy represents the strategy to update configuration.
type ConfigUpdateStrategy string

Expand Down Expand Up @@ -191,6 +136,8 @@ type ObjectMeta struct {
Annotations map[string]string `json:"annotations,omitempty"`
}

// ClusterReference is a reference to cluster
// NOTE: namespace may be added into the reference in the future
type ClusterReference struct {
Name string `json:"name"`
}
Expand Down
112 changes: 112 additions & 0 deletions api/core/v1alpha1/names.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// 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 v1alpha1

import meta "github.com/pingcap/tidb-operator/api/v2/meta/v1alpha1"

// All volume names
const (
// VolumeNameConfig defines volume name for main config file
VolumeNameConfig = meta.NamePrefix + "config"
// VolumeNamePrestopChecker defines volume name for pre stop checker cmd
VolumeNamePrestopChecker = meta.NamePrefix + "prestop-checker"
// VolumeNameBootstrapSQL is the volume name for bootstrap sql
VolumeNameBootstrapSQL = meta.NamePrefix + "tidb-bootstrap-sql"
// VolumeNameTiDBAuthToken is the volume name for tidb auth token
VolumeNameTiDBAuthToken = meta.NamePrefix + "tidb-auth-token"
// VolumeNameTiDBSlowLogDefault defines default volume name of tidb slowlog
// Users may claim another volume for tidb slowlog and the default one will be removed
VolumeNameTiDBSlowLogDefault = meta.NamePrefix + "slowlog"

// TLS
//
// VolumeNameClusterTLS defines volume name for the TLS secret used between components in the TiDB cluster
VolumeNameClusterTLS = meta.NamePrefix + "tls"
// VolumeNameMySQLTLS is the volume name for the TLS secret used by TLS communication between TiDB server and MySQL client.
VolumeNameMySQLTLS = meta.NamePrefix + "tidb-sql-tls"
)

// All container names
const (
// Main component containers of the tidb cluster
// These names are well known so the name prefix is not added.
ContainerNamePD = "pd"
ContainerNameTiKV = "tikv"
ContainerNameTiDB = "tidb"
ContainerNameTiFlash = "tiflash"

// An init container to copy pre stop checker cmd to main container
ContainerNamePrestopChecker = meta.NamePrefix + "prestop-checker"

// TiDB
//
// Container to redirect slowlog
ContainerNameTiDBSlowLog = meta.NamePrefix + "slowlog"

// TiFlash
//
// Container to redirect server log
ContainerNameTiFlashServerLog = meta.NamePrefix + "serverlog"
// Container to redirect error log
ContainerNameTiFlashErrorLog = meta.NamePrefix + "errorlog"
)

// All well known dir path
const (
// config dir path
DirPathConfigPD = "/etc/pd"
DirPathConfigTiKV = "/etc/tikv"
DirPathConfigTiDB = "/etc/tidb"
DirPathConfigTiFlash = "/etc/tiflash"

// DirPathPrestop defines dir path of pre stop checker cmd
DirPathPrestop = "/prestop"
// Dir path of bootstrap sql
DirPathBootstrapSQL = "/etc/tidb-bootstrap"
// Dir path of tidb auth token
DirPathTiDBAuthToken = "/var/lib/tidb-auth-token" // #nosec
// Default dir path of tidb slowlog
DirPathTiDBSlowLogDefault = "/var/log/tidb"

// TLS
//
// Dir path of cluster tls file
DirPathClusterTLSPD = "/var/lib/pd-tls"
DirPathClusterTLSTiKV = "/var/lib/tikv-tls"
DirPathClusterTLSTiDB = "/var/lib/tidb-tls"
DirPathClusterTLSTiFlash = "/var/lib/tiflash-tls"
// Dir path of tls file for tidb and mysql client
DirPathMySQLTLS = "/var/lib/tidb-sql-tls"
)

// All file names
const (
// FileNameConfig defines default name of config file
FileNameConfig = "config.toml"
// FileNameConfigTiFlashProxy defines default name of tiflash proxy config file
FileNameConfigTiFlashProxy = "proxy.toml"
// FileNameBootstrapSQL defines default file name of bootstrap sql
FileNameBootstrapSQL = "bootstrap.sql"
// FileNameTiDBAuthTokenJWKS defines default file name of auth token jwks
FileNameTiDBAuthTokenJWKS = "tidb_auth_token_jwks.json" // #nosec
// FileNameTiDBSlowLog defines default file name of tidb slowlog
FileNameTiDBSlowLog = "slowlog"
)

// All config map keys
const (
// Keep compatible with the bootstrap sql in v1
ConfigMapKeyBootstrapSQL = "bootstrap-sql"
)
35 changes: 4 additions & 31 deletions api/core/v1alpha1/tidb_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"strings"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metautil "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/utils/ptr"
Expand All @@ -32,8 +32,7 @@ var (

const (
// VolumeUsageTypeTiDBSlowLog means the data dir of slowlog
// The default sub path is "slowlog"
// Users can define a persistent volume for slowlog, or an empty dir will be used.
// Users can define a persistent volume for slowlog, or an emptydir will be used.
VolumeMountTypeTiDBSlowLog VolumeMountType = "slowlog"

VolumeMountTiDBSlowLogDefaultPath = "/var/log/tidb"
Expand All @@ -53,26 +52,6 @@ const (
CommandProbeType string = "command"
)

const (
// TiDBSQLTLSVolumeName is the volume name for the TLS secret used by TLS communication between TiDB server and MySQL client.
TiDBSQLTLSVolumeName = NamePrefix + "tidb-sql-tls"
// TiDBSQLTLSMountPath is the volume mount path for the TLS secret used by TLS communication between TiDB server and MySQL client.
TiDBSQLTLSMountPath = "/var/lib/tidb-sql-tls"
)

const (
BootstrapSQLVolumeName = NamePrefix + "tidb-bootstrap-sql"
BootstrapSQLFilePath = "/etc/tidb-bootstrap"
BootstrapSQLFileName = "bootstrap.sql"
BootstrapSQLConfigMapKey = "bootstrap-sql"
)

const (
TiDBAuthTokenVolumeName = NamePrefix + "tidb-auth-token"
TiDBAuthTokenPath = "/var/lib/tidb-auth-token"
TiDBAuthTokenJWKS = "tidb_auth_token_jwks.json"
)

const (
TiDBCondHealth = "Health"
TiDBHealthReason = "TiDBHealth"
Expand All @@ -87,12 +66,6 @@ const (
TiDBGroupSuspendReason = "TiDBGroupSuspend"
)

const (
TiDBSlowLogContainerName = NamePrefix + "slowlog"
TiDBDefaultSlowLogVolumeName = NamePrefix + "slowlog"
TiDBSlowLogFileName = "slowlog"
)

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:object:root=true

Expand Down Expand Up @@ -181,7 +154,7 @@ func (in *TiDBGroup) ComponentKind() ComponentKind {
}

func (in *TiDBGroup) IsHealthy() bool {
return meta.IsStatusConditionTrue(in.Status.Conditions, TiDBGroupCondAvailable) && in.DeletionTimestamp.IsZero()
return metautil.IsStatusConditionTrue(in.Status.Conditions, TiDBGroupCondAvailable) && in.DeletionTimestamp.IsZero()
}

func (in *TiDBGroup) GetClientPort() int32 {
Expand Down Expand Up @@ -270,7 +243,7 @@ func (in *TiDB) CollisionCount() *int32 {
}

func (in *TiDB) IsHealthy() bool {
return meta.IsStatusConditionTrue(in.Status.Conditions, TiDBCondHealth) && in.DeletionTimestamp.IsZero()
return metautil.IsStatusConditionTrue(in.Status.Conditions, TiDBCondHealth) && in.DeletionTimestamp.IsZero()
}

func (in *TiDB) GetClientPort() int32 {
Expand Down
5 changes: 0 additions & 5 deletions api/core/v1alpha1/tiflash_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ const (
TiFlashGroupSuspendReason = "TiFlashGroupSuspend"
)

const (
TiFlashServerLogContainerName = NamePrefix + "serverlog"
TiFlashErrorLogContainerName = NamePrefix + "errorlog"
)

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:object:root=true

Expand Down
Loading

0 comments on commit fa5d80e

Please sign in to comment.