node_controller_test.go 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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 cloud
  14. import (
  15. "errors"
  16. "testing"
  17. "time"
  18. "k8s.io/api/core/v1"
  19. "k8s.io/client-go/kubernetes/fake"
  20. "k8s.io/client-go/kubernetes/scheme"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/types"
  23. "k8s.io/client-go/informers"
  24. "k8s.io/client-go/tools/record"
  25. cloudprovider "k8s.io/cloud-provider"
  26. fakecloud "k8s.io/cloud-provider/fake"
  27. "k8s.io/kubernetes/pkg/controller"
  28. "k8s.io/kubernetes/pkg/controller/testutil"
  29. kubeletapis "k8s.io/kubernetes/pkg/kubelet/apis"
  30. schedulerapi "k8s.io/kubernetes/pkg/scheduler/api"
  31. "github.com/stretchr/testify/assert"
  32. "k8s.io/klog"
  33. )
  34. func TestEnsureNodeExistsByProviderID(t *testing.T) {
  35. testCases := []struct {
  36. testName string
  37. node *v1.Node
  38. expectedCalls []string
  39. expectedNodeExists bool
  40. hasInstanceID bool
  41. existsByProviderID bool
  42. nodeNameErr error
  43. providerIDErr error
  44. }{
  45. {
  46. testName: "node exists by provider id",
  47. existsByProviderID: true,
  48. providerIDErr: nil,
  49. hasInstanceID: true,
  50. nodeNameErr: errors.New("unimplemented"),
  51. expectedCalls: []string{"instance-exists-by-provider-id"},
  52. expectedNodeExists: true,
  53. node: &v1.Node{
  54. ObjectMeta: metav1.ObjectMeta{
  55. Name: "node0",
  56. },
  57. Spec: v1.NodeSpec{
  58. ProviderID: "node0",
  59. },
  60. },
  61. },
  62. {
  63. testName: "does not exist by provider id",
  64. existsByProviderID: false,
  65. providerIDErr: nil,
  66. hasInstanceID: true,
  67. nodeNameErr: errors.New("unimplemented"),
  68. expectedCalls: []string{"instance-exists-by-provider-id"},
  69. expectedNodeExists: false,
  70. node: &v1.Node{
  71. ObjectMeta: metav1.ObjectMeta{
  72. Name: "node0",
  73. },
  74. Spec: v1.NodeSpec{
  75. ProviderID: "node0",
  76. },
  77. },
  78. },
  79. {
  80. testName: "exists by instance id",
  81. existsByProviderID: true,
  82. providerIDErr: nil,
  83. hasInstanceID: true,
  84. nodeNameErr: nil,
  85. expectedCalls: []string{"instance-id", "instance-exists-by-provider-id"},
  86. expectedNodeExists: true,
  87. node: &v1.Node{
  88. ObjectMeta: metav1.ObjectMeta{
  89. Name: "node0",
  90. },
  91. },
  92. },
  93. {
  94. testName: "does not exist by no instance id",
  95. existsByProviderID: true,
  96. providerIDErr: nil,
  97. hasInstanceID: false,
  98. nodeNameErr: cloudprovider.InstanceNotFound,
  99. expectedCalls: []string{"instance-id"},
  100. expectedNodeExists: false,
  101. node: &v1.Node{
  102. ObjectMeta: metav1.ObjectMeta{
  103. Name: "node0",
  104. },
  105. },
  106. },
  107. {
  108. testName: "provider id returns error",
  109. existsByProviderID: false,
  110. providerIDErr: errors.New("unimplemented"),
  111. hasInstanceID: true,
  112. nodeNameErr: cloudprovider.InstanceNotFound,
  113. expectedCalls: []string{"instance-exists-by-provider-id"},
  114. expectedNodeExists: false,
  115. node: &v1.Node{
  116. ObjectMeta: metav1.ObjectMeta{
  117. Name: "node0",
  118. },
  119. Spec: v1.NodeSpec{
  120. ProviderID: "node0",
  121. },
  122. },
  123. },
  124. }
  125. for _, tc := range testCases {
  126. t.Run(tc.testName, func(t *testing.T) {
  127. fc := &fakecloud.Cloud{
  128. ExistsByProviderID: tc.existsByProviderID,
  129. Err: tc.nodeNameErr,
  130. ErrByProviderID: tc.providerIDErr,
  131. }
  132. if tc.hasInstanceID {
  133. fc.ExtID = map[types.NodeName]string{
  134. types.NodeName(tc.node.Name): "provider-id://a",
  135. }
  136. }
  137. instances, _ := fc.Instances()
  138. exists, err := ensureNodeExistsByProviderID(instances, tc.node)
  139. assert.Equal(t, err, tc.providerIDErr)
  140. assert.EqualValues(t, tc.expectedCalls, fc.Calls,
  141. "expected cloud provider methods `%v` to be called but `%v` was called ",
  142. tc.expectedCalls, fc.Calls)
  143. assert.Equal(t, tc.expectedNodeExists, exists,
  144. "expected exists to be `%t` but got `%t`",
  145. tc.existsByProviderID, exists)
  146. })
  147. }
  148. }
  149. // This test checks that a node with the external cloud provider taint is cloudprovider initialized
  150. func TestNodeInitialized(t *testing.T) {
  151. fnh := &testutil.FakeNodeHandler{
  152. Existing: []*v1.Node{
  153. {
  154. ObjectMeta: metav1.ObjectMeta{
  155. Name: "node0",
  156. CreationTimestamp: metav1.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
  157. },
  158. Status: v1.NodeStatus{
  159. Conditions: []v1.NodeCondition{
  160. {
  161. Type: v1.NodeReady,
  162. Status: v1.ConditionUnknown,
  163. LastHeartbeatTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
  164. LastTransitionTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
  165. },
  166. },
  167. },
  168. Spec: v1.NodeSpec{
  169. Taints: []v1.Taint{
  170. {
  171. Key: schedulerapi.TaintExternalCloudProvider,
  172. Value: "true",
  173. Effect: v1.TaintEffectNoSchedule,
  174. },
  175. },
  176. },
  177. },
  178. },
  179. Clientset: fake.NewSimpleClientset(&v1.PodList{}),
  180. DeleteWaitChan: make(chan struct{}),
  181. }
  182. factory := informers.NewSharedInformerFactory(fnh, controller.NoResyncPeriodFunc())
  183. fakeCloud := &fakecloud.Cloud{
  184. InstanceTypes: map[types.NodeName]string{
  185. types.NodeName("node0"): "t1.micro",
  186. },
  187. Addresses: []v1.NodeAddress{
  188. {
  189. Type: v1.NodeHostName,
  190. Address: "node0.cloud.internal",
  191. },
  192. {
  193. Type: v1.NodeInternalIP,
  194. Address: "10.0.0.1",
  195. },
  196. {
  197. Type: v1.NodeExternalIP,
  198. Address: "132.143.154.163",
  199. },
  200. },
  201. Err: nil,
  202. }
  203. eventBroadcaster := record.NewBroadcaster()
  204. cloudNodeController := &CloudNodeController{
  205. kubeClient: fnh,
  206. nodeInformer: factory.Core().V1().Nodes(),
  207. cloud: fakeCloud,
  208. recorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "cloud-node-controller"}),
  209. nodeStatusUpdateFrequency: 1 * time.Second,
  210. }
  211. eventBroadcaster.StartLogging(klog.Infof)
  212. cloudNodeController.AddCloudNode(fnh.Existing[0])
  213. assert.Equal(t, 1, len(fnh.UpdatedNodes), "Node was not updated")
  214. assert.Equal(t, "node0", fnh.UpdatedNodes[0].Name, "Node was not updated")
  215. assert.Equal(t, 0, len(fnh.UpdatedNodes[0].Spec.Taints), "Node Taint was not removed after cloud init")
  216. }
  217. // This test checks that a node without the external cloud provider taint are NOT cloudprovider initialized
  218. func TestNodeIgnored(t *testing.T) {
  219. fnh := &testutil.FakeNodeHandler{
  220. Existing: []*v1.Node{
  221. {
  222. ObjectMeta: metav1.ObjectMeta{
  223. Name: "node0",
  224. CreationTimestamp: metav1.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
  225. },
  226. Status: v1.NodeStatus{
  227. Conditions: []v1.NodeCondition{
  228. {
  229. Type: v1.NodeReady,
  230. Status: v1.ConditionUnknown,
  231. LastHeartbeatTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
  232. LastTransitionTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
  233. },
  234. },
  235. },
  236. },
  237. },
  238. Clientset: fake.NewSimpleClientset(&v1.PodList{}),
  239. DeleteWaitChan: make(chan struct{}),
  240. }
  241. factory := informers.NewSharedInformerFactory(fnh, controller.NoResyncPeriodFunc())
  242. fakeCloud := &fakecloud.Cloud{
  243. InstanceTypes: map[types.NodeName]string{
  244. types.NodeName("node0"): "t1.micro",
  245. },
  246. Addresses: []v1.NodeAddress{
  247. {
  248. Type: v1.NodeHostName,
  249. Address: "node0.cloud.internal",
  250. },
  251. {
  252. Type: v1.NodeInternalIP,
  253. Address: "10.0.0.1",
  254. },
  255. {
  256. Type: v1.NodeExternalIP,
  257. Address: "132.143.154.163",
  258. },
  259. },
  260. Err: nil,
  261. }
  262. eventBroadcaster := record.NewBroadcaster()
  263. cloudNodeController := &CloudNodeController{
  264. kubeClient: fnh,
  265. nodeInformer: factory.Core().V1().Nodes(),
  266. cloud: fakeCloud,
  267. recorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "cloud-node-controller"}),
  268. }
  269. eventBroadcaster.StartLogging(klog.Infof)
  270. cloudNodeController.AddCloudNode(fnh.Existing[0])
  271. assert.Equal(t, 0, len(fnh.UpdatedNodes), "Node was wrongly updated")
  272. }
  273. // This test checks that a node with the external cloud provider taint is cloudprovider initialized and
  274. // the GCE route condition is added if cloudprovider is GCE
  275. func TestGCECondition(t *testing.T) {
  276. fnh := &testutil.FakeNodeHandler{
  277. Existing: []*v1.Node{
  278. {
  279. ObjectMeta: metav1.ObjectMeta{
  280. Name: "node0",
  281. CreationTimestamp: metav1.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
  282. },
  283. Status: v1.NodeStatus{
  284. Conditions: []v1.NodeCondition{
  285. {
  286. Type: v1.NodeReady,
  287. Status: v1.ConditionUnknown,
  288. LastHeartbeatTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
  289. LastTransitionTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
  290. },
  291. },
  292. },
  293. Spec: v1.NodeSpec{
  294. Taints: []v1.Taint{
  295. {
  296. Key: schedulerapi.TaintExternalCloudProvider,
  297. Value: "true",
  298. Effect: v1.TaintEffectNoSchedule,
  299. },
  300. },
  301. },
  302. },
  303. },
  304. Clientset: fake.NewSimpleClientset(&v1.PodList{}),
  305. DeleteWaitChan: make(chan struct{}),
  306. }
  307. factory := informers.NewSharedInformerFactory(fnh, controller.NoResyncPeriodFunc())
  308. fakeCloud := &fakecloud.Cloud{
  309. InstanceTypes: map[types.NodeName]string{
  310. types.NodeName("node0"): "t1.micro",
  311. },
  312. Addresses: []v1.NodeAddress{
  313. {
  314. Type: v1.NodeHostName,
  315. Address: "node0.cloud.internal",
  316. },
  317. {
  318. Type: v1.NodeInternalIP,
  319. Address: "10.0.0.1",
  320. },
  321. {
  322. Type: v1.NodeExternalIP,
  323. Address: "132.143.154.163",
  324. },
  325. },
  326. Provider: "gce",
  327. Err: nil,
  328. }
  329. eventBroadcaster := record.NewBroadcaster()
  330. cloudNodeController := &CloudNodeController{
  331. kubeClient: fnh,
  332. nodeInformer: factory.Core().V1().Nodes(),
  333. cloud: fakeCloud,
  334. recorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "cloud-node-controller"}),
  335. }
  336. eventBroadcaster.StartLogging(klog.Infof)
  337. cloudNodeController.AddCloudNode(fnh.Existing[0])
  338. assert.Equal(t, 1, len(fnh.UpdatedNodes), "Node was not updated")
  339. assert.Equal(t, "node0", fnh.UpdatedNodes[0].Name, "Node was not updated")
  340. assert.Equal(t, 2, len(fnh.UpdatedNodes[0].Status.Conditions), "No new conditions were added for GCE")
  341. conditionAdded := false
  342. for _, cond := range fnh.UpdatedNodes[0].Status.Conditions {
  343. if cond.Status == "True" && cond.Type == "NetworkUnavailable" && cond.Reason == "NoRouteCreated" {
  344. conditionAdded = true
  345. }
  346. }
  347. assert.True(t, conditionAdded, "Network Route Condition for GCE not added by external cloud initializer")
  348. }
  349. // This test checks that a node with the external cloud provider taint is cloudprovider initialized and
  350. // and that zone labels are added correctly
  351. func TestZoneInitialized(t *testing.T) {
  352. fnh := &testutil.FakeNodeHandler{
  353. Existing: []*v1.Node{
  354. {
  355. ObjectMeta: metav1.ObjectMeta{
  356. Name: "node0",
  357. CreationTimestamp: metav1.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
  358. Labels: map[string]string{},
  359. },
  360. Status: v1.NodeStatus{
  361. Conditions: []v1.NodeCondition{
  362. {
  363. Type: v1.NodeReady,
  364. Status: v1.ConditionUnknown,
  365. LastHeartbeatTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
  366. LastTransitionTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
  367. },
  368. },
  369. },
  370. Spec: v1.NodeSpec{
  371. Taints: []v1.Taint{
  372. {
  373. Key: schedulerapi.TaintExternalCloudProvider,
  374. Value: "true",
  375. Effect: v1.TaintEffectNoSchedule,
  376. },
  377. },
  378. },
  379. },
  380. },
  381. Clientset: fake.NewSimpleClientset(&v1.PodList{}),
  382. DeleteWaitChan: make(chan struct{}),
  383. }
  384. factory := informers.NewSharedInformerFactory(fnh, controller.NoResyncPeriodFunc())
  385. fakeCloud := &fakecloud.Cloud{
  386. InstanceTypes: map[types.NodeName]string{
  387. types.NodeName("node0"): "t1.micro",
  388. },
  389. Addresses: []v1.NodeAddress{
  390. {
  391. Type: v1.NodeHostName,
  392. Address: "node0.cloud.internal",
  393. },
  394. {
  395. Type: v1.NodeInternalIP,
  396. Address: "10.0.0.1",
  397. },
  398. {
  399. Type: v1.NodeExternalIP,
  400. Address: "132.143.154.163",
  401. },
  402. },
  403. Provider: "aws",
  404. Zone: cloudprovider.Zone{
  405. FailureDomain: "us-west-1a",
  406. Region: "us-west",
  407. },
  408. Err: nil,
  409. }
  410. eventBroadcaster := record.NewBroadcaster()
  411. cloudNodeController := &CloudNodeController{
  412. kubeClient: fnh,
  413. nodeInformer: factory.Core().V1().Nodes(),
  414. cloud: fakeCloud,
  415. recorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "cloud-node-controller"}),
  416. }
  417. eventBroadcaster.StartLogging(klog.Infof)
  418. cloudNodeController.AddCloudNode(fnh.Existing[0])
  419. assert.Equal(t, 1, len(fnh.UpdatedNodes), "Node was not updated")
  420. assert.Equal(t, "node0", fnh.UpdatedNodes[0].Name, "Node was not updated")
  421. assert.Equal(t, 2, len(fnh.UpdatedNodes[0].ObjectMeta.Labels),
  422. "Node label for Region and Zone were not set")
  423. assert.Equal(t, "us-west", fnh.UpdatedNodes[0].ObjectMeta.Labels[v1.LabelZoneRegion],
  424. "Node Region not correctly updated")
  425. assert.Equal(t, "us-west-1a", fnh.UpdatedNodes[0].ObjectMeta.Labels[v1.LabelZoneFailureDomain],
  426. "Node FailureDomain not correctly updated")
  427. }
  428. // This test checks that a node with the external cloud provider taint is cloudprovider initialized and
  429. // and nodeAddresses are updated from the cloudprovider
  430. func TestNodeAddresses(t *testing.T) {
  431. fnh := &testutil.FakeNodeHandler{
  432. Existing: []*v1.Node{
  433. {
  434. ObjectMeta: metav1.ObjectMeta{
  435. Name: "node0",
  436. CreationTimestamp: metav1.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
  437. Labels: map[string]string{},
  438. },
  439. Status: v1.NodeStatus{
  440. Conditions: []v1.NodeCondition{
  441. {
  442. Type: v1.NodeReady,
  443. Status: v1.ConditionUnknown,
  444. LastHeartbeatTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
  445. LastTransitionTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
  446. },
  447. },
  448. },
  449. Spec: v1.NodeSpec{
  450. Taints: []v1.Taint{
  451. {
  452. Key: "ImproveCoverageTaint",
  453. Value: "true",
  454. Effect: v1.TaintEffectNoSchedule,
  455. },
  456. {
  457. Key: schedulerapi.TaintExternalCloudProvider,
  458. Value: "true",
  459. Effect: v1.TaintEffectNoSchedule,
  460. },
  461. },
  462. },
  463. },
  464. },
  465. Clientset: fake.NewSimpleClientset(&v1.PodList{}),
  466. DeleteWaitChan: make(chan struct{}),
  467. }
  468. factory := informers.NewSharedInformerFactory(fnh, controller.NoResyncPeriodFunc())
  469. fakeCloud := &fakecloud.Cloud{
  470. InstanceTypes: map[types.NodeName]string{},
  471. Addresses: []v1.NodeAddress{
  472. {
  473. Type: v1.NodeHostName,
  474. Address: "node0.cloud.internal",
  475. },
  476. {
  477. Type: v1.NodeInternalIP,
  478. Address: "10.0.0.1",
  479. },
  480. {
  481. Type: v1.NodeExternalIP,
  482. Address: "132.143.154.163",
  483. },
  484. },
  485. Provider: "aws",
  486. Zone: cloudprovider.Zone{
  487. FailureDomain: "us-west-1a",
  488. Region: "us-west",
  489. },
  490. ExistsByProviderID: true,
  491. Err: nil,
  492. }
  493. eventBroadcaster := record.NewBroadcaster()
  494. cloudNodeController := &CloudNodeController{
  495. kubeClient: fnh,
  496. nodeInformer: factory.Core().V1().Nodes(),
  497. cloud: fakeCloud,
  498. nodeStatusUpdateFrequency: 1 * time.Second,
  499. recorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "cloud-node-controller"}),
  500. }
  501. eventBroadcaster.StartLogging(klog.Infof)
  502. cloudNodeController.AddCloudNode(fnh.Existing[0])
  503. assert.Equal(t, 1, len(fnh.UpdatedNodes), "Node was not updated")
  504. assert.Equal(t, "node0", fnh.UpdatedNodes[0].Name, "Node was not updated")
  505. assert.Equal(t, 3, len(fnh.UpdatedNodes[0].Status.Addresses), "Node status not updated")
  506. fakeCloud.Addresses = []v1.NodeAddress{
  507. {
  508. Type: v1.NodeHostName,
  509. Address: "node0.cloud.internal",
  510. },
  511. {
  512. Type: v1.NodeInternalIP,
  513. Address: "10.0.0.1",
  514. },
  515. }
  516. cloudNodeController.UpdateNodeStatus()
  517. updatedNodes := fnh.GetUpdatedNodesCopy()
  518. assert.Equal(t, 2, len(updatedNodes[0].Status.Addresses), "Node Addresses not correctly updated")
  519. }
  520. // This test checks that a node with the external cloud provider taint is cloudprovider initialized and
  521. // and the provided node ip is validated with the cloudprovider and nodeAddresses are updated from the cloudprovider
  522. func TestNodeProvidedIPAddresses(t *testing.T) {
  523. fnh := &testutil.FakeNodeHandler{
  524. Existing: []*v1.Node{
  525. {
  526. ObjectMeta: metav1.ObjectMeta{
  527. Name: "node0",
  528. CreationTimestamp: metav1.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
  529. Labels: map[string]string{},
  530. Annotations: map[string]string{
  531. kubeletapis.AnnotationProvidedIPAddr: "10.0.0.1",
  532. },
  533. },
  534. Status: v1.NodeStatus{
  535. Conditions: []v1.NodeCondition{
  536. {
  537. Type: v1.NodeReady,
  538. Status: v1.ConditionUnknown,
  539. LastHeartbeatTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
  540. LastTransitionTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
  541. },
  542. },
  543. Addresses: []v1.NodeAddress{
  544. {
  545. Type: v1.NodeHostName,
  546. Address: "node0.cloud.internal",
  547. },
  548. },
  549. },
  550. Spec: v1.NodeSpec{
  551. Taints: []v1.Taint{
  552. {
  553. Key: "ImproveCoverageTaint",
  554. Value: "true",
  555. Effect: v1.TaintEffectNoSchedule,
  556. },
  557. {
  558. Key: schedulerapi.TaintExternalCloudProvider,
  559. Value: "true",
  560. Effect: v1.TaintEffectNoSchedule,
  561. },
  562. },
  563. ProviderID: "node0.aws.12345",
  564. },
  565. },
  566. },
  567. Clientset: fake.NewSimpleClientset(&v1.PodList{}),
  568. DeleteWaitChan: make(chan struct{}),
  569. }
  570. factory := informers.NewSharedInformerFactory(fnh, controller.NoResyncPeriodFunc())
  571. fakeCloud := &fakecloud.Cloud{
  572. InstanceTypes: map[types.NodeName]string{
  573. types.NodeName("node0"): "t1.micro",
  574. types.NodeName("node0.aws.12345"): "t2.macro",
  575. },
  576. Addresses: []v1.NodeAddress{
  577. {
  578. Type: v1.NodeInternalIP,
  579. Address: "10.0.0.1",
  580. },
  581. {
  582. Type: v1.NodeExternalIP,
  583. Address: "132.143.154.163",
  584. },
  585. },
  586. Provider: "aws",
  587. Zone: cloudprovider.Zone{
  588. FailureDomain: "us-west-1a",
  589. Region: "us-west",
  590. },
  591. ExistsByProviderID: true,
  592. Err: nil,
  593. }
  594. eventBroadcaster := record.NewBroadcaster()
  595. cloudNodeController := &CloudNodeController{
  596. kubeClient: fnh,
  597. nodeInformer: factory.Core().V1().Nodes(),
  598. cloud: fakeCloud,
  599. nodeStatusUpdateFrequency: 1 * time.Second,
  600. recorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "cloud-node-controller"}),
  601. }
  602. eventBroadcaster.StartLogging(klog.Infof)
  603. cloudNodeController.AddCloudNode(fnh.Existing[0])
  604. assert.Equal(t, 1, len(fnh.UpdatedNodes), "Node was not updated")
  605. assert.Equal(t, "node0", fnh.UpdatedNodes[0].Name, "Node was not updated")
  606. assert.Equal(t, 3, len(fnh.UpdatedNodes[0].Status.Addresses), "Node status unexpectedly updated")
  607. cloudNodeController.UpdateNodeStatus()
  608. updatedNodes := fnh.GetUpdatedNodesCopy()
  609. assert.Equal(t, 3, len(updatedNodes[0].Status.Addresses), "Node Addresses not correctly updated")
  610. assert.Equal(t, "10.0.0.1", updatedNodes[0].Status.Addresses[0].Address, "Node Addresses not correctly updated")
  611. }
  612. // Tests that node address changes are detected correctly
  613. func TestNodeAddressesChangeDetected(t *testing.T) {
  614. addressSet1 := []v1.NodeAddress{
  615. {
  616. Type: v1.NodeInternalIP,
  617. Address: "10.0.0.1",
  618. },
  619. {
  620. Type: v1.NodeExternalIP,
  621. Address: "132.143.154.163",
  622. },
  623. }
  624. addressSet2 := []v1.NodeAddress{
  625. {
  626. Type: v1.NodeInternalIP,
  627. Address: "10.0.0.1",
  628. },
  629. {
  630. Type: v1.NodeExternalIP,
  631. Address: "132.143.154.163",
  632. },
  633. }
  634. assert.False(t, nodeAddressesChangeDetected(addressSet1, addressSet2),
  635. "Node address changes are not detected correctly")
  636. addressSet1 = []v1.NodeAddress{
  637. {
  638. Type: v1.NodeInternalIP,
  639. Address: "10.0.0.1",
  640. },
  641. {
  642. Type: v1.NodeExternalIP,
  643. Address: "132.143.154.164",
  644. },
  645. }
  646. addressSet2 = []v1.NodeAddress{
  647. {
  648. Type: v1.NodeInternalIP,
  649. Address: "10.0.0.1",
  650. },
  651. {
  652. Type: v1.NodeExternalIP,
  653. Address: "132.143.154.163",
  654. },
  655. }
  656. assert.True(t, nodeAddressesChangeDetected(addressSet1, addressSet2),
  657. "Node address changes are not detected correctly")
  658. addressSet1 = []v1.NodeAddress{
  659. {
  660. Type: v1.NodeInternalIP,
  661. Address: "10.0.0.1",
  662. },
  663. {
  664. Type: v1.NodeExternalIP,
  665. Address: "132.143.154.164",
  666. },
  667. {
  668. Type: v1.NodeHostName,
  669. Address: "hostname.zone.region.aws.test",
  670. },
  671. }
  672. addressSet2 = []v1.NodeAddress{
  673. {
  674. Type: v1.NodeInternalIP,
  675. Address: "10.0.0.1",
  676. },
  677. {
  678. Type: v1.NodeExternalIP,
  679. Address: "132.143.154.164",
  680. },
  681. }
  682. assert.True(t, nodeAddressesChangeDetected(addressSet1, addressSet2),
  683. "Node address changes are not detected correctly")
  684. addressSet1 = []v1.NodeAddress{
  685. {
  686. Type: v1.NodeInternalIP,
  687. Address: "10.0.0.1",
  688. },
  689. {
  690. Type: v1.NodeExternalIP,
  691. Address: "132.143.154.164",
  692. },
  693. }
  694. addressSet2 = []v1.NodeAddress{
  695. {
  696. Type: v1.NodeInternalIP,
  697. Address: "10.0.0.1",
  698. },
  699. {
  700. Type: v1.NodeExternalIP,
  701. Address: "132.143.154.164",
  702. },
  703. {
  704. Type: v1.NodeHostName,
  705. Address: "hostname.zone.region.aws.test",
  706. },
  707. }
  708. assert.True(t, nodeAddressesChangeDetected(addressSet1, addressSet2),
  709. "Node address changes are not detected correctly")
  710. addressSet1 = []v1.NodeAddress{
  711. {
  712. Type: v1.NodeExternalIP,
  713. Address: "10.0.0.1",
  714. },
  715. {
  716. Type: v1.NodeInternalIP,
  717. Address: "132.143.154.163",
  718. },
  719. }
  720. addressSet2 = []v1.NodeAddress{
  721. {
  722. Type: v1.NodeInternalIP,
  723. Address: "10.0.0.1",
  724. },
  725. {
  726. Type: v1.NodeExternalIP,
  727. Address: "132.143.154.163",
  728. },
  729. }
  730. assert.True(t, nodeAddressesChangeDetected(addressSet1, addressSet2),
  731. "Node address changes are not detected correctly")
  732. }
  733. // This test checks that a node with the external cloud provider taint is cloudprovider initialized and
  734. // and node addresses will not be updated when node isn't present according to the cloudprovider
  735. func TestNodeAddressesNotUpdate(t *testing.T) {
  736. fnh := &testutil.FakeNodeHandler{
  737. Existing: []*v1.Node{
  738. {
  739. ObjectMeta: metav1.ObjectMeta{
  740. Name: "node0",
  741. CreationTimestamp: metav1.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
  742. Labels: map[string]string{},
  743. },
  744. Status: v1.NodeStatus{
  745. Conditions: []v1.NodeCondition{
  746. {
  747. Type: v1.NodeReady,
  748. Status: v1.ConditionUnknown,
  749. LastHeartbeatTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
  750. LastTransitionTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
  751. },
  752. },
  753. },
  754. Spec: v1.NodeSpec{
  755. Taints: []v1.Taint{
  756. {
  757. Key: "ImproveCoverageTaint",
  758. Value: "true",
  759. Effect: v1.TaintEffectNoSchedule,
  760. },
  761. },
  762. },
  763. },
  764. },
  765. }
  766. factory := informers.NewSharedInformerFactory(fnh, controller.NoResyncPeriodFunc())
  767. fakeCloud := &fakecloud.Cloud{
  768. InstanceTypes: map[types.NodeName]string{},
  769. Addresses: []v1.NodeAddress{
  770. {
  771. Type: v1.NodeHostName,
  772. Address: "node0.cloud.internal",
  773. },
  774. {
  775. Type: v1.NodeInternalIP,
  776. Address: "10.0.0.1",
  777. },
  778. {
  779. Type: v1.NodeExternalIP,
  780. Address: "132.143.154.163",
  781. },
  782. },
  783. ExistsByProviderID: false,
  784. Err: nil,
  785. }
  786. cloudNodeController := &CloudNodeController{
  787. kubeClient: fnh,
  788. nodeInformer: factory.Core().V1().Nodes(),
  789. cloud: fakeCloud,
  790. }
  791. cloudNodeController.updateNodeAddress(fnh.Existing[0], fakeCloud)
  792. if len(fnh.UpdatedNodes) != 0 {
  793. t.Errorf("Node was not correctly updated, the updated len(nodes) got: %v, wanted=0", len(fnh.UpdatedNodes))
  794. }
  795. }
  796. // This test checks that a node is set with the correct providerID
  797. func TestNodeProviderID(t *testing.T) {
  798. fnh := &testutil.FakeNodeHandler{
  799. Existing: []*v1.Node{
  800. {
  801. ObjectMeta: metav1.ObjectMeta{
  802. Name: "node0",
  803. CreationTimestamp: metav1.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
  804. Labels: map[string]string{},
  805. },
  806. Status: v1.NodeStatus{
  807. Conditions: []v1.NodeCondition{
  808. {
  809. Type: v1.NodeReady,
  810. Status: v1.ConditionUnknown,
  811. LastHeartbeatTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
  812. LastTransitionTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
  813. },
  814. },
  815. },
  816. Spec: v1.NodeSpec{
  817. Taints: []v1.Taint{
  818. {
  819. Key: "ImproveCoverageTaint",
  820. Value: "true",
  821. Effect: v1.TaintEffectNoSchedule,
  822. },
  823. {
  824. Key: schedulerapi.TaintExternalCloudProvider,
  825. Value: "true",
  826. Effect: v1.TaintEffectNoSchedule,
  827. },
  828. },
  829. },
  830. },
  831. },
  832. Clientset: fake.NewSimpleClientset(&v1.PodList{}),
  833. DeleteWaitChan: make(chan struct{}),
  834. }
  835. factory := informers.NewSharedInformerFactory(fnh, controller.NoResyncPeriodFunc())
  836. fakeCloud := &fakecloud.Cloud{
  837. InstanceTypes: map[types.NodeName]string{},
  838. Addresses: []v1.NodeAddress{
  839. {
  840. Type: v1.NodeHostName,
  841. Address: "node0.cloud.internal",
  842. },
  843. {
  844. Type: v1.NodeInternalIP,
  845. Address: "10.0.0.1",
  846. },
  847. {
  848. Type: v1.NodeExternalIP,
  849. Address: "132.143.154.163",
  850. },
  851. },
  852. Provider: "test",
  853. ExtID: map[types.NodeName]string{
  854. types.NodeName("node0"): "12345",
  855. },
  856. Err: nil,
  857. }
  858. eventBroadcaster := record.NewBroadcaster()
  859. cloudNodeController := &CloudNodeController{
  860. kubeClient: fnh,
  861. nodeInformer: factory.Core().V1().Nodes(),
  862. cloud: fakeCloud,
  863. nodeStatusUpdateFrequency: 1 * time.Second,
  864. recorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "cloud-node-controller"}),
  865. }
  866. eventBroadcaster.StartLogging(klog.Infof)
  867. cloudNodeController.AddCloudNode(fnh.Existing[0])
  868. assert.Equal(t, 1, len(fnh.UpdatedNodes), "Node was not updated")
  869. assert.Equal(t, "node0", fnh.UpdatedNodes[0].Name, "Node was not updated")
  870. assert.Equal(t, "test://12345", fnh.UpdatedNodes[0].Spec.ProviderID, "Node ProviderID not set correctly")
  871. }
  872. // This test checks that a node's provider ID will not be overwritten
  873. func TestNodeProviderIDAlreadySet(t *testing.T) {
  874. fnh := &testutil.FakeNodeHandler{
  875. Existing: []*v1.Node{
  876. {
  877. ObjectMeta: metav1.ObjectMeta{
  878. Name: "node0",
  879. CreationTimestamp: metav1.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC),
  880. Labels: map[string]string{},
  881. },
  882. Status: v1.NodeStatus{
  883. Conditions: []v1.NodeCondition{
  884. {
  885. Type: v1.NodeReady,
  886. Status: v1.ConditionUnknown,
  887. LastHeartbeatTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
  888. LastTransitionTime: metav1.Date(2015, 1, 1, 12, 0, 0, 0, time.UTC),
  889. },
  890. },
  891. },
  892. Spec: v1.NodeSpec{
  893. ProviderID: "test-provider-id",
  894. Taints: []v1.Taint{
  895. {
  896. Key: "ImproveCoverageTaint",
  897. Value: "true",
  898. Effect: v1.TaintEffectNoSchedule,
  899. },
  900. {
  901. Key: schedulerapi.TaintExternalCloudProvider,
  902. Value: "true",
  903. Effect: v1.TaintEffectNoSchedule,
  904. },
  905. },
  906. },
  907. },
  908. },
  909. Clientset: fake.NewSimpleClientset(&v1.PodList{}),
  910. DeleteWaitChan: make(chan struct{}),
  911. }
  912. factory := informers.NewSharedInformerFactory(fnh, controller.NoResyncPeriodFunc())
  913. fakeCloud := &fakecloud.Cloud{
  914. InstanceTypes: map[types.NodeName]string{},
  915. Addresses: []v1.NodeAddress{
  916. {
  917. Type: v1.NodeHostName,
  918. Address: "node0.cloud.internal",
  919. },
  920. {
  921. Type: v1.NodeInternalIP,
  922. Address: "10.0.0.1",
  923. },
  924. {
  925. Type: v1.NodeExternalIP,
  926. Address: "132.143.154.163",
  927. },
  928. },
  929. Provider: "test",
  930. ExtID: map[types.NodeName]string{
  931. types.NodeName("node0"): "12345",
  932. },
  933. Err: nil,
  934. }
  935. eventBroadcaster := record.NewBroadcaster()
  936. cloudNodeController := &CloudNodeController{
  937. kubeClient: fnh,
  938. nodeInformer: factory.Core().V1().Nodes(),
  939. cloud: fakeCloud,
  940. nodeStatusUpdateFrequency: 1 * time.Second,
  941. recorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "cloud-node-controller"}),
  942. }
  943. eventBroadcaster.StartLogging(klog.Infof)
  944. cloudNodeController.AddCloudNode(fnh.Existing[0])
  945. assert.Equal(t, 1, len(fnh.UpdatedNodes), "Node was not updated")
  946. assert.Equal(t, "node0", fnh.UpdatedNodes[0].Name, "Node was not updated")
  947. // CCM node controller should not overwrite provider if it's already set
  948. assert.Equal(t, "test-provider-id", fnh.UpdatedNodes[0].Spec.ProviderID, "Node ProviderID not set correctly")
  949. }