Skip to content

Commit a7505b4

Browse files
authored
Merge pull request #105 from openebs/sts_functions
add utils for statefulset pods
2 parents b4bfa6c + c122f90 commit a7505b4

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

common/k8stest/util_pod.go

+41
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
coreV1 "k8s.io/api/core/v1"
2121
corev1 "k8s.io/api/core/v1"
2222
v1 "k8s.io/api/core/v1"
23+
k8serrors "k8s.io/apimachinery/pkg/api/errors"
2324
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2425
logf "sigs.k8s.io/controller-runtime/pkg/log"
2526
)
@@ -770,3 +771,43 @@ func UnsuppressMayastorPodOnNode(nodeName string, timeout int) error {
770771
err = WaitForPodRunningOnNode(IoEnginePodRegexp, common.NSMayastor(), nodeName, timeout)
771772
return err
772773
}
774+
775+
func CheckPodExists(podName string, namespace string) (bool, error) {
776+
// Fetch the pod using the GetPod function
777+
podAPI := gTestEnv.KubeInt.CoreV1().Pods
778+
_, err := podAPI(namespace).Get(context.TODO(), podName, metaV1.GetOptions{})
779+
if err != nil {
780+
// If the error indicates that the pod is not found, it has been deleted successfully
781+
if k8serrors.IsNotFound(err) {
782+
return false, nil
783+
}
784+
// If it's another error, return it
785+
return false, fmt.Errorf("failed to check if pod %s in namespace %s is present, error: %v", podName, namespace, err)
786+
}
787+
return true, nil
788+
}
789+
790+
func WaitForPodDeletion(podName string, namespace string, podDeletionTimeoutSecs time.Duration) (bool, error) {
791+
792+
startTime := time.Now()
793+
794+
for {
795+
exists, err := CheckPodExists(podName, namespace)
796+
if err != nil {
797+
return false, err
798+
}
799+
if !exists {
800+
return true, nil
801+
}
802+
803+
// If the timeout is reached, exit the loop
804+
if time.Since(startTime) >= podDeletionTimeoutSecs {
805+
break
806+
}
807+
// Sleep for a short interval before retrying
808+
time.Sleep(1 * time.Second)
809+
}
810+
811+
// If the pod still exists after the timeout, return false
812+
return false, fmt.Errorf("timeout reached: pod %s in namespace %s was not deleted within %v", podName, namespace, podDeletionTimeoutSecs)
813+
}

common/k8stest/util_sts.go

+57
Original file line numberDiff line numberDiff line change
@@ -452,3 +452,60 @@ func StsExists(statefulSetName string, namespace string) (bool, error) {
452452
}
453453
return false, err
454454
}
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

Comments
 (0)