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 5 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
51 changes: 51 additions & 0 deletions docs/api-references/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -14552,6 +14552,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 @@ -15068,6 +15095,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 @@ -15154,6 +15195,16 @@ string
<td>
</td>
</tr>
<tr>
<td>
<code>initPassword</code></br>
<em>
bool
</em>
</td>
<td>
</td>
</tr>
</tbody>
</table>
<h3 id="tidbtlsclient">TiDBTLSClient</h3>
Expand Down
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
9 changes: 9 additions & 0 deletions manifests/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23746,6 +23746,13 @@ spec:
- name
type: object
type: array
initializer:
properties:
createPassword:
type: boolean
required:
- createPassword
type: object
labels:
additionalProperties:
type: string
Expand Down Expand Up @@ -29171,6 +29178,8 @@ spec:
type: object
image:
type: string
initPassword:
type: boolean
members:
additionalProperties:
properties:
Expand Down
9 changes: 9 additions & 0 deletions manifests/crd/v1/pingcap.com_tidbclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11759,6 +11759,13 @@ spec:
- name
type: object
type: array
initializer:
properties:
createPassword:
type: boolean
required:
- createPassword
type: object
labels:
additionalProperties:
type: string
Expand Down Expand Up @@ -17184,6 +17191,8 @@ spec:
type: object
image:
type: string
initPassword:
type: boolean
members:
additionalProperties:
properties:
Expand Down
9 changes: 9 additions & 0 deletions manifests/crd/v1beta1/pingcap.com_tidbclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11743,6 +11743,13 @@ spec:
- name
type: object
type: array
initializer:
properties:
createPassword:
type: boolean
required:
- createPassword
type: object
labels:
additionalProperties:
type: string
Expand Down Expand Up @@ -17161,6 +17168,8 @@ spec:
type: object
image:
type: string
initPassword:
type: boolean
members:
additionalProperties:
properties:
Expand Down
9 changes: 9 additions & 0 deletions manifests/crd_v1beta1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23730,6 +23730,13 @@ spec:
- name
type: object
type: array
initializer:
properties:
createPassword:
type: boolean
required:
- createPassword
type: object
labels:
additionalProperties:
type: string
Expand Down Expand Up @@ -29148,6 +29155,8 @@ spec:
type: object
image:
type: string
initPassword:
type: boolean
members:
additionalProperties:
properties:
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.

10 changes: 10 additions & 0 deletions pkg/apis/pingcap/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,15 @@ type TiDBSpec struct {
// the default behavior is like setting type as "tcp"
// +optional
ReadinessProbe *TiDBProbe `json:"readinessProbe,omitempty"`

// Initializer is the init configurations of TiDB
//
// +optional
Initializer *TiDBInitializer `json:"initializer,omitempty"`
}

type TiDBInitializer struct {
CreatePassword bool `json:"createPassword"`
mikechengwei marked this conversation as resolved.
Show resolved Hide resolved
}

