mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-20 17:38:50 +00:00
Hack PatchNodeStatus() to override the patch type on Status.Addresses
This commit is contained in:
@@ -12,6 +12,7 @@ go_library(
|
||||
importpath = "k8s.io/kubernetes/pkg/util/node",
|
||||
deps = [
|
||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/equality:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/strategicpatch:go_default_library",
|
||||
|
@@ -27,6 +27,7 @@ import (
|
||||
"k8s.io/klog"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/equality"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/util/strategicpatch"
|
||||
@@ -195,12 +196,21 @@ func preparePatchBytesforNodeStatus(nodeName types.NodeName, oldNode *v1.Node, n
|
||||
return nil, fmt.Errorf("failed to Marshal oldData for node %q: %v", nodeName, err)
|
||||
}
|
||||
|
||||
// NodeStatus.Addresses is incorrectly annotated as patchStrategy=merge, which
|
||||
// will cause strategicpatch.CreateTwoWayMergePatch to create an incorrect patch
|
||||
// if it changed.
|
||||
manuallyPatchAddresses := (len(oldNode.Status.Addresses) > 0) && !equality.Semantic.DeepEqual(oldNode.Status.Addresses, newNode.Status.Addresses)
|
||||
|
||||
// Reset spec to make sure only patch for Status or ObjectMeta is generated.
|
||||
// Note that we don't reset ObjectMeta here, because:
|
||||
// 1. This aligns with Nodes().UpdateStatus().
|
||||
// 2. Some component does use this to update node annotations.
|
||||
newNode.Spec = oldNode.Spec
|
||||
newData, err := json.Marshal(newNode)
|
||||
diffNode := newNode.DeepCopy()
|
||||
diffNode.Spec = oldNode.Spec
|
||||
if manuallyPatchAddresses {
|
||||
diffNode.Status.Addresses = oldNode.Status.Addresses
|
||||
}
|
||||
newData, err := json.Marshal(diffNode)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to Marshal newData for node %q: %v", nodeName, err)
|
||||
}
|
||||
@@ -209,5 +219,63 @@ func preparePatchBytesforNodeStatus(nodeName types.NodeName, oldNode *v1.Node, n
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to CreateTwoWayMergePatch for node %q: %v", nodeName, err)
|
||||
}
|
||||
if manuallyPatchAddresses {
|
||||
patchBytes, err = fixupPatchForNodeStatusAddresses(patchBytes, newNode.Status.Addresses)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to fix up NodeAddresses in patch for node %q: %v", nodeName, err)
|
||||
}
|
||||
}
|
||||
|
||||
return patchBytes, nil
|
||||
}
|
||||
|
||||
// fixupPatchForNodeStatusAddresses adds a replace-strategy patch for Status.Addresses to
|
||||
// the existing patch
|
||||
func fixupPatchForNodeStatusAddresses(patchBytes []byte, addresses []v1.NodeAddress) ([]byte, error) {
|
||||
// Given patchBytes='{"status": {"conditions": [ ... ], "phase": ...}}' and
|
||||
// addresses=[{"type": "InternalIP", "address": "10.0.0.1"}], we need to generate:
|
||||
//
|
||||
// {
|
||||
// "status": {
|
||||
// "conditions": [ ... ],
|
||||
// "phase": ...,
|
||||
// "addresses": [
|
||||
// {
|
||||
// "type": "InternalIP",
|
||||
// "address": "10.0.0.1"
|
||||
// },
|
||||
// {
|
||||
// "$patch": "replace"
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
// }
|
||||
|
||||
var patchMap map[string]interface{}
|
||||
if err := json.Unmarshal(patchBytes, &patchMap); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
addrBytes, err := json.Marshal(addresses)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var addrArray []interface{}
|
||||
if err := json.Unmarshal(addrBytes, &addrArray); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
addrArray = append(addrArray, map[string]interface{}{"$patch": "replace"})
|
||||
|
||||
status := patchMap["status"]
|
||||
if status == nil {
|
||||
status = map[string]interface{}{}
|
||||
patchMap["status"] = status
|
||||
}
|
||||
statusMap, ok := status.(map[string]interface{})
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected data in patch")
|
||||
}
|
||||
statusMap["addresses"] = addrArray
|
||||
|
||||
return json.Marshal(patchMap)
|
||||
}
|
||||
|
Reference in New Issue
Block a user