@@ -15,22 +15,23 @@ import (
1515)
1616
1717type OperatorContext struct {
18- Ctx context.Context
19-
18+ Ctx context.Context
19+ Labels map [string ]string
20+ Annotations map [string ]string
2021 Request * reconcile.Request
2122 Client controllerClient.Client
2223 Scheme * runtime.Scheme
2324 MarklogicGroup * marklogicv1.MarklogicGroup
2425 ReqLogger logr.Logger
2526 Recorder record.EventRecorder
26-
27- Services []* corev1.Service
28- StatefulSets []* appsv1.StatefulSet
27+ Services []* corev1.Service
28+ StatefulSets []* appsv1.StatefulSet
2929}
3030
3131type ClusterContext struct {
32- Ctx context.Context
33-
32+ Ctx context.Context
33+ Labels map [string ]string
34+ Annotations map [string ]string
3435 Request * reconcile.Request
3536 Client controllerClient.Client
3637 Scheme * runtime.Scheme
@@ -57,17 +58,16 @@ func CreateOperatorContext(
5758 oc .Scheme = scheme
5859 oc .ReqLogger = reqLogger
5960 oc .Recorder = rec
61+ oc .Labels = map [string ]string {}
62+ oc .Annotations = map [string ]string {}
6063 mlg := & marklogicv1.MarklogicGroup {}
6164 if err := retrieveMarkLogicGroup (oc , request , mlg ); err != nil {
6265 oc .ReqLogger .Error (err , "Failed to retrieve MarkLogicServer" )
6366 return nil , err
6467 }
65- // if err := retrieveMarklogicCluster(oc, request, mlc); err != nil {
66- // oc.ReqLogger.Error(err, "Failed to retrieve MarkLogicCluster")
67- // return nil, err
68- // }
6968 oc .MarklogicGroup = mlg
70- // oc.MarklogicCluster = mlc
69+ oc .SetOperatorLabels (oc .MarklogicGroup .GetLabels ())
70+ oc .SetOperatorAnnotations (oc .MarklogicGroup .GetAnnotations ())
7171
7272 oc .ReqLogger .Info ("==== CreateOperatorContext" )
7373
@@ -92,14 +92,16 @@ func CreateClusterContext(
9292 cc .Scheme = scheme
9393 cc .ReqLogger = reqLogger
9494 cc .Recorder = rec
95+ cc .Labels = map [string ]string {}
96+ cc .Annotations = map [string ]string {}
9597 mlc := & marklogicv1.MarklogicCluster {}
96-
9798 if err := retrieveMarklogicCluster (cc , request , mlc ); err != nil {
9899 cc .ReqLogger .Error (err , "Failed to retrieve MarkLogicCluster" )
99100 return nil , err
100101 }
101102 cc .MarklogicCluster = mlc
102-
103+ cc .SetClusterLabels (cc .MarklogicCluster .GetLabels ())
104+ cc .SetClusterAnnotations (cc .MarklogicCluster .GetAnnotations ())
103105 cc .ReqLogger .Info ("==== CreateOperatorContext" )
104106
105107 // cc.ReqLogger = cc.ReqLogger.WithValues("ML server name")
@@ -137,3 +139,83 @@ func (oc *OperatorContext) GetClient() controllerClient.Client {
137139func (oc * OperatorContext ) GetContext () context.Context {
138140 return oc .Ctx
139141}
142+
143+ func (cc * ClusterContext ) GetClusterLabels (name string ) map [string ]string {
144+ defaultLabels := getSelectorLabels (name )
145+ mergedLabels := map [string ]string {}
146+ if len (cc .Labels ) > 0 {
147+ for k , v := range defaultLabels {
148+ mergedLabels [k ] = v
149+ }
150+ for k , v := range cc .Labels {
151+ if _ , ok := defaultLabels [k ]; ! ok {
152+ mergedLabels [k ] = v
153+ }
154+ }
155+ } else {
156+ return defaultLabels
157+ }
158+ return mergedLabels
159+ }
160+
161+ func (cc * ClusterContext ) GetHAProxyLabels (name string ) map [string ]string {
162+ defaultHaproxyLabels := getHAProxySelectorLabels (name )
163+ mergedLabels := map [string ]string {}
164+ if len (cc .Labels ) > 0 {
165+ for k , v := range defaultHaproxyLabels {
166+ mergedLabels [k ] = v
167+ }
168+ for k , v := range cc .Labels {
169+ if _ , ok := defaultHaproxyLabels [k ]; ! ok {
170+ mergedLabels [k ] = v
171+ }
172+ }
173+ } else {
174+ return defaultHaproxyLabels
175+ }
176+ return mergedLabels
177+ }
178+
179+ func (cc * ClusterContext ) GetClusterAnnotations () map [string ]string {
180+ return cc .Annotations
181+ }
182+
183+ func (cc * ClusterContext ) SetClusterLabels (labels map [string ]string ) {
184+ cc .Labels = labels
185+ }
186+
187+ func (cc * ClusterContext ) SetClusterAnnotations (annotations map [string ]string ) {
188+ delete (annotations , "kubectl.kubernetes.io/last-applied-configuration" )
189+ cc .Annotations = annotations
190+ }
191+
192+ func (oc * OperatorContext ) GetOperatorLabels (name string ) map [string ]string {
193+ defaultLabels := getSelectorLabels (name )
194+ mergedLabels := map [string ]string {}
195+ if len (oc .Labels ) > 0 {
196+ for k , v := range defaultLabels {
197+ mergedLabels [k ] = v
198+ }
199+ for k , v := range oc .Labels {
200+ if _ , ok := defaultLabels [k ]; ! ok {
201+ mergedLabels [k ] = v
202+ }
203+ }
204+ } else {
205+ return defaultLabels
206+ }
207+ return mergedLabels
208+ }
209+
210+ func (oc * OperatorContext ) GetOperatorAnnotations () map [string ]string {
211+ return oc .Annotations
212+ }
213+
214+ func (oc * OperatorContext ) SetOperatorLabels (labels map [string ]string ) {
215+ oc .Labels = labels
216+ }
217+
218+ func (oc * OperatorContext ) SetOperatorAnnotations (annotations map [string ]string ) {
219+ delete (annotations , "kubectl.kubernetes.io/last-applied-configuration" )
220+ oc .Annotations = annotations
221+ }
0 commit comments