From 0c07ddf469613a3041865fd08356f85a97497a80 Mon Sep 17 00:00:00 2001 From: changzhen Date: Fri, 12 Mar 2021 18:59:36 +0800 Subject: [PATCH] retain pod fields when update with objectwatcher Signed-off-by: changzhen --- pkg/util/objectwatcher/retain.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pkg/util/objectwatcher/retain.go b/pkg/util/objectwatcher/retain.go index 92d759090..180f6e5d9 100644 --- a/pkg/util/objectwatcher/retain.go +++ b/pkg/util/objectwatcher/retain.go @@ -15,6 +15,8 @@ For reference: https://github.com/kubernetes-sigs/kubefed/blob/master/pkg/contro const ( // ServiceKind indicates the target resource is a service ServiceKind = "Service" + // PodKind indicates the target resource is a pod + PodKind = "Pod" // ServiceAccountKind indicates the target resource is a serviceaccount ServiceAccountKind = "ServiceAccount" // SecretsField indicates the 'secrets' field of a service account @@ -34,6 +36,9 @@ func RetainClusterFields(desiredObj, clusterObj *unstructured.Unstructured) erro desiredObj.SetFinalizers(clusterObj.GetFinalizers()) desiredObj.SetAnnotations(clusterObj.GetAnnotations()) + if targetKind == PodKind { + return retainPodFields(desiredObj, clusterObj) + } if targetKind == ServiceKind { return retainServiceFields(desiredObj, clusterObj) } @@ -43,6 +48,20 @@ func RetainClusterFields(desiredObj, clusterObj *unstructured.Unstructured) erro return nil } +func retainPodFields(desiredObj, clusterObj *unstructured.Unstructured) error { + nodeName, ok, err := unstructured.NestedString(clusterObj.Object, "spec", "nodeName") + if err != nil { + return fmt.Errorf("error retrieving nodeName from cluster pod: %w", err) + } + if ok && nodeName != "" { + err := unstructured.SetNestedField(desiredObj.Object, nodeName, "spec", "nodeName") + if err != nil { + return fmt.Errorf("error setting nodeName for pod: %w", err) + } + } + return nil +} + func retainServiceFields(desiredObj, clusterObj *unstructured.Unstructured) error { // healthCheckNodePort is allocated by APIServer and unchangeable, so it should be retained while updating healthCheckNodePort, ok, err := unstructured.NestedInt64(clusterObj.Object, "spec", "healthCheckNodePort")