diff --git a/src/go/k8s/controllers/redpanda/cluster_controller.go b/src/go/k8s/controllers/redpanda/cluster_controller.go index 82b755b2e4376..3a66f3fcfc55b 100644 --- a/src/go/k8s/controllers/redpanda/cluster_controller.go +++ b/src/go/k8s/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,