findDeletableInstance get the Pod who is supposed to be deleted when the cluster is scaled down
(cluster *apiv1.Cluster, instances []corev1.Pod)
| 462 | |
| 463 | // findDeletableInstance get the Pod who is supposed to be deleted when the cluster is scaled down |
| 464 | func 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 | } |
no test coverage detected