-
Notifications
You must be signed in to change notification settings - Fork 499
/
tidbcluster.go
925 lines (837 loc) · 36.9 KB
/
tidbcluster.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
// Copyright 2019 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package tidbcluster
import (
"context"
"fmt"
_ "net/http/pprof"
"strconv"
"strings"
"time"
"github.com/onsi/ginkgo"
"github.com/onsi/gomega"
"github.com/pingcap/advanced-statefulset/pkg/apis/apps/v1/helper"
asclientset "github.com/pingcap/advanced-statefulset/pkg/client/clientset/versioned"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/client/clientset/versioned"
"github.com/pingcap/tidb-operator/pkg/controller"
"github.com/pingcap/tidb-operator/pkg/features"
"github.com/pingcap/tidb-operator/pkg/label"
"github.com/pingcap/tidb-operator/pkg/manager/member"
"github.com/pingcap/tidb-operator/pkg/scheme"
operatorUtils "github.com/pingcap/tidb-operator/pkg/util"
tcconfig "github.com/pingcap/tidb-operator/pkg/util/config"
"github.com/pingcap/tidb-operator/tests"
"github.com/pingcap/tidb-operator/tests/apiserver"
e2econfig "github.com/pingcap/tidb-operator/tests/e2e/config"
utilimage "github.com/pingcap/tidb-operator/tests/e2e/util/image"
"github.com/pingcap/tidb-operator/tests/e2e/util/portforward"
"github.com/pingcap/tidb-operator/tests/pkg/apimachinery"
"github.com/pingcap/tidb-operator/tests/pkg/blockwriter"
"github.com/pingcap/tidb-operator/tests/pkg/fixture"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
utilversion "k8s.io/apimachinery/pkg/util/version"
"k8s.io/apimachinery/pkg/util/wait"
clientset "k8s.io/client-go/kubernetes"
typedappsv1 "k8s.io/client-go/kubernetes/typed/apps/v1"
restclient "k8s.io/client-go/rest"
"k8s.io/klog"
aggregatorclient "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"
"k8s.io/kubernetes/test/e2e/framework"
e2elog "k8s.io/kubernetes/test/e2e/framework/log"
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
"k8s.io/kubernetes/test/utils"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var _ = ginkgo.Describe("[tidb-operator] TiDBCluster", func() {
f := framework.NewDefaultFramework("tidb-cluster")
var ns string
var c clientset.Interface
var cli versioned.Interface
var asCli asclientset.Interface
var aggrCli aggregatorclient.Interface
var apiExtCli apiextensionsclientset.Interface
var oa tests.OperatorActions
var cfg *tests.Config
var config *restclient.Config
var ocfg *tests.OperatorConfig
var genericCli client.Client
var fwCancel context.CancelFunc
var fw portforward.PortForward
/**
* StatefulSet or AdvancedStatefulSet interface.
*/
var stsGetter func(namespace string) typedappsv1.StatefulSetInterface
ginkgo.BeforeEach(func() {
ns = f.Namespace.Name
c = f.ClientSet
var err error
config, err = framework.LoadConfig()
framework.ExpectNoError(err, "failed to load config")
cli, err = versioned.NewForConfig(config)
framework.ExpectNoError(err, "failed to create clientset")
asCli, err = asclientset.NewForConfig(config)
framework.ExpectNoError(err, "failed to create clientset")
genericCli, err = client.New(config, client.Options{Scheme: scheme.Scheme})
framework.ExpectNoError(err, "failed to create clientset")
aggrCli, err = aggregatorclient.NewForConfig(config)
framework.ExpectNoError(err, "failed to create clientset")
apiExtCli, err = apiextensionsclientset.NewForConfig(config)
framework.ExpectNoError(err, "failed to create clientset")
clientRawConfig, err := e2econfig.LoadClientRawConfig()
framework.ExpectNoError(err, "failed to load raw config")
ctx, cancel := context.WithCancel(context.Background())
fw, err = portforward.NewPortForwarder(ctx, e2econfig.NewSimpleRESTClientGetter(clientRawConfig))
framework.ExpectNoError(err, "failed to create port forwarder")
fwCancel = cancel
cfg = e2econfig.TestConfig
ocfg = e2econfig.NewDefaultOperatorConfig(cfg)
if ocfg.Enabled(features.AdvancedStatefulSet) {
stsGetter = helper.NewHijackClient(c, asCli).AppsV1().StatefulSets
} else {
stsGetter = c.AppsV1().StatefulSets
}
oa = tests.NewOperatorActions(cli, c, asCli, aggrCli, apiExtCli, tests.DefaultPollInterval, ocfg, e2econfig.TestConfig, nil, fw, f)
})
ginkgo.AfterEach(func() {
if fwCancel != nil {
fwCancel()
}
})
ginkgo.Context("Basic: Deploying, Scaling, Update Configuration", func() {
clusterCfgs := []struct {
Version string
Name string // helm release name, should not conflict with names used in other tests
Values map[string]string
}{
{
Version: utilimage.TiDBV3Version,
Name: "basic-v3",
},
{
Version: utilimage.TiDBV2Version,
Name: "basic-v2",
Values: map[string]string{
// verify v2.1.x configuration compatibility
// https://github.com/pingcap/tidb-operator/pull/950
"tikv.resources.limits.storage": "1G",
},
},
{
Version: utilimage.TiDBTLSVersion,
Name: "basic-v3-cluster-tls",
Values: map[string]string{
"enableTLSCluster": "true",
},
},
}
for _, clusterCfg := range clusterCfgs {
localCfg := clusterCfg
ginkgo.It(fmt.Sprintf("[TiDB Version: %s] %s", localCfg.Version, localCfg.Name), func() {
cluster := newTidbClusterConfig(e2econfig.TestConfig, ns, localCfg.Name, "", localCfg.Version)
if len(localCfg.Values) > 0 {
for k, v := range localCfg.Values {
cluster.Resources[k] = v
}
}
// support reclaim pv when scale in tikv or pd component
cluster.EnablePVReclaim = true
oa.DeployTidbClusterOrDie(&cluster)
oa.CheckTidbClusterStatusOrDie(&cluster)
oa.CheckDisasterToleranceOrDie(&cluster)
oa.CheckInitSQLOrDie(&cluster)
// scale
cluster.ScaleTiDB(3).ScaleTiKV(5).ScalePD(5)
oa.ScaleTidbClusterOrDie(&cluster)
oa.CheckTidbClusterStatusOrDie(&cluster)
oa.CheckDisasterToleranceOrDie(&cluster)
cluster.ScaleTiDB(2).ScaleTiKV(4).ScalePD(3)
oa.ScaleTidbClusterOrDie(&cluster)
oa.CheckTidbClusterStatusOrDie(&cluster)
oa.CheckDisasterToleranceOrDie(&cluster)
// configuration change
cluster.EnableConfigMapRollout = true
cluster.UpdatePdMaxReplicas(cfg.PDMaxReplicas).
UpdateTiKVGrpcConcurrency(cfg.TiKVGrpcConcurrency).
UpdateTiDBTokenLimit(cfg.TiDBTokenLimit)
oa.UpgradeTidbClusterOrDie(&cluster)
oa.CheckTidbClusterStatusOrDie(&cluster)
})
}
})
/**
* This test case switches back and forth between pod network and host network of a single cluster.
* Note that only one cluster can run in host network mode at the same time.
*/
ginkgo.It("Switching back and forth between pod network and host network", func() {
if !ocfg.Enabled(features.AdvancedStatefulSet) {
serverVersion, err := c.Discovery().ServerVersion()
framework.ExpectNoError(err, "failed to fetch Kubernetes version")
sv := utilversion.MustParseSemantic(serverVersion.GitVersion)
klog.Infof("ServerVersion: %v", serverVersion.String())
if sv.LessThan(utilversion.MustParseSemantic("v1.13.11")) || // < v1.13.11
(sv.AtLeast(utilversion.MustParseSemantic("v1.14.0")) && sv.LessThan(utilversion.MustParseSemantic("v1.14.7"))) || // >= v1.14.0 but < v1.14.7
(sv.AtLeast(utilversion.MustParseSemantic("v1.15.0")) && sv.LessThan(utilversion.MustParseSemantic("v1.15.4"))) { // >= v1.15.0 but < v1.15.4
// https://github.com/pingcap/tidb-operator/issues/1042#issuecomment-547742565
framework.Skipf("Skipping HostNetwork test. Kubernetes %v has a bug that StatefulSet may apply revision incorrectly, HostNetwork cannot work well in this cluster", serverVersion)
}
ginkgo.By(fmt.Sprintf("Testing HostNetwork feature with Kubernetes %v", serverVersion))
} else {
ginkgo.By("Testing HostNetwork feature with Advanced StatefulSet")
}
cluster := newTidbClusterConfig(e2econfig.TestConfig, ns, "host-network", "", "")
cluster.Resources["pd.replicas"] = "1"
cluster.Resources["tidb.replicas"] = "1"
cluster.Resources["tikv.replicas"] = "1"
oa.DeployTidbClusterOrDie(&cluster)
ginkgo.By("switch to host network")
cluster.RunInHost(true)
oa.UpgradeTidbClusterOrDie(&cluster)
oa.CheckTidbClusterStatusOrDie(&cluster)
ginkgo.By("switch back to pod network")
cluster.RunInHost(false)
oa.UpgradeTidbClusterOrDie(&cluster)
oa.CheckTidbClusterStatusOrDie(&cluster)
})
ginkgo.It("Upgrading TiDB Cluster", func() {
cluster := newTidbClusterConfig(e2econfig.TestConfig, ns, "cluster", "admin", "")
cluster.Resources["pd.replicas"] = "3"
ginkgo.By("Creating webhook certs and self signing it")
svcName := "webhook"
certCtx, err := apimachinery.SetupServerCert(ns, svcName)
framework.ExpectNoError(err, fmt.Sprintf("unable to setup certs for webservice %s", tests.WebhookServiceName))
ginkgo.By("Starting webhook pod")
webhookPod, svc := startWebhook(f, cfg.E2EImage, ns, svcName, certCtx.Cert, certCtx.Key)
ginkgo.By("Register webhook")
oa.RegisterWebHookAndServiceOrDie(ocfg.WebhookConfigName, ns, svc.Name, certCtx)
ginkgo.By(fmt.Sprintf("Deploying tidb cluster %s", cluster.ClusterVersion))
oa.DeployTidbClusterOrDie(&cluster)
oa.CheckTidbClusterStatusOrDie(&cluster)
oa.CheckDisasterToleranceOrDie(&cluster)
upgradeVersions := cfg.GetUpgradeTidbVersionsOrDie()
ginkgo.By(fmt.Sprintf("Upgrading tidb cluster from %s to %s", cluster.ClusterVersion, upgradeVersions[0]))
ctx, cancel := context.WithCancel(context.Background())
assignedNodes := oa.GetTidbMemberAssignedNodesOrDie(&cluster)
cluster.UpgradeAll(upgradeVersions[0])
oa.UpgradeTidbClusterOrDie(&cluster)
oa.CheckUpgradeOrDie(ctx, &cluster)
oa.CheckTidbClusterStatusOrDie(&cluster)
oa.CheckTidbMemberAssignedNodesOrDie(&cluster, assignedNodes)
cancel()
ginkgo.By("Check webhook is still running")
webhookPod, err = c.CoreV1().Pods(webhookPod.Namespace).Get(webhookPod.Name, metav1.GetOptions{})
framework.ExpectNoError(err, fmt.Sprintf("unable to get pod %s/%s", webhookPod.Namespace, webhookPod.Name))
if webhookPod.Status.Phase != v1.PodRunning {
logs, err := e2epod.GetPodLogs(c, webhookPod.Namespace, webhookPod.Name, "webhook")
framework.ExpectNoError(err)
e2elog.Logf("webhook logs: %s", logs)
e2elog.Fail("webhook pod is not running")
}
oa.CleanWebHookAndServiceOrDie(ocfg.WebhookConfigName)
})
ginkgo.It("Backup and restore TiDB Cluster", func() {
clusterA := newTidbClusterConfig(e2econfig.TestConfig, ns, "cluster3", "admin", "")
clusterB := newTidbClusterConfig(e2econfig.TestConfig, ns, "cluster4", "admin", "")
oa.DeployTidbClusterOrDie(&clusterA)
oa.DeployTidbClusterOrDie(&clusterB)
oa.CheckTidbClusterStatusOrDie(&clusterA)
oa.CheckTidbClusterStatusOrDie(&clusterB)
oa.CheckDisasterToleranceOrDie(&clusterA)
oa.CheckDisasterToleranceOrDie(&clusterB)
ginkgo.By(fmt.Sprintf("Begin inserting data into cluster %q", clusterA.ClusterName))
oa.BeginInsertDataToOrDie(&clusterA)
// backup and restore
ginkgo.By(fmt.Sprintf("Backup %q and restore into %q", clusterA.ClusterName, clusterB.ClusterName))
oa.BackupRestoreOrDie(&clusterA, &clusterB)
ginkgo.By(fmt.Sprintf("Stop inserting data into cluster %q", clusterA.ClusterName))
oa.StopInsertDataTo(&clusterA)
})
ginkgo.It("Test aggregated apiserver", func() {
ginkgo.By(fmt.Sprintf("Starting to test apiserver, test apiserver image: %s", cfg.E2EImage))
framework.Logf("config: %v", config)
aaCtx := apiserver.NewE2eContext(ns, config, cfg.E2EImage)
defer aaCtx.Clean()
aaCtx.Setup()
aaCtx.Do()
})
ginkgo.It("Service: Sync TiDB service", func() {
cluster := newTidbClusterConfig(e2econfig.TestConfig, ns, "service-it", "admin", "")
cluster.Resources["pd.replicas"] = "1"
cluster.Resources["tidb.replicas"] = "1"
cluster.Resources["tikv.replicas"] = "1"
oa.DeployTidbClusterOrDie(&cluster)
oa.CheckTidbClusterStatusOrDie(&cluster)
ns := cluster.Namespace
tcName := cluster.ClusterName
oldSvc, err := c.CoreV1().Services(ns).Get(controller.TiDBMemberName(tcName), metav1.GetOptions{})
framework.ExpectNoError(err, "Expected TiDB service created by helm chart")
tc, err := cli.PingcapV1alpha1().TidbClusters(ns).Get(tcName, metav1.GetOptions{})
framework.ExpectNoError(err, "Expected TiDB cluster created by helm chart")
if isNil, err := gomega.BeNil().Match(metav1.GetControllerOf(oldSvc)); !isNil {
e2elog.Failf("Expected TiDB service created by helm chart is orphaned: %v", err)
}
ginkgo.By(fmt.Sprintf("Adopt orphaned service created by helm"))
err = controller.GuaranteedUpdate(genericCli, tc, func() error {
tc.Spec.TiDB.Service = &v1alpha1.TiDBServiceSpec{}
return nil
})
framework.ExpectNoError(err, "Expected update TiDB cluster")
err = wait.PollImmediate(5*time.Second, 5*time.Minute, func() (bool, error) {
svc, err := c.CoreV1().Services(ns).Get(controller.TiDBMemberName(tcName), metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
return false, err
}
e2elog.Logf("error get TiDB service: %v", err)
return false, nil
}
owner := metav1.GetControllerOf(svc)
if owner == nil {
e2elog.Logf("tidb service has not been adopted by TidbCluster yet")
return false, nil
}
framework.ExpectEqual(metav1.IsControlledBy(svc, tc), true, "Expected owner is TidbCluster")
framework.ExpectEqual(svc.Spec.ClusterIP, oldSvc.Spec.ClusterIP, "ClusterIP should be stable across adopting and updating")
return true, nil
})
framework.ExpectNoError(err)
ginkgo.By("Sync TiDB service properties")
ginkgo.By("Updating TiDB service")
svcType := corev1.ServiceTypeNodePort
trafficPolicy := corev1.ServiceExternalTrafficPolicyTypeLocal
err = controller.GuaranteedUpdate(genericCli, tc, func() error {
tc.Spec.TiDB.Service.Type = svcType
tc.Spec.TiDB.Service.ExternalTrafficPolicy = &trafficPolicy
tc.Spec.TiDB.Service.Annotations = map[string]string{
"test": "test",
}
return nil
})
framework.ExpectNoError(err)
ginkgo.By("Waiting for the TiDB service to be synced")
err = wait.PollImmediate(5*time.Second, 5*time.Minute, func() (bool, error) {
svc, err := c.CoreV1().Services(ns).Get(controller.TiDBMemberName(tcName), metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
return false, err
}
e2elog.Logf("error get TiDB service: %v", err)
return false, nil
}
if isEqual, err := gomega.Equal(svcType).Match(svc.Spec.Type); !isEqual {
e2elog.Logf("tidb service is not synced, %v", err)
return false, nil
}
if isEqual, err := gomega.Equal(trafficPolicy).Match(svc.Spec.ExternalTrafficPolicy); !isEqual {
e2elog.Logf("tidb service is not synced, %v", err)
return false, nil
}
if haveKV, err := gomega.HaveKeyWithValue("test", "test").Match(svc.Annotations); !haveKV {
e2elog.Logf("tidb service is not synced, %v", err)
return false, nil
}
return true, nil
})
framework.ExpectNoError(err)
})
updateStrategy := v1alpha1.ConfigUpdateStrategyInPlace
// Basic IT for managed in TidbCluster CR
// TODO: deploy pump through CR in backup and restore IT
ginkgo.It("Pump: Test managing Pump in TidbCluster CRD", func() {
cluster := newTidbClusterConfig(e2econfig.TestConfig, ns, "pump-it", "admin", "")
cluster.Resources["pd.replicas"] = "1"
cluster.Resources["tikv.replicas"] = "1"
cluster.Resources["tidb.replicas"] = "1"
oa.DeployTidbClusterOrDie(&cluster)
oa.CheckTidbClusterStatusOrDie(&cluster)
ginkgo.By("Test adopting pump statefulset created by helm could avoid rolling-update.")
err := oa.DeployAndCheckPump(&cluster)
framework.ExpectNoError(err, "Expected pump deployed")
tc, err := cli.PingcapV1alpha1().TidbClusters(cluster.Namespace).Get(cluster.ClusterName, metav1.GetOptions{})
framework.ExpectNoError(err, "Expected get tidbcluster")
// If using advanced statefulset, we must upgrade all Kubernetes statefulsets to advanced statefulsets first.
if ocfg.Enabled(features.AdvancedStatefulSet) {
stsList, err := c.AppsV1().StatefulSets(ns).List(metav1.ListOptions{})
framework.ExpectNoError(err)
for _, sts := range stsList.Items {
_, err = helper.Upgrade(c, asCli, &sts)
framework.ExpectNoError(err)
}
}
oldPumpSet, err := stsGetter(tc.Namespace).Get(controller.PumpMemberName(tc.Name), metav1.GetOptions{})
framework.ExpectNoError(err, "Expected get pump statefulset")
oldRev := oldPumpSet.Status.CurrentRevision
framework.ExpectEqual(oldPumpSet.Status.UpdateRevision, oldRev, "Expected pump is not upgrading")
err = controller.GuaranteedUpdate(genericCli, tc, func() error {
pullPolicy := corev1.PullIfNotPresent
tc.Spec.Pump = &v1alpha1.PumpSpec{
BaseImage: "pingcap/tidb-binlog",
ComponentSpec: v1alpha1.ComponentSpec{
Version: &cluster.ClusterVersion,
ImagePullPolicy: &pullPolicy,
Affinity: &corev1.Affinity{
PodAntiAffinity: &corev1.PodAntiAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{
{
PodAffinityTerm: corev1.PodAffinityTerm{
Namespaces: []string{cluster.Namespace},
TopologyKey: "rack",
},
Weight: 50,
},
},
},
},
Tolerations: []corev1.Toleration{
{
Effect: corev1.TaintEffectNoSchedule,
Key: "node-role",
Operator: corev1.TolerationOpEqual,
Value: "tidb",
},
},
SchedulerName: pointer.StringPtr("default-scheduler"),
ConfigUpdateStrategy: &updateStrategy,
},
Replicas: 1,
StorageClassName: pointer.StringPtr("local-storage"),
ResourceRequirements: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceStorage: resource.MustParse("10Gi"),
},
},
GenericConfig: tcconfig.New(map[string]interface{}{
"addr": "0.0.0.0:8250",
"gc": 7,
"data-dir": "/data",
"heartbeat-interval": 2,
}),
}
return nil
})
framework.ExpectNoError(err, "Expected update tc")
err = wait.PollImmediate(5*time.Second, 5*time.Minute, func() (bool, error) {
pumpSet, err := stsGetter(tc.Namespace).Get(controller.PumpMemberName(tc.Name), metav1.GetOptions{})
if errors.IsNotFound(err) {
return false, err
}
if err != nil {
e2elog.Logf("error get pump statefulset: %v", err)
return false, nil
}
if !metav1.IsControlledBy(pumpSet, tc) {
e2elog.Logf("expect pump staetfulset adopted by tidbcluster, still waiting...")
return false, nil
}
// The desired state encoded in CRD should be exactly same with the one created by helm chart
framework.ExpectEqual(pumpSet.Status.CurrentRevision, oldRev, "Expected no rolling-update when adopting pump statefulset")
framework.ExpectEqual(pumpSet.Status.UpdateRevision, oldRev, "Expected no rolling-update when adopting pump statefulset")
usingName := member.FindConfigMapVolume(&pumpSet.Spec.Template.Spec, func(name string) bool {
return strings.HasPrefix(name, controller.PumpMemberName(tc.Name))
})
if usingName == "" {
e2elog.Fail("cannot find configmap that used by pump statefulset")
}
pumpConfigMap, err := c.CoreV1().ConfigMaps(tc.Namespace).Get(usingName, metav1.GetOptions{})
if errors.IsNotFound(err) {
return false, err
}
if err != nil {
e2elog.Logf("error get pump configmap: %v", err)
return false, nil
}
if !metav1.IsControlledBy(pumpConfigMap, tc) {
e2elog.Logf("expect pump configmap adopted by tidbcluster, still waiting...")
return false, nil
}
pumpPeerSvc, err := c.CoreV1().Services(tc.Namespace).Get(controller.PumpPeerMemberName(tc.Name), metav1.GetOptions{})
if errors.IsNotFound(err) {
return false, err
}
if err != nil {
e2elog.Logf("error get pump peer service: %v", err)
return false, nil
}
if !metav1.IsControlledBy(pumpPeerSvc, tc) {
e2elog.Logf("expect pump peer service adopted by tidbcluster, still waiting...")
return false, nil
}
return true, nil
})
framework.ExpectNoError(err)
// TODO: Add pump configmap rolling-update case
})
ginkgo.It("API: Migrate from helm to CRD", func() {
cluster := newTidbClusterConfig(e2econfig.TestConfig, ns, "helm-migration", "admin", "")
cluster.Resources["pd.replicas"] = "1"
cluster.Resources["tikv.replicas"] = "1"
cluster.Resources["tidb.replicas"] = "1"
oa.DeployTidbClusterOrDie(&cluster)
oa.CheckTidbClusterStatusOrDie(&cluster)
tc, err := cli.PingcapV1alpha1().TidbClusters(cluster.Namespace).Get(cluster.ClusterName, metav1.GetOptions{})
framework.ExpectNoError(err, "Expected get tidbcluster")
ginkgo.By("Discovery service should be reconciled by tidb-operator")
discoveryName := controller.DiscoveryMemberName(tc.Name)
discoveryDep, err := c.AppsV1().Deployments(tc.Namespace).Get(discoveryName, metav1.GetOptions{})
framework.ExpectNoError(err, "Expected get discovery deployment")
WaitObjectToBeControlledByOrDie(genericCli, discoveryDep, tc, 5*time.Minute)
err = utils.WaitForDeploymentComplete(c, discoveryDep, e2elog.Logf, 10*time.Second, 5*time.Minute)
framework.ExpectNoError(err, "Discovery Deployment should be healthy after managed by tidb-operator")
err = genericCli.Delete(context.TODO(), discoveryDep)
framework.ExpectNoError(err, "Expected to delete deployment")
err = wait.PollImmediate(10*time.Second, 5*time.Minute, func() (bool, error) {
_, err := c.AppsV1().Deployments(tc.Namespace).Get(discoveryName, metav1.GetOptions{})
if err != nil {
e2elog.Logf("wait discovery deployment get created again: %v", err)
return false, nil
}
return true, nil
})
framework.ExpectNoError(err, "Discovery Deployment should be recovered by tidb-operator after deletion")
ginkgo.By("Managing TiDB configmap in TidbCluster CRD in-place should not trigger rolling-udpate")
// TODO: modify other cases to manage TiDB configmap in CRD by default
setNameToRevision := map[string]string{
controller.PDMemberName(tc.Name): "",
controller.TiKVMemberName(tc.Name): "",
controller.TiDBMemberName(tc.Name): "",
}
for setName := range setNameToRevision {
oldSet, err := stsGetter(tc.Namespace).Get(setName, metav1.GetOptions{})
framework.ExpectNoError(err, "Expected get statefulset %s", setName)
oldRev := oldSet.Status.CurrentRevision
framework.ExpectEqual(oldSet.Status.UpdateRevision, oldRev, "Expected statefulset %s is not upgrading", setName)
setNameToRevision[setName] = oldRev
}
tc, err = cli.PingcapV1alpha1().TidbClusters(cluster.Namespace).Get(cluster.ClusterName, metav1.GetOptions{})
framework.ExpectNoError(err, "Expected get tidbcluster")
err = controller.GuaranteedUpdate(genericCli, tc, func() error {
tc.Spec.TiDB.Config = &v1alpha1.TiDBConfig{}
tc.Spec.TiDB.ConfigUpdateStrategy = &updateStrategy
tc.Spec.TiKV.Config = &v1alpha1.TiKVConfig{}
tc.Spec.TiKV.ConfigUpdateStrategy = &updateStrategy
tc.Spec.PD.Config = &v1alpha1.PDConfig{}
tc.Spec.PD.ConfigUpdateStrategy = &updateStrategy
return nil
})
framework.ExpectNoError(err, "Expected update tidbcluster")
// check for 2 minutes to ensure the tidb statefulset do not get rolling-update
err = wait.PollImmediate(10*time.Second, 2*time.Minute, func() (bool, error) {
tc, err := cli.PingcapV1alpha1().TidbClusters(cluster.Namespace).Get(cluster.ClusterName, metav1.GetOptions{})
framework.ExpectNoError(err, "Expected get tidbcluster")
framework.ExpectEqual(tc.Status.PD.Phase, v1alpha1.NormalPhase, "PD should not be updated")
framework.ExpectEqual(tc.Status.TiKV.Phase, v1alpha1.NormalPhase, "TiKV should not be updated")
framework.ExpectEqual(tc.Status.TiDB.Phase, v1alpha1.NormalPhase, "TiDB should not be updated")
for setName, oldRev := range setNameToRevision {
newSet, err := stsGetter(tc.Namespace).Get(setName, metav1.GetOptions{})
framework.ExpectNoError(err, "Expected get tidb statefulset")
framework.ExpectEqual(newSet.Status.CurrentRevision, oldRev, "Expected no rolling-update of %s when manage config in-place", setName)
framework.ExpectEqual(newSet.Status.UpdateRevision, oldRev, "Expected no rolling-update of %s when manage config in-place", setName)
}
return false, nil
})
if err != wait.ErrWaitTimeout {
e2elog.Failf("Unexpected error when checking tidb statefulset will not get rolling-update: %v", err)
}
err = wait.PollImmediate(5*time.Second, 3*time.Minute, func() (bool, error) {
for setName := range setNameToRevision {
newSet, err := stsGetter(tc.Namespace).Get(setName, metav1.GetOptions{})
if err != nil {
return false, err
}
usingName := member.FindConfigMapVolume(&newSet.Spec.Template.Spec, func(name string) bool {
return strings.HasPrefix(name, setName)
})
if usingName == "" {
e2elog.Failf("cannot find configmap that used by %s", setName)
}
usingCm, err := c.CoreV1().ConfigMaps(tc.Namespace).Get(usingName, metav1.GetOptions{})
if err != nil {
return false, err
}
if !metav1.IsControlledBy(usingCm, tc) {
e2elog.Logf("expect configmap of %s adopted by tidbcluster, still waiting...", setName)
return false, nil
}
}
return true, nil
})
framework.ExpectNoError(err)
})
ginkgo.It("Restarter: Testing restarting by annotations", func() {
cluster := newTidbClusterConfig(e2econfig.TestConfig, ns, "restarter", "admin", "")
cluster.Resources["pd.replicas"] = "1"
cluster.Resources["tikv.replicas"] = "1"
cluster.Resources["tidb.replicas"] = "1"
oa.DeployTidbClusterOrDie(&cluster)
oa.CheckTidbClusterStatusOrDie(&cluster)
tc, err := cli.PingcapV1alpha1().TidbClusters(cluster.Namespace).Get(cluster.ClusterName, metav1.GetOptions{})
framework.ExpectNoError(err, "Expected get tidbcluster")
pd_0, err := c.CoreV1().Pods(ns).Get(operatorUtils.GetPodName(tc, v1alpha1.PDMemberType, 0), metav1.GetOptions{})
framework.ExpectNoError(err, "Expected get pd-0")
tikv_0, err := c.CoreV1().Pods(ns).Get(operatorUtils.GetPodName(tc, v1alpha1.TiKVMemberType, 0), metav1.GetOptions{})
framework.ExpectNoError(err, "Expected get tikv-0")
tidb_0, err := c.CoreV1().Pods(ns).Get(operatorUtils.GetPodName(tc, v1alpha1.TiDBMemberType, 0), metav1.GetOptions{})
framework.ExpectNoError(err, "Expected get tidb-0")
pd_0.Annotations[label.AnnPodDeferDeleting] = "true"
tikv_0.Annotations[label.AnnPodDeferDeleting] = "true"
tidb_0.Annotations[label.AnnPodDeferDeleting] = "true"
_, err = c.CoreV1().Pods(ns).Update(pd_0)
framework.ExpectNoError(err, "Expected update pd-0 restarting ann")
_, err = c.CoreV1().Pods(ns).Update(tikv_0)
framework.ExpectNoError(err, "Expected update tikv-0 restarting ann")
_, err = c.CoreV1().Pods(ns).Update(tidb_0)
framework.ExpectNoError(err, "Expected update tidb-0 restarting ann")
f := func(name, namespace string, uid types.UID) (bool, error) {
pod, err := c.CoreV1().Pods(namespace).Get(name, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
// ignore not found error (pod is deleted and recreated again in restarting)
return false, nil
}
return false, err
}
if _, existed := pod.Annotations[label.AnnPodDeferDeleting]; existed {
return false, nil
}
if uid == pod.UID {
return false, nil
}
return true, nil
}
err = wait.Poll(5*time.Second, 5*time.Minute, func() (done bool, err error) {
isPdRestarted, err := f(pd_0.Name, ns, pd_0.UID)
if !(isPdRestarted && err == nil) {
return isPdRestarted, err
}
isTiKVRestarted, err := f(tikv_0.Name, ns, tikv_0.UID)
if !(isTiKVRestarted && err == nil) {
return isTiKVRestarted, err
}
isTiDBRestarted, err := f(tidb_0.Name, ns, tidb_0.UID)
if !(isTiDBRestarted && err == nil) {
return isTiDBRestarted, err
}
return true, nil
})
framework.ExpectNoError(err, "Expected tidbcluster pod restarted")
})
ginkgo.It("should be operable without helm [API]", func() {
tc := fixture.GetTidbCluster(ns, "plain-cr", utilimage.TiDBV3Version)
err := genericCli.Create(context.TODO(), tc)
framework.ExpectNoError(err, "Expected TiDB cluster created")
err = oa.WaitForTidbClusterReady(tc, 30*time.Minute, 15*time.Second)
framework.ExpectNoError(err, "Expected TiDB cluster ready")
err = controller.GuaranteedUpdate(genericCli, tc, func() error {
tc.Spec.PD.Replicas = 5
tc.Spec.TiKV.Replicas = 5
tc.Spec.TiDB.Replicas = 4
return nil
})
framework.ExpectNoError(err, "Expected TiDB cluster updated")
err = oa.WaitForTidbClusterReady(tc, 30*time.Minute, 15*time.Second)
framework.ExpectNoError(err, "Expected TiDB cluster scaled out and ready")
err = controller.GuaranteedUpdate(genericCli, tc, func() error {
tc.Spec.Version = utilimage.TiDBV3UpgradeVersion
return nil
})
framework.ExpectNoError(err, "Expected TiDB cluster updated")
err = oa.WaitForTidbClusterReady(tc, 30*time.Minute, 15*time.Second)
framework.ExpectNoError(err, "Expected TiDB cluster upgraded to new version and ready")
err = controller.GuaranteedUpdate(genericCli, tc, func() error {
tc.Spec.PD.Replicas = 3
tc.Spec.TiKV.Replicas = 3
tc.Spec.TiDB.Replicas = 2
return nil
})
framework.ExpectNoError(err, "Expected TiDB cluster updated")
err = oa.WaitForTidbClusterReady(tc, 30*time.Minute, 15*time.Second)
framework.ExpectNoError(err, "Expected TiDB cluster scaled in and ready")
})
ginkgo.It("TidbMonitor: Deploying and checking monitor", func() {
cluster := newTidbClusterConfig(e2econfig.TestConfig, ns, "monitor-test", "admin", "")
cluster.Resources["pd.replicas"] = "1"
cluster.Resources["tikv.replicas"] = "1"
cluster.Resources["tidb.replicas"] = "1"
oa.DeployTidbClusterOrDie(&cluster)
oa.CheckTidbClusterStatusOrDie(&cluster)
tc, err := cli.PingcapV1alpha1().TidbClusters(cluster.Namespace).Get(cluster.ClusterName, metav1.GetOptions{})
framework.ExpectNoError(err, "Expected get tidbcluster")
tm := fixture.NewTidbMonitor("e2e-monitor", tc.Namespace, tc, true, true)
_, err = cli.PingcapV1alpha1().TidbMonitors(tc.Namespace).Create(tm)
framework.ExpectNoError(err, "Expected tidbmonitor deployed success")
err = tests.CheckTidbMonitor(tm, c, fw)
framework.ExpectNoError(err, "Expected tidbmonitor checked success")
pvc, err := c.CoreV1().PersistentVolumeClaims(ns).Get("e2e-monitor-monitor", metav1.GetOptions{})
framework.ExpectNoError(err, "Expected fetch tidbmonitor pvc success")
pvName := pvc.Spec.VolumeName
pv, err := c.CoreV1().PersistentVolumes().Get(pvName, metav1.GetOptions{})
framework.ExpectNoError(err, "Expected fetch tidbmonitor pv success")
value, existed := pv.Labels[label.ComponentLabelKey]
framework.ExpectEqual(existed, true)
framework.ExpectEqual(value, label.TiDBMonitorVal)
value, existed = pv.Labels[label.InstanceLabelKey]
framework.ExpectEqual(existed, true)
framework.ExpectEqual(value, "e2e-monitor")
value, existed = pv.Labels[label.InstanceLabelKey]
framework.ExpectEqual(existed, true)
framework.ExpectEqual(value, "e2e-monitor")
value, existed = pv.Labels[label.ManagedByLabelKey]
framework.ExpectEqual(existed, true)
framework.ExpectEqual(value, label.TiDBOperator)
})
ginkgo.It("[Feature: AdvancedStatefulSet] Upgrading tidb cluster while pods are not consecutive", func() {
if !ocfg.Enabled(features.AdvancedStatefulSet) {
framework.Skipf("AdvancedStatefulSet feature of default operator is not enabled, skipping")
}
tc := fixture.GetTidbCluster(ns, "upgrade-cluster", utilimage.TiDBV3Version)
tc.Spec.PD.Replicas = 5
tc.Spec.TiKV.Replicas = 4
tc.Spec.TiDB.Replicas = 3
err := genericCli.Create(context.TODO(), tc)
framework.ExpectNoError(err)
err = oa.WaitForTidbClusterReady(tc, 30*time.Minute, 15*time.Second)
framework.ExpectNoError(err)
ginkgo.By("Scaling in the cluster by deleting some pods not at the end")
tc, err = cli.PingcapV1alpha1().TidbClusters(ns).Get(tc.Name, metav1.GetOptions{})
framework.ExpectNoError(err)
err = controller.GuaranteedUpdate(genericCli, tc, func() error {
if tc.Annotations == nil {
tc.Annotations = map[string]string{}
}
tc.Annotations[label.AnnPDDeleteSlots] = "[1]"
tc.Annotations[label.AnnTiKVDeleteSlots] = "[0]"
tc.Annotations[label.AnnTiDBDeleteSlots] = "[1]"
tc.Spec.PD.Replicas = 3
tc.Spec.TiKV.Replicas = 3
tc.Spec.TiDB.Replicas = 2
return nil
})
framework.ExpectNoError(err)
ginkgo.By("Checking for tidb cluster is ready")
err = oa.WaitForTidbClusterReady(tc, 30*time.Minute, 15*time.Second)
framework.ExpectNoError(err)
ginkgo.By("Upgrding the cluster")
err = controller.GuaranteedUpdate(genericCli, tc, func() error {
tc.Spec.Version = utilimage.TiDBV3UpgradeVersion
return nil
})
framework.ExpectNoError(err)
ginkgo.By("Checking for tidb cluster is ready")
err = oa.WaitForTidbClusterReady(tc, 30*time.Minute, 15*time.Second)
framework.ExpectNoError(err)
})
ginkgo.It("tidb-scale: clear TiDB failureMembers when scale TiDB to zero", func() {
cluster := newTidbClusterConfig(e2econfig.TestConfig, ns, "tidb-scale", "admin", "")
cluster.Resources["pd.replicas"] = "3"
cluster.Resources["tikv.replicas"] = "1"
cluster.Resources["tidb.replicas"] = "1"
cluster.TiDBPreStartScript = strconv.Quote("exit 1")
oa.DeployTidbClusterOrDie(&cluster)
e2elog.Logf("checking tidb cluster [%s/%s] failed member", cluster.Namespace, cluster.ClusterName)
ns := cluster.Namespace
tcName := cluster.ClusterName
err := wait.PollImmediate(15*time.Second, 15*time.Minute, func() (bool, error) {
var tc *v1alpha1.TidbCluster
var err error
if tc, err = cli.PingcapV1alpha1().TidbClusters(ns).Get(tcName, metav1.GetOptions{}); err != nil {
e2elog.Logf("failed to get tidbcluster: %s/%s, %v", ns, tcName, err)
return false, nil
}
if len(tc.Status.TiDB.FailureMembers) == 0 {
e2elog.Logf("the number of failed member is zero")
return false, nil
}
e2elog.Logf("the number of failed member is not zero (current: %d)", len(tc.Status.TiDB.FailureMembers))
return true, nil
})
framework.ExpectNoError(err, "tidb failover not work")
cluster.ScaleTiDB(0)
oa.ScaleTidbClusterOrDie(&cluster)
e2elog.Logf("checking tidb cluster [%s/%s] scale to zero", cluster.Namespace, cluster.ClusterName)
err = wait.PollImmediate(15*time.Second, 10*time.Minute, func() (bool, error) {
var tc *v1alpha1.TidbCluster
var err error
if tc, err = cli.PingcapV1alpha1().TidbClusters(ns).Get(tcName, metav1.GetOptions{}); err != nil {
e2elog.Logf("failed to get tidbcluster: %s/%s, %v", ns, tcName, err)
return false, nil
}
if tc.Status.TiDB.StatefulSet.Replicas != 0 {
e2elog.Logf("failed to scale tidb member to zero (current: %d)", tc.Status.TiDB.StatefulSet.Replicas)
return false, nil
}
if len(tc.Status.TiDB.FailureMembers) != 0 {
e2elog.Logf("failed to clear fail member (current: %d)", len(tc.Status.TiDB.FailureMembers))
return false, nil
}
e2elog.Logf("scale tidb member to zero successfully")
return true, nil
})
framework.ExpectNoError(err, "not clear TiDB failureMembers when scale TiDB to zero")
})
})
func newTidbClusterConfig(cfg *tests.Config, ns, clusterName, password, tidbVersion string) tests.TidbClusterConfig {
if tidbVersion == "" {
tidbVersion = cfg.GetTiDBVersionOrDie()
}
topologyKey := "rack"
return tests.TidbClusterConfig{
Namespace: ns,
ClusterName: clusterName,
EnablePVReclaim: false,
OperatorTag: cfg.OperatorTag,
PDImage: fmt.Sprintf("pingcap/pd:%s", tidbVersion),
TiKVImage: fmt.Sprintf("pingcap/tikv:%s", tidbVersion),
TiDBImage: fmt.Sprintf("pingcap/tidb:%s", tidbVersion),
PumpImage: fmt.Sprintf("pingcap/tidb-binlog:%s", tidbVersion),
StorageClassName: "local-storage",
Password: password,
UserName: "root",
InitSecretName: fmt.Sprintf("%s-set-secret", clusterName),
BackupSecretName: fmt.Sprintf("%s-backup-secret", clusterName),
BackupName: "backup",
Resources: map[string]string{
"pd.resources.limits.cpu": "1000m",
"pd.resources.limits.memory": "2Gi",
"pd.resources.requests.cpu": "200m",
"pd.resources.requests.memory": "200Mi",
"tikv.resources.limits.cpu": "2000m",
"tikv.resources.limits.memory": "4Gi",
"tikv.resources.requests.cpu": "200m",
"tikv.resources.requests.memory": "200Mi",
"tidb.resources.limits.cpu": "2000m",
"tidb.resources.limits.memory": "4Gi",
"tidb.resources.requests.cpu": "200m",
"tidb.resources.requests.memory": "200Mi",
"tidb.initSql": strconv.Quote("create database e2e;"),
"discovery.image": cfg.OperatorImage,
},
Args: map[string]string{},
Monitor: true,
BlockWriteConfig: blockwriter.Config{
TableNum: 1,
Concurrency: 1,
BatchSize: 1,
RawSize: 1,
},
TopologyKey: topologyKey,
EnableConfigMapRollout: true,
ClusterVersion: tidbVersion,
}
}