mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
79 lines
2.3 KiB
Go
79 lines
2.3 KiB
Go
package job
|
|
|
|
import (
|
|
"context"
|
|
"pandax/base/global"
|
|
|
|
"go.uber.org/zap"
|
|
batch "k8s.io/api/batch/v1"
|
|
v1 "k8s.io/api/core/v1"
|
|
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/fields"
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
"k8s.io/client-go/kubernetes"
|
|
"pandax/apps/devops/entity/k8s"
|
|
k8scommon "pandax/apps/devops/services/k8s/common"
|
|
"pandax/apps/devops/services/k8s/event"
|
|
"pandax/apps/devops/services/k8s/pods"
|
|
)
|
|
|
|
type PodList struct {
|
|
ListMeta k8s.ListMeta `json:"listMeta"`
|
|
|
|
// Basic information about resources status on the list.
|
|
Status k8scommon.ResourceStatus `json:"status"`
|
|
|
|
// Unordered list of Pods.
|
|
Pods []pods.Pod `json:"pods"`
|
|
}
|
|
|
|
// Returns simple info about pods(running, desired, failing, etc.) related to given job.
|
|
func getJobPodInfo(client *kubernetes.Clientset, job *batch.Job) (*k8scommon.PodInfo, error) {
|
|
labelSelector := labels.SelectorFromSet(job.Spec.Selector.MatchLabels)
|
|
channels := &k8scommon.ResourceChannels{
|
|
PodList: k8scommon.GetPodListChannelWithOptions(client, k8scommon.NewSameNamespaceQuery(
|
|
job.Namespace),
|
|
metaV1.ListOptions{
|
|
LabelSelector: labelSelector.String(),
|
|
FieldSelector: fields.Everything().String(),
|
|
}, 1),
|
|
}
|
|
|
|
podList := <-channels.PodList.List
|
|
if err := <-channels.PodList.Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
podInfo := k8scommon.GetPodInfo(job.Status.Active, job.Spec.Completions, podList.Items)
|
|
|
|
// This pod info for jobs should be get from job status, similar to kubectl describe logic.
|
|
podInfo.Running = job.Status.Active
|
|
podInfo.Succeeded = job.Status.Succeeded
|
|
podInfo.Failed = job.Status.Failed
|
|
return &podInfo, nil
|
|
}
|
|
|
|
func getJobToPod(client *kubernetes.Clientset, job *batch.Job) (po *PodList) {
|
|
|
|
selector, err := metaV1.LabelSelectorAsSelector(job.Spec.Selector)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
options := metaV1.ListOptions{LabelSelector: selector.String()}
|
|
|
|
podData, err := client.CoreV1().Pods(job.Namespace).List(context.TODO(), options)
|
|
if err != nil {
|
|
global.Log.Error("Get a pod exception from the job", zap.Any("err", err))
|
|
}
|
|
podList := PodList{
|
|
Pods: make([]pods.Pod, 0),
|
|
}
|
|
podList.ListMeta = k8s.ListMeta{TotalItems: len(podData.Items)}
|
|
for _, pod := range podData.Items {
|
|
warnings := event.GetPodsEventWarnings(nil, []v1.Pod{pod})
|
|
podDetail := pods.ToPod(&pod, warnings)
|
|
podList.Pods = append(podList.Pods, podDetail)
|
|
}
|
|
return &podList
|
|
}
|