-
Notifications
You must be signed in to change notification settings - Fork 22
/
status.go
134 lines (116 loc) · 4.36 KB
/
status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package util
import (
"context"
"encoding/json"
"fmt"
v1 "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"
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
)
// PatchNodeStatus patches node status.
func PatchNodeStatus(c v1core.CoreV1Interface, nodeName types.NodeName, oldNode *v1.Node, newNode *v1.Node) (*v1.Node, []byte, error) {
patchBytes, err := preparePatchBytesforNodeStatus(nodeName, oldNode, newNode)
if err != nil {
return nil, nil, err
}
updatedNode, err := c.Nodes().Patch(context.TODO(), string(nodeName), types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{}, "status")
if err != nil {
return nil, nil, fmt.Errorf("failed to patch status %q for node %q: %v", patchBytes, nodeName, err)
}
return updatedNode, patchBytes, nil
}
func preparePatchBytesforNodeStatus(nodeName types.NodeName, oldNode *v1.Node, newNode *v1.Node) ([]byte, error) {
oldData, err := json.Marshal(oldNode)
if err != nil {
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.
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)
}
patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, v1.Node{})
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)
}