watch.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Copyright 2017 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 kubeletconfig
  14. import (
  15. "math/rand"
  16. "time"
  17. apiv1 "k8s.io/api/core/v1"
  18. apiequality "k8s.io/apimachinery/pkg/api/equality"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/fields"
  21. clientset "k8s.io/client-go/kubernetes"
  22. "k8s.io/client-go/tools/cache"
  23. utillog "k8s.io/kubernetes/pkg/kubelet/kubeletconfig/util/log"
  24. )
  25. // newSharedNodeInformer returns a shared informer that uses `client` to watch the Node with
  26. // `nodeName` for changes and respond with `addFunc`, `updateFunc`, and `deleteFunc`.
  27. func newSharedNodeInformer(client clientset.Interface, nodeName string,
  28. addFunc func(newObj interface{}),
  29. updateFunc func(oldObj interface{}, newObj interface{}),
  30. deleteFunc func(deletedObj interface{})) cache.SharedInformer {
  31. // select nodes by name
  32. fieldSelector := fields.OneTermEqualSelector("metadata.name", nodeName)
  33. // add some randomness to resync period, which can help avoid controllers falling into lock-step
  34. minResyncPeriod := 15 * time.Minute
  35. factor := rand.Float64() + 1
  36. resyncPeriod := time.Duration(float64(minResyncPeriod.Nanoseconds()) * factor)
  37. lw := cache.NewListWatchFromClient(client.CoreV1().RESTClient(), "nodes", metav1.NamespaceAll, fieldSelector)
  38. handler := cache.ResourceEventHandlerFuncs{
  39. AddFunc: addFunc,
  40. UpdateFunc: updateFunc,
  41. DeleteFunc: deleteFunc,
  42. }
  43. informer := cache.NewSharedInformer(lw, &apiv1.Node{}, resyncPeriod)
  44. informer.AddEventHandler(handler)
  45. return informer
  46. }
  47. // onAddNodeEvent calls onUpdateNodeEvent with the new object and a nil old object
  48. func (cc *Controller) onAddNodeEvent(newObj interface{}) {
  49. cc.onUpdateNodeEvent(nil, newObj)
  50. }
  51. // onUpdateNodeEvent checks whether the configSource changed between oldObj and newObj, and pokes the
  52. // configuration sync worker if there was a change
  53. func (cc *Controller) onUpdateNodeEvent(oldObj interface{}, newObj interface{}) {
  54. newNode, ok := newObj.(*apiv1.Node)
  55. if !ok {
  56. utillog.Errorf("failed to cast new object to Node, couldn't handle event")
  57. return
  58. }
  59. if oldObj == nil {
  60. // Node was just added, need to sync
  61. utillog.Infof("initial Node watch event")
  62. cc.pokeConfigSourceWorker()
  63. return
  64. }
  65. oldNode, ok := oldObj.(*apiv1.Node)
  66. if !ok {
  67. utillog.Errorf("failed to cast old object to Node, couldn't handle event")
  68. return
  69. }
  70. if !apiequality.Semantic.DeepEqual(oldNode.Spec.ConfigSource, newNode.Spec.ConfigSource) {
  71. utillog.Infof("Node.Spec.ConfigSource was updated")
  72. cc.pokeConfigSourceWorker()
  73. }
  74. }
  75. // onDeleteNodeEvent logs a message if the Node was deleted
  76. // We allow the sync-loop to continue, because it is possible that the Kubelet detected
  77. // a Node with unexpected externalID and is attempting to delete and re-create the Node
  78. // (see pkg/kubelet/kubelet_node_status.go), or that someone accidentally deleted the Node
  79. // (the Kubelet will re-create it).
  80. func (cc *Controller) onDeleteNodeEvent(deletedObj interface{}) {
  81. // For this case, we just log the event.
  82. // We don't want to poke the worker, because a temporary deletion isn't worth reporting an error for.
  83. // If the Node is deleted because the VM is being deleted, then the Kubelet has nothing to do.
  84. utillog.Infof("Node was deleted")
  85. }
  86. // onAddRemoteConfigSourceEvent calls onUpdateConfigMapEvent with the new object and a nil old object
  87. func (cc *Controller) onAddRemoteConfigSourceEvent(newObj interface{}) {
  88. cc.onUpdateRemoteConfigSourceEvent(nil, newObj)
  89. }
  90. // onUpdateRemoteConfigSourceEvent checks whether the configSource changed between oldObj and newObj,
  91. // and pokes the sync worker if there was a change
  92. func (cc *Controller) onUpdateRemoteConfigSourceEvent(oldObj interface{}, newObj interface{}) {
  93. // since ConfigMap is currently the only source type, we handle that here
  94. newConfigMap, ok := newObj.(*apiv1.ConfigMap)
  95. if !ok {
  96. utillog.Errorf("failed to cast new object to ConfigMap, couldn't handle event")
  97. return
  98. }
  99. if oldObj == nil {
  100. // ConfigMap was just added, need to sync
  101. utillog.Infof("initial ConfigMap watch event")
  102. cc.pokeConfigSourceWorker()
  103. return
  104. }
  105. oldConfigMap, ok := oldObj.(*apiv1.ConfigMap)
  106. if !ok {
  107. utillog.Errorf("failed to cast old object to ConfigMap, couldn't handle event")
  108. return
  109. }
  110. if !apiequality.Semantic.DeepEqual(oldConfigMap, newConfigMap) {
  111. utillog.Infof("assigned ConfigMap was updated")
  112. cc.pokeConfigSourceWorker()
  113. }
  114. }
  115. // onDeleteRemoteConfigSourceEvent logs a message if the ConfigMap was deleted and pokes the sync worker
  116. func (cc *Controller) onDeleteRemoteConfigSourceEvent(deletedObj interface{}) {
  117. // If the ConfigMap we're watching is deleted, we log the event and poke the sync worker.
  118. // This requires a sync, because if the Node is still configured to use the deleted ConfigMap,
  119. // the Kubelet should report a DownloadError.
  120. utillog.Infof("assigned ConfigMap was deleted")
  121. cc.pokeConfigSourceWorker()
  122. }