-
Notifications
You must be signed in to change notification settings - Fork 175
/
util.go
136 lines (117 loc) · 4.75 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
/*
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 controllerutil
import (
"context"
"reflect"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/constant"
viper "github.com/apecloud/kubeblocks/pkg/viperx"
)
// GetUncachedObjects returns a list of K8s objects, for these object types,
// and their list types, client.Reader will read directly from the API server instead
// of the cache, which may not be up-to-date.
// see sigs.k8s.io/controller-runtime/pkg/client/split.go to understand how client
// works with this UncachedObjects filter.
func GetUncachedObjects() []client.Object {
// client-side read cache reduces the number of requests processed in the API server,
// which is good for performance. However, it can sometimes lead to obscure issues,
// most notably lacking read-after-write consistency, i.e. reading a value immediately
// after updating it may miss to see the changes.
// while in most cases this problem can be mitigated by retrying later in an idempotent
// manner, there are some cases where it cannot, for example if a decision is to be made
// that has side-effect operations such as returning an error message to the user
// (in webhook) or deleting certain resources (in controllerutil.HandleCRDeletion).
// additionally, retry loops cause unnecessary delays when reconciliations are processed.
// for the sake of performance, now only the objects created by the end-user is listed here,
// to solve the two problems mentioned above.
// consider carefully before adding new objects to this list.
return []client.Object{
// avoid to cache potential large data objects
&corev1.ConfigMap{},
&corev1.Secret{},
&appsv1alpha1.Cluster{},
&appsv1alpha1.Configuration{},
}
}
// Event is wrapper for Recorder.Event, if Recorder is nil, then it's no-op.
func (r *RequestCtx) Event(object runtime.Object, eventtype, reason, message string) {
if r == nil || r.Recorder == nil {
return
}
r.Recorder.Event(object, eventtype, reason, message)
}
// Eventf is wrapper for Recorder.Eventf, if Recorder is nil, then it's no-op.
func (r *RequestCtx) Eventf(object runtime.Object, eventtype, reason, messageFmt string, args ...interface{}) {
if r == nil || r.Recorder == nil {
return
}
r.Recorder.Eventf(object, eventtype, reason, messageFmt, args...)
}
// UpdateCtxValue updates Context value, returns parent Context.
func (r *RequestCtx) UpdateCtxValue(key, val any) context.Context {
p := r.Ctx
r.Ctx = context.WithValue(r.Ctx, key, val)
return p
}
// WithValue returns a copy of parent in which the value associated with key is
// val.
func (r *RequestCtx) WithValue(key, val any) context.Context {
return context.WithValue(r.Ctx, key, val)
}
// IsRSMEnabled enables rsm by default.
// respect the feature gate if set, keep the ability to disable it.
func IsRSMEnabled() bool {
if viper.IsSet(constant.FeatureGateReplicatedStateMachine) {
return viper.GetBool(constant.FeatureGateReplicatedStateMachine)
}
return true
}
func IsNil(i interface{}) bool {
if i == nil {
return true
}
switch reflect.TypeOf(i).Kind() {
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
return reflect.ValueOf(i).IsNil()
}
return false
}
var innerScheme, _ = appsv1alpha1.SchemeBuilder.Build()
func SetOwnerReference(owner, object metav1.Object) error {
return controllerutil.SetOwnerReference(owner, object, innerScheme)
}
func SetControllerReference(owner, object metav1.Object) error {
return controllerutil.SetControllerReference(owner, object, innerScheme)
}
func GeKubeRestConfig() *rest.Config {
cfg := ctrl.GetConfigOrDie()
clientQPS := viper.GetInt(constant.CfgClientQPS)
if clientQPS != 0 {
cfg.QPS = float32(clientQPS)
}
clientBurst := viper.GetInt(constant.CfgClientBurst)
if clientBurst != 0 {
cfg.Burst = clientBurst
}
return cfg
}