Skip to content

Commit b726317

Browse files
pengzhoumlPeng Zhou
andauthored
MLE-20428: add labels and annotation in group level (#82)
* MLE-20428: add labels and annotation in group level * delete annotation used for kubectl apply * Fix the issue with HAProxy Label * Working Solution * fix haproxy selector * fix update for haproxy service * fix issue with haproxy updated after creation * fix haproxy deployment label update * fix label for all services * remove debug logging * fix the bug with update panic * fix group level label not prompt to services * update the Helm CRD * Fix Unit Test Failuer * tidy code * add comment out back * remove excess logging * uncomment the logic in predicate * improve timeout for testing * Fix typo * add label and annotation sample for complete.yaml --------- Co-authored-by: Peng Zhou <[email protected]>
1 parent 33a6b9b commit b726317

18 files changed

+301
-52
lines changed

api/v1/marklogiccluster_types.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ type MarklogicGroups struct {
9191
// +kubebuilder:default:=1
9292
Replicas *int32 `json:"replicas,omitempty"`
9393
// +kubebuilder:validation:Required
94-
Name string `json:"name,omitempty"`
94+
Name string `json:"name,omitempty"`
95+
Labels map[string]string `json:"labels,omitempty"`
96+
Annotations map[string]string `json:"annotations,omitempty"`
9597
// +kubebuilder:default:={name: "Default", enableXdqpSsl: true}
9698
GroupConfig *GroupConfig `json:"groupConfig,omitempty"`
9799
Image string `json:"image,omitempty"`

api/v1/marklogicgroup_types.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ import (
2828
// MarklogicGroupSpec defines the desired state of MarklogicGroup
2929
type MarklogicGroupSpec struct {
3030
// +kubebuilder:default:=1
31-
Replicas *int32 `json:"replicas,omitempty"`
32-
Name string `json:"name,omitempty"`
31+
Replicas *int32 `json:"replicas,omitempty"`
32+
Name string `json:"name,omitempty"`
33+
Labels map[string]string `json:"labels,omitempty"`
34+
Annotations map[string]string `json:"annotations,omitempty"`
3335
// +kubebuilder:default:="cluster.local"
3436
ClusterDomain string `json:"clusterDomain,omitempty"`
3537
// +kubebuilder:default:="progressofficial/marklogic-db:11.3.0-ubi-rootless"

api/v1/zz_generated.deepcopy.go

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

charts/marklogic-operator-kubernetes/templates/marklogiccluster-crd.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7965,6 +7965,10 @@ spec:
79657965
x-kubernetes-list-type: atomic
79667966
type: object
79677967
type: object
7968+
annotations:
7969+
additionalProperties:
7970+
type: string
7971+
type: object
79687972
groupConfig:
79697973
default:
79707974
enableXdqpSsl: true
@@ -9331,6 +9335,10 @@ spec:
93319335
isBootstrap:
93329336
default: false
93339337
type: boolean
9338+
labels:
9339+
additionalProperties:
9340+
type: string
9341+
type: object
93349342
logCollection:
93359343
properties:
93369344
enabled:

charts/marklogic-operator-kubernetes/templates/marklogicgroup-crd.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3243,6 +3243,10 @@ spec:
32433243
x-kubernetes-list-type: atomic
32443244
type: object
32453245
type: object
3246+
annotations:
3247+
additionalProperties:
3248+
type: string
3249+
type: object
32463250
auth:
32473251
properties:
32483252
adminPassword:
@@ -3310,6 +3314,10 @@ spec:
33103314
type: object
33113315
x-kubernetes-map-type: atomic
33123316
type: array
3317+
labels:
3318+
additionalProperties:
3319+
type: string
3320+
type: object
33133321
license:
33143322
properties:
33153323
key:

config/crd/bases/marklogic.progress.com_marklogicclusters.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7987,6 +7987,10 @@ spec:
79877987
x-kubernetes-list-type: atomic
79887988
type: object
79897989
type: object
7990+
annotations:
7991+
additionalProperties:
7992+
type: string
7993+
type: object
79907994
groupConfig:
79917995
default:
79927996
enableXdqpSsl: true
@@ -9356,6 +9360,10 @@ spec:
93569360
isBootstrap:
93579361
default: false
93589362
type: boolean
9363+
labels:
9364+
additionalProperties:
9365+
type: string
9366+
type: object
93599367
logCollection:
93609368
properties:
93619369
enabled:

config/crd/bases/marklogic.progress.com_marklogicgroups.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3250,6 +3250,10 @@ spec:
32503250
x-kubernetes-list-type: atomic
32513251
type: object
32523252
type: object
3253+
annotations:
3254+
additionalProperties:
3255+
type: string
3256+
type: object
32533257
auth:
32543258
properties:
32553259
adminPassword:
@@ -3317,6 +3321,10 @@ spec:
33173321
type: object
33183322
x-kubernetes-map-type: atomic
33193323
type: array
3324+
labels:
3325+
additionalProperties:
3326+
type: string
3327+
type: object
33203328
license:
33213329
properties:
33223330
key:

config/samples/complete.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ spec:
195195
# additionalVolumeClaimTemplates: []
196196
markLogicGroups:
197197
- name: dnode
198+
labels:
199+
group-level-label: "group-level-label"
200+
annotations:
201+
group-level-annotation: "group-level-annotation"
198202
replicas: 3
199203
groupConfig:
200204
name: dnode

internal/controller/marklogiccluster_controller.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ import (
3131
"github.com/go-logr/logr"
3232
marklogicv1 "github.com/marklogic/marklogic-operator-kubernetes/api/v1"
3333
"github.com/marklogic/marklogic-operator-kubernetes/pkg/k8sutil"
34+
"reflect"
35+
"sigs.k8s.io/controller-runtime/pkg/event"
36+
"sigs.k8s.io/controller-runtime/pkg/predicate"
3437
)
3538

3639
// MarklogicClusterReconciler reconciles a MarklogicCluster object
@@ -81,10 +84,55 @@ func (r *MarklogicClusterReconciler) Reconcile(ctx context.Context, req ctrl.Req
8184
return result, nil
8285
}
8386

87+
func markLogicClusterCreateUpdateDeletePredicate() predicate.Predicate {
88+
return predicate.Funcs{
89+
CreateFunc: func(e event.CreateEvent) bool {
90+
return true // Reconcile on create
91+
},
92+
UpdateFunc: func(e event.UpdateEvent) bool {
93+
switch e.ObjectNew.(type) {
94+
case *marklogicv1.MarklogicCluster:
95+
oldAnnotations := e.ObjectOld.GetAnnotations()
96+
newAnnotations := e.ObjectNew.GetAnnotations()
97+
delete(newAnnotations, "banzaicloud.com/last-applied")
98+
delete(oldAnnotations, "banzaicloud.com/last-applied")
99+
delete(newAnnotations, "kubectl.kubernetes.io/last-applied-configuration")
100+
delete(oldAnnotations, "kubectl.kubernetes.io/last-applied-configuration")
101+
if !reflect.DeepEqual(oldAnnotations, newAnnotations) {
102+
return true // Reconcile if annotations have changed
103+
}
104+
oldLables := e.ObjectOld.GetLabels()
105+
newLabels := e.ObjectNew.GetLabels()
106+
if !reflect.DeepEqual(oldLables, newLabels) {
107+
return true // Reconcile if labels have changed
108+
}
109+
// If annotations and labels are the same, check if the spec has changed
110+
oldObj := e.ObjectOld.(*marklogicv1.MarklogicCluster)
111+
// Check if the spec has changed
112+
newObj := e.ObjectNew.(*marklogicv1.MarklogicCluster)
113+
if !reflect.DeepEqual(oldObj.Spec, newObj.Spec) {
114+
return true // Reconcile if spec has changed
115+
}
116+
default:
117+
return false // Ignore updates for other types
118+
}
119+
return false // Reconcile on update of MarklogicCluster
120+
121+
},
122+
DeleteFunc: func(e event.DeleteEvent) bool {
123+
return true // Reconcile on delete
124+
},
125+
GenericFunc: func(e event.GenericEvent) bool {
126+
return false // Ignore generic events (optional)
127+
},
128+
}
129+
}
130+
84131
// SetupWithManager sets up the controller with the Manager.
85132
func (r *MarklogicClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
86133
return ctrl.NewControllerManagedBy(mgr).
87134
For(&marklogicv1.MarklogicCluster{}).
135+
WithEventFilter(markLogicClusterCreateUpdateDeletePredicate()).
88136
Owns(&marklogicv1.MarklogicGroup{}).
89137
Complete(r)
90138
}

internal/controller/marklogicgroup_controller.go

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@ package controller
1919
import (
2020
"context"
2121

22+
"github.com/go-logr/logr"
23+
marklogicv1 "github.com/marklogic/marklogic-operator-kubernetes/api/v1"
24+
"github.com/marklogic/marklogic-operator-kubernetes/pkg/k8sutil"
2225
appsv1 "k8s.io/api/apps/v1"
2326
corev1 "k8s.io/api/core/v1"
2427
"k8s.io/apimachinery/pkg/api/errors"
2528
"k8s.io/apimachinery/pkg/runtime"
2629
"k8s.io/client-go/tools/record"
30+
"reflect"
2731
ctrl "sigs.k8s.io/controller-runtime"
2832
"sigs.k8s.io/controller-runtime/pkg/client"
33+
"sigs.k8s.io/controller-runtime/pkg/event"
2934
"sigs.k8s.io/controller-runtime/pkg/log"
30-
31-
"github.com/go-logr/logr"
32-
marklogicv1 "github.com/marklogic/marklogic-operator-kubernetes/api/v1"
33-
"github.com/marklogic/marklogic-operator-kubernetes/pkg/k8sutil"
35+
"sigs.k8s.io/controller-runtime/pkg/predicate"
3436
)
3537

3638
// MarklogicGroupReconciler reconciles a MarklogicGroup object
@@ -94,12 +96,63 @@ func (r *MarklogicGroupReconciler) Reconcile(ctx context.Context, req ctrl.Reque
9496
return result, nil
9597
}
9698

99+
func markLogicGroupCreateUpdateDeletePredicate() predicate.Predicate {
100+
return predicate.Funcs{
101+
CreateFunc: func(e event.CreateEvent) bool {
102+
return true // Reconcile on create
103+
},
104+
UpdateFunc: func(e event.UpdateEvent) bool {
105+
switch e.ObjectNew.(type) {
106+
case *marklogicv1.MarklogicGroup:
107+
oldAnnotations := e.ObjectOld.GetAnnotations()
108+
newAnnotations := e.ObjectNew.GetAnnotations()
109+
delete(newAnnotations, "banzaicloud.com/last-applied")
110+
delete(oldAnnotations, "banzaicloud.com/last-applied")
111+
delete(newAnnotations, "kubectl.kubernetes.io/last-applied-configuration")
112+
delete(oldAnnotations, "kubectl.kubernetes.io/last-applied-configuration")
113+
if !reflect.DeepEqual(oldAnnotations, newAnnotations) {
114+
return true // Reconcile if annotations have changed
115+
}
116+
oldLables := e.ObjectOld.GetLabels()
117+
newLabels := e.ObjectNew.GetLabels()
118+
if !reflect.DeepEqual(oldLables, newLabels) {
119+
return true // Reconcile if labels have changed
120+
}
121+
oldObj := e.ObjectOld.(*marklogicv1.MarklogicGroup)
122+
newObj := e.ObjectNew.(*marklogicv1.MarklogicGroup)
123+
if !reflect.DeepEqual(oldObj.Spec, newObj.Spec) {
124+
return true // Reconcile if the spec has changed
125+
}
126+
return false
127+
case *appsv1.StatefulSet:
128+
return true // Reconcile on update of StatefulSet
129+
case *corev1.Service:
130+
oldObj := e.ObjectOld.(*corev1.Service)
131+
newObj := e.ObjectNew.(*corev1.Service)
132+
if !reflect.DeepEqual(oldObj.Spec, newObj.Spec) {
133+
return true // Reconcile if the spec has changed
134+
}
135+
return false // Reconcile on update of Service
136+
default:
137+
return false // Ignore updates for other types
138+
}
139+
},
140+
DeleteFunc: func(e event.DeleteEvent) bool {
141+
return true // Reconcile on delete
142+
},
143+
GenericFunc: func(e event.GenericEvent) bool {
144+
return false // Ignore generic events (optional)
145+
},
146+
}
147+
}
148+
97149
// SetupWithManager sets up the controller with the Manager.
98150
func (r *MarklogicGroupReconciler) SetupWithManager(mgr ctrl.Manager) error {
99151

100152
builder := ctrl.NewControllerManagedBy(mgr).
101153
Named("marklogicgroup-controller").
102154
For(&marklogicv1.MarklogicGroup{}).
155+
WithEventFilter(markLogicGroupCreateUpdateDeletePredicate()).
103156
Owns(&appsv1.StatefulSet{}).
104157
Owns(&corev1.Service{})
105158

0 commit comments

Comments
 (0)