-
Notifications
You must be signed in to change notification settings - Fork 14
/
util.go
1118 lines (1004 loc) · 32.8 KB
/
util.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 util
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"io"
"math"
mrand "math/rand"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"sort"
"strings"
"sync"
"text/template"
"time"
"github.com/fatih/color"
"github.com/go-logr/logr"
"github.com/pkg/errors"
"github.com/pmezard/go-difflib/difflib"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh"
corev1 "k8s.io/api/core/v1"
apiextv1 "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/apis/meta/v1/unstructured"
apiruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
k8sapitypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/duration"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
cmdget "k8s.io/kubectl/pkg/cmd/get"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/kustomize/kyaml/yaml"
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/configuration/core"
"github.com/apecloud/kubeblocks/pkg/configuration/openapi"
cfgutil "github.com/apecloud/kubeblocks/pkg/configuration/util"
"github.com/apecloud/kubeblocks/pkg/constant"
viper "github.com/apecloud/kubeblocks/pkg/viperx"
"github.com/apecloud/kbcli/pkg/testing"
"github.com/apecloud/kbcli/pkg/types"
)
// CloseQuietly closes `io.Closer` quietly. Very handy and helpful for code
// quality too.
func CloseQuietly(d io.Closer) {
_ = d.Close()
}
// GetCliHomeDir returns kbcli home dir
func GetCliHomeDir() (string, error) {
var cliHome string
if custom := os.Getenv(types.CliHomeEnv); custom != "" {
cliHome = custom
} else {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
cliHome = filepath.Join(home, types.CliDefaultHome)
}
if _, err := os.Stat(cliHome); err != nil && os.IsNotExist(err) {
if err = os.MkdirAll(cliHome, 0750); err != nil {
return "", errors.Wrap(err, "error when create kbcli home directory")
}
}
return cliHome, nil
}
// GetCliLogDir returns kbcli log dir
func GetCliLogDir() (string, error) {
cliHome, err := GetCliHomeDir()
if err != nil {
return "", err
}
logDir := filepath.Join(cliHome, types.CliLogDir)
if _, err := os.Stat(logDir); err != nil && os.IsNotExist(err) {
if err = os.MkdirAll(logDir, 0750); err != nil {
return "", errors.Wrap(err, "error when create kbcli log directory")
}
}
return logDir, nil
}
// GetCliAddonDir returns kbcli addon index dir
func GetCliAddonDir() (string, error) {
var addonIndexDir string
if custom := os.Getenv(types.AddonIndexDirEnv); custom != "" {
addonIndexDir = custom
} else {
home, err := GetCliHomeDir()
if err != nil {
return "", err
}
addonIndexDir = path.Join(home, types.AddonIndexDir)
}
if _, err := os.Stat(addonIndexDir); err != nil && os.IsNotExist(err) {
if err = os.MkdirAll(addonIndexDir, 0750); err != nil {
return "", errors.Wrap(err, "error when create addon index directory")
}
}
return addonIndexDir, nil
}
// GetKubeconfigDir returns the kubeconfig directory.
func GetKubeconfigDir() string {
var kubeconfigDir string
switch runtime.GOOS {
case types.GoosDarwin, types.GoosLinux:
kubeconfigDir = filepath.Join(os.Getenv("HOME"), ".kube")
case types.GoosWindows:
kubeconfigDir = filepath.Join(os.Getenv("USERPROFILE"), ".kube")
}
return kubeconfigDir
}
func ConfigPath(name string) string {
if len(name) == 0 {
return ""
}
return filepath.Join(GetKubeconfigDir(), name)
}
func RemoveConfig(name string) error {
if err := os.Remove(ConfigPath(name)); err != nil {
return err
}
return nil
}
func GetPublicIP() (string, error) {
resp, err := http.Get("https://ifconfig.me")
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
// MakeSSHKeyPair makes a pair of public and private keys for SSH access.
// Public key is encoded in the format for inclusion in an OpenSSH authorized_keys file.
// Private Key generated is PEM encoded
func MakeSSHKeyPair(pubKeyPath, privateKeyPath string) error {
if err := os.MkdirAll(path.Dir(pubKeyPath), os.FileMode(0700)); err != nil {
return err
}
if err := os.MkdirAll(path.Dir(privateKeyPath), os.FileMode(0700)); err != nil {
return err
}
privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return err
}
// generate and write private key as PEM
privateKeyFile, err := os.OpenFile(privateKeyPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer privateKeyFile.Close()
privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
if err := pem.Encode(privateKeyFile, privateKeyPEM); err != nil {
return err
}
// generate and write public key
pub, err := ssh.NewPublicKey(&privateKey.PublicKey)
if err != nil {
return err
}
return os.WriteFile(pubKeyPath, ssh.MarshalAuthorizedKey(pub), 0655)
}
func PrintObjYAML(obj *unstructured.Unstructured) error {
data, err := yaml.Marshal(obj)
if err != nil {
return err
}
fmt.Println(string(data))
return nil
}
type RetryOptions struct {
MaxRetry int
Delay time.Duration
}
func DoWithRetry(ctx context.Context, logger logr.Logger, operation func() error, options *RetryOptions) error {
err := operation()
for attempt := 0; err != nil && attempt < options.MaxRetry; attempt++ {
delay := time.Duration(int(math.Pow(2, float64(attempt)))) * time.Second
if options.Delay != 0 {
delay = options.Delay
}
logger.Info(fmt.Sprintf("Failed, retrying in %s ... (%d/%d). Error: %v", delay, attempt+1, options.MaxRetry, err))
select {
case <-time.After(delay):
case <-ctx.Done():
return err
}
err = operation()
}
return err
}
func PrintGoTemplate(wr io.Writer, tpl string, values interface{}) error {
tmpl, err := template.New("output").Parse(tpl)
if err != nil {
return err
}
err = tmpl.Execute(wr, values)
if err != nil {
return err
}
return nil
}
// SetKubeConfig sets KUBECONFIG environment
func SetKubeConfig(cfg string) error {
return os.Setenv("KUBECONFIG", cfg)
}
var addToScheme sync.Once
func NewFactory() cmdutil.Factory {
configFlags := NewConfigFlagNoWarnings()
// Add CRDs to the scheme. They are missing by default.
addToScheme.Do(func() {
if err := apiextv1.AddToScheme(scheme.Scheme); err != nil {
// This should never happen.
panic(err)
}
})
return cmdutil.NewFactory(configFlags)
}
// NewConfigFlagNoWarnings returns a ConfigFlags that disables warnings.
func NewConfigFlagNoWarnings() *genericclioptions.ConfigFlags {
configFlags := genericclioptions.NewConfigFlags(true)
configFlags.WrapConfigFn = func(c *rest.Config) *rest.Config {
c.WarningHandler = rest.NoWarnings{}
return c
}
return configFlags
}
func GVRToString(gvr schema.GroupVersionResource) string {
return strings.Join([]string{gvr.Resource, gvr.Version, gvr.Group}, ".")
}
// GetNodeByName chooses node by name from a node array
func GetNodeByName(nodes []*corev1.Node, name string) *corev1.Node {
for _, node := range nodes {
if node.Name == name {
return node
}
}
return nil
}
// ResourceIsEmpty checks if resource is empty or not
func ResourceIsEmpty(res *resource.Quantity) bool {
resStr := res.String()
if resStr == "0" || resStr == "<nil>" {
return true
}
return false
}
func GetPodStatus(pods []corev1.Pod) (running, waiting, succeeded, failed int) {
for _, pod := range pods {
switch pod.Status.Phase {
case corev1.PodRunning:
running++
case corev1.PodPending:
waiting++
case corev1.PodSucceeded:
succeeded++
case corev1.PodFailed:
failed++
}
}
return
}
// OpenBrowser opens browser with url in different OS system
func OpenBrowser(url string) error {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("cmd", "/C", "start", url).Run()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
return err
}
func TimeFormat(t *metav1.Time) string {
return TimeFormatWithDuration(t, time.Minute)
}
// TimeFormatWithDuration formats time with specified precision
func TimeFormatWithDuration(t *metav1.Time, duration time.Duration) string {
if t == nil || t.IsZero() {
return ""
}
return TimeTimeFormatWithDuration(t.Time, duration)
}
func TimeTimeFormat(t time.Time) string {
const layout = "Jan 02,2006 15:04 UTC-0700"
return t.Format(layout)
}
func timeLayout(precision time.Duration) string {
layout := "Jan 02,2006 15:04 UTC-0700"
switch precision {
case time.Second:
layout = "Jan 02,2006 15:04:05 UTC-0700"
case time.Millisecond:
layout = "Jan 02,2006 15:04:05.000 UTC-0700"
}
return layout
}
func TimeTimeFormatWithDuration(t time.Time, precision time.Duration) string {
layout := timeLayout(precision)
return t.Format(layout)
}
func TimeParse(t string, precision time.Duration) (time.Time, error) {
layout := timeLayout(precision)
return time.Parse(layout, t)
}
// GetHumanReadableDuration returns a succinct representation of the provided startTime and endTime
// with limited precision for consumption by humans.
func GetHumanReadableDuration(startTime metav1.Time, endTime metav1.Time) string {
if startTime.IsZero() {
return "<Unknown>"
}
if endTime.IsZero() {
endTime = metav1.NewTime(time.Now())
}
d := endTime.Sub(startTime.Time)
// if the
if d < time.Second {
d = time.Second
}
return duration.HumanDuration(d)
}
// CheckEmpty checks if string is empty, if yes, returns <none> for displaying
func CheckEmpty(str string) string {
if len(str) == 0 {
return types.None
}
return str
}
// BuildLabelSelectorByNames builds the label selector by instance names, the label selector is
// like "instance-key in (name1, name2)"
func BuildLabelSelectorByNames(selector string, names []string) string {
if len(names) == 0 {
return selector
}
label := fmt.Sprintf("%s in (%s)", constant.AppInstanceLabelKey, strings.Join(names, ","))
if len(selector) == 0 {
return label
} else {
return selector + "," + label
}
}
// SortEventsByLastTimestamp sorts events by lastTimestamp
func SortEventsByLastTimestamp(events *corev1.EventList, eventType string) *[]apiruntime.Object {
objs := make([]apiruntime.Object, 0, len(events.Items))
for i, e := range events.Items {
if eventType != "" && e.Type != eventType {
continue
}
objs = append(objs, &events.Items[i])
}
sorter := cmdget.NewRuntimeSort("{.lastTimestamp}", objs)
sort.Sort(sorter)
return &objs
}
func GetEventTimeStr(e *corev1.Event) string {
t := &e.CreationTimestamp
if !e.LastTimestamp.Time.IsZero() {
t = &e.LastTimestamp
}
return TimeFormat(t)
}
func GetEventObject(e *corev1.Event) string {
kind := e.InvolvedObject.Kind
if kind == "Pod" {
kind = "Instance"
}
return fmt.Sprintf("%s/%s", kind, e.InvolvedObject.Name)
}
// GetConfigTemplateList returns ConfigTemplate list used by the component.
func GetConfigTemplateList(clusterName string, namespace string, cli dynamic.Interface, componentName string, reloadTpl bool) ([]appsv1alpha1.ComponentConfigSpec, error) {
var (
clusterObj = appsv1alpha1.Cluster{}
clusterDefObj = appsv1alpha1.ClusterDefinition{}
clusterVersionObj = appsv1alpha1.ClusterVersion{}
)
clusterKey := client.ObjectKey{
Namespace: namespace,
Name: clusterName,
}
if err := GetResourceObjectFromGVR(types.ClusterGVR(), clusterKey, cli, &clusterObj); err != nil {
return nil, err
}
clusterDefKey := client.ObjectKey{
Namespace: "",
Name: clusterObj.Spec.ClusterDefRef,
}
if err := GetResourceObjectFromGVR(types.ClusterDefGVR(), clusterDefKey, cli, &clusterDefObj); err != nil {
return nil, err
}
clusterVerKey := client.ObjectKey{
Namespace: "",
Name: clusterObj.Spec.ClusterVersionRef,
}
if clusterVerKey.Name != "" {
if err := GetResourceObjectFromGVR(types.ClusterVersionGVR(), clusterVerKey, cli, &clusterVersionObj); err != nil {
return nil, err
}
}
return GetConfigTemplateListWithResource(clusterObj.Spec.ComponentSpecs, clusterDefObj.Spec.ComponentDefs, clusterVersionObj.Spec.ComponentVersions, componentName, reloadTpl)
}
func GetConfigTemplateListWithResource(cComponents []appsv1alpha1.ClusterComponentSpec,
dComponents []appsv1alpha1.ClusterComponentDefinition,
vComponents []appsv1alpha1.ClusterComponentVersion,
componentName string,
reloadTpl bool) ([]appsv1alpha1.ComponentConfigSpec, error) {
configSpecs, err := core.GetConfigTemplatesFromComponent(cComponents, dComponents, vComponents, componentName)
if err != nil {
return nil, err
}
return getValidConfigSpecs(reloadTpl, configSpecs)
}
func getValidConfigSpecs(reloadTpl bool, configSpecs []appsv1alpha1.ComponentConfigSpec) ([]appsv1alpha1.ComponentConfigSpec, error) {
if !reloadTpl || len(configSpecs) == 1 {
return configSpecs, nil
}
validConfigSpecs := make([]appsv1alpha1.ComponentConfigSpec, 0, len(configSpecs))
for _, configSpec := range configSpecs {
if configSpec.ConfigConstraintRef != "" && configSpec.TemplateRef != "" {
validConfigSpecs = append(validConfigSpecs, configSpec)
}
}
return validConfigSpecs, nil
}
func GetConfigSpecsFromComponentName(namespace, clusterName, componentName string, reloadTpl bool, cli dynamic.Interface) ([]appsv1alpha1.ComponentConfigSpec, error) {
configKey := client.ObjectKey{
Namespace: namespace,
Name: core.GenerateComponentConfigurationName(clusterName, componentName),
}
config := appsv1alpha1.Configuration{}
if err := GetResourceObjectFromGVR(types.ConfigurationGVR(), configKey, cli, &config); err != nil {
return nil, err
}
if len(config.Spec.ConfigItemDetails) == 0 {
return nil, nil
}
configSpecs := make([]appsv1alpha1.ComponentConfigSpec, 0, len(config.Spec.ConfigItemDetails))
for _, item := range config.Spec.ConfigItemDetails {
if item.ConfigSpec != nil {
configSpecs = append(configSpecs, *item.ConfigSpec)
}
}
return getValidConfigSpecs(reloadTpl, configSpecs)
}
// GetK8SClientObject gets the client object of k8s,
// obj must be a struct pointer so that obj can be updated with the response.
func GetK8SClientObject(dynamic dynamic.Interface,
obj client.Object,
gvr schema.GroupVersionResource,
namespace,
name string) error {
unstructuredObj, err := dynamic.Resource(gvr).Namespace(namespace).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
return err
}
return apiruntime.DefaultUnstructuredConverter.FromUnstructured(unstructuredObj.UnstructuredContent(), obj)
}
// GetResourceObjectFromGVR queries the resource object using GVR.
func GetResourceObjectFromGVR(gvr schema.GroupVersionResource, key client.ObjectKey, client dynamic.Interface, k8sObj interface{}) error {
unstructuredObj, err := client.
Resource(gvr).
Namespace(key.Namespace).
Get(context.TODO(), key.Name, metav1.GetOptions{})
if err != nil {
return core.WrapError(err, "failed to get resource[%v]", key)
}
return apiruntime.DefaultUnstructuredConverter.FromUnstructured(unstructuredObj.Object, k8sObj)
}
// GetComponentsFromResource returns name of component.
func GetComponentsFromResource(namespace, clusterName string, componentSpecs []appsv1alpha1.ClusterComponentSpec, cli dynamic.Interface) ([]string, error) {
componentNames := make([]string, 0, len(componentSpecs))
for _, component := range componentSpecs {
configKey := client.ObjectKey{
Namespace: namespace,
Name: core.GenerateComponentConfigurationName(clusterName, component.Name),
}
config := appsv1alpha1.Configuration{}
if err := GetResourceObjectFromGVR(types.ConfigurationGVR(), configKey, cli, &config); err != nil {
return nil, err
}
if len(config.Spec.ConfigItemDetails) == 0 {
continue
}
if enableReconfiguring(&config.Spec) {
componentNames = append(componentNames, component.Name)
}
}
return componentNames, nil
}
func enableReconfiguring(component *appsv1alpha1.ConfigurationSpec) bool {
if component == nil {
return false
}
for _, item := range component.ConfigItemDetails {
if item.ConfigSpec == nil {
continue
}
tpl := item.ConfigSpec
if len(tpl.ConfigConstraintRef) > 0 && len(tpl.TemplateRef) > 0 {
return true
}
}
return false
}
// IsSupportReconfigureParams checks whether all updated parameters belong to config template parameters.
func IsSupportReconfigureParams(tpl appsv1alpha1.ComponentConfigSpec, values map[string]*string, cli dynamic.Interface) (bool, error) {
var (
err error
configConstraint = appsv1alpha1.ConfigConstraint{}
)
if err := GetResourceObjectFromGVR(types.ConfigConstraintGVR(), client.ObjectKey{
Namespace: "",
Name: tpl.ConfigConstraintRef,
}, cli, &configConstraint); err != nil {
return false, err
}
if configConstraint.Spec.ConfigurationSchema == nil {
return true, nil
}
schema := configConstraint.Spec.ConfigurationSchema.DeepCopy()
if schema.Schema == nil {
schema.Schema, err = openapi.GenerateOpenAPISchema(schema.CUE, configConstraint.Spec.CfgSchemaTopLevelName)
if err != nil {
return false, err
}
if schema.Schema == nil {
return true, nil
}
}
schemaSpec := schema.Schema.Properties["spec"]
for key := range values {
if _, ok := schemaSpec.Properties[key]; !ok {
return false, nil
}
}
return true, nil
}
func ValidateParametersModified(tpl *appsv1alpha1.ComponentConfigSpec, parameters sets.Set[string], cli dynamic.Interface) (err error) {
cc := appsv1alpha1.ConfigConstraint{}
ccKey := client.ObjectKey{
Namespace: "",
Name: tpl.ConfigConstraintRef,
}
if err = GetResourceObjectFromGVR(types.ConfigConstraintGVR(), ccKey, cli, &cc); err != nil {
return
}
return ValidateParametersModified2(parameters, cc.Spec)
}
func ValidateParametersModified2(parameters sets.Set[string], cc appsv1alpha1.ConfigConstraintSpec) error {
if len(cc.ImmutableParameters) == 0 {
return nil
}
immutableParameters := sets.New(cc.ImmutableParameters...)
uniqueParameters := immutableParameters.Intersection(parameters)
if uniqueParameters.Len() == 0 {
return nil
}
return core.MakeError("parameter[%v] is immutable, cannot be modified!", cfgutil.ToSet(uniqueParameters).AsSlice())
}
func GetIPLocation() (string, error) {
client := &http.Client{Timeout: 10 * time.Second}
req, err := http.NewRequest("GET", "https://ifconfig.io/country_code", nil)
if err != nil {
return "", err
}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
location, err := io.ReadAll(resp.Body)
if len(location) == 0 || err != nil {
return "", err
}
// remove last "\n"
return string(location[:len(location)-1]), nil
}
// GetHelmChartRepoURL gets helm chart repo, chooses one from GitHub and GitLab based on the IP location
func GetHelmChartRepoURL() string {
if types.KubeBlocksChartURL == testing.KubeBlocksChartURL {
return testing.KubeBlocksChartURL
}
// if helm repo url is specified by config or environment, use it
url := viper.GetString(types.CfgKeyHelmRepoURL)
if url != "" {
klog.V(1).Infof("Using helm repo url set by config or environment: %s", url)
return url
}
// if helm repo url is not specified, choose one from GitHub and GitLab based on the IP location
// if location is CN, or we can not get location, use GitLab helm chart repo
repo := types.KubeBlocksChartURL
location, _ := GetIPLocation()
if location == "CN" || location == "" {
repo = types.GitLabHelmChartRepo
}
klog.V(1).Infof("Using helm repo url: %s", repo)
return repo
}
// GetKubeBlocksNamespace gets namespace of KubeBlocks installation, infer namespace from helm secrets
func GetKubeBlocksNamespace(client kubernetes.Interface) (string, error) {
secrets, err := client.CoreV1().Secrets(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{LabelSelector: types.KubeBlocksHelmLabel})
// if KubeBlocks is upgraded, there will be multiple secrets
if err == nil && len(secrets.Items) >= 1 {
return secrets.Items[0].Namespace, nil
}
return "", errors.New("failed to get KubeBlocks installation namespace")
}
// GetKubeBlocksCRDsURL gets the crds url by IP region.
func GetKubeBlocksCRDsURL(kbVersion string) string {
kbVersion = TrimVersionPrefix(kbVersion)
location, _ := GetIPLocation()
crdsURL := fmt.Sprintf("https://github.com/apecloud/kubeblocks/releases/download/v%s/kubeblocks_crds.yaml", kbVersion)
if location == "CN" || location == "" {
crdsURL = fmt.Sprintf("https://jihulab.com/api/v4/projects/98723/packages/generic/kubeblocks/v%s/kubeblocks_crds.yaml", kbVersion)
}
klog.V(1).Infof("CRDs url: %s", crdsURL)
return crdsURL
}
// GetKubeBlocksNamespaceByDynamic gets namespace of KubeBlocks installation, infer namespace from helm secrets
func GetKubeBlocksNamespaceByDynamic(dynamic dynamic.Interface) (string, error) {
list, err := dynamic.Resource(types.SecretGVR()).List(context.TODO(), metav1.ListOptions{LabelSelector: types.KubeBlocksHelmLabel})
if err == nil && len(list.Items) >= 1 {
return list.Items[0].GetNamespace(), nil
}
return "", errors.New("failed to get KubeBlocks installation namespace")
}
type ExposeType string
const (
ExposeToVPC ExposeType = "vpc"
ExposeToInternet ExposeType = "internet"
EnableValue string = "true"
DisableValue string = "false"
)
var ProviderExposeAnnotations = map[K8sProvider]map[ExposeType]map[string]string{
EKSProvider: {
ExposeToVPC: map[string]string{
"service.beta.kubernetes.io/aws-load-balancer-type": "nlb",
"service.beta.kubernetes.io/aws-load-balancer-internal": "true",
},
ExposeToInternet: map[string]string{
"service.beta.kubernetes.io/aws-load-balancer-type": "nlb",
"service.beta.kubernetes.io/aws-load-balancer-internal": "false",
},
},
GKEProvider: {
ExposeToVPC: map[string]string{
"networking.gke.io/load-balancer-type": "Internal",
},
ExposeToInternet: map[string]string{},
},
AKSProvider: {
ExposeToVPC: map[string]string{
"service.beta.kubernetes.io/azure-load-balancer-internal": "true",
},
ExposeToInternet: map[string]string{
"service.beta.kubernetes.io/azure-load-balancer-internal": "false",
},
},
ACKProvider: {
ExposeToVPC: map[string]string{
"service.beta.kubernetes.io/alibaba-cloud-loadbalancer-address-type": "intranet",
},
ExposeToInternet: map[string]string{
"service.beta.kubernetes.io/alibaba-cloud-loadbalancer-address-type": "internet",
},
},
// TKE VPC LoadBalancer needs the subnet id, it's difficult for KB to get it, so we just support the internet on TKE now.
// reference: https://cloud.tencent.com/document/product/457/45487
TKEProvider: {
ExposeToInternet: map[string]string{},
},
KINDProvider: {
ExposeToVPC: map[string]string{},
},
K3SProvider: {
ExposeToVPC: map[string]string{},
},
}
func GetExposeAnnotations(provider K8sProvider, exposeType ExposeType) (map[string]string, error) {
exposeAnnotations, ok := ProviderExposeAnnotations[provider]
if !ok {
return nil, fmt.Errorf("unsupported provider: %s", provider)
}
annotations, ok := exposeAnnotations[exposeType]
if !ok {
return nil, fmt.Errorf("unsupported expose type: %s on provider %s", exposeType, provider)
}
return annotations, nil
}
// BuildAddonReleaseName returns the release name of addon, its f
func BuildAddonReleaseName(addon string) string {
return fmt.Sprintf("%s-%s", types.AddonReleasePrefix, addon)
}
// CombineLabels combines labels into a string
func CombineLabels(labels map[string]string) string {
var labelStr []string
for k, v := range labels {
labelStr = append(labelStr, fmt.Sprintf("%s=%s", k, v))
}
// sort labelStr to make sure the order is stable
sort.Strings(labelStr)
return strings.Join(labelStr, ",")
}
func BuildComponentNameLabels(prefix string, names []string) string {
return buildLabelSelectors(prefix, constant.KBAppComponentLabelKey, names)
}
// buildLabelSelectors builds the label selector by given label key, the label selector is
// like "label-key in (name1, name2)"
func buildLabelSelectors(prefix string, key string, names []string) string {
if len(names) == 0 {
return prefix
}
label := fmt.Sprintf("%s in (%s)", key, strings.Join(names, ","))
if len(prefix) == 0 {
return label
} else {
return prefix + "," + label
}
}
// NewOpsRequestForReconfiguring returns a new common OpsRequest for Reconfiguring operation
func NewOpsRequestForReconfiguring(opsName, namespace, clusterName string) *appsv1alpha1.OpsRequest {
return &appsv1alpha1.OpsRequest{
TypeMeta: metav1.TypeMeta{
APIVersion: fmt.Sprintf("%s/%s", types.AppsAPIGroup, types.AppsAPIVersion),
Kind: types.KindOps,
},
ObjectMeta: metav1.ObjectMeta{
Name: opsName,
Namespace: namespace,
},
Spec: appsv1alpha1.OpsRequestSpec{
ClusterRef: clusterName,
Type: appsv1alpha1.ReconfiguringType,
Reconfigure: &appsv1alpha1.Reconfigure{},
},
}
}
func ConvertObjToUnstructured(obj any) (*unstructured.Unstructured, error) {
var (
contentBytes []byte
err error
unstructuredObj = &unstructured.Unstructured{}
)
if contentBytes, err = json.Marshal(obj); err != nil {
return nil, err
}
if err = json.Unmarshal(contentBytes, unstructuredObj); err != nil {
return nil, err
}
return unstructuredObj, nil
}
func CreateResourceIfAbsent(
dynamic dynamic.Interface,
gvr schema.GroupVersionResource,
namespace string,
unstructuredObj *unstructured.Unstructured) error {
objectName, isFound, err := unstructured.NestedString(unstructuredObj.Object, "metadata", "name")
if !isFound || err != nil {
return err
}
objectByte, err := json.Marshal(unstructuredObj)
if err != nil {
return err
}
if _, err = dynamic.Resource(gvr).Namespace(namespace).Patch(
context.TODO(), objectName, k8sapitypes.MergePatchType,
objectByte, metav1.PatchOptions{}); err != nil {
if apierrors.IsNotFound(err) {
if _, err = dynamic.Resource(gvr).Namespace(namespace).Create(
context.TODO(), unstructuredObj, metav1.CreateOptions{}); err != nil {
return err
}
} else {
return err
}
}
return nil
}
func BuildClusterDefinitionRefLabel(prefix string, clusterDef []string) string {
return buildLabelSelectors(prefix, constant.AppNameLabelKey, clusterDef)
}
func BuildClusterLabel(prefix string, addon []string) string {
return buildLabelSelectors(prefix, constant.ClusterDefLabelKey, addon)
}
// IsWindows returns true if the kbcli runtime situation is windows
func IsWindows() bool {
return runtime.GOOS == types.GoosWindows
}
func GetUnifiedDiffString(original, edited string, from, to string, contextLine int) (string, error) {
if contextLine <= 0 {
contextLine = 3
}
diff := difflib.UnifiedDiff{
A: difflib.SplitLines(original),
B: difflib.SplitLines(edited),
FromFile: from,
ToFile: to,
Context: contextLine,
}
return difflib.GetUnifiedDiffString(diff)
}
func DisplayDiffWithColor(out io.Writer, diffText string) {
for _, line := range difflib.SplitLines(diffText) {
switch {
case strings.HasPrefix(line, "---"), strings.HasPrefix(line, "+++"):
line = color.HiYellowString(line)
case strings.HasPrefix(line, "@@"):
line = color.HiBlueString(line)
case strings.HasPrefix(line, "-"):
line = color.RedString(line)
case strings.HasPrefix(line, "+"):
line = color.GreenString(line)
}
fmt.Fprint(out, line)
}
}
// BuildTolerations toleration format: key=value:effect or key:effect,
func BuildTolerations(raw []string) ([]interface{}, error) {
tolerations := make([]interface{}, 0)
for _, tolerationRaw := range raw {
for _, entries := range strings.Split(tolerationRaw, ",") {
toleration := make(map[string]interface{})
parts := strings.Split(entries, ":")
if len(parts) != 2 {
return tolerations, fmt.Errorf("invalid toleration %s", entries)
}
toleration["effect"] = parts[1]
partsKV := strings.Split(parts[0], "=")
switch len(partsKV) {
case 1:
toleration["operator"] = "Exists"
toleration["key"] = partsKV[0]
case 2:
toleration["operator"] = "Equal"
toleration["key"] = partsKV[0]
toleration["value"] = partsKV[1]
default:
return tolerations, fmt.Errorf("invalid toleration %s", entries)
}
tolerations = append(tolerations, toleration)
}
}
return tolerations, nil
}
// BuildNodeAffinity build node affinity from node labels
func BuildNodeAffinity(nodeLabels map[string]string) *corev1.NodeAffinity {
var nodeAffinity *corev1.NodeAffinity
var matchExpressions []corev1.NodeSelectorRequirement
for key, value := range nodeLabels {
values := strings.Split(value, ",")
matchExpressions = append(matchExpressions, corev1.NodeSelectorRequirement{
Key: key,
Operator: corev1.NodeSelectorOpIn,
Values: values,
})
}
if len(matchExpressions) > 0 {
nodeSelectorTerm := corev1.NodeSelectorTerm{
MatchExpressions: matchExpressions,