placement_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. Copyright 2016 The Rook Authors. All rights reserved.
  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 v1
  14. import (
  15. "encoding/json"
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. v1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/util/yaml"
  21. )
  22. func TestPlacementSpec(t *testing.T) {
  23. specYaml := []byte(`
  24. nodeAffinity:
  25. requiredDuringSchedulingIgnoredDuringExecution:
  26. nodeSelectorTerms:
  27. - matchExpressions:
  28. - key: foo
  29. operator: In
  30. values:
  31. - bar
  32. tolerations:
  33. - key: foo
  34. operator: Exists
  35. topologySpreadConstraints:
  36. - maxSkew: 1
  37. topologyKey: zone
  38. whenUnsatisfiable: DoNotSchedule
  39. labelSelector:
  40. matchLabels:
  41. foo: bar`)
  42. // convert the raw spec yaml into JSON
  43. rawJSON, err := yaml.ToJSON(specYaml)
  44. assert.Nil(t, err)
  45. // unmarshal the JSON into a strongly typed placement spec object
  46. var placement Placement
  47. err = json.Unmarshal(rawJSON, &placement)
  48. assert.Nil(t, err)
  49. // the unmarshalled placement spec should equal the expected spec below
  50. expected := Placement{
  51. NodeAffinity: &v1.NodeAffinity{
  52. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  53. NodeSelectorTerms: []v1.NodeSelectorTerm{
  54. {
  55. MatchExpressions: []v1.NodeSelectorRequirement{
  56. {
  57. Key: "foo",
  58. Operator: v1.NodeSelectorOpIn,
  59. Values: []string{"bar"},
  60. },
  61. },
  62. },
  63. },
  64. },
  65. },
  66. Tolerations: []v1.Toleration{
  67. {
  68. Key: "foo",
  69. Operator: v1.TolerationOpExists,
  70. },
  71. },
  72. TopologySpreadConstraints: []v1.TopologySpreadConstraint{
  73. {
  74. MaxSkew: 1,
  75. TopologyKey: "zone",
  76. WhenUnsatisfiable: "DoNotSchedule",
  77. LabelSelector: &metav1.LabelSelector{
  78. MatchLabels: map[string]string{"foo": "bar"},
  79. },
  80. },
  81. },
  82. }
  83. assert.Equal(t, expected, placement)
  84. }
  85. func TestMergeNodeAffinity(t *testing.T) {
  86. // affinity is nil
  87. p := Placement{}
  88. result := p.mergeNodeAffinity(nil)
  89. assert.Nil(t, result)
  90. // node affinity is only set on the placement and should remain unchanged
  91. p.NodeAffinity = placementTestGenerateNodeAffinity()
  92. result = p.mergeNodeAffinity(nil)
  93. assert.Equal(t, p.NodeAffinity, result)
  94. // preferred set, but required not set
  95. affinityToMerge := placementTestGenerateNodeAffinity()
  96. affinityToMerge.RequiredDuringSchedulingIgnoredDuringExecution = nil
  97. result = p.mergeNodeAffinity(affinityToMerge)
  98. assert.Equal(t, 2, len(result.PreferredDuringSchedulingIgnoredDuringExecution))
  99. assert.Equal(t, p.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution, result.RequiredDuringSchedulingIgnoredDuringExecution)
  100. // preferred and required expressions set
  101. affinityToMerge = placementTestGenerateNodeAffinity()
  102. affinityToMerge.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[0].MatchExpressions[0].Key = "baz"
  103. result = p.mergeNodeAffinity(affinityToMerge)
  104. assert.Equal(t, 2, len(result.PreferredDuringSchedulingIgnoredDuringExecution))
  105. assert.Equal(t, 2, len(result.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[0].MatchExpressions))
  106. assert.Equal(t, "baz", result.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[0].MatchExpressions[0].Key)
  107. assert.Equal(t, "foo", result.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms[0].MatchExpressions[1].Key)
  108. }
  109. func TestPlacementApplyToPodSpec(t *testing.T) {
  110. to := placementTestGetTolerations("foo", "bar")
  111. na := placementTestGenerateNodeAffinity()
  112. antiaffinity := placementAntiAffinity("v1")
  113. tc := placementTestGetTopologySpreadConstraints("zone")
  114. expected := &v1.PodSpec{
  115. Affinity: &v1.Affinity{NodeAffinity: na, PodAntiAffinity: antiaffinity},
  116. Tolerations: to,
  117. TopologySpreadConstraints: tc,
  118. }
  119. var p Placement
  120. var ps *v1.PodSpec
  121. p = Placement{
  122. NodeAffinity: na,
  123. Tolerations: to,
  124. PodAntiAffinity: antiaffinity,
  125. TopologySpreadConstraints: tc,
  126. }
  127. ps = &v1.PodSpec{}
  128. p.ApplyToPodSpec(ps)
  129. assert.Equal(t, expected, ps)
  130. assert.Equal(t, 1, len(ps.Affinity.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution))
  131. // Appending some other antiaffinity to the pod spec should not alter the original placement antiaffinity
  132. otherAntiAffinity := placementAntiAffinity("v2")
  133. ps.Affinity.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution = append(
  134. ps.Affinity.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution,
  135. otherAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution...)
  136. assert.Equal(t, 1, len(antiaffinity.PreferredDuringSchedulingIgnoredDuringExecution))
  137. // partial update
  138. p = Placement{NodeAffinity: na, PodAntiAffinity: antiaffinity}
  139. ps = &v1.PodSpec{Tolerations: to, TopologySpreadConstraints: tc}
  140. p.ApplyToPodSpec(ps)
  141. assert.Equal(t, expected, ps)
  142. // overridden attributes
  143. p = Placement{
  144. NodeAffinity: na,
  145. PodAntiAffinity: antiaffinity,
  146. Tolerations: to,
  147. TopologySpreadConstraints: tc,
  148. }
  149. ps = &v1.PodSpec{
  150. TopologySpreadConstraints: placementTestGetTopologySpreadConstraints("rack"),
  151. }
  152. p.ApplyToPodSpec(ps)
  153. assert.Equal(t, expected, ps)
  154. // The preferred affinity is merged from both sources to result in two node affinities
  155. p = Placement{NodeAffinity: na, PodAntiAffinity: antiaffinity}
  156. nap := placementTestGenerateNodeAffinity()
  157. nap.PreferredDuringSchedulingIgnoredDuringExecution[0].Weight = 5
  158. ps = &v1.PodSpec{
  159. Affinity: &v1.Affinity{NodeAffinity: nap},
  160. Tolerations: to,
  161. TopologySpreadConstraints: tc,
  162. }
  163. p.ApplyToPodSpec(ps)
  164. assert.Equal(t, 2, len(ps.Affinity.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution))
  165. p = Placement{NodeAffinity: na, PodAntiAffinity: antiaffinity}
  166. to = placementTestGetTolerations("foo", "bar")
  167. ps = &v1.PodSpec{
  168. Tolerations: to,
  169. }
  170. p.ApplyToPodSpec(ps)
  171. assert.Equal(t, 1, len(ps.Tolerations))
  172. p = Placement{Tolerations: to, NodeAffinity: na, PodAntiAffinity: antiaffinity}
  173. p.ApplyToPodSpec(ps)
  174. assert.Equal(t, 2, len(ps.Tolerations))
  175. }
  176. func TestPlacementMerge(t *testing.T) {
  177. to := placementTestGetTolerations("foo", "bar")
  178. na := placementTestGenerateNodeAffinity()
  179. tc := placementTestGetTopologySpreadConstraints("zone")
  180. var original, with, expected, merged Placement
  181. original = Placement{}
  182. with = Placement{Tolerations: to}
  183. expected = Placement{Tolerations: to}
  184. merged = original.Merge(with)
  185. assert.Equal(t, expected, merged)
  186. original = Placement{NodeAffinity: na}
  187. with = Placement{Tolerations: to}
  188. expected = Placement{NodeAffinity: na, Tolerations: to}
  189. merged = original.Merge(with)
  190. assert.Equal(t, expected, merged)
  191. original = Placement{}
  192. with = Placement{TopologySpreadConstraints: tc}
  193. expected = Placement{TopologySpreadConstraints: tc}
  194. merged = original.Merge(with)
  195. assert.Equal(t, expected, merged)
  196. original = Placement{
  197. Tolerations: placementTestGetTolerations("bar", "baz"),
  198. TopologySpreadConstraints: placementTestGetTopologySpreadConstraints("rack"),
  199. }
  200. with = Placement{
  201. NodeAffinity: na,
  202. Tolerations: to,
  203. TopologySpreadConstraints: tc,
  204. }
  205. var ts int64 = 10
  206. expected = Placement{
  207. NodeAffinity: na,
  208. Tolerations: []v1.Toleration{
  209. {
  210. Key: "bar",
  211. Operator: v1.TolerationOpExists,
  212. Value: "baz",
  213. Effect: v1.TaintEffectNoSchedule,
  214. TolerationSeconds: &ts,
  215. },
  216. {
  217. Key: "foo",
  218. Operator: v1.TolerationOpExists,
  219. Value: "bar",
  220. Effect: v1.TaintEffectNoSchedule,
  221. TolerationSeconds: &ts,
  222. },
  223. },
  224. TopologySpreadConstraints: tc,
  225. }
  226. merged = original.Merge(with)
  227. assert.Equal(t, expected, merged)
  228. }
  229. func placementTestGetTolerations(key, value string) []v1.Toleration {
  230. var ts int64 = 10
  231. return []v1.Toleration{
  232. {
  233. Key: key,
  234. Operator: v1.TolerationOpExists,
  235. Value: value,
  236. Effect: v1.TaintEffectNoSchedule,
  237. TolerationSeconds: &ts,
  238. },
  239. }
  240. }
  241. func placementTestGetTopologySpreadConstraints(topologyKey string) []v1.TopologySpreadConstraint {
  242. return []v1.TopologySpreadConstraint{
  243. {
  244. MaxSkew: 1,
  245. TopologyKey: topologyKey,
  246. WhenUnsatisfiable: "DoNotSchedule",
  247. LabelSelector: &metav1.LabelSelector{
  248. MatchLabels: map[string]string{"foo": "bar"},
  249. },
  250. },
  251. }
  252. }
  253. func placementAntiAffinity(value string) *v1.PodAntiAffinity {
  254. return &v1.PodAntiAffinity{
  255. PreferredDuringSchedulingIgnoredDuringExecution: []v1.WeightedPodAffinityTerm{
  256. {
  257. Weight: 50,
  258. PodAffinityTerm: v1.PodAffinityTerm{
  259. LabelSelector: &metav1.LabelSelector{
  260. MatchLabels: map[string]string{
  261. "app": value,
  262. },
  263. },
  264. TopologyKey: v1.LabelHostname,
  265. },
  266. },
  267. },
  268. }
  269. }
  270. func placementTestGenerateNodeAffinity() *v1.NodeAffinity {
  271. return &v1.NodeAffinity{
  272. RequiredDuringSchedulingIgnoredDuringExecution: &v1.NodeSelector{
  273. NodeSelectorTerms: []v1.NodeSelectorTerm{
  274. {
  275. MatchExpressions: []v1.NodeSelectorRequirement{
  276. {
  277. Key: "foo",
  278. Operator: v1.NodeSelectorOpExists,
  279. Values: []string{"bar"},
  280. },
  281. },
  282. },
  283. },
  284. },
  285. PreferredDuringSchedulingIgnoredDuringExecution: []v1.PreferredSchedulingTerm{
  286. {
  287. Weight: 10,
  288. Preference: v1.NodeSelectorTerm{
  289. MatchExpressions: []v1.NodeSelectorRequirement{
  290. {
  291. Key: "foo",
  292. Operator: v1.NodeSelectorOpExists,
  293. Values: []string{"bar"},
  294. },
  295. },
  296. },
  297. },
  298. },
  299. }
  300. }
  301. func TestMergeToleration(t *testing.T) {
  302. // placement is nil
  303. p := Placement{}
  304. result := p.mergeTolerations(nil)
  305. assert.Nil(t, result)
  306. placementToleration := []v1.Toleration{
  307. {
  308. Key: "foo",
  309. Operator: v1.TolerationOpEqual,
  310. },
  311. }
  312. p.Tolerations = placementToleration
  313. result = p.mergeTolerations(nil)
  314. assert.Equal(t, p.Tolerations, result)
  315. newToleration := []v1.Toleration{
  316. {
  317. Key: "new",
  318. Operator: v1.TolerationOpExists,
  319. },
  320. }
  321. result = p.mergeTolerations(newToleration)
  322. assert.Equal(t, 2, len(result))
  323. assert.Equal(t, placementToleration[0].Key, result[0].Key)
  324. assert.Equal(t, newToleration[0].Key, result[1].Key)
  325. }