From e6191ff65125f8e05d274f7a7c195ba2c589a0e1 Mon Sep 17 00:00:00 2001 From: yowenter Date: Wed, 26 Apr 2023 17:56:59 +0800 Subject: [PATCH] GetPodsForJob filter the pod list owned by job Signed-off-by: yowenter --- pkg/common/util/util.go | 26 ++++--------------- pkg/controller.v1/mpi/mpijob_controller.go | 13 ++-------- pkg/controller.v1/mxnet/mxjob_controller.go | 2 +- .../paddlepaddle/paddlepaddle_controller.go | 2 +- .../pytorch/pytorchjob_controller.go | 2 +- .../tensorflow/tfjob_controller.go | 2 +- .../xgboost/xgboostjob_controller.go | 2 +- 7 files changed, 12 insertions(+), 37 deletions(-) diff --git a/pkg/common/util/util.go b/pkg/common/util/util.go index 8f4a1d7dea..1bc3f8ac01 100644 --- a/pkg/common/util/util.go +++ b/pkg/common/util/util.go @@ -38,33 +38,17 @@ func ConvertServiceList(list []corev1.Service) []*corev1.Service { return ret } -// ConvertPodList convert pod list to pod pointer list -func ConvertPodList(list []corev1.Pod) []*corev1.Pod { +// JobControlledPodList filter pod list owned by the job. +func JobControlledPodList(list []corev1.Pod, job metav1.Object) []*corev1.Pod { if list == nil { return nil } ret := make([]*corev1.Pod, 0, len(list)) for i := range list { - ret = append(ret, &list[i]) - } - return ret -} - -// ConvertPodListWithFilter converts pod list to pod pointer list with ObjectFilterFunction -func ConvertPodListWithFilter(list []corev1.Pod, pass ObjectFilterFunction) []*corev1.Pod { - if list == nil { - return nil - } - ret := make([]*corev1.Pod, 0, len(list)) - for i := range list { - obj := &list[i] - if pass != nil { - if pass(obj) { - ret = append(ret, obj) - } - } else { - ret = append(ret, obj) + if !metav1.IsControlledBy(&list[i], job) { + continue } + ret = append(ret, &list[i]) } return ret } diff --git a/pkg/controller.v1/mpi/mpijob_controller.go b/pkg/controller.v1/mpi/mpijob_controller.go index 2af6b72565..c6f280fe0d 100644 --- a/pkg/controller.v1/mpi/mpijob_controller.go +++ b/pkg/controller.v1/mpi/mpijob_controller.go @@ -551,11 +551,7 @@ func (jc *MPIJobReconciler) GetPodsForJob(jobObject interface{}) ([]*corev1.Pod, return nil, err } - var filter util.ObjectFilterFunction = func(obj metav1.Object) bool { - return metav1.IsControlledBy(obj, job) - } - - return util.ConvertPodListWithFilter(podlist.Items, filter), nil + return util.JobControlledPodList(podlist.Items, job), nil } func (jc *MPIJobReconciler) DeleteJob(job interface{}) error { @@ -1243,12 +1239,7 @@ func (jc *MPIJobReconciler) getRunningWorkerPods(mpiJob *kubeflowv1.MPIJob) ([]* podList = append(podList, podFullList.Items[idx]) } } - - var filter util.ObjectFilterFunction = func(obj metav1.Object) bool { - return metav1.IsControlledBy(obj, mpiJob) - } - - return util.ConvertPodListWithFilter(podList, filter), nil + return util.JobControlledPodList(podList, mpiJob), nil } // newConfigMap creates a new ConfigMap containing configurations for an MPIJob diff --git a/pkg/controller.v1/mxnet/mxjob_controller.go b/pkg/controller.v1/mxnet/mxjob_controller.go index 470e4cbf7a..f3d71ba375 100644 --- a/pkg/controller.v1/mxnet/mxjob_controller.go +++ b/pkg/controller.v1/mxnet/mxjob_controller.go @@ -315,7 +315,7 @@ func (r *MXJobReconciler) GetPodsForJob(obj interface{}) ([]*corev1.Pod, error) if err != nil { return nil, err } - return util.ConvertPodList(podlist.Items), nil + return util.JobControlledPodList(podlist.Items, job), nil } func (r *MXJobReconciler) GetServicesForJob(job interface{}) ([]*corev1.Service, error) { diff --git a/pkg/controller.v1/paddlepaddle/paddlepaddle_controller.go b/pkg/controller.v1/paddlepaddle/paddlepaddle_controller.go index bc8ac7feaf..9045ffb926 100644 --- a/pkg/controller.v1/paddlepaddle/paddlepaddle_controller.go +++ b/pkg/controller.v1/paddlepaddle/paddlepaddle_controller.go @@ -311,7 +311,7 @@ func (r *PaddleJobReconciler) GetPodsForJob(obj interface{}) ([]*corev1.Pod, err return nil, err } - return util.ConvertPodList(podlist.Items), nil + return util.JobControlledPodList(podlist.Items, job), nil } func (r *PaddleJobReconciler) GetServicesForJob(obj interface{}) ([]*corev1.Service, error) { diff --git a/pkg/controller.v1/pytorch/pytorchjob_controller.go b/pkg/controller.v1/pytorch/pytorchjob_controller.go index 550a008429..355ca24684 100644 --- a/pkg/controller.v1/pytorch/pytorchjob_controller.go +++ b/pkg/controller.v1/pytorch/pytorchjob_controller.go @@ -315,7 +315,7 @@ func (r *PyTorchJobReconciler) GetPodsForJob(obj interface{}) ([]*corev1.Pod, er return nil, err } - return util.ConvertPodList(podlist.Items), nil + return util.JobControlledPodList(podlist.Items, job), nil } func (r *PyTorchJobReconciler) GetServicesForJob(obj interface{}) ([]*corev1.Service, error) { diff --git a/pkg/controller.v1/tensorflow/tfjob_controller.go b/pkg/controller.v1/tensorflow/tfjob_controller.go index da06a0ce7d..1039bd6d6c 100644 --- a/pkg/controller.v1/tensorflow/tfjob_controller.go +++ b/pkg/controller.v1/tensorflow/tfjob_controller.go @@ -335,7 +335,7 @@ func (r *TFJobReconciler) GetPodsForJob(jobObject interface{}) ([]*corev1.Pod, e return nil, err } - pods := util.ConvertPodList(podlist.Items) + pods := util.JobControlledPodList(podlist.Items, job) // If any adoptions are attempted, we should first recheck for deletion // with an uncached quorum read sometime after listing Pods (see #42639). diff --git a/pkg/controller.v1/xgboost/xgboostjob_controller.go b/pkg/controller.v1/xgboost/xgboostjob_controller.go index e850e2f30b..b429e7aff0 100644 --- a/pkg/controller.v1/xgboost/xgboostjob_controller.go +++ b/pkg/controller.v1/xgboost/xgboostjob_controller.go @@ -325,7 +325,7 @@ func (r *XGBoostJobReconciler) GetPodsForJob(obj interface{}) ([]*corev1.Pod, er return nil, err } - return util.ConvertPodList(podlist.Items), nil + return util.JobControlledPodList(podlist.Items, job), nil } // GetServicesForJob returns the services managed by the job. This can be achieved by selecting services using label key "job-name"