const (
Expand Down Expand Up @@ -1120,6 +1129,7 @@ type TiDBStatus struct {
FailureMembers map[string]TiDBFailureMember `json:"failureMembers,omitempty"`
ResignDDLOwnerRetryCount int32 `json:"resignDDLOwnerRetryCount,omitempty"`
Image string `json:"image,omitempty"`
InitPassword bool `json:"initPassword,omitempty"`
}

// TiDBMember is TiDB member
Expand Down
21 changes: 21 additions & 0 deletions pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go

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

21 changes: 21 additions & 0 deletions pkg/controller/generic_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"fmt"
"strings"

"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"

"github.com/pingcap/tidb-operator/pkg/scheme"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -62,6 +64,8 @@ type TypedControlInterface interface {
CreateOrUpdateIngress(controller client.Object, ingress *networkingv1.Ingress) (*networkingv1.Ingress, error)
// CreateOrUpdateIngressV1beta1 create the desired v1beta1 ingress or update the current one to desired state if already existed
CreateOrUpdateIngressV1beta1(controller client.Object, ingress *extensionsv1beta1.Ingress) (*extensionsv1beta1.Ingress, error)
// CreateOrUpdateTidbInitializer create the desired v1alpha1 TidbInitializer or update the current one to desired state if already existed
CreateOrUpdateTidbInitializer(controller client.Object, tidbInitializer *v1alpha1.TidbInitializer) (*v1alpha1.TidbInitializer, error)
// UpdateStatus update the /status subresource of the object
UpdateStatus(newStatus client.Object) error
// Delete delete the given object from the cluster
Expand Down Expand Up @@ -263,6 +267,23 @@ func (w *typedWrapper) CreateOrUpdateConfigMap(controller client.Object, cm *cor
return result.(*corev1.ConfigMap), nil
}

func (w *typedWrapper) CreateOrUpdateTidbInitializer(controller client.Object, tidbInitializer *v1alpha1.TidbInitializer) (*v1alpha1.TidbInitializer, error) {
mikechengwei marked this conversation as resolved.
Show resolved Hide resolved
result, err := w.GenericControlInterface.CreateOrUpdate(controller, tidbInitializer, func(existing, desired client.Object) error {
existingInitializer := existing.(*v1alpha1.TidbInitializer)
desiredInitializer := desired.(*v1alpha1.TidbInitializer)

existingInitializer.Labels = desiredInitializer.Labels
for k, v := range desiredInitializer.Annotations {
existingInitializer.Annotations[k] = v
}
return nil
}, true)
if err != nil {
return nil, err
}
return result.(*v1alpha1.TidbInitializer), nil
}

func (w *typedWrapper) CreateOrUpdateService(controller client.Object, svc *corev1.Service) (*corev1.Service, error) {
result, err := w.GenericControlInterface.CreateOrUpdate(controller, svc, func(existing, desired client.Object) error {
existingSvc := existing.(*corev1.Service)
Expand Down
67 changes: 67 additions & 0 deletions pkg/manager/member/tidb_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"strconv"
"strings"

"github.com/pingcap/tidb-operator/pkg/backup/constants"

"github.com/pingcap/tidb-operator/pkg/apis/label"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/controller"
Expand All @@ -39,6 +41,7 @@ import (
"k8s.io/klog/v2"
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
Expand Down Expand Up @@ -241,10 +244,74 @@ func (m *tidbMemberManager) syncTiDBStatefulSetForTidbCluster(tc *v1alpha1.TidbC
return err
}
}
// set random password
mikechengwei marked this conversation as resolved.
Show resolved Hide resolved
if tc.Spec.TiDB.Initializer != nil && tc.Spec.TiDB.Initializer.CreatePassword && !tc.Status.TiDB.InitPassword {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it better we define a phase in the status as we do for the initializer here https://github.com/pingcap/tidb-operator/blob/master/pkg/apis/pingcap/v1alpha1/tidbinitializer_types.go#L109 so that we can know if the password creation is ongoing, successful, or failed?
The bool InitPassword cannot represent all the statuses.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok,i use same type as the phase field in tidbinitializer.

// sync password secret
secret := m.buildRandomPasswordSecret(tc)
secret, err := m.deps.TypedControl.CreateOrUpdateSecret(tc, secret)
if err != nil {
return err
}
tidbInitializer := &v1alpha1.TidbInitializer{
TypeMeta: metav1.TypeMeta{
Kind: "TidbCluster",
APIVersion: "pingcap.com/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-init", tc.Name),
Namespace: tc.Namespace,
},
Spec: v1alpha1.TidbInitializerSpec{
Clusters: v1alpha1.TidbClusterRef{
Name: tc.Name,
Namespace: tc.Namespace,
},
PasswordSecret: pointer.StringPtr(secret.Name),
},
}

exist, err := m.deps.TypedControl.Exist(client.ObjectKey{
Namespace: tidbInitializer.Namespace,
Name: tidbInitializer.Name,
}, tidbInitializer)
if err != nil {
return err
}
if !exist {
_, err = m.deps.TypedControl.CreateOrUpdateTidbInitializer(tc, tidbInitializer)
if err != nil {
return err
}

}

existInitializer, err := m.deps.TiDBInitializerLister.TidbInitializers(tidbInitializer.Namespace).Get(tidbInitializer.Name)
if err != nil {
return err
}
if existInitializer.Status.Phase == v1alpha1.InitializePhaseCompleted {
tc.Status.TiDB.InitPassword = true
}
}

return mngerutils.UpdateStatefulSet(m.deps.StatefulSetControl, tc, newTiDBSet, oldTiDBSet)
}

func (m *tidbMemberManager) buildRandomPasswordSecret(tc *v1alpha1.TidbCluster) *corev1.Secret {

s := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-secret", tc.Name),
mikechengwei marked this conversation as resolved.
Show resolved Hide resolved
Namespace: tc.Namespace,
mikechengwei marked this conversation as resolved.
Show resolved Hide resolved
},
}
password := util.FixedLengthRandomPasswordBytes()
s.Data = map[string][]byte{
constants.TidbPasswordKey: password,
}
return s
}

func (m *tidbMemberManager) shouldRecover(tc *v1alpha1.TidbCluster) bool {
if tc.Status.TiDB.FailureMembers == nil {
return false
Expand Down
Loading