From 3ab1d4cebca35a94bf26fd3512e40cc29f42a092 Mon Sep 17 00:00:00 2001 From: Rafal Korepta Date: Mon, 2 Jan 2023 15:22:27 +0100 Subject: [PATCH] (split) k8s: Move ordinal extraction to seperated function --- controllers/redpanda/cluster_controller.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/controllers/redpanda/cluster_controller.go b/controllers/redpanda/cluster_controller.go index 82b755b2e437..3a66f3fcfc55 100644 --- a/controllers/redpanda/cluster_controller.go +++ b/controllers/redpanda/cluster_controller.go @@ -551,7 +551,7 @@ func (r *ClusterReconciler) fetchAdminNodeID(ctx context.Context, rp *redpandav1 return -1, fmt.Errorf("creating pki: %w", err) } - ordinal, err := strconv.ParseInt(pod.Name[len(rp.Name)+1:], 10, 0) + ordinal, err := strconv.ParseInt(getPodOrdinal(pod.Name, rp.Name), 10, 0) if err != nil { return -1, fmt.Errorf("cluster %s: cannot convert pod name (%s) to ordinal: %w", rp.Name, pod.Name, err) } @@ -567,6 +567,16 @@ func (r *ClusterReconciler) fetchAdminNodeID(ctx context.Context, rp *redpandav1 return int32(cfg.NodeID), nil } +func getPodOrdinal(podName string, clusterName string) string { + // Pod name needs to have at least 2 more characters + if len(podName) < len(clusterName)+2 { + return "" + } + + // The +1 is for the separator between stateful set name and pod ordinal + return podName[len(clusterName)+1:] +} + func (r *ClusterReconciler) reportStatus( ctx context.Context, redpandaCluster *redpandav1alpha1.Cluster,