progress_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 deployment
  14. import (
  15. "math"
  16. "testing"
  17. "time"
  18. apps "k8s.io/api/apps/v1"
  19. "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/client-go/kubernetes/fake"
  22. "k8s.io/client-go/util/workqueue"
  23. "k8s.io/kubernetes/pkg/controller/deployment/util"
  24. )
  25. func newDeploymentStatus(replicas, updatedReplicas, availableReplicas int32) apps.DeploymentStatus {
  26. return apps.DeploymentStatus{
  27. Replicas: replicas,
  28. UpdatedReplicas: updatedReplicas,
  29. AvailableReplicas: availableReplicas,
  30. }
  31. }
  32. // assumes the retuned deployment is always observed - not needed to be tested here.
  33. func currentDeployment(pds *int32, replicas, statusReplicas, updatedReplicas, availableReplicas int32, conditions []apps.DeploymentCondition) *apps.Deployment {
  34. d := &apps.Deployment{
  35. ObjectMeta: metav1.ObjectMeta{
  36. Name: "progress-test",
  37. },
  38. Spec: apps.DeploymentSpec{
  39. ProgressDeadlineSeconds: pds,
  40. Replicas: &replicas,
  41. Strategy: apps.DeploymentStrategy{
  42. Type: apps.RecreateDeploymentStrategyType,
  43. },
  44. },
  45. Status: newDeploymentStatus(statusReplicas, updatedReplicas, availableReplicas),
  46. }
  47. d.Status.Conditions = conditions
  48. return d
  49. }
  50. // helper to create RS with given availableReplicas
  51. func newRSWithAvailable(name string, specReplicas, statusReplicas, availableReplicas int) *apps.ReplicaSet {
  52. rs := rs(name, specReplicas, nil, metav1.Time{})
  53. rs.Status = apps.ReplicaSetStatus{
  54. Replicas: int32(statusReplicas),
  55. AvailableReplicas: int32(availableReplicas),
  56. }
  57. return rs
  58. }
  59. func TestRequeueStuckDeployment(t *testing.T) {
  60. pds := int32(60)
  61. infinite := int32(math.MaxInt32)
  62. failed := []apps.DeploymentCondition{
  63. {
  64. Type: apps.DeploymentProgressing,
  65. Status: v1.ConditionFalse,
  66. Reason: util.TimedOutReason,
  67. },
  68. }
  69. stuck := []apps.DeploymentCondition{
  70. {
  71. Type: apps.DeploymentProgressing,
  72. Status: v1.ConditionTrue,
  73. LastUpdateTime: metav1.Date(2017, 2, 15, 18, 49, 00, 00, time.UTC),
  74. },
  75. }
  76. tests := []struct {
  77. name string
  78. d *apps.Deployment
  79. status apps.DeploymentStatus
  80. nowFn func() time.Time
  81. expected time.Duration
  82. }{
  83. {
  84. name: "nil progressDeadlineSeconds specified",
  85. d: currentDeployment(nil, 4, 3, 3, 2, nil),
  86. status: newDeploymentStatus(3, 3, 2),
  87. expected: time.Duration(-1),
  88. },
  89. {
  90. name: "infinite progressDeadlineSeconds specified",
  91. d: currentDeployment(&infinite, 4, 3, 3, 2, nil),
  92. status: newDeploymentStatus(3, 3, 2),
  93. expected: time.Duration(-1),
  94. },
  95. {
  96. name: "no progressing condition found",
  97. d: currentDeployment(&pds, 4, 3, 3, 2, nil),
  98. status: newDeploymentStatus(3, 3, 2),
  99. expected: time.Duration(-1),
  100. },
  101. {
  102. name: "complete deployment does not need to be requeued",
  103. d: currentDeployment(&pds, 3, 3, 3, 3, nil),
  104. status: newDeploymentStatus(3, 3, 3),
  105. expected: time.Duration(-1),
  106. },
  107. {
  108. name: "already failed deployment does not need to be requeued",
  109. d: currentDeployment(&pds, 3, 3, 3, 0, failed),
  110. status: newDeploymentStatus(3, 3, 0),
  111. expected: time.Duration(-1),
  112. },
  113. {
  114. name: "stuck deployment - 30s",
  115. d: currentDeployment(&pds, 3, 3, 3, 1, stuck),
  116. status: newDeploymentStatus(3, 3, 1),
  117. nowFn: func() time.Time { return metav1.Date(2017, 2, 15, 18, 49, 30, 00, time.UTC).Time },
  118. expected: 30 * time.Second,
  119. },
  120. {
  121. name: "stuck deployment - 1s",
  122. d: currentDeployment(&pds, 3, 3, 3, 1, stuck),
  123. status: newDeploymentStatus(3, 3, 1),
  124. nowFn: func() time.Time { return metav1.Date(2017, 2, 15, 18, 49, 59, 00, time.UTC).Time },
  125. expected: 1 * time.Second,
  126. },
  127. {
  128. name: "failed deployment - less than a second => now",
  129. d: currentDeployment(&pds, 3, 3, 3, 1, stuck),
  130. status: newDeploymentStatus(3, 3, 1),
  131. nowFn: func() time.Time { return metav1.Date(2017, 2, 15, 18, 49, 59, 1, time.UTC).Time },
  132. expected: time.Duration(0),
  133. },
  134. {
  135. name: "failed deployment - now",
  136. d: currentDeployment(&pds, 3, 3, 3, 1, stuck),
  137. status: newDeploymentStatus(3, 3, 1),
  138. nowFn: func() time.Time { return metav1.Date(2017, 2, 15, 18, 50, 00, 00, time.UTC).Time },
  139. expected: time.Duration(0),
  140. },
  141. {
  142. name: "failed deployment - 1s after deadline",
  143. d: currentDeployment(&pds, 3, 3, 3, 1, stuck),
  144. status: newDeploymentStatus(3, 3, 1),
  145. nowFn: func() time.Time { return metav1.Date(2017, 2, 15, 18, 50, 01, 00, time.UTC).Time },
  146. expected: time.Duration(0),
  147. },
  148. {
  149. name: "failed deployment - 60s after deadline",
  150. d: currentDeployment(&pds, 3, 3, 3, 1, stuck),
  151. status: newDeploymentStatus(3, 3, 1),
  152. nowFn: func() time.Time { return metav1.Date(2017, 2, 15, 18, 51, 00, 00, time.UTC).Time },
  153. expected: time.Duration(0),
  154. },
  155. }
  156. dc := &DeploymentController{
  157. queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "doesnt_matter"),
  158. }
  159. dc.enqueueDeployment = dc.enqueue
  160. for _, test := range tests {
  161. t.Run(test.name, func(t *testing.T) {
  162. if test.nowFn != nil {
  163. nowFn = test.nowFn
  164. }
  165. got := dc.requeueStuckDeployment(test.d, test.status)
  166. if got != test.expected {
  167. t.Errorf("%s: got duration: %v, expected duration: %v", test.name, got, test.expected)
  168. }
  169. })
  170. }
  171. }
  172. func TestSyncRolloutStatus(t *testing.T) {
  173. pds := int32(60)
  174. testTime := metav1.Date(2017, 2, 15, 18, 49, 00, 00, time.UTC)
  175. failedTimedOut := apps.DeploymentCondition{
  176. Type: apps.DeploymentProgressing,
  177. Status: v1.ConditionFalse,
  178. Reason: util.TimedOutReason,
  179. }
  180. newRSAvailable := apps.DeploymentCondition{
  181. Type: apps.DeploymentProgressing,
  182. Status: v1.ConditionTrue,
  183. Reason: util.NewRSAvailableReason,
  184. LastUpdateTime: testTime,
  185. LastTransitionTime: testTime,
  186. }
  187. replicaSetUpdated := apps.DeploymentCondition{
  188. Type: apps.DeploymentProgressing,
  189. Status: v1.ConditionTrue,
  190. Reason: util.ReplicaSetUpdatedReason,
  191. LastUpdateTime: testTime,
  192. LastTransitionTime: testTime,
  193. }
  194. tests := []struct {
  195. name string
  196. d *apps.Deployment
  197. allRSs []*apps.ReplicaSet
  198. newRS *apps.ReplicaSet
  199. conditionType apps.DeploymentConditionType
  200. conditionStatus v1.ConditionStatus
  201. conditionReason string
  202. lastUpdate metav1.Time
  203. lastTransition metav1.Time
  204. }{
  205. {
  206. name: "General: remove Progressing condition and do not estimate progress if deployment has no Progress Deadline",
  207. d: currentDeployment(nil, 3, 2, 2, 2, []apps.DeploymentCondition{replicaSetUpdated}),
  208. allRSs: []*apps.ReplicaSet{newRSWithAvailable("bar", 0, 1, 1)},
  209. newRS: newRSWithAvailable("foo", 3, 2, 2),
  210. },
  211. {
  212. name: "General: do not estimate progress of deployment with only one active ReplicaSet",
  213. d: currentDeployment(&pds, 3, 3, 3, 3, []apps.DeploymentCondition{newRSAvailable}),
  214. allRSs: []*apps.ReplicaSet{newRSWithAvailable("bar", 3, 3, 3)},
  215. conditionType: apps.DeploymentProgressing,
  216. conditionStatus: v1.ConditionTrue,
  217. conditionReason: util.NewRSAvailableReason,
  218. lastUpdate: testTime,
  219. lastTransition: testTime,
  220. },
  221. {
  222. name: "DeploymentProgressing: dont update lastTransitionTime if deployment already has Progressing=True",
  223. d: currentDeployment(&pds, 3, 2, 2, 2, []apps.DeploymentCondition{replicaSetUpdated}),
  224. allRSs: []*apps.ReplicaSet{newRSWithAvailable("bar", 0, 1, 1)},
  225. newRS: newRSWithAvailable("foo", 3, 2, 2),
  226. conditionType: apps.DeploymentProgressing,
  227. conditionStatus: v1.ConditionTrue,
  228. conditionReason: util.ReplicaSetUpdatedReason,
  229. lastTransition: testTime,
  230. },
  231. {
  232. name: "DeploymentProgressing: update everything if deployment has Progressing=False",
  233. d: currentDeployment(&pds, 3, 2, 2, 2, []apps.DeploymentCondition{failedTimedOut}),
  234. allRSs: []*apps.ReplicaSet{newRSWithAvailable("bar", 0, 1, 1)},
  235. newRS: newRSWithAvailable("foo", 3, 2, 2),
  236. conditionType: apps.DeploymentProgressing,
  237. conditionStatus: v1.ConditionTrue,
  238. conditionReason: util.ReplicaSetUpdatedReason,
  239. },
  240. {
  241. name: "DeploymentProgressing: create Progressing condition if it does not exist",
  242. d: currentDeployment(&pds, 3, 2, 2, 2, []apps.DeploymentCondition{}),
  243. allRSs: []*apps.ReplicaSet{newRSWithAvailable("bar", 0, 1, 1)},
  244. newRS: newRSWithAvailable("foo", 3, 2, 2),
  245. conditionType: apps.DeploymentProgressing,
  246. conditionStatus: v1.ConditionTrue,
  247. conditionReason: util.ReplicaSetUpdatedReason,
  248. },
  249. {
  250. name: "DeploymentComplete: dont update lastTransitionTime if deployment already has Progressing=True",
  251. d: currentDeployment(&pds, 3, 3, 3, 3, []apps.DeploymentCondition{replicaSetUpdated}),
  252. allRSs: []*apps.ReplicaSet{},
  253. newRS: newRSWithAvailable("foo", 3, 3, 3),
  254. conditionType: apps.DeploymentProgressing,
  255. conditionStatus: v1.ConditionTrue,
  256. conditionReason: util.NewRSAvailableReason,
  257. lastTransition: testTime,
  258. },
  259. {
  260. name: "DeploymentComplete: update everything if deployment has Progressing=False",
  261. d: currentDeployment(&pds, 3, 3, 3, 3, []apps.DeploymentCondition{failedTimedOut}),
  262. allRSs: []*apps.ReplicaSet{},
  263. newRS: newRSWithAvailable("foo", 3, 3, 3),
  264. conditionType: apps.DeploymentProgressing,
  265. conditionStatus: v1.ConditionTrue,
  266. conditionReason: util.NewRSAvailableReason,
  267. },
  268. {
  269. name: "DeploymentComplete: create Progressing condition if it does not exist",
  270. d: currentDeployment(&pds, 3, 3, 3, 3, []apps.DeploymentCondition{}),
  271. allRSs: []*apps.ReplicaSet{},
  272. newRS: newRSWithAvailable("foo", 3, 3, 3),
  273. conditionType: apps.DeploymentProgressing,
  274. conditionStatus: v1.ConditionTrue,
  275. conditionReason: util.NewRSAvailableReason,
  276. },
  277. {
  278. name: "DeploymentComplete: defend against NPE when newRS=nil",
  279. d: currentDeployment(&pds, 0, 3, 3, 3, []apps.DeploymentCondition{replicaSetUpdated}),
  280. allRSs: []*apps.ReplicaSet{newRSWithAvailable("foo", 0, 0, 0)},
  281. conditionType: apps.DeploymentProgressing,
  282. conditionStatus: v1.ConditionTrue,
  283. conditionReason: util.NewRSAvailableReason,
  284. },
  285. {
  286. name: "DeploymentTimedOut: update status if rollout exceeds Progress Deadline",
  287. d: currentDeployment(&pds, 3, 2, 2, 2, []apps.DeploymentCondition{replicaSetUpdated}),
  288. allRSs: []*apps.ReplicaSet{},
  289. newRS: newRSWithAvailable("foo", 3, 2, 2),
  290. conditionType: apps.DeploymentProgressing,
  291. conditionStatus: v1.ConditionFalse,
  292. conditionReason: util.TimedOutReason,
  293. },
  294. {
  295. name: "DeploymentTimedOut: do not update status if deployment has existing timedOut condition",
  296. d: currentDeployment(&pds, 3, 2, 2, 2, []apps.DeploymentCondition{failedTimedOut}),
  297. allRSs: []*apps.ReplicaSet{},
  298. newRS: newRSWithAvailable("foo", 3, 2, 2),
  299. conditionType: apps.DeploymentProgressing,
  300. conditionStatus: v1.ConditionFalse,
  301. conditionReason: util.TimedOutReason,
  302. lastUpdate: testTime,
  303. lastTransition: testTime,
  304. },
  305. }
  306. for _, test := range tests {
  307. t.Run(test.name, func(t *testing.T) {
  308. fake := fake.Clientset{}
  309. dc := &DeploymentController{
  310. client: &fake,
  311. }
  312. if test.newRS != nil {
  313. test.allRSs = append(test.allRSs, test.newRS)
  314. }
  315. err := dc.syncRolloutStatus(test.allRSs, test.newRS, test.d)
  316. if err != nil {
  317. t.Error(err)
  318. }
  319. newCond := util.GetDeploymentCondition(test.d.Status, test.conditionType)
  320. switch {
  321. case newCond == nil:
  322. if test.d.Spec.ProgressDeadlineSeconds != nil && *test.d.Spec.ProgressDeadlineSeconds != math.MaxInt32 {
  323. t.Errorf("%s: expected deployment condition: %s", test.name, test.conditionType)
  324. }
  325. case newCond.Status != test.conditionStatus || newCond.Reason != test.conditionReason:
  326. t.Errorf("%s: DeploymentProgressing has status %s with reason %s. Expected %s with %s.", test.name, newCond.Status, newCond.Reason, test.conditionStatus, test.conditionReason)
  327. case !test.lastUpdate.IsZero() && test.lastUpdate != testTime:
  328. t.Errorf("%s: LastUpdateTime was changed to %s but expected %s;", test.name, test.lastUpdate, testTime)
  329. case !test.lastTransition.IsZero() && test.lastTransition != testTime:
  330. t.Errorf("%s: LastTransitionTime was changed to %s but expected %s;", test.name, test.lastTransition, testTime)
  331. }
  332. })
  333. }
  334. }