github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/controllers/apps/utils.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package apps 21 22 import ( 23 "context" 24 "fmt" 25 "time" 26 27 corev1 "k8s.io/api/core/v1" 28 "k8s.io/client-go/tools/record" 29 "sigs.k8s.io/controller-runtime/pkg/client" 30 31 appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1" 32 "github.com/1aal/kubeblocks/controllers/apps/components" 33 "github.com/1aal/kubeblocks/pkg/constant" 34 intctrlutil "github.com/1aal/kubeblocks/pkg/controllerutil" 35 ) 36 37 // default reconcile requeue after duration 38 var requeueDuration = time.Millisecond * 1000 39 40 func getEnvReplacementMapForAccount(name, passwd string) map[string]string { 41 return map[string]string{ 42 "$(USERNAME)": name, 43 "$(PASSWD)": passwd, 44 } 45 } 46 47 // notifyClusterStatusChange notifies cluster changes occurred and triggers it to reconcile. 48 func notifyClusterStatusChange(ctx context.Context, cli client.Client, recorder record.EventRecorder, obj client.Object, event *corev1.Event) error { 49 if obj == nil || !intctrlutil.WorkloadFilterPredicate(obj) { 50 return nil 51 } 52 53 cluster, ok := obj.(*appsv1alpha1.Cluster) 54 if !ok { 55 var err error 56 if cluster, err = components.GetClusterByObject(ctx, cli, obj); err != nil { 57 return err 58 } 59 } 60 61 patch := client.MergeFrom(cluster.DeepCopy()) 62 if cluster.Annotations == nil { 63 cluster.Annotations = map[string]string{} 64 } 65 cluster.Annotations[constant.ReconcileAnnotationKey] = time.Now().Format(time.RFC3339Nano) 66 if err := cli.Patch(ctx, cluster, patch); err != nil { 67 return err 68 } 69 70 if recorder != nil && event != nil { 71 recorder.Eventf(cluster, corev1.EventTypeWarning, event.Reason, getFinalEventMessageForRecorder(event)) 72 } 73 return nil 74 } 75 76 // getFinalEventMessageForRecorder gets final event message by event involved object kind for recorded it 77 func getFinalEventMessageForRecorder(event *corev1.Event) string { 78 if event.InvolvedObject.Kind == constant.PodKind { 79 return fmt.Sprintf("Pod %s: %s", event.InvolvedObject.Name, event.Message) 80 } 81 return event.Message 82 } 83 84 func boolValue(b *bool) bool { 85 if b == nil { 86 return false 87 } 88 return *b 89 } 90 91 func mergeMap(dst, src map[string]string) { 92 for key, val := range src { 93 dst[key] = val 94 } 95 }