wait_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. Copyright 2019 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. "errors"
  16. "testing"
  17. v1 "k8s.io/api/core/v1"
  18. apierrors "k8s.io/apimachinery/pkg/api/errors"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. "k8s.io/client-go/kubernetes/fake"
  22. k8stesting "k8s.io/client-go/testing"
  23. )
  24. // TestCheckReadyForTests specifically is concerned about the multi-node logic
  25. // since single node checks are in TestReadyForTests.
  26. func TestCheckReadyForTests(t *testing.T) {
  27. // This is a duplicate definition of the constant in pkg/controller/service/controller.go
  28. labelNodeRoleMaster := "node-role.kubernetes.io/master"
  29. fromVanillaNode := func(f func(*v1.Node)) v1.Node {
  30. vanillaNode := &v1.Node{
  31. ObjectMeta: metav1.ObjectMeta{Name: "test-node"},
  32. Status: v1.NodeStatus{
  33. Conditions: []v1.NodeCondition{
  34. {Type: v1.NodeReady, Status: v1.ConditionTrue},
  35. },
  36. },
  37. }
  38. f(vanillaNode)
  39. return *vanillaNode
  40. }
  41. tcs := []struct {
  42. desc string
  43. nonblockingTaints string
  44. allowedNotReadyNodes int
  45. nodes []v1.Node
  46. nodeListErr error
  47. expected bool
  48. expectedErr string
  49. }{
  50. {
  51. desc: "Vanilla node should pass",
  52. nodes: []v1.Node{
  53. fromVanillaNode(func(n *v1.Node) {}),
  54. },
  55. expected: true,
  56. }, {
  57. desc: "Default value for nonblocking taints tolerates master taint",
  58. nonblockingTaints: `node-role.kubernetes.io/master`,
  59. nodes: []v1.Node{
  60. fromVanillaNode(func(n *v1.Node) {
  61. n.Spec.Taints = []v1.Taint{{Key: labelNodeRoleMaster, Effect: v1.TaintEffectNoSchedule}}
  62. }),
  63. },
  64. expected: true,
  65. }, {
  66. desc: "Tainted node should fail if effect is TaintEffectNoExecute",
  67. nonblockingTaints: "bar",
  68. nodes: []v1.Node{
  69. fromVanillaNode(func(n *v1.Node) {
  70. n.Spec.Taints = []v1.Taint{{Key: "foo", Effect: v1.TaintEffectNoExecute}}
  71. })},
  72. expected: false,
  73. }, {
  74. desc: "Tainted node can be allowed via allowedNotReadyNodes",
  75. nonblockingTaints: "bar",
  76. allowedNotReadyNodes: 1,
  77. nodes: []v1.Node{
  78. fromVanillaNode(func(n *v1.Node) {
  79. n.Spec.Taints = []v1.Taint{{Key: "foo", Effect: v1.TaintEffectNoExecute}}
  80. })},
  81. expected: true,
  82. }, {
  83. desc: "Multi-node, all OK",
  84. nodes: []v1.Node{
  85. fromVanillaNode(func(n *v1.Node) {}),
  86. fromVanillaNode(func(n *v1.Node) {}),
  87. },
  88. expected: true,
  89. }, {
  90. desc: "Multi-node, single blocking node blocks",
  91. nodes: []v1.Node{
  92. fromVanillaNode(func(n *v1.Node) {}),
  93. fromVanillaNode(func(n *v1.Node) {
  94. n.Spec.Taints = []v1.Taint{{Key: "foo", Effect: v1.TaintEffectNoSchedule}}
  95. }),
  96. },
  97. expected: false,
  98. }, {
  99. desc: "Multi-node, single blocking node allowed via allowedNotReadyNodes",
  100. allowedNotReadyNodes: 1,
  101. nodes: []v1.Node{
  102. fromVanillaNode(func(n *v1.Node) {}),
  103. fromVanillaNode(func(n *v1.Node) {
  104. n.Spec.Taints = []v1.Taint{{Key: "foo", Effect: v1.TaintEffectNoSchedule}}
  105. }),
  106. },
  107. expected: true,
  108. }, {
  109. desc: "Multi-node, single blocking node allowed via nonblocking taint",
  110. nonblockingTaints: "foo",
  111. nodes: []v1.Node{
  112. fromVanillaNode(func(n *v1.Node) {}),
  113. fromVanillaNode(func(n *v1.Node) {
  114. n.Spec.Taints = []v1.Taint{{Key: "foo", Effect: v1.TaintEffectNoSchedule}}
  115. }),
  116. },
  117. expected: true,
  118. }, {
  119. desc: "Multi-node, both blocking nodes allowed via separate nonblocking taints",
  120. nonblockingTaints: "foo,bar",
  121. nodes: []v1.Node{
  122. fromVanillaNode(func(n *v1.Node) {}),
  123. fromVanillaNode(func(n *v1.Node) {
  124. n.Spec.Taints = []v1.Taint{{Key: "foo", Effect: v1.TaintEffectNoSchedule}}
  125. }),
  126. fromVanillaNode(func(n *v1.Node) {
  127. n.Spec.Taints = []v1.Taint{{Key: "bar", Effect: v1.TaintEffectNoSchedule}}
  128. }),
  129. },
  130. expected: true,
  131. }, {
  132. desc: "Multi-node, one blocking node allowed via nonblocking taints still blocked",
  133. nonblockingTaints: "foo,notbar",
  134. nodes: []v1.Node{
  135. fromVanillaNode(func(n *v1.Node) {}),
  136. fromVanillaNode(func(n *v1.Node) {
  137. n.Spec.Taints = []v1.Taint{{Key: "foo", Effect: v1.TaintEffectNoSchedule}}
  138. }),
  139. fromVanillaNode(func(n *v1.Node) {
  140. n.Spec.Taints = []v1.Taint{{Key: "bar", Effect: v1.TaintEffectNoSchedule}}
  141. }),
  142. },
  143. expected: false,
  144. }, {
  145. desc: "Errors from node list are reported",
  146. nodeListErr: errors.New("Forced error"),
  147. expected: false,
  148. expectedErr: "Forced error",
  149. }, {
  150. desc: "Retryable errors from node list are reported but still return false",
  151. nodeListErr: apierrors.NewTimeoutError("Retryable error", 10),
  152. expected: false,
  153. },
  154. }
  155. // Only determines some logging functionality; not relevant so set to a large value.
  156. testLargeClusterThreshold := 1000
  157. for _, tc := range tcs {
  158. t.Run(tc.desc, func(t *testing.T) {
  159. c := fake.NewSimpleClientset()
  160. c.PrependReactor("list", "nodes", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
  161. nodeList := &v1.NodeList{Items: tc.nodes}
  162. return true, nodeList, tc.nodeListErr
  163. })
  164. checkFunc := CheckReadyForTests(c, tc.nonblockingTaints, tc.allowedNotReadyNodes, testLargeClusterThreshold)
  165. out, err := checkFunc()
  166. if out != tc.expected {
  167. t.Errorf("Expected %v but got %v", tc.expected, out)
  168. }
  169. switch {
  170. case err == nil && len(tc.expectedErr) > 0:
  171. t.Errorf("Expected error %q nil", tc.expectedErr)
  172. case err != nil && err.Error() != tc.expectedErr:
  173. t.Errorf("Expected error %q but got %q", tc.expectedErr, err.Error())
  174. }
  175. })
  176. }
  177. }
  178. func TestReadyForTests(t *testing.T) {
  179. fromVanillaNode := func(f func(*v1.Node)) *v1.Node {
  180. vanillaNode := &v1.Node{
  181. ObjectMeta: metav1.ObjectMeta{Name: "test-node"},
  182. Status: v1.NodeStatus{
  183. Conditions: []v1.NodeCondition{
  184. {Type: v1.NodeReady, Status: v1.ConditionTrue},
  185. },
  186. },
  187. }
  188. f(vanillaNode)
  189. return vanillaNode
  190. }
  191. _ = fromVanillaNode
  192. tcs := []struct {
  193. desc string
  194. node *v1.Node
  195. nonblockingTaints string
  196. expected bool
  197. }{
  198. {
  199. desc: "Vanilla node should pass",
  200. node: fromVanillaNode(func(n *v1.Node) {
  201. }),
  202. expected: true,
  203. }, {
  204. desc: "Vanilla node should pass with non-applicable nonblocking taint",
  205. nonblockingTaints: "foo",
  206. node: fromVanillaNode(func(n *v1.Node) {
  207. }),
  208. expected: true,
  209. }, {
  210. desc: "Tainted node should pass if effect is TaintEffectPreferNoSchedule",
  211. node: fromVanillaNode(func(n *v1.Node) {
  212. n.Spec.Taints = []v1.Taint{{Key: "foo", Effect: v1.TaintEffectPreferNoSchedule}}
  213. }),
  214. expected: true,
  215. }, {
  216. desc: "Tainted node should fail if effect is TaintEffectNoExecute",
  217. node: fromVanillaNode(func(n *v1.Node) {
  218. n.Spec.Taints = []v1.Taint{{Key: "foo", Effect: v1.TaintEffectNoExecute}}
  219. }),
  220. expected: false,
  221. }, {
  222. desc: "Tainted node should fail",
  223. node: fromVanillaNode(func(n *v1.Node) {
  224. n.Spec.Taints = []v1.Taint{{Key: "foo", Effect: v1.TaintEffectNoSchedule}}
  225. }),
  226. expected: false,
  227. }, {
  228. desc: "Tainted node should pass if nonblocking",
  229. nonblockingTaints: "foo",
  230. node: fromVanillaNode(func(n *v1.Node) {
  231. n.Spec.Taints = []v1.Taint{{Key: "foo", Effect: v1.TaintEffectNoSchedule}}
  232. }),
  233. expected: true,
  234. }, {
  235. desc: "Node with network not ready fails",
  236. node: fromVanillaNode(func(n *v1.Node) {
  237. n.Status.Conditions = append(n.Status.Conditions,
  238. v1.NodeCondition{Type: v1.NodeNetworkUnavailable, Status: v1.ConditionTrue},
  239. )
  240. }),
  241. expected: false,
  242. }, {
  243. desc: "Node fails unless NodeReady status",
  244. node: fromVanillaNode(func(n *v1.Node) {
  245. n.Status.Conditions = []v1.NodeCondition{}
  246. }),
  247. expected: false,
  248. },
  249. }
  250. for _, tc := range tcs {
  251. t.Run(tc.desc, func(t *testing.T) {
  252. out := readyForTests(tc.node, tc.nonblockingTaints)
  253. if out != tc.expected {
  254. t.Errorf("Expected %v but got %v", tc.expected, out)
  255. }
  256. })
  257. }
  258. }