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

Tidb support setting a random password. #4328

Merged
merged 87 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from 85 commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
88fc3b2
random password
mikechengwei Dec 26, 2021
f9cb7fe
random password
mikechengwei Dec 26, 2021
5b31a93
optimize code
mikechengwei Dec 27, 2021
b5de123
optimize code
mikechengwei Dec 27, 2021
324ffbf
Merge branch 'master' into feature/addpassword
mikechengwei Dec 28, 2021
ab37b18
optimize code
mikechengwei Jan 3, 2022
d3e7898
Merge branch 'feature/addpassword' of github.com:mikechengwei/tidb-op…
mikechengwei Jan 3, 2022
41b888f
optimize code
mikechengwei Jan 4, 2022
68f4994
Merge branch 'master' into feature/addpassword
mikechengwei Jan 4, 2022
c269faa
optimize code
mikechengwei Jan 5, 2022
412dd41
optimize code
mikechengwei Jan 5, 2022
280dab8
Update README.md
DanielZhangQD Jan 6, 2022
2fe7dfd
Update pkg/manager/member/tidb_member_manager.go
mikechengwei Jan 6, 2022
cb4d1d0
Merge branch 'master' into feature/addpassword
mikechengwei Jan 6, 2022
00cde54
update policy
mikechengwei Jan 6, 2022
d1fe63d
optimize code
mikechengwei Jan 6, 2022
09d5b5a
optimize code
mikechengwei Jan 6, 2022
417a0b4
Merge branch 'master' into feature/addpassword
mikechengwei Jan 6, 2022
bfba0ab
Merge branch 'master' into feature/addpassword
mikechengwei Jan 7, 2022
7b5cb24
optimize tidb check
mikechengwei Jan 19, 2022
5c18f22
Merge branch 'master' of github.com:pingcap/tidb-operator into featur…
mikechengwei Jan 19, 2022
7013280
optimize code
mikechengwei Jan 19, 2022
45e1884
optimize code
mikechengwei Jan 19, 2022
fee697a
optimize code
mikechengwei Jan 20, 2022
501643e
optimize code
mikechengwei Jan 20, 2022
afef7cd
Merge branch 'master' into feature/addpassword
mikechengwei Jan 20, 2022
3cc4750
optimize code
mikechengwei Jan 20, 2022
e71840f
Merge branch 'feature/addpassword' of github.com:mikechengwei/tidb-op…
mikechengwei Jan 20, 2022
2cb2c9b
Update pkg/manager/member/tidb_member_manager.go
mikechengwei Jan 20, 2022
f61774f
Update examples/basic-random-password/tidb-cluster.yaml
mikechengwei Jan 20, 2022
c583907
optimize code
mikechengwei Jan 20, 2022
0dee2e3
Merge branch 'feature/addpassword' of github.com:mikechengwei/tidb-op…
mikechengwei Jan 20, 2022
79d78a5
optimize code
mikechengwei Jan 20, 2022
1bf113c
optimize code
mikechengwei Jan 20, 2022
3d90a28
optimize code
mikechengwei Jan 20, 2022
47b4489
optimize code
mikechengwei Jan 20, 2022
f0e3701
optimize code
mikechengwei Jan 20, 2022
1496f18
optimize code
mikechengwei Jan 20, 2022
4e14c3d
optimize code
mikechengwei Jan 20, 2022
63a30d7
Update cmd/backup-manager/app/backup/manager.go
mikechengwei Jan 21, 2022
6c40b18
Update pkg/apis/pingcap/v1alpha1/types.go
mikechengwei Jan 21, 2022
3e6ef17
optimize code
mikechengwei Jan 21, 2022
e1a45b8
Merge branch 'feature/addpassword' of github.com:mikechengwei/tidb-op…
mikechengwei Jan 21, 2022
587fedf
optimize code
mikechengwei Jan 21, 2022
e6a15a7
Merge branch 'master' of github.com:pingcap/tidb-operator into featur…
mikechengwei Jan 21, 2022
fb5f93e
Update pkg/manager/member/tidb_member_manager.go
mikechengwei Jan 23, 2022
d87ff52
Update pkg/apis/pingcap/v1alpha1/tidbcluster.go
mikechengwei Jan 23, 2022
b9ae888
Update cmd/backup-manager/app/restore/manager.go
mikechengwei Jan 23, 2022
640b16f
Update cmd/backup-manager/app/export/manager.go
mikechengwei Jan 23, 2022
3dda6f1
optimize code
mikechengwei Jan 23, 2022
c7815fd
Merge branch 'feature/addpassword' of github.com:mikechengwei/tidb-op…
mikechengwei Jan 23, 2022
6517afa
optimize code
mikechengwei Jan 23, 2022
427be40
Update pkg/manager/member/tidb_member_manager.go
mikechengwei Jan 23, 2022
0feac45
Update pkg/manager/member/tidb_member_manager.go
mikechengwei Jan 23, 2022
4ea3a08
Update pkg/manager/member/tidb_member_manager.go
mikechengwei Jan 23, 2022
9f4495e
optimize tls example
mikechengwei Jan 23, 2022
00e42df
Merge branch 'feature/addpassword' of github.com:mikechengwei/tidb-op…
mikechengwei Jan 23, 2022
e1cf3fd
optimize code
mikechengwei Jan 23, 2022
f58ba69
optimize code
mikechengwei Jan 23, 2022
9c89860
Update pkg/manager/member/tidb_member_manager.go
mikechengwei Jan 23, 2022
49e7afc
Update pkg/manager/member/tidb_member_manager.go
mikechengwei Jan 23, 2022
a542c0a
optimize code
mikechengwei Jan 23, 2022
a961848
Update pkg/manager/member/tidb_member_manager.go
mikechengwei Jan 23, 2022
44c7591
optimize code
mikechengwei Jan 23, 2022
2707cfb
optimize code
mikechengwei Jan 23, 2022
0b7d438
Update pkg/manager/member/tidb_member_manager.go
mikechengwei Jan 23, 2022
d7190ea
optimize code
mikechengwei Jan 23, 2022
1111249
Merge branch 'feature/addpassword' of github.com:mikechengwei/tidb-op…
mikechengwei Jan 23, 2022
aecf90a
optimize code
mikechengwei Jan 23, 2022
0f4a01a
Update pkg/manager/member/tidb_member_manager.go
mikechengwei Jan 24, 2022
56f3fc8
Update pkg/manager/member/tidb_member_manager.go
mikechengwei Jan 24, 2022
f36f667
Update pkg/manager/member/tidb_member_manager.go
mikechengwei Jan 24, 2022
fdb2411
optimize code
mikechengwei Jan 24, 2022
d08c32c
Merge branch 'feature/addpassword' of github.com:mikechengwei/tidb-op…
mikechengwei Jan 24, 2022
9186b3d
optimize code
mikechengwei Jan 24, 2022
6629a53
Update pkg/manager/member/tidb_member_manager.go
mikechengwei Jan 24, 2022
1d74087
Update pkg/manager/member/tidb_member_manager.go
mikechengwei Jan 24, 2022
be13332
Update pkg/manager/member/tidb_member_manager.go
mikechengwei Jan 24, 2022
066e064
Update pkg/manager/member/tidb_member_manager.go
mikechengwei Jan 24, 2022
d753684
Update pkg/manager/member/tidb_member_manager.go
mikechengwei Jan 24, 2022
993e9c6
Update pkg/manager/member/tidb_member_manager.go
mikechengwei Jan 24, 2022
8bb1e6d
Update pkg/util/util.go
mikechengwei Jan 24, 2022
590785c
optimize code
mikechengwei Jan 24, 2022
86c50fb
optimize code
mikechengwei Jan 24, 2022
1a7dd1b
optimize code
mikechengwei Jan 25, 2022
1fda059
optimize code
mikechengwei Jan 25, 2022
8f9156d
Merge branch 'master' into feature/addpassword
mikechengwei Jan 25, 2022
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
3 changes: 2 additions & 1 deletion cmd/backup-manager/app/backup/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
bkconstants "github.com/pingcap/tidb-operator/pkg/backup/constants"
listers "github.com/pingcap/tidb-operator/pkg/client/listers/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/controller"
pkgutil "github.com/pingcap/tidb-operator/pkg/util"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
errorutils "k8s.io/apimachinery/pkg/util/errors"
Expand Down Expand Up @@ -110,7 +111,7 @@ func (bm *Manager) ProcessBackup() error {
klog.Errorf("can't get dsn of tidb cluster %s, err: %s", bm, err)
return false, err
}
db, err = util.OpenDB(ctx, dsn)
db, err = pkgutil.OpenDB(ctx, dsn)
if err != nil {
klog.Warningf("can't connect to tidb cluster %s, err: %s", bm, err)
if ctx.Err() != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/backup-manager/app/export/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
backuputil "github.com/pingcap/tidb-operator/pkg/backup/util"
listers "github.com/pingcap/tidb-operator/pkg/client/listers/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/controller"
pkgutil "github.com/pingcap/tidb-operator/pkg/util"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
errorutils "k8s.io/apimachinery/pkg/util/errors"
Expand Down Expand Up @@ -121,7 +122,7 @@ func (bm *BackupManager) ProcessBackup() error {
return false, err
}

db, err = util.OpenDB(ctx, dsn)
db, err = pkgutil.OpenDB(ctx, dsn)
if err != nil {
klog.Warningf("can't connect to tidb cluster %s, err: %s", bm, err)
if ctx.Err() != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/backup-manager/app/restore/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
bkconstants "github.com/pingcap/tidb-operator/pkg/backup/constants"
listers "github.com/pingcap/tidb-operator/pkg/client/listers/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/controller"
pkgutil "github.com/pingcap/tidb-operator/pkg/util"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
errorutils "k8s.io/apimachinery/pkg/util/errors"
Expand Down Expand Up @@ -107,7 +108,7 @@ func (rm *Manager) ProcessRestore() error {
return false, err
}

db, err = util.OpenDB(ctx, dsn)
db, err = pkgutil.OpenDB(ctx, dsn)
if err != nil {
klog.Warningf("can't connect to tidb cluster %s, err: %s", rm, err)
if ctx.Err() != nil {
Expand Down
14 changes: 0 additions & 14 deletions cmd/backup-manager/app/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package util

import (
"context"
"database/sql"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -113,19 +112,6 @@ func GetStoragePath(backup *v1alpha1.Backup) (string, error) {
}
}

// OpenDB opens db
func OpenDB(ctx context.Context, dsn string) (*sql.DB, error) {
db, err := sql.Open("mysql", dsn)
if err != nil {
return nil, fmt.Errorf("open datasource failed, err: %v", err)
}
if err := db.PingContext(ctx); err != nil {
db.Close()
return nil, fmt.Errorf("cannot connect to mysql, err: %v", err)
}
return db, nil
}

// IsFileExist return true if file exist and is a regular file, other cases return false
func IsFileExist(file string) bool {
fi, err := os.Stat(file)
Expand Down
51 changes: 51 additions & 0 deletions docs/api-references/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -14624,6 +14624,33 @@ Kubernetes meta/v1.Time
</tr>
</tbody>
</table>
<h3 id="tidbinitializer">TiDBInitializer</h3>
<p>
(<em>Appears on:</em>
<a href="#tidbspec">TiDBSpec</a>)
</p>
<p>
</p>
<table>
<thead>
<tr>
<th>Field</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<code>createPassword</code></br>
<em>
bool
</em>
</td>
<td>
</td>
</tr>
</tbody>
</table>
<h3 id="tidbmember">TiDBMember</h3>
<p>
(<em>Appears on:</em>
Expand Down Expand Up @@ -15140,6 +15167,20 @@ TiDBProbe
the default behavior is like setting type as &ldquo;tcp&rdquo;</p>
</td>
</tr>
<tr>
<td>
<code>initializer</code></br>
<em>
<a href="#tidbinitializer">
TiDBInitializer
</a>
</em>
</td>
<td>
<em>(Optional)</em>
<p>Initializer is the init configurations of TiDB</p>
</td>
</tr>
</tbody>
</table>
<h3 id="tidbstatus">TiDBStatus</h3>
Expand Down Expand Up @@ -15226,6 +15267,16 @@ string
<td>
</td>
</tr>
<tr>
<td>
<code>passwordInitialized</code></br>
<em>
bool
</em>
</td>
<td>
</td>
</tr>
</tbody>
</table>
<h3 id="tidbtlsclient">TiDBTLSClient</h3>
Expand Down
55 changes: 55 additions & 0 deletions examples/basic-random-password/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# A Basic TiDB cluster with random password initialized

> **Note:**
>
> This setup is for test or demo purpose only and **IS NOT** applicable for critical environment.

The following steps will create a TiDB cluster with random password initialized.

## Install

The following commands is assumed to be executed in this directory.

Install the cluster:

```bash
kubectl -n <namespace> apply -f ./
```

Wait for cluster Pods ready:

```bash
watch kubectl -n <namespace> get pod
```

## Explore

Get the password from secret:

```bash
kubectl get secret basic-init -o=jsonpath='{.data.root}' -n <namespace> | base64 --decode
```

Explore the TiDB SQL interface:

```bash
kubectl -n <namespace> port-forward svc/basic-tidb 4000:4000
```

Test connection successfully:

```bash
mysql -h 127.0.0.1 -P 4000 -u root -p <password> --comments
```

## Destroy

```bash
kubectl -n <namespace> delete -f ./
```

The PVCs used by TiDB cluster will not be deleted in the above process, therefore, the PVs will be not be released neither. You can delete PVCs and release the PVs by the following command:

```bash
kubectl -n <namespace> delete pvc -l app.kubernetes.io/instance=basic,app.kubernetes.io/managed-by=tidb-operator
```
56 changes: 56 additions & 0 deletions examples/basic-random-password/tidb-cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# IT IS NOT SUITABLE FOR PRODUCTION USE.
# This YAML describes a basic TiDB cluster with minimum resource requirements,
# which should be able to run in any Kubernetes cluster with storage support.
apiVersion: pingcap.com/v1alpha1
kind: TidbCluster
metadata:
name: basic
spec:
version: v5.3.0
timezone: UTC
pvReclaimPolicy: Retain
enableDynamicConfiguration: true
configUpdateStrategy: RollingUpdate
discovery: {}
helper:
image: busybox:1.34.1
pd:
baseImage: pingcap/pd
maxFailoverCount: 0
replicas: 1
# if storageClassName is not set, the default Storage Class of the Kubernetes cluster will be used
# storageClassName: local-storage
requests:
storage: "1Gi"
config: {}
tikv:
baseImage: pingcap/tikv
maxFailoverCount: 0
# If only 1 TiKV is deployed, the TiKV region leader
# cannot be transferred during upgrade, so we have
# to configure a short timeout
evictLeaderTimeout: 1m
replicas: 1
# if storageClassName is not set, the default Storage Class of the Kubernetes cluster will be used
# storageClassName: local-storage
requests:
storage: "1Gi"
config:
storage:
# In basic examples, we set this to avoid using too much storage.
reserve-space: "0MB"
rocksdb:
# In basic examples, we set this to avoid the following error in some Kubernetes clusters:
# "the maximum number of open file descriptors is too small, got 1024, expect greater or equal to 82920"
max-open-files: 256
raftdb:
max-open-files: 256
tidb:
initializer:
createPassword: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a comment to highlight this line?

baseImage: pingcap/tidb
maxFailoverCount: 0
replicas: 1
service:
type: ClusterIP
config: {}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ require (
github.com/prometheus/prom2json v1.3.0
github.com/prometheus/prometheus v1.8.2
github.com/robfig/cron v1.1.0
github.com/sethvargo/go-password v0.2.0
github.com/sirupsen/logrus v1.6.0
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,8 @@ github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdh
github.com/seccomp/libseccomp-golang v0.9.1/go.mod h1:GbW5+tmTXfcxTToHLXlScSlAvWlF4P2Ca7zGrPiEpWo=
github.com/serenize/snaker v0.0.0-20171204205717-a683aaf2d516/go.mod h1:Yow6lPLSAXx2ifx470yD/nUe22Dv5vBvxK/UK9UUTVs=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sethvargo/go-password v0.2.0 h1:BTDl4CC/gjf/axHMaDQtw507ogrXLci6XRiLc7i/UHI=
github.com/sethvargo/go-password v0.2.0/go.mod h1:Ym4Mr9JXLBycr02MFuVQ/0JHidNetSgbzutTr3zsYXE=
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
Expand Down
7 changes: 7 additions & 0 deletions manifests/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23710,6 +23710,11 @@ spec:
- name
type: object
type: array
initializer:
properties:
createPassword:
type: boolean
type: object
labels:
additionalProperties:
type: string
Expand Down Expand Up @@ -29163,6 +29168,8 @@ spec:
- name
type: object
type: object
passwordInitialized:
type: boolean
phase:
type: string
resignDDLOwnerRetryCount:
Expand Down
7 changes: 7 additions & 0 deletions manifests/crd/v1/pingcap.com_tidbclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11764,6 +11764,11 @@ spec:
- name
type: object
type: array
initializer:
properties:
createPassword:
type: boolean
type: object
labels:
additionalProperties:
type: string
Expand Down Expand Up @@ -17217,6 +17222,8 @@ spec:
- name
type: object
type: object
passwordInitialized:
type: boolean
phase:
type: string
resignDDLOwnerRetryCount:
Expand Down
7 changes: 7 additions & 0 deletions manifests/crd/v1beta1/pingcap.com_tidbclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11748,6 +11748,11 @@ spec:
- name
type: object
type: array
initializer:
properties:
createPassword:
type: boolean
type: object
labels:
additionalProperties:
type: string
Expand Down Expand Up @@ -17194,6 +17199,8 @@ spec:
- name
type: object
type: object
passwordInitialized:
type: boolean
phase:
type: string
resignDDLOwnerRetryCount:
Expand Down
7 changes: 7 additions & 0 deletions manifests/crd_v1beta1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23694,6 +23694,11 @@ spec:
- name
type: object
type: array
initializer:
properties:
createPassword:
type: boolean
type: object
labels:
additionalProperties:
type: string
Expand Down Expand Up @@ -29140,6 +29145,8 @@ spec:
- name
type: object
type: object
passwordInitialized:
type: boolean
phase:
type: string
resignDDLOwnerRetryCount:
Expand Down
8 changes: 7 additions & 1 deletion pkg/apis/pingcap/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkg/apis/pingcap/v1alpha1/tidbcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,10 @@ func (tc *TidbCluster) IsTLSClusterEnabled() bool {
return tc.Spec.TLSCluster != nil && tc.Spec.TLSCluster.Enabled
}

func (tc *TidbCluster) NeedToSyncTiDBInitializer() bool {
return tc.Spec.TiDB != nil && tc.Spec.TiDB.Initializer != nil && tc.Spec.TiDB.Initializer.CreatePassword && tc.Status.TiDB.PasswordInitialized == nil
}

func (tc *TidbCluster) Scheme() string {
if tc.IsTLSClusterEnabled() {
return "https"
Expand Down
Loading