Skip to content

Commit

Permalink
Add helper function to filter database pods (#820)
Browse files Browse the repository at this point in the history
Signed-off-by: Emruz Hossain <emruz@appscode.com>
  • Loading branch information
Emruz Hossain authored Nov 19, 2021
1 parent ff3a417 commit fcda9c6
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions apis/kubedb/v1alpha2/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
appslister "k8s.io/client-go/listers/apps/v1"
apps_util "kmodules.xyz/client-go/apps/v1"
Expand Down Expand Up @@ -121,6 +122,33 @@ func SetDefaultResourceLimits(req *core.ResourceRequirements, defaultResources c
}
}

func GetDatabasePods(db metav1.Object, stsLister appslister.StatefulSetLister, pods []core.Pod) ([]core.Pod, error) {
var dbPods []core.Pod

for i := range pods {
owner := metav1.GetControllerOf(&pods[i])
if owner == nil {
continue
}

// If the Pod is not control by a StatefulSet, then it is not a KubeDB database Pod
if owner.Kind == ResourceKindStatefulSet {
// Find the controlling StatefulSet
sts, err := stsLister.StatefulSets(db.GetNamespace()).Get(owner.Name)
if err != nil {
return nil, err
}

// Check if the StatefulSet is controlled by the database
if metav1.IsControlledBy(sts, db) {
dbPods = append(dbPods, pods[i])
}
}
}

return dbPods, nil
}

// Upsert elements to string slice
func upsertStringSlice(inSlice []string, values ...string) []string {
upsert := func(m string) {
Expand Down

0 comments on commit fcda9c6

Please sign in to comment.