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

add controller for tidb initializer #1403

Merged
merged 5 commits into from
Dec 26, 2019
Merged

Conversation

DanielZhangQD
Copy link
Contributor

What problem does this PR solve?

fix #1262

What is changed and how does it work?

Add controller for TidbInitializer CR

Check List

Tests

  • Manual test (add detailed scripts or steps below)

Code changes

  • Has Go code change
  • Has documents change

Side effects

Related changes

  • Need to update the documentation

Does this PR introduce a user-facing change?:

Support initializing TiDB Cluster with TidbInitializer Custom Resource.

@DanielZhangQD
Copy link
Contributor Author

Manual test passed, UT and e2e test will be done in #1404.

pkg/apis/pingcap/v1alpha1/tidbcluster.go Outdated Show resolved Hide resolved
@@ -71,13 +71,17 @@ type TidbInitializerSpec struct {

// InitSqlConfigMapName reference a configmap that provide init-sql, take high precedence than initSql if set
// +optional
InitSqlConfigMap *corev1.LocalObjectReference `json:"initSqlConfigMap,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be more extensible to use a struct, for example, we could support reference configmap cross namespaces by adding namespace to the object, that is, change LocalObjectReference to ObjectReference

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A Pod cannot mount a configmap from other ns, from the definition of ConfigMapVolumeSource, only LocalObjectReference is included.

pkg/manager/member/tidb_init_manager.go Outdated Show resolved Hide resolved
pkg/controller/controller_utils.go Outdated Show resolved Hide resolved
pkg/manager/member/tidb_init_manager.go Outdated Show resolved Hide resolved
pkg/manager/member/tidb_init_manager.go Outdated Show resolved Hide resolved
pkg/manager/member/tidb_init_manager.go Outdated Show resolved Hide resolved
pkg/label/label.go Outdated Show resolved Hide resolved
@cofyc
Copy link
Contributor

cofyc commented Dec 25, 2019

do we have a basic TiDBInitializer e2e case right now? if not, I prefer adding one with this feature PR instead of another PR. This help reviewers to review PR.

now, we can create tidb cluster without helm, it's very easy to start a tidb cluster. A basic e2e case can be:

  • create a TiDBInitializer object
  • create a TiDBCluster object
  • wait and verify tidb cluster is ready in a certain time

This is an example e2e case which create and verify tidb cluster without helm: https://github.com/pingcap/tidb-operator/pull/1396/files#diff-92579a43c91e473b9716eeeee2068004R653

@DanielZhangQD
Copy link
Contributor Author

@onlymellb @aylei comments addressed, PTAL.
@cofyc I will add e2e soon.

Copy link
Contributor

@aylei aylei left a comment

Choose a reason for hiding this comment

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

Rest LGTM

Comment on lines 84 to 88
name := controller.TiDBInitializerMemberName(ti.Spec.Clusters.Name)
ns := ti.Namespace
cm := &corev1.ConfigMap{}

exist, err := tm.typedControl.Exist(client.ObjectKey{
Namespace: ns,
Name: name,
}, cm)
if err != nil {
return err
}
if !exist {
err = tm.syncTiDBInitConfigMap(ti)
if err != nil {
return err
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of check-and-create, it is recommended to create the object directly and expect no err or an AlreadyExisted error if the only purpose of the check is to determine whether we should create the object, which is atomic compared to two separate API calls.

Also, this could make the logic simple and the name calculation of TiDB init configmap don't have to be maintained in two places

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For a one-time deal, I agree that we can just create the object, but here is the logic in sync, it will be done at least periodically, it's not reasonable to create the object each time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check before creation can reduce the API call to kube-apiserver, and I have moved the check block to separate sync functions.

Comment on lines 102 to 112
job, err := tm.jobLister.Jobs(ns).Get(name)
if err == nil {
// job has been created
return tm.updateStatus(ti.DeepCopy(), job)
}

if !errors.IsNotFound(err) {
return fmt.Errorf("TiDBInitializer %s/%s get job %s failed, err: %v", ns, ti.Name, name, err)
}

return tm.syncTiDBInitJob(ti)
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check before creation can reduce the API call to kube-apiserver, and I have moved the check block to separate sync functions.

pkg/manager/member/tidb_init_manager.go Outdated Show resolved Hide resolved
pkg/manager/member/tidb_init_manager.go Outdated Show resolved Hide resolved
Comment on lines 132 to 130
var update bool
if !apiequality.Semantic.DeepEqual(ti.Status.JobStatus, job.Status) {
job.Status.DeepCopyInto(&ti.Status.JobStatus)
update = true
}
if ti.Status.Phase != phase {
ti.Status.Phase = phase
update = true
}
if update {
return tm.retryUpdateStatus(ti)
}
return nil
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be simplified using GuaranteedUpdate, we don't have to check the equality in client-side actually, if the update do not mutate the object, the resourceVersion of object won't be changed, that is, the object will not be affected at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check before creation can reduce the API call to kube-apiserver, and I have updated to use GuaranteedUpdate.

@DanielZhangQD
Copy link
Contributor Author

@aylei comments addressed, PTAL again.

Copy link
Contributor

@onlymellb onlymellb left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@aylei aylei left a comment

Choose a reason for hiding this comment

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

LGTM

@aylei
Copy link
Contributor

aylei commented Dec 26, 2019

/merge

@sre-bot
Copy link
Contributor

sre-bot commented Dec 26, 2019

/run-all-tests

@sre-bot
Copy link
Contributor

sre-bot commented Dec 26, 2019

/run-all-tests

@sre-bot sre-bot merged commit eb67f58 into pingcap:master Dec 26, 2019
yahonda pushed a commit that referenced this pull request Dec 27, 2021
* zh: doc for how to evict tikv pod

* zh: fix script syntax

* zh: add notes for deprecated commands

* zh: add doc to maintain PD instance

* Update maintain-a-kubernetes-node.md

* fix broken link

* Update maintain-a-kubernetes-node.md

* Update maintain-a-kubernetes-node.md

* Update maintain-a-kubernetes-node.md

* Apply suggestions from code review

Co-authored-by: Grace Cai <qqzczy@126.com>

* fix comments

* Apply suggestions from code review

Co-authored-by: Grace Cai <qqzczy@126.com>

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: Grace Cai <qqzczy@126.com>

* fix lint

* fix lint

Co-authored-by: DanielZhangQD <36026334+DanielZhangQD@users.noreply.github.com>
Co-authored-by: Grace Cai <qqzczy@126.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New custom resource TidbInitializer
5 participants