Skip to content

Commit

Permalink
more logic cleaning!
Browse files Browse the repository at this point in the history
  • Loading branch information
dmolik committed Aug 2, 2024
1 parent 9478a82 commit 152db68
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions internal/controller/valkey_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ func (r *ValkeyReconciler) balanceNodes(ctx context.Context, valkey *hyperv1.Val
ids[hostname] = id
}
if oldnodes > newnodes {
r.Recorder.Event(valkey, "Normal", "Updated", fmt.Sprintf("Scaling in cluster nodes %s/%s", valkey.Namespace, valkey.Name))
for i := oldnodes - 1; i >= newnodes; i-- { // remove nodes
if _, ok := ids[fmt.Sprintf("%s-%d", valkey.Name, i)]; !ok {
logger.Info("node not found", "valkey", valkey.Name, "namespace", valkey.Namespace, "node", fmt.Sprintf("%s-%d", valkey.Name, i))
Expand All @@ -439,6 +440,7 @@ func (r *ValkeyReconciler) balanceNodes(ctx context.Context, valkey *hyperv1.Val
}
}
} else {
r.Recorder.Event(valkey, "Normal", "Updated", fmt.Sprintf("Scaling out cluster nodes %s/%s", valkey.Namespace, valkey.Name))
for i := oldnodes; i < newnodes; i++ { // add nodes
name := fmt.Sprintf("%s-%d", valkey.Name, i)
if err := r.waitForPod(ctx, name, valkey.Namespace); err != nil {
Expand Down Expand Up @@ -762,20 +764,6 @@ fi
if err := controllerutil.SetControllerReference(valkey, sts, r.Scheme); err != nil {
return err
}
if err := r.Create(ctx, sts); err != nil {
if errors.IsAlreadyExists(err) {
if err := r.Update(ctx, sts); err != nil {
logger.Error(err, "failed to update statefulset", "valkey", valkey.Name, "namespace", valkey.Namespace)
return err
}
} else {
logger.Error(err, "failed to create statefulset", "valkey", valkey.Name, "namespace", valkey.Namespace)
return err
}
} else {
r.Recorder.Event(valkey, "Normal", "Created",
fmt.Sprintf("StatefulSet %s/%s is created", valkey.Namespace, valkey.Name))
}

err := r.Get(ctx, types.NamespacedName{Namespace: valkey.Namespace, Name: valkey.Name}, sts)
if err != nil && errors.IsNotFound(err) {
Expand All @@ -793,6 +781,7 @@ fi
if *sts.Spec.Replicas != valkey.Spec.MasterNodes {
oldnodes := *sts.Spec.Replicas
sts.Spec.Replicas = &valkey.Spec.MasterNodes
sts.Spec.Template.Spec.Containers[0].Env[1].Value = getMasterNodes(valkey)
if err := r.Update(ctx, sts); err != nil {
logger.Error(err, "failed to update statefulset", "valkey", valkey.Name, "namespace", valkey.Namespace)
return err
Expand Down

0 comments on commit 152db68

Please sign in to comment.