node_status_updater.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright 2016 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 statusupdater implements interfaces that enable updating the status
  14. // of API objects.
  15. package statusupdater
  16. import (
  17. "k8s.io/klog"
  18. "k8s.io/api/core/v1"
  19. "k8s.io/apimachinery/pkg/api/errors"
  20. "k8s.io/apimachinery/pkg/types"
  21. clientset "k8s.io/client-go/kubernetes"
  22. corelisters "k8s.io/client-go/listers/core/v1"
  23. "k8s.io/kubernetes/pkg/controller/volume/attachdetach/cache"
  24. nodeutil "k8s.io/kubernetes/pkg/util/node"
  25. )
  26. // NodeStatusUpdater defines a set of operations for updating the
  27. // VolumesAttached field in the Node Status.
  28. type NodeStatusUpdater interface {
  29. // Gets a list of node statuses that should be updated from the actual state
  30. // of the world and updates them.
  31. UpdateNodeStatuses() error
  32. }
  33. // NewNodeStatusUpdater returns a new instance of NodeStatusUpdater.
  34. func NewNodeStatusUpdater(
  35. kubeClient clientset.Interface,
  36. nodeLister corelisters.NodeLister,
  37. actualStateOfWorld cache.ActualStateOfWorld) NodeStatusUpdater {
  38. return &nodeStatusUpdater{
  39. actualStateOfWorld: actualStateOfWorld,
  40. nodeLister: nodeLister,
  41. kubeClient: kubeClient,
  42. }
  43. }
  44. type nodeStatusUpdater struct {
  45. kubeClient clientset.Interface
  46. nodeLister corelisters.NodeLister
  47. actualStateOfWorld cache.ActualStateOfWorld
  48. }
  49. func (nsu *nodeStatusUpdater) UpdateNodeStatuses() error {
  50. // TODO: investigate right behavior if nodeName is empty
  51. // kubernetes/kubernetes/issues/37777
  52. nodesToUpdate := nsu.actualStateOfWorld.GetVolumesToReportAttached()
  53. for nodeName, attachedVolumes := range nodesToUpdate {
  54. nodeObj, err := nsu.nodeLister.Get(string(nodeName))
  55. if errors.IsNotFound(err) {
  56. // If node does not exist, its status cannot be updated.
  57. // Do nothing so that there is no retry until node is created.
  58. klog.V(2).Infof(
  59. "Could not update node status. Failed to find node %q in NodeInformer cache. Error: '%v'",
  60. nodeName,
  61. err)
  62. continue
  63. } else if err != nil {
  64. // For all other errors, log error and reset flag statusUpdateNeeded
  65. // back to true to indicate this node status needs to be updated again.
  66. klog.V(2).Infof("Error retrieving nodes from node lister. Error: %v", err)
  67. nsu.actualStateOfWorld.SetNodeStatusUpdateNeeded(nodeName)
  68. continue
  69. }
  70. if err := nsu.updateNodeStatus(nodeName, nodeObj, attachedVolumes); err != nil {
  71. // If update node status fails, reset flag statusUpdateNeeded back to true
  72. // to indicate this node status needs to be updated again
  73. nsu.actualStateOfWorld.SetNodeStatusUpdateNeeded(nodeName)
  74. klog.V(2).Infof(
  75. "Could not update node status for %q; re-marking for update. %v",
  76. nodeName,
  77. err)
  78. // We currently always return immediately on error
  79. return err
  80. }
  81. }
  82. return nil
  83. }
  84. func (nsu *nodeStatusUpdater) updateNodeStatus(nodeName types.NodeName, nodeObj *v1.Node, attachedVolumes []v1.AttachedVolume) error {
  85. node := nodeObj.DeepCopy()
  86. node.Status.VolumesAttached = attachedVolumes
  87. _, patchBytes, err := nodeutil.PatchNodeStatus(nsu.kubeClient.CoreV1(), nodeName, nodeObj, node)
  88. if err != nil {
  89. return err
  90. }
  91. klog.V(4).Infof("Updating status %q for node %q succeeded. VolumesAttached: %v", patchBytes, nodeName, attachedVolumes)
  92. return nil
  93. }