From ca50789d62ad90df48a0920c9ed886b451538456 Mon Sep 17 00:00:00 2001 From: bruce Date: Tue, 31 May 2022 15:30:43 +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..329fdc4d1648 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 tempStatus.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 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 + } + + if containerSum == readySum { + newStatus.Phase = corev1.PodRunning + } else { + newStatus.Phase = corev1.PodPending + } + + pod.Status = *newStatus + return helper.ToUnstructured(pod) +}