@@ -452,3 +452,60 @@ func StsExists(statefulSetName string, namespace string) (bool, error) {
452
452
}
453
453
return false , err
454
454
}
455
+
456
+ func GetSts (statefulSetName string , namespace string ) (* appsv1.StatefulSet , error ) {
457
+ sts , err := gTestEnv .KubeInt .AppsV1 ().StatefulSets (namespace ).Get (
458
+ context .TODO (),
459
+ statefulSetName ,
460
+ metaV1.GetOptions {},
461
+ )
462
+ if err != nil {
463
+ return nil , fmt .Errorf ("failed to get statefulset %s in namespace %s: %v" , statefulSetName , namespace , err )
464
+ }
465
+ return sts , nil
466
+ }
467
+
468
+ func ListSts (namespace string ) ([]appsv1.StatefulSet , error ) {
469
+ stsList , err := gTestEnv .KubeInt .AppsV1 ().StatefulSets (namespace ).List (
470
+ context .TODO (),
471
+ metaV1.ListOptions {},
472
+ )
473
+ if err != nil {
474
+ return nil , fmt .Errorf ("failed to list statefulsets in namespace %s: %v" , namespace , err )
475
+ }
476
+ return stsList .Items , nil
477
+ }
478
+
479
+ func GetStsPodNames (statefulSetName string , namespace string ) ([]string , error ) {
480
+ // Get the StatefulSet to ensure it exists
481
+ _ , err := GetSts (statefulSetName , namespace )
482
+ if err != nil {
483
+ return nil , fmt .Errorf ("failed to get statefulset %s in namespace %s: %v" , statefulSetName , namespace , err )
484
+ }
485
+
486
+ // List all pods in the namespace
487
+ podList , err := ListPod (namespace )
488
+ if err != nil {
489
+ return nil , fmt .Errorf ("failed to list pods in namespace %s: %v" , namespace , err )
490
+ }
491
+
492
+ // Filter pods based on owner references
493
+ var podNames []string
494
+ for _ , pod := range podList .Items {
495
+ for _ , owner := range pod .OwnerReferences {
496
+ // Check if the owner is the target StatefulSet
497
+ if owner .Kind == "StatefulSet" && owner .Name == statefulSetName {
498
+ podNames = append (podNames , pod .Name )
499
+ break
500
+ }
501
+ }
502
+ }
503
+
504
+ // If no pods were found, return a specific error
505
+ if len (podNames ) == 0 {
506
+ return nil , fmt .Errorf ("no pods found for statefulset %s in namespace %s" , statefulSetName , namespace )
507
+ }
508
+
509
+ // Return the list of pod names
510
+ return podNames , nil
511
+ }
0 commit comments