@@ -38,6 +38,10 @@ import (
3838 "github.com/apecloud/kubeblocks/pkg/controller/model"
3939)
4040
41+ const (
42+ assistantObjectAnnotationKey = "workloads.kubeblocks.io/assistant-object"
43+ )
44+
4145func NewAssistantObjectReconciler () kubebuilderx.Reconciler {
4246 return & assistantObjectReconciler {}
4347}
@@ -67,10 +71,29 @@ func (r *assistantObjectReconciler) Reconcile(tree *kubebuilderx.ObjectTree) (ku
6771}
6872
6973func (r * assistantObjectReconciler ) createOrUpdate (tree * kubebuilderx.ObjectTree , inst * workloads.Instance , assistantObj workloads.InstanceAssistantObject ) error {
70- obj := r .checkObjectProvisionPolicy (inst , r .instanceAssistantObject (assistantObj ))
71- if obj == nil {
72- return nil // skip the object
74+ obj , ok := instanceAssistantObject (assistantObj )
75+ if ! ok {
76+ return nil
77+ }
78+ if isOrdinalAssistantObject (obj ) {
79+ obj = r .checkObjectProvisionPolicy (inst , obj )
80+ if obj == nil {
81+ return nil // skip the object
82+ }
83+ return r .createOrUpdateOwned (tree , inst , assistantObj , obj )
84+ }
85+ return r .createOrUpdateShared (tree , inst , assistantObj , obj )
86+ }
87+
88+ func (r * assistantObjectReconciler ) checkObjectProvisionPolicy (inst * workloads.Instance , obj client.Object ) client.Object {
89+ if isCurrentInstanceOrdinalAssistantObject (inst , obj ) {
90+ return obj
7391 }
92+ return nil
93+ }
94+
95+ func (r * assistantObjectReconciler ) createOrUpdateOwned (tree * kubebuilderx.ObjectTree , inst * workloads.Instance ,
96+ assistantObj workloads.InstanceAssistantObject , obj client.Object ) error {
7497 robj , err := tree .Get (obj )
7598 if err != nil && ! errors .IsNotFound (err ) {
7699 return err
@@ -94,43 +117,95 @@ func (r *assistantObjectReconciler) createOrUpdate(tree *kubebuilderx.ObjectTree
94117 return nil
95118}
96119
97- func (r * assistantObjectReconciler ) instanceAssistantObject (obj workloads.InstanceAssistantObject ) client.Object {
120+ func (r * assistantObjectReconciler ) createOrUpdateShared (tree * kubebuilderx.ObjectTree , inst * workloads.Instance ,
121+ assistantObj workloads.InstanceAssistantObject , obj client.Object ) error {
122+ robj , err := tree .Get (obj )
123+ if err != nil {
124+ return err
125+ }
126+ if robj == nil {
127+ markSharedAssistantObject (inst , obj )
128+ return tree .Add (obj )
129+ }
130+
131+ desired := obj .DeepCopyObject ().(client.Object )
132+ markSharedAssistantObject (inst , desired )
133+ if merged := r .copyAndMerge (assistantObj , robj , desired ); merged != nil {
134+ return tree .Update (merged )
135+ }
136+ return nil
137+ }
138+
139+ func instanceAssistantObject (obj workloads.InstanceAssistantObject ) (client.Object , bool ) {
98140 if obj .Service != nil {
99- return obj .Service
141+ return obj .Service , true
100142 }
101143 if obj .ConfigMap != nil {
102- return obj .ConfigMap
144+ return obj .ConfigMap , true
103145 }
104146 if obj .Secret != nil {
105- return obj .Secret
147+ return obj .Secret , true
106148 }
107149 if obj .ServiceAccount != nil {
108- return obj .ServiceAccount
150+ return obj .ServiceAccount , true
109151 }
110152 if obj .Role != nil {
111- return obj .Role
153+ return obj .Role , true
154+ }
155+ if obj .RoleBinding != nil {
156+ return obj .RoleBinding , true
112157 }
113- return obj . RoleBinding
158+ return nil , false
114159}
115160
116- func (r * assistantObjectReconciler ) checkObjectProvisionPolicy (inst * workloads.Instance , obj client.Object ) client.Object {
117- var policy string
118- if obj .GetAnnotations () != nil {
119- policy = obj .GetAnnotations ()[constant .KBAppMultiClusterObjectProvisionPolicyKey ]
120- }
121- if policy != "ordinal" { // HACK
122- return obj
161+ func isOrdinalAssistantObject (obj client.Object ) bool {
162+ if obj .GetAnnotations () == nil {
163+ return false
123164 }
165+ return obj .GetAnnotations ()[constant .KBAppMultiClusterObjectProvisionPolicyKey ] == constant .KBAppMultiClusterObjectProvisionOrdinal
166+ }
124167
168+ func isCurrentInstanceOrdinalAssistantObject (inst * workloads.Instance , obj client.Object ) bool {
125169 ordinal := func () int {
126170 subs := strings .Split (inst .GetName (), "-" )
127171 o , _ := strconv .Atoi (subs [len (subs )- 1 ])
128172 return o
129173 }
130- if strings .HasSuffix (obj .GetName (), fmt .Sprintf ("-%d" , ordinal ())) {
131- return obj
174+ return strings .HasSuffix (obj .GetName (), fmt .Sprintf ("-%d" , ordinal ()))
175+ }
176+
177+ func markSharedAssistantObject (inst * workloads.Instance , obj client.Object ) {
178+ labels := obj .GetLabels ()
179+ if labels == nil {
180+ labels = map [string ]string {}
132181 }
133- return nil
182+ labels [constant .AppManagedByLabelKey ] = constant .AppName
183+ if clusterName := inst .Labels [constant .AppInstanceLabelKey ]; clusterName != "" {
184+ labels [constant .AppInstanceLabelKey ] = clusterName
185+ }
186+ if compName := inst .Labels [constant .KBAppComponentLabelKey ]; compName != "" {
187+ labels [constant .KBAppComponentLabelKey ] = compName
188+ }
189+ delete (labels , constant .KBAppInstanceNameLabelKey )
190+ obj .SetLabels (labels )
191+
192+ annotations := obj .GetAnnotations ()
193+ if annotations == nil {
194+ annotations = map [string ]string {}
195+ }
196+ annotations [assistantObjectAnnotationKey ] = "true"
197+ obj .SetAnnotations (annotations )
198+ }
199+
200+ func isSharedAssistantObject (obj client.Object , inst * workloads.Instance ) bool {
201+ labels := obj .GetLabels ()
202+ annotations := obj .GetAnnotations ()
203+ return labels [constant .AppManagedByLabelKey ] == constant .AppName &&
204+ labels [constant .AppInstanceLabelKey ] != "" &&
205+ labels [constant .AppInstanceLabelKey ] == inst .Labels [constant .AppInstanceLabelKey ] &&
206+ labels [constant .KBAppComponentLabelKey ] != "" &&
207+ labels [constant .KBAppComponentLabelKey ] == inst .Labels [constant .KBAppComponentLabelKey ] &&
208+ annotations [assistantObjectAnnotationKey ] == "true"
134209}
135210
136211func (r * assistantObjectReconciler ) copyAndMerge (obj workloads.InstanceAssistantObject , oldObj , newObj client.Object ) client.Object {
0 commit comments