Skip to content

Commit 273d342

Browse files
pengzhoumlPeng Zhou
andauthored
MLE-24840: Fix the bugMarkLogic group fails to scale up after being scaled down to 0 replicas (#104)
* MLE-24840: fix bug when scale node to 0 failed * fix context issue --------- Co-authored-by: Peng Zhou <[email protected]>
1 parent 26295cc commit 273d342

File tree

1 file changed

+38
-40
lines changed

1 file changed

+38
-40
lines changed

pkg/k8sutil/statefulset.go

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,44 @@ func (oc *OperatorContext) ReconcileStatefulset() (reconcile.Result, error) {
8888
oc.Recorder.Event(oc.MarklogicGroup, "Normal", "StatefulSetCreated", "MarkLogic statefulSet created successfully")
8989
return result.Done().Output()
9090
}
91-
_, outputErr := result.Error(err).Output()
92-
if outputErr != nil {
93-
logger.Error(outputErr, "Failed to process result error")
94-
}
91+
logger.Error(err, "Cannot get statefulSet for MarkLogic")
92+
return result.Error(err).Output()
9593
}
94+
95+
patchDiff, err := patch.DefaultPatchMaker.Calculate(currentSts, statefulSetDef,
96+
patch.IgnoreStatusFields(),
97+
patch.IgnoreVolumeClaimTemplateTypeMetaAndStatus(),
98+
patch.IgnoreField("kind"))
99+
logger.Info("Patch Diff:", "Diff", patchDiff.String())
100+
logger.Info("statefulSetDef Spec:", "Spec", statefulSetDef.Spec.Replicas)
96101
if err != nil {
97-
logger.Error(err, "Cannot create standalone statefulSet for MarkLogic")
102+
logger.Error(err, "Error calculating patch")
98103
return result.Error(err).Output()
99104
}
105+
106+
if !patchDiff.IsEmpty() {
107+
logger.Info("MarkLogic statefulSet spec is different from the MarkLogicGroup spec, updating the statefulSet")
108+
currentSts.Spec = statefulSetDef.Spec
109+
currentSts.ObjectMeta.Annotations = statefulSetDef.ObjectMeta.Annotations
110+
currentSts.ObjectMeta.Labels = statefulSetDef.ObjectMeta.Labels
111+
err := oc.Client.Update(oc.Ctx, currentSts)
112+
if err != nil {
113+
logger.Error(err, "Error updating statefulSet")
114+
return result.Error(err).Output()
115+
}
116+
} else {
117+
logger.Info("MarkLogic statefulSet spec is the same as the current spec, no update needed")
118+
}
119+
logger.Info("Operator Status:", "Stage", cr.Status.Stage)
120+
if cr.Status.Stage == "STS_CREATED" {
121+
logger.Info("MarkLogic statefulSet created successfully, waiting for pods to be ready")
122+
pods, err := GetPodsForStatefulSet(oc.Ctx, cr.Namespace, cr.Spec.Name)
123+
if err != nil {
124+
logger.Error(err, "Error getting pods for statefulset")
125+
}
126+
logger.Info("Pods in statefulSet: ", "Pods", pods)
127+
}
128+
100129
patchClient := client.MergeFrom(oc.MarklogicGroup.DeepCopy())
101130
updated := false
102131
if currentSts.Status.ReadyReplicas == 0 || currentSts.Status.ReadyReplicas != currentSts.Status.Replicas {
@@ -132,37 +161,6 @@ func (oc *OperatorContext) ReconcileStatefulset() (reconcile.Result, error) {
132161
}
133162
}
134163

135-
patchDiff, err := patch.DefaultPatchMaker.Calculate(currentSts, statefulSetDef,
136-
patch.IgnoreStatusFields(),
137-
patch.IgnoreVolumeClaimTemplateTypeMetaAndStatus(),
138-
patch.IgnoreField("kind"))
139-
if err != nil {
140-
logger.Error(err, "Error calculating patch")
141-
return result.Error(err).Output()
142-
}
143-
if !patchDiff.IsEmpty() {
144-
logger.Info("MarkLogic statefulSet spec is different from the MarkLogicGroup spec, updating the statefulSet")
145-
currentSts.Spec = statefulSetDef.Spec
146-
currentSts.ObjectMeta.Annotations = statefulSetDef.ObjectMeta.Annotations
147-
currentSts.ObjectMeta.Labels = statefulSetDef.ObjectMeta.Labels
148-
err := oc.Client.Update(oc.Ctx, currentSts)
149-
if err != nil {
150-
logger.Error(err, "Error updating statefulSet")
151-
return result.Error(err).Output()
152-
}
153-
} else {
154-
logger.Info("MarkLogic statefulSet spec is the same as the current spec, no update needed")
155-
}
156-
logger.Info("Operator Status:", "Stage", cr.Status.Stage)
157-
if cr.Status.Stage == "STS_CREATED" {
158-
logger.Info("MarkLogic statefulSet created successfully, waiting for pods to be ready")
159-
pods, err := GetPodsForStatefulSet(cr.Namespace, cr.Spec.Name)
160-
if err != nil {
161-
logger.Error(err, "Error getting pods for statefulset")
162-
}
163-
logger.Info("Pods in statefulSet: ", "Pods", pods)
164-
}
165-
166164
return result.Done().Output()
167165
}
168166

@@ -180,7 +178,7 @@ func (oc *OperatorContext) setCondition(condition *metav1.Condition) bool {
180178
func (oc *OperatorContext) GetStatefulSet(namespace string, stateful string) (*appsv1.StatefulSet, error) {
181179
logger := oc.ReqLogger
182180
statefulInfo := &appsv1.StatefulSet{}
183-
err := oc.Client.Get(context.TODO(), client.ObjectKey{Namespace: namespace, Name: stateful}, statefulInfo)
181+
err := oc.Client.Get(oc.Ctx, client.ObjectKey{Namespace: namespace, Name: stateful}, statefulInfo)
184182
if err != nil {
185183
logger.Info("MarkLogic statefulSet get action failed")
186184
return nil, err
@@ -191,7 +189,7 @@ func (oc *OperatorContext) GetStatefulSet(namespace string, stateful string) (*a
191189

192190
func (oc *OperatorContext) createStatefulSet(statefulset *appsv1.StatefulSet, cr *marklogicv1.MarklogicGroup) error {
193191
logger := oc.ReqLogger
194-
err := oc.Client.Create(context.TODO(), statefulset)
192+
err := oc.Client.Create(oc.Ctx, statefulset)
195193
if err != nil {
196194
logger.Error(err, "MarkLogic stateful creation failed")
197195
return err
@@ -306,11 +304,11 @@ func generateStatefulSetsDef(stsMeta metav1.ObjectMeta, params statefulSetParame
306304
return statefulSet
307305
}
308306

309-
func GetPodsForStatefulSet(namespace, name string) ([]corev1.Pod, error) {
307+
func GetPodsForStatefulSet(ctx context.Context, namespace, name string) ([]corev1.Pod, error) {
310308
selector := fmt.Sprintf("app.kubernetes.io/name=marklogic,app.kubernetes.io/instance=%s", name)
311309
// List Pods with the label selector
312310
listOptions := metav1.ListOptions{LabelSelector: selector}
313-
pods, err := GenerateK8sClient().CoreV1().Pods(namespace).List(context.TODO(), listOptions)
311+
pods, err := GenerateK8sClient().CoreV1().Pods(namespace).List(ctx, listOptions)
314312
if err != nil {
315313
return nil, err
316314
}

0 commit comments

Comments
 (0)