retain pod fields when update with objectwatcher

Signed-off-by: changzhen <changzhen5@huawei.com>
This commit is contained in:
changzhen 2021-03-12 18:59:36 +08:00 committed by Hongcai Ren
parent e3dd8cfaa6
commit 0c07ddf469
1 changed files with 19 additions and 0 deletions

View File

@ -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")