From 7dba38a9d8211f0c4b4e48567c8be1e5c93f2f58 Mon Sep 17 00:00:00 2001 From: bruce Date: Wed, 1 Jun 2022 13:42:10 +0800 Subject: [PATCH] sync status of pod Signed-off-by: bruce --- .../defaultinterpreter/aggregatestatus.go | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/pkg/resourceinterpreter/defaultinterpreter/aggregatestatus.go b/pkg/resourceinterpreter/defaultinterpreter/aggregatestatus.go index 6a714f67e975..b01f8f4c5c00 100644 --- a/pkg/resourceinterpreter/defaultinterpreter/aggregatestatus.go +++ b/pkg/resourceinterpreter/defaultinterpreter/aggregatestatus.go @@ -27,6 +27,7 @@ func getAllDefaultAggregateStatusInterpreter() map[schema.GroupVersionKind]aggre s[batchv1.SchemeGroupVersion.WithKind(util.JobKind)] = aggregateJobStatus s[appsv1.SchemeGroupVersion.WithKind(util.DaemonSetKind)] = aggregateDaemonSetStatus s[appsv1.SchemeGroupVersion.WithKind(util.StatefulSetKind)] = aggregateStatefulSetStatus + s[corev1.SchemeGroupVersion.WithKind(util.PodKind)] = aggregatePodStatus return s } @@ -264,3 +265,50 @@ func aggregateStatefulSetStatus(object *unstructured.Unstructured, aggregatedSta return helper.ToUnstructured(statefulSet) } + +func aggregatePodStatus(object *unstructured.Unstructured, aggregatedStatusItems []workv1alpha2.AggregatedStatusItem) (*unstructured.Unstructured, error) { + pod, err := helper.ConvertToPod(object) + if err != nil { + return nil, err + } + + newStatus := &corev1.PodStatus{} + newStatus.ContainerStatuses = make([]corev1.ContainerStatus, 0) + readySum := 0 + containerSum := 0 + for _, item := range aggregatedStatusItems { + if item.Status == nil { + continue + } + + temp := &corev1.PodStatus{} + if err = json.Unmarshal(item.Status.Raw, temp); err != nil { + return nil, err + } + + for _, containerStatus := range temp.ContainerStatuses { + tempStatus := containerStatus + newStatus.ContainerStatuses = append(newStatus.ContainerStatuses, tempStatus) + if containerStatus.Ready { + readySum++ + } + containerSum++ + } + klog.V(3).Infof("Grab pod(%s/%s) status from cluster(%s), phase: %s", pod.Namespace, + pod.Name, item.ClusterName, temp.Phase) + } + + if containerSum == readySum { + newStatus.Phase = corev1.PodRunning + } else { + newStatus.Phase = corev1.PodPending + } + + if reflect.DeepEqual(pod.Status, *newStatus) { + klog.V(3).Infof("ignore update pod(%s/%s) status as up to date", pod.Namespace, pod.Name) + return object, nil + } + + pod.Status = *newStatus + return helper.ToUnstructured(pod) +}