-
Notifications
You must be signed in to change notification settings - Fork 10
/
operations.go
executable file
·1199 lines (1096 loc) · 43.2 KB
/
operations.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
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cluster
import (
"context"
"errors"
"fmt"
"strings"
"github.com/apecloud/kubeblocks/pkg/common"
jsonpatch "github.com/evanphx/json-patch"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/stoewer/go-strcase"
"golang.org/x/exp/slices"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
apitypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/cli-runtime/pkg/genericiooptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/templates"
"sigs.k8s.io/controller-runtime/pkg/client"
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/constant"
"github.com/apecloud/kbcli/pkg/action"
"github.com/apecloud/kbcli/pkg/cluster"
classutil "github.com/apecloud/kbcli/pkg/cmd/class"
"github.com/apecloud/kbcli/pkg/printer"
"github.com/apecloud/kbcli/pkg/types"
"github.com/apecloud/kbcli/pkg/util"
"github.com/apecloud/kbcli/pkg/util/flags"
"github.com/apecloud/kbcli/pkg/util/prompt"
)
const oceanbase = "oceanbase"
type OperationsOptions struct {
action.CreateOptions `json:"-"`
HasComponentNamesFlag bool `json:"-"`
// AutoApprove when set true, skip the double check.
AutoApprove bool `json:"-"`
ComponentNames []string `json:"componentNames,omitempty"`
OpsRequestName string `json:"opsRequestName"`
TTLSecondsAfterSucceed int `json:"ttlSecondsAfterSucceed"`
// OpsType operation type
OpsType appsv1alpha1.OpsType `json:"type"`
// OpsTypeLower lower OpsType
OpsTypeLower string `json:"typeLower"`
// Upgrade options
ClusterVersionRef string `json:"clusterVersionRef"`
// VerticalScaling options
CPU string `json:"cpu"`
Memory string `json:"memory"`
Class string `json:"class"`
ClassDefRef appsv1alpha1.ClassDefRef `json:"classDefRef,omitempty"`
// HorizontalScaling options
Replicas int `json:"replicas"`
// Reconfiguring options
KeyValues map[string]*string `json:"keyValues"`
CfgTemplateName string `json:"cfgTemplateName"`
CfgFile string `json:"cfgFile"`
ForceRestart bool `json:"forceRestart"`
FileContent string `json:"fileContent"`
HasPatch bool `json:"hasPatch"`
// VolumeExpansion options.
// VCTNames VolumeClaimTemplate names
VCTNames []string `json:"vctNames,omitempty"`
Storage string `json:"storage"`
// Expose options
ExposeType string `json:"-"`
ExposeEnabled string `json:"exposeEnabled,omitempty"`
Services []appsv1alpha1.OpsService `json:"services,omitempty"`
// Switchover options
Component string `json:"component"`
Instance string `json:"instance"`
Primary string `json:"-"`
Force bool `json:"-"`
CharacterType string `json:"-"`
LorryHAEnabled bool `json:"-"`
ExecPod *corev1.Pod `json:"-"`
}
func newBaseOperationsOptions(f cmdutil.Factory, streams genericiooptions.IOStreams,
opsType appsv1alpha1.OpsType, hasComponentNamesFlag bool) *OperationsOptions {
customOutPut := func(opt *action.CreateOptions) {
output := fmt.Sprintf("OpsRequest %s created successfully, you can view the progress:", opt.Name)
printer.PrintLine(output)
nextLine := fmt.Sprintf("\tkbcli cluster describe-ops %s -n %s", opt.Name, opt.Namespace)
printer.PrintLine(nextLine)
}
o := &OperationsOptions{
// nil cannot be set to a map struct in CueLang, so init the map of KeyValues.
KeyValues: map[string]*string{},
HasPatch: true,
OpsType: opsType,
HasComponentNamesFlag: hasComponentNamesFlag,
AutoApprove: false,
CreateOptions: action.CreateOptions{
Factory: f,
IOStreams: streams,
CueTemplateName: "cluster_operations_template.cue",
GVR: types.OpsGVR(),
CustomOutPut: customOutPut,
},
}
o.OpsTypeLower = strings.ToLower(string(o.OpsType))
o.CreateOptions.Options = o
return o
}
// addCommonFlags adds common flags for operations command
func (o *OperationsOptions) addCommonFlags(cmd *cobra.Command, f cmdutil.Factory) {
// add print flags
printer.AddOutputFlagForCreate(cmd, &o.Format, false)
cmd.Flags().StringVar(&o.OpsRequestName, "name", "", "OpsRequest name. if not specified, it will be randomly generated ")
cmd.Flags().IntVar(&o.TTLSecondsAfterSucceed, "ttlSecondsAfterSucceed", 0, "Time to live after the OpsRequest succeed")
cmd.Flags().StringVar(&o.DryRun, "dry-run", "none", `Must be "client", or "server". If with client strategy, only print the object that would be sent, and no data is actually sent. If with server strategy, submit the server-side request, but no data is persistent.`)
cmd.Flags().Lookup("dry-run").NoOptDefVal = "unchanged"
if o.HasComponentNamesFlag {
flags.AddComponentsFlag(f, cmd, &o.ComponentNames, "Component names to this operations")
}
}
// CompleteRestartOps restarts all components of the cluster
// we should set all component names to ComponentNames flag.
func (o *OperationsOptions) CompleteRestartOps() error {
if o.Name == "" {
return makeMissingClusterNameErr()
}
if len(o.ComponentNames) != 0 {
return nil
}
clusterObj, err := cluster.GetClusterByName(o.Dynamic, o.Name, o.Namespace)
if err != nil {
return err
}
componentSpecs := clusterObj.Spec.ComponentSpecs
o.ComponentNames = make([]string, len(componentSpecs))
for i := range componentSpecs {
o.ComponentNames[i] = componentSpecs[i].Name
}
return nil
}
// CompleteComponentsFlag when components flag is null and the cluster only has one component, auto complete it.
func (o *OperationsOptions) CompleteComponentsFlag() error {
if o.Name == "" {
return makeMissingClusterNameErr()
}
if len(o.ComponentNames) != 0 {
return nil
}
clusterObj, err := cluster.GetClusterByName(o.Dynamic, o.Name, o.Namespace)
if err != nil {
return err
}
if len(clusterObj.Spec.ComponentSpecs) == 1 {
o.ComponentNames = []string{clusterObj.Spec.ComponentSpecs[0].Name}
}
return nil
}
func (o *OperationsOptions) CompletePromoteOps() error {
clusterObj, err := cluster.GetClusterByName(o.Dynamic, o.Name, o.Namespace)
if err != nil {
return err
}
if o.Component == "" {
if len(clusterObj.Spec.ComponentSpecs) > 1 {
return fmt.Errorf("there are multiple components in cluster, please use --component to specify the component for promote")
}
o.Component = clusterObj.Spec.ComponentSpecs[0].Name
}
o.CompleteHaEnabled()
return o.CompleteCharacterType(clusterObj)
}
// CompleteCharacterType will get the cluster character type compatible with 0.7
// If both componentDefRef and componentDef are provided, the componentDef will take precedence over componentDefRef.
func (o *OperationsOptions) CompleteCharacterType(clusterObj *appsv1alpha1.Cluster) error {
var primaryRoles []string
var componentSpec appsv1alpha1.ClusterComponentSpec
for _, compSpec := range clusterObj.Spec.ComponentSpecs {
if compSpec.Name == o.Component {
componentSpec = compSpec
break
}
}
if componentSpec.ComponentDef != "" {
componentDefV2 := &appsv1alpha1.ComponentDefinition{}
if err := util.GetK8SClientObject(o.Dynamic, componentDefV2, types.CompDefGVR(), "", componentSpec.ComponentDef); err != nil {
return err
}
o.CharacterType = componentDefV2.Spec.ServiceKind
primaryRole, _ := func(roles []appsv1alpha1.ReplicaRole) (string, error) {
targetRole := ""
if len(roles) == 0 {
return targetRole, fmt.Errorf("component has no roles definition, does not support switchover")
}
for _, role := range roles {
if role.Serviceable && role.Writable {
if targetRole != "" {
return targetRole, fmt.Errorf("componentDefinition has more than role is serviceable and writable, does not support switchover")
}
targetRole = role.Name
}
}
return targetRole, nil
}(componentDefV2.Spec.Roles)
primaryRoles = append(primaryRoles, primaryRole)
} else {
clusterDefObj := appsv1alpha1.ClusterDefinition{}
clusterDefKey := client.ObjectKey{
Namespace: "",
Name: clusterObj.Spec.ClusterDefRef,
}
if err := util.GetResourceObjectFromGVR(types.ClusterDefGVR(), clusterDefKey, o.Dynamic, &clusterDefObj); err != nil {
return err
}
componentDef := clusterDefObj.GetComponentDefByName(componentSpec.ComponentDefRef)
if componentDef == nil {
return fmt.Errorf("failed to get component def :%s", componentSpec.ComponentDefRef)
}
o.CharacterType = componentDef.CharacterType
primaryRoles = []string{constant.Primary, constant.Leader}
}
if o.Instance != "" && o.CharacterType != oceanbase {
pod, err := o.Client.CoreV1().Pods(o.Namespace).Get(context.Background(), o.Instance, metav1.GetOptions{})
if err != nil {
return err
}
o.ExecPod = pod
return nil
}
if len(primaryRoles) == 0 {
return nil
}
labelsMap := map[string]string{
constant.AppInstanceLabelKey: o.Name,
constant.AppManagedByLabelKey: "kubeblocks",
constant.KBAppComponentLabelKey: o.Component,
}
selector := labels.SelectorFromSet(labelsMap)
podList, err := o.Client.CoreV1().Pods(o.Namespace).List(context.Background(), metav1.ListOptions{LabelSelector: selector.String()})
if err != nil {
return err
}
for _, pod := range podList.Items {
podRole, ok := pod.Labels[constant.RoleLabelKey]
for _, role := range primaryRoles {
if ok && podRole == role {
o.ExecPod = pod.DeepCopy()
o.Primary = pod.Name
break
}
}
}
return nil
}
func (o *OperationsOptions) CompleteHaEnabled() {
cmName := fmt.Sprintf("%s-%s-haconfig", o.Name, o.Component)
cm, err := o.Client.CoreV1().ConfigMaps(o.Namespace).Get(context.Background(), cmName, metav1.GetOptions{})
if err != nil {
return
}
enable, ok := cm.Annotations["enable"]
if ok && strings.EqualFold(enable, "true") {
o.LorryHAEnabled = true
}
}
func (o *OperationsOptions) validateUpgrade() error {
if len(o.ClusterVersionRef) == 0 {
return fmt.Errorf("missing cluster-version")
}
return nil
}
func (o *OperationsOptions) validateVolumeExpansion() error {
if len(o.VCTNames) == 0 {
return fmt.Errorf("missing volume-claim-templates")
}
if len(o.Storage) == 0 {
return fmt.Errorf("missing storage")
}
for _, cName := range o.ComponentNames {
for _, vctName := range o.VCTNames {
labels := fmt.Sprintf("%s=%s,%s=%s,%s=%s",
constant.AppInstanceLabelKey, o.Name,
constant.KBAppComponentLabelKey, cName,
constant.VolumeClaimTemplateNameLabelKey, vctName,
)
pvcs, err := o.Client.CoreV1().PersistentVolumeClaims(o.Namespace).List(context.Background(),
metav1.ListOptions{LabelSelector: labels, Limit: 1})
if err != nil {
return err
}
if len(pvcs.Items) == 0 {
continue
}
pvc := pvcs.Items[0]
specStorage := pvc.Spec.Resources.Requests.Storage()
statusStorage := pvc.Status.Capacity.Storage()
targetStorage, err := resource.ParseQuantity(o.Storage)
if err != nil {
return fmt.Errorf("cannot parse '%v', %v", o.Storage, err)
}
// determine whether the opsRequest is a recovery action for volume expansion failure
if specStorage.Cmp(targetStorage) > 0 &&
statusStorage.Cmp(targetStorage) <= 0 {
o.AutoApprove = false
fmt.Fprintln(o.Out, printer.BoldYellow("Warning: this opsRequest is a recovery action for volume expansion failure and will re-create the PersistentVolumeClaims when RECOVER_VOLUME_EXPANSION_FAILURE=false"))
break
}
}
}
return nil
}
func (o *OperationsOptions) validateVScale(cluster *appsv1alpha1.Cluster) error {
if o.Class != "" && (o.CPU != "" || o.Memory != "") {
return fmt.Errorf("class and cpu/memory cannot be both specified")
}
if o.Class == "" && o.CPU == "" && o.Memory == "" {
return fmt.Errorf("class or cpu/memory must be specified")
}
clsMgr, resourceConstraintList, err := classutil.GetManager(o.Dynamic, cluster.Spec.ClusterDefRef)
if err != nil {
return err
}
fillClassParams := func(comp *appsv1alpha1.ClusterComponentSpec) error {
if o.Class != "" {
clsDefRef := appsv1alpha1.ClassDefRef{}
parts := strings.SplitN(o.Class, ":", 2)
if len(parts) == 1 {
clsDefRef.Class = parts[0]
} else {
clsDefRef.Name = parts[0]
clsDefRef.Class = parts[1]
}
comp.ClassDefRef = &clsDefRef
comp.Resources = corev1.ResourceRequirements{}
} else {
comp.ClassDefRef = &appsv1alpha1.ClassDefRef{}
requests := make(corev1.ResourceList)
if o.CPU != "" {
cpu, err := resource.ParseQuantity(o.CPU)
if err != nil {
return fmt.Errorf("cannot parse '%v', %v", o.CPU, err)
}
requests[corev1.ResourceCPU] = cpu
}
if o.Memory != "" {
memory, err := resource.ParseQuantity(o.Memory)
if err != nil {
return fmt.Errorf("cannot parse '%v', %v", o.Memory, err)
}
requests[corev1.ResourceMemory] = memory
}
requests.DeepCopyInto(&comp.Resources.Requests)
requests.DeepCopyInto(&comp.Resources.Limits)
}
return nil
}
for _, name := range o.ComponentNames {
for _, comp := range cluster.Spec.ComponentSpecs {
if comp.Name != name {
continue
}
if err := fillClassParams(&comp); err != nil {
return err
}
// validate component classes
if err = classutil.ValidateResources(clsMgr, resourceConstraintList, cluster.Spec.ClusterDefRef, &comp); err != nil {
return err
}
}
}
return nil
}
// Validate command flags or args is legal
func (o *OperationsOptions) Validate() error {
if o.Name == "" {
return makeMissingClusterNameErr()
}
// check if cluster exist
cluster, err := cluster.GetClusterByName(o.Dynamic, o.Name, o.Namespace)
if err != nil {
return err
}
// common validate for componentOps
if o.HasComponentNamesFlag && len(o.ComponentNames) == 0 {
return fmt.Errorf(`missing components, please specify the "--components" flag for multi-components cluster`)
}
switch o.OpsType {
case appsv1alpha1.VolumeExpansionType:
if err = o.validateVolumeExpansion(); err != nil {
return err
}
case appsv1alpha1.UpgradeType:
if err = o.validateUpgrade(); err != nil {
return err
}
case appsv1alpha1.VerticalScalingType:
if err = o.validateVScale(cluster); err != nil {
return err
}
case appsv1alpha1.ExposeType:
if err = o.validateExpose(); err != nil {
return err
}
case appsv1alpha1.SwitchoverType:
if err = o.validatePromote(cluster); err != nil {
return err
}
}
if !o.AutoApprove && o.DryRun == "none" {
return prompt.Confirm([]string{o.Name}, o.In, "", "")
}
return nil
}
func (o *OperationsOptions) validatePromote(cluster *appsv1alpha1.Cluster) error {
var (
clusterDefObj = appsv1alpha1.ClusterDefinition{}
compDefObj = appsv1alpha1.ComponentDefinition{}
podObj = &corev1.Pod{}
componentName = o.Component
)
if len(cluster.Spec.ComponentSpecs) == 0 {
return fmt.Errorf("cluster.Spec.ComponentSpecs cannot be empty")
}
// check clusterDefinition exist
clusterDefKey := client.ObjectKey{
Namespace: "",
Name: cluster.Spec.ClusterDefRef,
}
if err := util.GetResourceObjectFromGVR(types.ClusterDefGVR(), clusterDefKey, o.Dynamic, &clusterDefObj); err != nil {
return err
}
getAndValidatePod := func(targetRoles ...string) error {
// if the instance is not specified, do not need to check the validity of the instance
if o.Instance == "" || o.CharacterType == oceanbase {
return nil
}
// checks the validity of the instance whether it belongs to the current component and ensure it is not the primary or leader instance currently.
podKey := client.ObjectKey{
Namespace: cluster.Namespace,
Name: o.Instance,
}
if err := util.GetResourceObjectFromGVR(types.PodGVR(), podKey, o.Dynamic, podObj); err != nil || podObj == nil {
return fmt.Errorf("instance %s not found, please check the validity of the instance using \"kbcli cluster list-instances\"", o.Instance)
}
v, ok := podObj.Labels[constant.RoleLabelKey]
if !ok || v == "" {
return fmt.Errorf("instance %s cannot be promoted because it had a invalid role label", o.Instance)
}
for _, targetRole := range targetRoles {
if v == targetRole {
return fmt.Errorf("instanceName %s cannot be promoted because it is already the targetRole %s instance", o.Instance, targetRole)
}
}
if !strings.HasPrefix(podObj.Name, fmt.Sprintf("%s-%s", cluster.Name, componentName)) {
return fmt.Errorf("instanceName %s does not belong to the current component, please check the validity of the instance using \"kbcli cluster list-instances\"", o.Instance)
}
return nil
}
// TODO(xingran): this will be removed in the future.
validateBaseOnClusterCompDef := func() error {
var clusterCompDefObj *appsv1alpha1.ClusterComponentDefinition
for _, clusterCompDef := range clusterDefObj.Spec.ComponentDefs {
if clusterCompDef.Name == cluster.Spec.GetComponentDefRefName(componentName) {
clusterCompDefObj = &clusterCompDef
break
}
}
if clusterCompDefObj == nil {
return fmt.Errorf("cluster component %s is invalid", componentName)
}
if !o.LorryHAEnabled && o.CharacterType != oceanbase {
if clusterCompDefObj.SwitchoverSpec == nil {
return fmt.Errorf("cluster component %s does not support switchover", componentName)
}
switch o.Instance {
case "":
if clusterCompDefObj.SwitchoverSpec.WithoutCandidate == nil {
return fmt.Errorf("cluster component %s does not support promote without specifying an instance. Please specify a specific instance for the promotion", componentName)
}
default:
if clusterCompDefObj.SwitchoverSpec.WithCandidate == nil {
return fmt.Errorf("cluster component %s does not support specifying an instance for promote. If you want to perform a promote operation, please do not specify an instance", componentName)
}
}
}
targetRoles := []string{constant.Primary, constant.Leader}
if err := getAndValidatePod(targetRoles...); err != nil {
return err
}
return nil
}
validateBaseOnCompDef := func(compDef string) error {
getTargetRole := func(roles []appsv1alpha1.ReplicaRole) (string, error) {
targetRole := ""
if len(roles) == 0 {
return targetRole, fmt.Errorf("component has no roles definition, does not support switchover")
}
for _, role := range roles {
if role.Serviceable && role.Writable {
if targetRole != "" {
return targetRole, fmt.Errorf("componentDefinition has more than role is serviceable and writable, does not support switchover")
}
targetRole = role.Name
}
}
return targetRole, nil
}
// check componentDefinition exist
compDefKey := client.ObjectKey{
Namespace: "",
Name: compDef,
}
if err := util.GetResourceObjectFromGVR(types.CompDefGVR(), compDefKey, o.Dynamic, &compDefObj); err != nil {
return err
}
if !o.LorryHAEnabled && o.CharacterType != oceanbase {
if compDefObj.Spec.LifecycleActions == nil || compDefObj.Spec.LifecycleActions.Switchover == nil {
return fmt.Errorf("this cluster component %s does not support switchover", componentName)
}
switch o.Instance {
case "":
if compDefObj.Spec.LifecycleActions.Switchover.WithoutCandidate == nil {
return fmt.Errorf("this cluster component %s does not support promote without specifying an instance. Please specify a specific instance for the promotion", componentName)
}
default:
if compDefObj.Spec.LifecycleActions.Switchover.WithCandidate == nil {
return fmt.Errorf("this cluster component %s does not support specifying an instance for promote. If you want to perform a promote operation, please do not specify an instance", componentName)
}
}
}
targetRole, err := getTargetRole(compDefObj.Spec.Roles)
if err != nil {
return err
}
if targetRole == "" {
return fmt.Errorf("componentDefinition has no role is serviceable and writable, does not support switchover")
}
if err := getAndValidatePod(targetRole); err != nil {
return err
}
return nil
}
if cluster.Spec.ComponentSpecs[0].ComponentDef != "" {
return validateBaseOnCompDef(cluster.Spec.ComponentSpecs[0].ComponentDef)
} else {
return validateBaseOnClusterCompDef()
}
}
func (o *OperationsOptions) validateExpose() error {
switch util.ExposeType(o.ExposeType) {
case "", util.ExposeToVPC, util.ExposeToInternet:
default:
return fmt.Errorf("invalid expose type %q", o.ExposeType)
}
switch strings.ToLower(o.ExposeEnabled) {
case util.EnableValue, util.DisableValue:
default:
return fmt.Errorf("invalid value for enable flag: %s", o.ExposeEnabled)
}
return nil
}
func (o *OperationsOptions) fillExpose() error {
version, err := util.GetK8sVersion(o.Client.Discovery())
if err != nil {
return err
}
provider, err := util.GetK8sProvider(version, o.Client)
if err != nil {
return err
}
if provider == util.UnknownProvider {
return fmt.Errorf("unknown k8s provider")
}
// default expose to internet
exposeType := util.ExposeType(o.ExposeType)
if exposeType == "" {
exposeType = util.ExposeToInternet
}
annotations, err := util.GetExposeAnnotations(provider, exposeType)
if err != nil {
return err
}
o.Services = append(o.Services, appsv1alpha1.OpsService{
// currently, we use the expose type as service name
Name: string(exposeType),
ServiceType: corev1.ServiceTypeLoadBalancer,
Annotations: annotations,
})
return nil
}
var restartExample = templates.Examples(`
# restart all components
kbcli cluster restart mycluster
# specified component to restart, separate with commas for multiple components
kbcli cluster restart mycluster --components=mysql
`)
// NewRestartCmd creates a restart command
func NewRestartCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := newBaseOperationsOptions(f, streams, appsv1alpha1.RestartType, true)
cmd := &cobra.Command{
Use: "restart NAME",
Short: "Restart the specified components in the cluster.",
Example: restartExample,
ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()),
Run: func(cmd *cobra.Command, args []string) {
o.Args = args
cmdutil.BehaviorOnFatal(printer.FatalWithRedColor)
cmdutil.CheckErr(o.Complete())
cmdutil.CheckErr(o.CompleteRestartOps())
cmdutil.CheckErr(o.Validate())
cmdutil.CheckErr(o.Run())
},
}
o.addCommonFlags(cmd, f)
cmd.Flags().BoolVar(&o.AutoApprove, "auto-approve", false, "Skip interactive approval before restarting the cluster")
return cmd
}
var upgradeExample = templates.Examples(`
# upgrade the cluster to the target version
kbcli cluster upgrade mycluster --cluster-version=ac-mysql-8.0.30
`)
// NewUpgradeCmd creates an upgrade command
func NewUpgradeCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := newBaseOperationsOptions(f, streams, appsv1alpha1.UpgradeType, false)
cmd := &cobra.Command{
Use: "upgrade NAME",
Short: "Upgrade the cluster version.",
Example: upgradeExample,
ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()),
Run: func(cmd *cobra.Command, args []string) {
o.Args = args
cmdutil.BehaviorOnFatal(printer.FatalWithRedColor)
cmdutil.CheckErr(o.Complete())
cmdutil.CheckErr(o.Validate())
cmdutil.CheckErr(o.Run())
},
}
o.addCommonFlags(cmd, f)
cmd.Flags().StringVar(&o.ClusterVersionRef, "cluster-version", "", "Reference cluster version (required)")
cmd.Flags().BoolVar(&o.AutoApprove, "auto-approve", false, "Skip interactive approval before upgrading the cluster")
_ = cmd.MarkFlagRequired("cluster-version")
return cmd
}
var verticalScalingExample = templates.Examples(`
# scale the computing resources of specified components, separate with commas for multiple components
kbcli cluster vscale mycluster --components=mysql --cpu=500m --memory=500Mi
# scale the computing resources of specified components by class, run command 'kbcli class list --cluster-definition cluster-definition-name' to get available classes
kbcli cluster vscale mycluster --components=mysql --class=general-2c4g
`)
// NewVerticalScalingCmd creates a vertical scaling command
func NewVerticalScalingCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := newBaseOperationsOptions(f, streams, appsv1alpha1.VerticalScalingType, true)
cmd := &cobra.Command{
Use: "vscale NAME",
Short: "Vertically scale the specified components in the cluster.",
Example: verticalScalingExample,
ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()),
Run: func(cmd *cobra.Command, args []string) {
o.Args = args
cmdutil.BehaviorOnFatal(printer.FatalWithRedColor)
cmdutil.CheckErr(o.Complete())
cmdutil.CheckErr(o.CompleteComponentsFlag())
cmdutil.CheckErr(o.Validate())
cmdutil.CheckErr(o.Run())
},
}
o.addCommonFlags(cmd, f)
cmd.Flags().StringVar(&o.CPU, "cpu", "", "Request and limit size of component cpu")
cmd.Flags().StringVar(&o.Memory, "memory", "", "Request and limit size of component memory")
cmd.Flags().StringVar(&o.Class, "class", "", "Component class")
cmd.Flags().BoolVar(&o.AutoApprove, "auto-approve", false, "Skip interactive approval before vertically scaling the cluster")
_ = cmd.MarkFlagRequired("components")
return cmd
}
var horizontalScalingExample = templates.Examples(`
# expand storage resources of specified components, separate with commas for multiple components
kbcli cluster hscale mycluster --components=mysql --replicas=3
`)
// NewHorizontalScalingCmd creates a horizontal scaling command
func NewHorizontalScalingCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := newBaseOperationsOptions(f, streams, appsv1alpha1.HorizontalScalingType, true)
cmd := &cobra.Command{
Use: "hscale NAME",
Short: "Horizontally scale the specified components in the cluster.",
Example: horizontalScalingExample,
ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()),
Run: func(cmd *cobra.Command, args []string) {
o.Args = args
cmdutil.BehaviorOnFatal(printer.FatalWithRedColor)
cmdutil.CheckErr(o.Complete())
cmdutil.CheckErr(o.CompleteComponentsFlag())
cmdutil.CheckErr(o.Validate())
cmdutil.CheckErr(o.Run())
},
}
o.addCommonFlags(cmd, f)
cmd.Flags().IntVar(&o.Replicas, "replicas", o.Replicas, "Replicas with the specified components")
cmd.Flags().BoolVar(&o.AutoApprove, "auto-approve", false, "Skip interactive approval before horizontally scaling the cluster")
_ = cmd.MarkFlagRequired("replicas")
_ = cmd.MarkFlagRequired("components")
return cmd
}
var volumeExpansionExample = templates.Examples(`
# restart specifies the component, separate with commas for multiple components
kbcli cluster volume-expand mycluster --components=mysql --volume-claim-templates=data --storage=10Gi
`)
// NewVolumeExpansionCmd creates a volume expanding command
func NewVolumeExpansionCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := newBaseOperationsOptions(f, streams, appsv1alpha1.VolumeExpansionType, true)
cmd := &cobra.Command{
Use: "volume-expand NAME",
Short: "Expand volume with the specified components and volumeClaimTemplates in the cluster.",
Example: volumeExpansionExample,
ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()),
Run: func(cmd *cobra.Command, args []string) {
o.Args = args
cmdutil.BehaviorOnFatal(printer.FatalWithRedColor)
cmdutil.CheckErr(o.Complete())
cmdutil.CheckErr(o.CompleteComponentsFlag())
cmdutil.CheckErr(o.Validate())
cmdutil.CheckErr(o.Run())
},
}
o.addCommonFlags(cmd, f)
cmd.Flags().StringSliceVarP(&o.VCTNames, "volume-claim-templates", "t", nil, "VolumeClaimTemplate names in components (required)")
cmd.Flags().StringVar(&o.Storage, "storage", "", "Volume storage size (required)")
cmd.Flags().BoolVar(&o.AutoApprove, "auto-approve", false, "Skip interactive approval before expanding the cluster volume")
_ = cmd.MarkFlagRequired("volume-claim-templates")
_ = cmd.MarkFlagRequired("storage")
_ = cmd.MarkFlagRequired("components")
return cmd
}
var (
exposeExamples = templates.Examples(`
# Expose a cluster to vpc
kbcli cluster expose mycluster --type vpc --enable=true
# Expose a cluster to public internet
kbcli cluster expose mycluster --type internet --enable=true
# Stop exposing a cluster
kbcli cluster expose mycluster --type vpc --enable=false
`)
)
// NewExposeCmd creates an expose command
func NewExposeCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := newBaseOperationsOptions(f, streams, appsv1alpha1.ExposeType, true)
cmd := &cobra.Command{
Use: "expose NAME --enable=[true|false] --type=[vpc|internet]",
Short: "Expose a cluster with a new endpoint, the new endpoint can be found by executing 'kbcli cluster describe NAME'.",
Example: exposeExamples,
ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()),
Run: func(cmd *cobra.Command, args []string) {
o.Args = args
cmdutil.BehaviorOnFatal(printer.FatalWithRedColor)
cmdutil.CheckErr(o.Complete())
cmdutil.CheckErr(o.CompleteComponentsFlag())
cmdutil.CheckErr(o.fillExpose())
cmdutil.CheckErr(o.Validate())
cmdutil.CheckErr(o.Run())
},
}
o.addCommonFlags(cmd, f)
cmd.Flags().StringVar(&o.ExposeType, "type", "", "Expose type, currently supported types are 'vpc', 'internet'")
cmd.Flags().StringVar(&o.ExposeEnabled, "enable", "", "Enable or disable the expose, values can be true or false")
cmd.Flags().BoolVar(&o.AutoApprove, "auto-approve", false, "Skip interactive approval before exposing the cluster")
util.CheckErr(cmd.RegisterFlagCompletionFunc("type", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{string(util.ExposeToVPC), string(util.ExposeToInternet)}, cobra.ShellCompDirectiveNoFileComp
}))
util.CheckErr(cmd.RegisterFlagCompletionFunc("enable", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"true", "false"}, cobra.ShellCompDirectiveNoFileComp
}))
_ = cmd.MarkFlagRequired("enable")
return cmd
}
var stopExample = templates.Examples(`
# stop the cluster and release all the pods of the cluster
kbcli cluster stop mycluster
`)
// NewStopCmd creates a stop command
func NewStopCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := newBaseOperationsOptions(f, streams, appsv1alpha1.StopType, false)
cmd := &cobra.Command{
Use: "stop NAME",
Short: "Stop the cluster and release all the pods of the cluster.",
Example: stopExample,
ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()),
Run: func(cmd *cobra.Command, args []string) {
o.Args = args
cmdutil.BehaviorOnFatal(printer.FatalWithRedColor)
cmdutil.CheckErr(o.Complete())
cmdutil.CheckErr(o.Validate())
cmdutil.CheckErr(o.Run())
},
}
o.addCommonFlags(cmd, f)
cmd.Flags().BoolVar(&o.AutoApprove, "auto-approve", false, "Skip interactive approval before stopping the cluster")
return cmd
}
var startExample = templates.Examples(`
# start the cluster when cluster is stopped
kbcli cluster start mycluster
`)
// NewStartCmd creates a start command
func NewStartCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := newBaseOperationsOptions(f, streams, appsv1alpha1.StartType, false)
o.AutoApprove = true
cmd := &cobra.Command{
Use: "start NAME",
Short: "Start the cluster if cluster is stopped.",
Example: startExample,
ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.ClusterGVR()),
Run: func(cmd *cobra.Command, args []string) {
o.Args = args
cmdutil.BehaviorOnFatal(printer.FatalWithRedColor)
cmdutil.CheckErr(o.Complete())
cmdutil.CheckErr(o.Validate())
cmdutil.CheckErr(o.Run())
},
}
o.addCommonFlags(cmd, f)
return cmd
}
var cancelExample = templates.Examples(`
# cancel the opsRequest which is not completed.
kbcli cluster cancel-ops <opsRequestName>
`)
func cancelOps(o *OperationsOptions) error {
opsRequest := &appsv1alpha1.OpsRequest{}
if err := util.GetK8SClientObject(o.Dynamic, opsRequest, o.GVR, o.Namespace, o.Name); err != nil {
return err
}
notSupportedPhases := []appsv1alpha1.OpsPhase{appsv1alpha1.OpsFailedPhase, appsv1alpha1.OpsSucceedPhase, appsv1alpha1.OpsCancelledPhase}
if slices.Contains(notSupportedPhases, opsRequest.Status.Phase) {
return fmt.Errorf("can not cancel the opsRequest when phase is %s", opsRequest.Status.Phase)
}
if opsRequest.Status.Phase == appsv1alpha1.OpsCancellingPhase {
return fmt.Errorf(`opsRequest "%s" is cancelling`, opsRequest.Name)
}
supportedType := []appsv1alpha1.OpsType{appsv1alpha1.HorizontalScalingType, appsv1alpha1.VerticalScalingType}
if !slices.Contains(supportedType, opsRequest.Spec.Type) {
return fmt.Errorf("opsRequest type: %s not support cancel action", opsRequest.Spec.Type)
}
if !o.AutoApprove {
if err := prompt.Confirm([]string{o.Name}, o.In, "", ""); err != nil {
return err
}
}
oldOps := opsRequest.DeepCopy()
opsRequest.Spec.Cancel = true
oldData, err := json.Marshal(oldOps)
if err != nil {
return err
}
newData, err := json.Marshal(opsRequest)
if err != nil {
return err
}
patchBytes, err := jsonpatch.CreateMergePatch(oldData, newData)
if err != nil {
return err
}
if _, err = o.Dynamic.Resource(types.OpsGVR()).Namespace(opsRequest.Namespace).Patch(context.TODO(),
opsRequest.Name, apitypes.MergePatchType, patchBytes, metav1.PatchOptions{}); err != nil {
return err
}
fmt.Fprintf(o.Out, "start to cancel opsRequest \"%s\", you can view the progress:\n\tkbcli cluster list-ops --name %s\n", o.Name, o.Name)
return nil
}
func NewCancelCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := newBaseOperationsOptions(f, streams, "", false)
cmd := &cobra.Command{
Use: "cancel-ops NAME",
Short: "Cancel the pending/creating/running OpsRequest which type is vscale or hscale.",
Example: cancelExample,
ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.OpsGVR()),
Run: func(cmd *cobra.Command, args []string) {
o.Args = args
cmdutil.BehaviorOnFatal(printer.FatalWithRedColor)
cmdutil.CheckErr(o.Complete())
cmdutil.CheckErr(cancelOps(o))
},
}
cmd.Flags().BoolVar(&o.AutoApprove, "auto-approve", false, "Skip interactive approval before cancel the opsRequest")
return cmd
}
var promoteExample = templates.Examples(`
# Promote the instance mycluster-mysql-1 as the new primary or leader.
kbcli cluster promote mycluster --instance mycluster-mysql-1
# Promote a non-primary or non-leader instance as the new primary or leader, the new primary or leader is determined by the system.
kbcli cluster promote mycluster
# If the cluster has multiple components, you need to specify a component, otherwise an error will be reported.
kbcli cluster promote mycluster --component=mysql --instance mycluster-mysql-1
`)
// NewPromoteCmd creates a promote command
func NewPromoteCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {