node.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package node
  14. import (
  15. "context"
  16. "encoding/json"
  17. "fmt"
  18. "net"
  19. "os"
  20. "strings"
  21. "time"
  22. "k8s.io/klog"
  23. v1 "k8s.io/api/core/v1"
  24. "k8s.io/apimachinery/pkg/api/equality"
  25. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  26. "k8s.io/apimachinery/pkg/types"
  27. "k8s.io/apimachinery/pkg/util/strategicpatch"
  28. "k8s.io/apimachinery/pkg/util/wait"
  29. clientset "k8s.io/client-go/kubernetes"
  30. v1core "k8s.io/client-go/kubernetes/typed/core/v1"
  31. )
  32. const (
  33. // NodeUnreachablePodReason is the reason on a pod when its state cannot be confirmed as kubelet is unresponsive
  34. // on the node it is (was) running.
  35. NodeUnreachablePodReason = "NodeLost"
  36. // NodeUnreachablePodMessage is the message on a pod when its state cannot be confirmed as kubelet is unresponsive
  37. // on the node it is (was) running.
  38. NodeUnreachablePodMessage = "Node %v which was running pod %v is unresponsive"
  39. )
  40. // GetHostname returns OS's hostname if 'hostnameOverride' is empty; otherwise, return 'hostnameOverride'.
  41. func GetHostname(hostnameOverride string) (string, error) {
  42. hostName := hostnameOverride
  43. if len(hostName) == 0 {
  44. nodeName, err := os.Hostname()
  45. if err != nil {
  46. return "", fmt.Errorf("couldn't determine hostname: %v", err)
  47. }
  48. hostName = nodeName
  49. }
  50. // Trim whitespaces first to avoid getting an empty hostname
  51. // For linux, the hostname is read from file /proc/sys/kernel/hostname directly
  52. hostName = strings.TrimSpace(hostName)
  53. if len(hostName) == 0 {
  54. return "", fmt.Errorf("empty hostname is invalid")
  55. }
  56. return strings.ToLower(hostName), nil
  57. }
  58. // NoMatchError is a typed implementation of the error interface. It indicates a failure to get a matching Node.
  59. type NoMatchError struct {
  60. addresses []v1.NodeAddress
  61. }
  62. // Error is the implementation of the conventional interface for
  63. // representing an error condition, with the nil value representing no error.
  64. func (e *NoMatchError) Error() string {
  65. return fmt.Sprintf("no preferred addresses found; known addresses: %v", e.addresses)
  66. }
  67. // GetPreferredNodeAddress returns the address of the provided node, using the provided preference order.
  68. // If none of the preferred address types are found, an error is returned.
  69. func GetPreferredNodeAddress(node *v1.Node, preferredAddressTypes []v1.NodeAddressType) (string, error) {
  70. for _, addressType := range preferredAddressTypes {
  71. for _, address := range node.Status.Addresses {
  72. if address.Type == addressType {
  73. return address.Address, nil
  74. }
  75. }
  76. }
  77. return "", &NoMatchError{addresses: node.Status.Addresses}
  78. }
  79. // GetNodeHostIP returns the provided node's IP, based on the priority:
  80. // 1. NodeInternalIP
  81. // 2. NodeExternalIP
  82. func GetNodeHostIP(node *v1.Node) (net.IP, error) {
  83. addresses := node.Status.Addresses
  84. addressMap := make(map[v1.NodeAddressType][]v1.NodeAddress)
  85. for i := range addresses {
  86. addressMap[addresses[i].Type] = append(addressMap[addresses[i].Type], addresses[i])
  87. }
  88. if addresses, ok := addressMap[v1.NodeInternalIP]; ok {
  89. return net.ParseIP(addresses[0].Address), nil
  90. }
  91. if addresses, ok := addressMap[v1.NodeExternalIP]; ok {
  92. return net.ParseIP(addresses[0].Address), nil
  93. }
  94. return nil, fmt.Errorf("host IP unknown; known addresses: %v", addresses)
  95. }
  96. // GetNodeIP returns the ip of node with the provided hostname
  97. // If required, wait for the node to be defined.
  98. func GetNodeIP(client clientset.Interface, hostname string) net.IP {
  99. var nodeIP net.IP
  100. backoff := wait.Backoff{
  101. Steps: 6,
  102. Duration: 1 * time.Second,
  103. Factor: 2.0,
  104. Jitter: 0.2,
  105. }
  106. err := wait.ExponentialBackoff(backoff, func() (bool, error) {
  107. node, err := client.CoreV1().Nodes().Get(context.TODO(), hostname, metav1.GetOptions{})
  108. if err != nil {
  109. klog.Errorf("Failed to retrieve node info: %v", err)
  110. return false, nil
  111. }
  112. nodeIP, err = GetNodeHostIP(node)
  113. if err != nil {
  114. klog.Errorf("Failed to retrieve node IP: %v", err)
  115. return false, err
  116. }
  117. return true, nil
  118. })
  119. if err == nil {
  120. klog.Infof("Successfully retrieved node IP: %v", nodeIP)
  121. }
  122. return nodeIP
  123. }
  124. // GetZoneKey is a helper function that builds a string identifier that is unique per failure-zone;
  125. // it returns empty-string for no zone.
  126. // Since there are currently two separate zone keys:
  127. // * "failure-domain.beta.kubernetes.io/zone"
  128. // * "topology.kubernetes.io/zone"
  129. // GetZoneKey will first check failure-domain.beta.kubernetes.io/zone and if not exists, will then check
  130. // topology.kubernetes.io/zone
  131. func GetZoneKey(node *v1.Node) string {
  132. labels := node.Labels
  133. if labels == nil {
  134. return ""
  135. }
  136. // TODO: prefer stable labels for zone in v1.18
  137. zone, ok := labels[v1.LabelZoneFailureDomain]
  138. if !ok {
  139. zone, _ = labels[v1.LabelZoneFailureDomainStable]
  140. }
  141. // TODO: prefer stable labels for region in v1.18
  142. region, ok := labels[v1.LabelZoneRegion]
  143. if !ok {
  144. region, _ = labels[v1.LabelZoneRegionStable]
  145. }
  146. if region == "" && zone == "" {
  147. return ""
  148. }
  149. // We include the null character just in case region or failureDomain has a colon
  150. // (We do assume there's no null characters in a region or failureDomain)
  151. // As a nice side-benefit, the null character is not printed by fmt.Print or glog
  152. return region + ":\x00:" + zone
  153. }
  154. type nodeForConditionPatch struct {
  155. Status nodeStatusForPatch `json:"status"`
  156. }
  157. type nodeStatusForPatch struct {
  158. Conditions []v1.NodeCondition `json:"conditions"`
  159. }
  160. // SetNodeCondition updates specific node condition with patch operation.
  161. func SetNodeCondition(c clientset.Interface, node types.NodeName, condition v1.NodeCondition) error {
  162. generatePatch := func(condition v1.NodeCondition) ([]byte, error) {
  163. patch := nodeForConditionPatch{
  164. Status: nodeStatusForPatch{
  165. Conditions: []v1.NodeCondition{
  166. condition,
  167. },
  168. },
  169. }
  170. patchBytes, err := json.Marshal(&patch)
  171. if err != nil {
  172. return nil, err
  173. }
  174. return patchBytes, nil
  175. }
  176. condition.LastHeartbeatTime = metav1.NewTime(time.Now())
  177. patch, err := generatePatch(condition)
  178. if err != nil {
  179. return nil
  180. }
  181. _, err = c.CoreV1().Nodes().PatchStatus(context.TODO(), string(node), patch)
  182. return err
  183. }
  184. type nodeForCIDRMergePatch struct {
  185. Spec nodeSpecForMergePatch `json:"spec"`
  186. }
  187. type nodeSpecForMergePatch struct {
  188. PodCIDR string `json:"podCIDR"`
  189. PodCIDRs []string `json:"podCIDRs,omitempty"`
  190. }
  191. // PatchNodeCIDR patches the specified node's CIDR to the given value.
  192. func PatchNodeCIDR(c clientset.Interface, node types.NodeName, cidr string) error {
  193. patch := nodeForCIDRMergePatch{
  194. Spec: nodeSpecForMergePatch{
  195. PodCIDR: cidr,
  196. },
  197. }
  198. patchBytes, err := json.Marshal(&patch)
  199. if err != nil {
  200. return fmt.Errorf("failed to json.Marshal CIDR: %v", err)
  201. }
  202. if _, err := c.CoreV1().Nodes().Patch(context.TODO(), string(node), types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{}); err != nil {
  203. return fmt.Errorf("failed to patch node CIDR: %v", err)
  204. }
  205. return nil
  206. }
  207. // PatchNodeCIDRs patches the specified node.CIDR=cidrs[0] and node.CIDRs to the given value.
  208. func PatchNodeCIDRs(c clientset.Interface, node types.NodeName, cidrs []string) error {
  209. // set the pod cidrs list and set the old pod cidr field
  210. patch := nodeForCIDRMergePatch{
  211. Spec: nodeSpecForMergePatch{
  212. PodCIDR: cidrs[0],
  213. PodCIDRs: cidrs,
  214. },
  215. }
  216. patchBytes, err := json.Marshal(&patch)
  217. if err != nil {
  218. return fmt.Errorf("failed to json.Marshal CIDR: %v", err)
  219. }
  220. klog.V(4).Infof("cidrs patch bytes are:%s", string(patchBytes))
  221. if _, err := c.CoreV1().Nodes().Patch(context.TODO(), string(node), types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{}); err != nil {
  222. return fmt.Errorf("failed to patch node CIDR: %v", err)
  223. }
  224. return nil
  225. }
  226. // PatchNodeStatus patches node status.
  227. func PatchNodeStatus(c v1core.CoreV1Interface, nodeName types.NodeName, oldNode *v1.Node, newNode *v1.Node) (*v1.Node, []byte, error) {
  228. patchBytes, err := preparePatchBytesforNodeStatus(nodeName, oldNode, newNode)
  229. if err != nil {
  230. return nil, nil, err
  231. }
  232. updatedNode, err := c.Nodes().Patch(context.TODO(), string(nodeName), types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{}, "status")
  233. if err != nil {
  234. return nil, nil, fmt.Errorf("failed to patch status %q for node %q: %v", patchBytes, nodeName, err)
  235. }
  236. return updatedNode, patchBytes, nil
  237. }
  238. func preparePatchBytesforNodeStatus(nodeName types.NodeName, oldNode *v1.Node, newNode *v1.Node) ([]byte, error) {
  239. oldData, err := json.Marshal(oldNode)
  240. if err != nil {
  241. return nil, fmt.Errorf("failed to Marshal oldData for node %q: %v", nodeName, err)
  242. }
  243. // NodeStatus.Addresses is incorrectly annotated as patchStrategy=merge, which
  244. // will cause strategicpatch.CreateTwoWayMergePatch to create an incorrect patch
  245. // if it changed.
  246. manuallyPatchAddresses := (len(oldNode.Status.Addresses) > 0) && !equality.Semantic.DeepEqual(oldNode.Status.Addresses, newNode.Status.Addresses)
  247. // Reset spec to make sure only patch for Status or ObjectMeta is generated.
  248. // Note that we don't reset ObjectMeta here, because:
  249. // 1. This aligns with Nodes().UpdateStatus().
  250. // 2. Some component does use this to update node annotations.
  251. diffNode := newNode.DeepCopy()
  252. diffNode.Spec = oldNode.Spec
  253. if manuallyPatchAddresses {
  254. diffNode.Status.Addresses = oldNode.Status.Addresses
  255. }
  256. newData, err := json.Marshal(diffNode)
  257. if err != nil {
  258. return nil, fmt.Errorf("failed to Marshal newData for node %q: %v", nodeName, err)
  259. }
  260. patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, v1.Node{})
  261. if err != nil {
  262. return nil, fmt.Errorf("failed to CreateTwoWayMergePatch for node %q: %v", nodeName, err)
  263. }
  264. if manuallyPatchAddresses {
  265. patchBytes, err = fixupPatchForNodeStatusAddresses(patchBytes, newNode.Status.Addresses)
  266. if err != nil {
  267. return nil, fmt.Errorf("failed to fix up NodeAddresses in patch for node %q: %v", nodeName, err)
  268. }
  269. }
  270. return patchBytes, nil
  271. }
  272. // fixupPatchForNodeStatusAddresses adds a replace-strategy patch for Status.Addresses to
  273. // the existing patch
  274. func fixupPatchForNodeStatusAddresses(patchBytes []byte, addresses []v1.NodeAddress) ([]byte, error) {
  275. // Given patchBytes='{"status": {"conditions": [ ... ], "phase": ...}}' and
  276. // addresses=[{"type": "InternalIP", "address": "10.0.0.1"}], we need to generate:
  277. //
  278. // {
  279. // "status": {
  280. // "conditions": [ ... ],
  281. // "phase": ...,
  282. // "addresses": [
  283. // {
  284. // "type": "InternalIP",
  285. // "address": "10.0.0.1"
  286. // },
  287. // {
  288. // "$patch": "replace"
  289. // }
  290. // ]
  291. // }
  292. // }
  293. var patchMap map[string]interface{}
  294. if err := json.Unmarshal(patchBytes, &patchMap); err != nil {
  295. return nil, err
  296. }
  297. addrBytes, err := json.Marshal(addresses)
  298. if err != nil {
  299. return nil, err
  300. }
  301. var addrArray []interface{}
  302. if err := json.Unmarshal(addrBytes, &addrArray); err != nil {
  303. return nil, err
  304. }
  305. addrArray = append(addrArray, map[string]interface{}{"$patch": "replace"})
  306. status := patchMap["status"]
  307. if status == nil {
  308. status = map[string]interface{}{}
  309. patchMap["status"] = status
  310. }
  311. statusMap, ok := status.(map[string]interface{})
  312. if !ok {
  313. return nil, fmt.Errorf("unexpected data in patch")
  314. }
  315. statusMap["addresses"] = addrArray
  316. return json.Marshal(patchMap)
  317. }