MCPcopy Create free account
hub / github.com/cloudnative-pg/cloudnative-pg / findDeletableInstance

Function findDeletableInstance

internal/controller/replicas.go:464–501  ·  view source on GitHub ↗

findDeletableInstance get the Pod who is supposed to be deleted when the cluster is scaled down

(cluster *apiv1.Cluster, instances []corev1.Pod)

Source from the content-addressed store, hash-verified

462
463// findDeletableInstance get the Pod who is supposed to be deleted when the cluster is scaled down
464func findDeletableInstance(cluster *apiv1.Cluster, instances []corev1.Pod) string {
465 resultIdx := -1
466 var lastFoundSerial int
467
468 instancesNotRunning := cluster.Status.InstanceNames
469
470 for idx, pod := range instances {
471 if nameIndex := slices.Index(instancesNotRunning, pod.Name); nameIndex != -1 {
472 instancesNotRunning = slices.Delete(instancesNotRunning, nameIndex, nameIndex+1)
473 }
474
475 // Avoid parting non ready nodes, non active nodes, or primary nodes
476 if !utils.IsPodReady(pod) || !utils.IsPodActive(pod) || specs.IsPodPrimary(pod) {
477 continue
478 }
479
480 // If we cannot get the node serial this is not one of our Pods
481 podSerial, err := specs.GetNodeSerial(pod.ObjectMeta)
482 if err != nil {
483 continue
484 }
485
486 if lastFoundSerial == 0 || lastFoundSerial < podSerial {
487 resultIdx = idx
488 lastFoundSerial = podSerial
489 }
490 }
491
492 if len(instancesNotRunning) > 0 {
493 return instancesNotRunning[len(instancesNotRunning)-1]
494 }
495
496 if resultIdx == -1 {
497 return ""
498 }
499
500 return instances[resultIdx].Name
501}

Callers 3

scaleDownClusterMethod · 0.85
replicas_test.goFile · 0.85

Calls 5

IsPodReadyFunction · 0.92
IsPodActiveFunction · 0.92
IsPodPrimaryFunction · 0.92
GetNodeSerialFunction · 0.92
DeleteMethod · 0.45

Tested by

no test coverage detected