Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper function to filter database pods #820

Merged
merged 2 commits into from
Nov 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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