|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/* |
| 18 | +
|
| 19 | +NOTE: the contents of this file has been copied from k8s.io/kubernetes/pkg/util/node. The reason for duplicating this code is to remove |
| 20 | +dependencies for cloud controller manager. |
| 21 | +*/ |
| 22 | + |
| 23 | +package helpers |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + "encoding/json" |
| 28 | + "fmt" |
| 29 | + |
| 30 | + v1 "k8s.io/api/core/v1" |
| 31 | + "k8s.io/apimachinery/pkg/api/equality" |
| 32 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 33 | + "k8s.io/apimachinery/pkg/types" |
| 34 | + "k8s.io/apimachinery/pkg/util/strategicpatch" |
| 35 | + v1core "k8s.io/client-go/kubernetes/typed/core/v1" |
| 36 | +) |
| 37 | + |
| 38 | +// PatchNodeStatus patches node status. |
| 39 | +func PatchNodeStatus(c v1core.CoreV1Interface, nodeName types.NodeName, oldNode *v1.Node, newNode *v1.Node) (*v1.Node, []byte, error) { |
| 40 | + patchBytes, err := preparePatchBytesforNodeStatus(nodeName, oldNode, newNode) |
| 41 | + if err != nil { |
| 42 | + return nil, nil, err |
| 43 | + } |
| 44 | + |
| 45 | + updatedNode, err := c.Nodes().Patch(context.TODO(), string(nodeName), types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{}, "status") |
| 46 | + if err != nil { |
| 47 | + return nil, nil, fmt.Errorf("failed to patch status %q for node %q: %v", patchBytes, nodeName, err) |
| 48 | + } |
| 49 | + return updatedNode, patchBytes, nil |
| 50 | +} |
| 51 | + |
| 52 | +func preparePatchBytesforNodeStatus(nodeName types.NodeName, oldNode *v1.Node, newNode *v1.Node) ([]byte, error) { |
| 53 | + oldData, err := json.Marshal(oldNode) |
| 54 | + if err != nil { |
| 55 | + return nil, fmt.Errorf("failed to Marshal oldData for node %q: %v", nodeName, err) |
| 56 | + } |
| 57 | + |
| 58 | + // NodeStatus.Addresses is incorrectly annotated as patchStrategy=merge, which |
| 59 | + // will cause strategicpatch.CreateTwoWayMergePatch to create an incorrect patch |
| 60 | + // if it changed. |
| 61 | + manuallyPatchAddresses := (len(oldNode.Status.Addresses) > 0) && !equality.Semantic.DeepEqual(oldNode.Status.Addresses, newNode.Status.Addresses) |
| 62 | + |
| 63 | + // Reset spec to make sure only patch for Status or ObjectMeta is generated. |
| 64 | + // Note that we don't reset ObjectMeta here, because: |
| 65 | + // 1. This aligns with Nodes().UpdateStatus(). |
| 66 | + // 2. Some component does use this to update node annotations. |
| 67 | + diffNode := newNode.DeepCopy() |
| 68 | + diffNode.Spec = oldNode.Spec |
| 69 | + if manuallyPatchAddresses { |
| 70 | + diffNode.Status.Addresses = oldNode.Status.Addresses |
| 71 | + } |
| 72 | + newData, err := json.Marshal(diffNode) |
| 73 | + if err != nil { |
| 74 | + return nil, fmt.Errorf("failed to Marshal newData for node %q: %v", nodeName, err) |
| 75 | + } |
| 76 | + |
| 77 | + patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, v1.Node{}) |
| 78 | + if err != nil { |
| 79 | + return nil, fmt.Errorf("failed to CreateTwoWayMergePatch for node %q: %v", nodeName, err) |
| 80 | + } |
| 81 | + if manuallyPatchAddresses { |
| 82 | + patchBytes, err = fixupPatchForNodeStatusAddresses(patchBytes, newNode.Status.Addresses) |
| 83 | + if err != nil { |
| 84 | + return nil, fmt.Errorf("failed to fix up NodeAddresses in patch for node %q: %v", nodeName, err) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return patchBytes, nil |
| 89 | +} |
| 90 | + |
| 91 | +// fixupPatchForNodeStatusAddresses adds a replace-strategy patch for Status.Addresses to |
| 92 | +// the existing patch |
| 93 | +func fixupPatchForNodeStatusAddresses(patchBytes []byte, addresses []v1.NodeAddress) ([]byte, error) { |
| 94 | + // Given patchBytes='{"status": {"conditions": [ ... ], "phase": ...}}' and |
| 95 | + // addresses=[{"type": "InternalIP", "address": "10.0.0.1"}], we need to generate: |
| 96 | + // |
| 97 | + // { |
| 98 | + // "status": { |
| 99 | + // "conditions": [ ... ], |
| 100 | + // "phase": ..., |
| 101 | + // "addresses": [ |
| 102 | + // { |
| 103 | + // "type": "InternalIP", |
| 104 | + // "address": "10.0.0.1" |
| 105 | + // }, |
| 106 | + // { |
| 107 | + // "$patch": "replace" |
| 108 | + // } |
| 109 | + // ] |
| 110 | + // } |
| 111 | + // } |
| 112 | + |
| 113 | + var patchMap map[string]interface{} |
| 114 | + if err := json.Unmarshal(patchBytes, &patchMap); err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + |
| 118 | + addrBytes, err := json.Marshal(addresses) |
| 119 | + if err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + var addrArray []interface{} |
| 123 | + if err := json.Unmarshal(addrBytes, &addrArray); err != nil { |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + addrArray = append(addrArray, map[string]interface{}{"$patch": "replace"}) |
| 127 | + |
| 128 | + status := patchMap["status"] |
| 129 | + if status == nil { |
| 130 | + status = map[string]interface{}{} |
| 131 | + patchMap["status"] = status |
| 132 | + } |
| 133 | + statusMap, ok := status.(map[string]interface{}) |
| 134 | + if !ok { |
| 135 | + return nil, fmt.Errorf("unexpected data in patch") |
| 136 | + } |
| 137 | + statusMap["addresses"] = addrArray |
| 138 | + |
| 139 | + return json.Marshal(patchMap) |
| 140 | +} |
0 commit comments