sync.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 sync
  14. import (
  15. "context"
  16. "fmt"
  17. "net"
  18. "time"
  19. "k8s.io/klog"
  20. "k8s.io/api/core/v1"
  21. "k8s.io/kubernetes/pkg/controller/nodeipam/ipam/cidrset"
  22. )
  23. const (
  24. // InvalidPodCIDR is the event recorded when a node is found with an
  25. // invalid PodCIDR.
  26. InvalidPodCIDR = "CloudCIDRAllocatorInvalidPodCIDR"
  27. // InvalidModeEvent is the event recorded when the CIDR range cannot be
  28. // sync'd due to the cluster running in the wrong mode.
  29. InvalidModeEvent = "CloudCIDRAllocatorInvalidMode"
  30. // MismatchEvent is the event recorded when the CIDR range allocated in the
  31. // node spec does not match what has been allocated in the cloud.
  32. MismatchEvent = "CloudCIDRAllocatorMismatch"
  33. )
  34. // cloudAlias is the interface to the cloud platform APIs.
  35. type cloudAlias interface {
  36. // Alias returns the IP alias for the node.
  37. Alias(ctx context.Context, nodeName string) (*net.IPNet, error)
  38. // AddAlias adds an alias to the node.
  39. AddAlias(ctx context.Context, nodeName string, cidrRange *net.IPNet) error
  40. }
  41. // kubeAPI is the interface to the Kubernetes APIs.
  42. type kubeAPI interface {
  43. // Node returns the spec for the Node object.
  44. Node(ctx context.Context, name string) (*v1.Node, error)
  45. // UpdateNodePodCIDR updates the PodCIDR in the Node spec.
  46. UpdateNodePodCIDR(ctx context.Context, node *v1.Node, cidrRange *net.IPNet) error
  47. // UpdateNodeNetworkUnavailable updates the network unavailable status for the node.
  48. UpdateNodeNetworkUnavailable(nodeName string, unavailable bool) error
  49. // EmitNodeWarningEvent emits an event for the given node.
  50. EmitNodeWarningEvent(nodeName, reason, fmt string, args ...interface{})
  51. }
  52. // controller is the interface to the controller.
  53. type controller interface {
  54. // ReportResult updates the controller with the result of the latest
  55. // sync operation.
  56. ReportResult(err error)
  57. // ResyncTimeout returns the amount of time to wait before retrying
  58. // a sync with a node.
  59. ResyncTimeout() time.Duration
  60. }
  61. // NodeSyncMode is the mode the cloud CIDR allocator runs in.
  62. type NodeSyncMode string
  63. var (
  64. // SyncFromCloud is the mode that synchronizes the IP allocation from the cloud
  65. // platform to the node.
  66. SyncFromCloud NodeSyncMode = "SyncFromCloud"
  67. // SyncFromCluster is the mode that synchronizes the IP allocation determined
  68. // by the k8s controller to the cloud provider.
  69. SyncFromCluster NodeSyncMode = "SyncFromCluster"
  70. )
  71. // IsValidMode returns true if the given mode is valid.
  72. func IsValidMode(m NodeSyncMode) bool {
  73. switch m {
  74. case SyncFromCloud:
  75. case SyncFromCluster:
  76. default:
  77. return false
  78. }
  79. return true
  80. }
  81. // NodeSync synchronizes the state for a single node in the cluster.
  82. type NodeSync struct {
  83. c controller
  84. cloudAlias cloudAlias
  85. kubeAPI kubeAPI
  86. mode NodeSyncMode
  87. nodeName string
  88. opChan chan syncOp
  89. set *cidrset.CidrSet
  90. }
  91. // New returns a new syncer for a given node.
  92. func New(c controller, cloudAlias cloudAlias, kubeAPI kubeAPI, mode NodeSyncMode, nodeName string, set *cidrset.CidrSet) *NodeSync {
  93. return &NodeSync{
  94. c: c,
  95. cloudAlias: cloudAlias,
  96. kubeAPI: kubeAPI,
  97. mode: mode,
  98. nodeName: nodeName,
  99. opChan: make(chan syncOp, 1),
  100. set: set,
  101. }
  102. }
  103. // Loop runs the sync loop for a given node. done is an optional channel that
  104. // is closed when the Loop() returns.
  105. func (sync *NodeSync) Loop(done chan struct{}) {
  106. klog.V(2).Infof("Starting sync loop for node %q", sync.nodeName)
  107. defer func() {
  108. if done != nil {
  109. close(done)
  110. }
  111. }()
  112. timeout := sync.c.ResyncTimeout()
  113. delayTimer := time.NewTimer(timeout)
  114. klog.V(4).Infof("Resync node %q in %v", sync.nodeName, timeout)
  115. for {
  116. select {
  117. case op, more := <-sync.opChan:
  118. if !more {
  119. klog.V(2).Infof("Stopping sync loop")
  120. return
  121. }
  122. sync.c.ReportResult(op.run(sync))
  123. if !delayTimer.Stop() {
  124. <-delayTimer.C
  125. }
  126. case <-delayTimer.C:
  127. klog.V(4).Infof("Running resync for node %q", sync.nodeName)
  128. sync.c.ReportResult((&updateOp{}).run(sync))
  129. }
  130. timeout := sync.c.ResyncTimeout()
  131. delayTimer.Reset(timeout)
  132. klog.V(4).Infof("Resync node %q in %v", sync.nodeName, timeout)
  133. }
  134. }
  135. // Update causes an update operation on the given node. If node is nil, then
  136. // the syncer will fetch the node spec from the API server before syncing.
  137. //
  138. // This method is safe to call from multiple goroutines.
  139. func (sync *NodeSync) Update(node *v1.Node) {
  140. sync.opChan <- &updateOp{node}
  141. }
  142. // Delete performs the sync operations necessary to remove the node from the
  143. // IPAM state.
  144. //
  145. // This method is safe to call from multiple goroutines.
  146. func (sync *NodeSync) Delete(node *v1.Node) {
  147. sync.opChan <- &deleteOp{node}
  148. close(sync.opChan)
  149. }
  150. // syncOp is the interface for generic sync operation.
  151. type syncOp interface {
  152. // run the requested sync operation.
  153. run(sync *NodeSync) error
  154. }
  155. // updateOp handles creation and updates of a node.
  156. type updateOp struct {
  157. node *v1.Node
  158. }
  159. func (op *updateOp) String() string {
  160. if op.node == nil {
  161. return fmt.Sprintf("updateOp(nil)")
  162. }
  163. return fmt.Sprintf("updateOp(%q,%v)", op.node.Name, op.node.Spec.PodCIDR)
  164. }
  165. func (op *updateOp) run(sync *NodeSync) error {
  166. klog.V(3).Infof("Running updateOp %+v", op)
  167. ctx := context.Background()
  168. if op.node == nil {
  169. klog.V(3).Infof("Getting node spec for %q", sync.nodeName)
  170. node, err := sync.kubeAPI.Node(ctx, sync.nodeName)
  171. if err != nil {
  172. klog.Errorf("Error getting node %q spec: %v", sync.nodeName, err)
  173. return err
  174. }
  175. op.node = node
  176. }
  177. aliasRange, err := sync.cloudAlias.Alias(ctx, sync.nodeName)
  178. if err != nil {
  179. klog.Errorf("Error getting cloud alias for node %q: %v", sync.nodeName, err)
  180. return err
  181. }
  182. switch {
  183. case op.node.Spec.PodCIDR == "" && aliasRange == nil:
  184. err = op.allocateRange(ctx, sync, op.node)
  185. case op.node.Spec.PodCIDR == "" && aliasRange != nil:
  186. err = op.updateNodeFromAlias(ctx, sync, op.node, aliasRange)
  187. case op.node.Spec.PodCIDR != "" && aliasRange == nil:
  188. err = op.updateAliasFromNode(ctx, sync, op.node)
  189. case op.node.Spec.PodCIDR != "" && aliasRange != nil:
  190. err = op.validateRange(ctx, sync, op.node, aliasRange)
  191. }
  192. return err
  193. }
  194. // validateRange checks that the allocated range and the alias range
  195. // match.
  196. func (op *updateOp) validateRange(ctx context.Context, sync *NodeSync, node *v1.Node, aliasRange *net.IPNet) error {
  197. if node.Spec.PodCIDR != aliasRange.String() {
  198. klog.Errorf("Inconsistency detected between node PodCIDR and node alias (%v != %v)",
  199. node.Spec.PodCIDR, aliasRange)
  200. sync.kubeAPI.EmitNodeWarningEvent(node.Name, MismatchEvent,
  201. "Node.Spec.PodCIDR != cloud alias (%v != %v)", node.Spec.PodCIDR, aliasRange)
  202. // User intervention is required in this case, as this is most likely due
  203. // to the user mucking around with their VM aliases on the side.
  204. } else {
  205. klog.V(4).Infof("Node %q CIDR range %v is matches cloud assignment", node.Name, node.Spec.PodCIDR)
  206. }
  207. return nil
  208. }
  209. // updateNodeFromAlias updates the node from the cloud allocated
  210. // alias.
  211. func (op *updateOp) updateNodeFromAlias(ctx context.Context, sync *NodeSync, node *v1.Node, aliasRange *net.IPNet) error {
  212. if sync.mode != SyncFromCloud {
  213. sync.kubeAPI.EmitNodeWarningEvent(node.Name, InvalidModeEvent,
  214. "Cannot sync from cloud in mode %q", sync.mode)
  215. return fmt.Errorf("cannot sync from cloud in mode %q", sync.mode)
  216. }
  217. klog.V(2).Infof("Updating node spec with alias range, node.PodCIDR = %v", aliasRange)
  218. if err := sync.set.Occupy(aliasRange); err != nil {
  219. klog.Errorf("Error occupying range %v for node %v", aliasRange, sync.nodeName)
  220. return err
  221. }
  222. if err := sync.kubeAPI.UpdateNodePodCIDR(ctx, node, aliasRange); err != nil {
  223. klog.Errorf("Could not update node %q PodCIDR to %v: %v", node.Name, aliasRange, err)
  224. return err
  225. }
  226. klog.V(2).Infof("Node %q PodCIDR set to %v", node.Name, aliasRange)
  227. if err := sync.kubeAPI.UpdateNodeNetworkUnavailable(node.Name, false); err != nil {
  228. klog.Errorf("Could not update node NetworkUnavailable status to false: %v", err)
  229. return err
  230. }
  231. klog.V(2).Infof("Updated node %q PodCIDR from cloud alias %v", node.Name, aliasRange)
  232. return nil
  233. }
  234. // updateAliasFromNode updates the cloud alias given the node allocation.
  235. func (op *updateOp) updateAliasFromNode(ctx context.Context, sync *NodeSync, node *v1.Node) error {
  236. if sync.mode != SyncFromCluster {
  237. sync.kubeAPI.EmitNodeWarningEvent(
  238. node.Name, InvalidModeEvent, "Cannot sync to cloud in mode %q", sync.mode)
  239. return fmt.Errorf("cannot sync to cloud in mode %q", sync.mode)
  240. }
  241. _, aliasRange, err := net.ParseCIDR(node.Spec.PodCIDR)
  242. if err != nil {
  243. klog.Errorf("Could not parse PodCIDR (%q) for node %q: %v",
  244. node.Spec.PodCIDR, node.Name, err)
  245. return err
  246. }
  247. if err := sync.set.Occupy(aliasRange); err != nil {
  248. klog.Errorf("Error occupying range %v for node %v", aliasRange, sync.nodeName)
  249. return err
  250. }
  251. if err := sync.cloudAlias.AddAlias(ctx, node.Name, aliasRange); err != nil {
  252. klog.Errorf("Could not add alias %v for node %q: %v", aliasRange, node.Name, err)
  253. return err
  254. }
  255. if err := sync.kubeAPI.UpdateNodeNetworkUnavailable(node.Name, false); err != nil {
  256. klog.Errorf("Could not update node NetworkUnavailable status to false: %v", err)
  257. return err
  258. }
  259. klog.V(2).Infof("Updated node %q cloud alias with node spec, node.PodCIDR = %v",
  260. node.Name, node.Spec.PodCIDR)
  261. return nil
  262. }
  263. // allocateRange allocates a new range and updates both the cloud
  264. // platform and the node allocation.
  265. func (op *updateOp) allocateRange(ctx context.Context, sync *NodeSync, node *v1.Node) error {
  266. if sync.mode != SyncFromCluster {
  267. sync.kubeAPI.EmitNodeWarningEvent(node.Name, InvalidModeEvent,
  268. "Cannot allocate CIDRs in mode %q", sync.mode)
  269. return fmt.Errorf("controller cannot allocate CIDRS in mode %q", sync.mode)
  270. }
  271. cidrRange, err := sync.set.AllocateNext()
  272. if err != nil {
  273. return err
  274. }
  275. // If addAlias returns a hard error, cidrRange will be leaked as there
  276. // is no durable record of the range. The missing space will be
  277. // recovered on the next restart of the controller.
  278. if err := sync.cloudAlias.AddAlias(ctx, node.Name, cidrRange); err != nil {
  279. klog.Errorf("Could not add alias %v for node %q: %v", cidrRange, node.Name, err)
  280. return err
  281. }
  282. if err := sync.kubeAPI.UpdateNodePodCIDR(ctx, node, cidrRange); err != nil {
  283. klog.Errorf("Could not update node %q PodCIDR to %v: %v", node.Name, cidrRange, err)
  284. return err
  285. }
  286. if err := sync.kubeAPI.UpdateNodeNetworkUnavailable(node.Name, false); err != nil {
  287. klog.Errorf("Could not update node NetworkUnavailable status to false: %v", err)
  288. return err
  289. }
  290. klog.V(2).Infof("Allocated PodCIDR %v for node %q", cidrRange, node.Name)
  291. return nil
  292. }
  293. // deleteOp handles deletion of a node.
  294. type deleteOp struct {
  295. node *v1.Node
  296. }
  297. func (op *deleteOp) String() string {
  298. if op.node == nil {
  299. return fmt.Sprintf("deleteOp(nil)")
  300. }
  301. return fmt.Sprintf("deleteOp(%q,%v)", op.node.Name, op.node.Spec.PodCIDR)
  302. }
  303. func (op *deleteOp) run(sync *NodeSync) error {
  304. klog.V(3).Infof("Running deleteOp %+v", op)
  305. if op.node.Spec.PodCIDR == "" {
  306. klog.V(2).Infof("Node %q was deleted, node had no PodCIDR range assigned", op.node.Name)
  307. return nil
  308. }
  309. _, cidrRange, err := net.ParseCIDR(op.node.Spec.PodCIDR)
  310. if err != nil {
  311. klog.Errorf("Deleted node %q has an invalid podCIDR %q: %v",
  312. op.node.Name, op.node.Spec.PodCIDR, err)
  313. sync.kubeAPI.EmitNodeWarningEvent(op.node.Name, InvalidPodCIDR,
  314. "Node %q has an invalid PodCIDR: %q", op.node.Name, op.node.Spec.PodCIDR)
  315. return nil
  316. }
  317. sync.set.Release(cidrRange)
  318. klog.V(2).Infof("Node %q was deleted, releasing CIDR range %v",
  319. op.node.Name, op.node.Spec.PodCIDR)
  320. return nil
  321. }