ttlafterfinished.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Copyright 2018 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. "time"
  16. batch "k8s.io/api/batch/v1"
  17. v1 "k8s.io/api/core/v1"
  18. "k8s.io/apimachinery/pkg/util/wait"
  19. "k8s.io/kubernetes/pkg/util/slice"
  20. "k8s.io/kubernetes/test/e2e/framework"
  21. jobutil "k8s.io/kubernetes/test/e2e/framework/job"
  22. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  23. "github.com/onsi/ginkgo"
  24. "github.com/onsi/gomega"
  25. )
  26. const dummyFinalizer = "k8s.io/dummy-finalizer"
  27. var _ = framework.KubeDescribe("[Feature:TTLAfterFinished][NodeAlphaFeature:TTLAfterFinished]", func() {
  28. f := framework.NewDefaultFramework("ttlafterfinished")
  29. ginkgo.It("job should be deleted once it finishes after TTL seconds", func() {
  30. testFinishedJob(f)
  31. })
  32. })
  33. func cleanupJob(f *framework.Framework, job *batch.Job) {
  34. ns := f.Namespace.Name
  35. c := f.ClientSet
  36. e2elog.Logf("Remove the Job's dummy finalizer; the Job should be deleted cascadingly")
  37. removeFinalizerFunc := func(j *batch.Job) {
  38. j.ObjectMeta.Finalizers = slice.RemoveString(j.ObjectMeta.Finalizers, dummyFinalizer, nil)
  39. }
  40. _, err := jobutil.UpdateJobWithRetries(c, ns, job.Name, removeFinalizerFunc)
  41. framework.ExpectNoError(err)
  42. jobutil.WaitForJobGone(c, ns, job.Name, wait.ForeverTestTimeout)
  43. err = jobutil.WaitForAllJobPodsGone(c, ns, job.Name)
  44. framework.ExpectNoError(err)
  45. }
  46. func testFinishedJob(f *framework.Framework) {
  47. ns := f.Namespace.Name
  48. c := f.ClientSet
  49. parallelism := int32(1)
  50. completions := int32(1)
  51. backoffLimit := int32(2)
  52. ttl := int32(10)
  53. job := jobutil.NewTestJob("randomlySucceedOrFail", "rand-non-local", v1.RestartPolicyNever, parallelism, completions, nil, backoffLimit)
  54. job.Spec.TTLSecondsAfterFinished = &ttl
  55. job.ObjectMeta.Finalizers = []string{dummyFinalizer}
  56. defer cleanupJob(f, job)
  57. e2elog.Logf("Create a Job %s/%s with TTL", ns, job.Name)
  58. job, err := jobutil.CreateJob(c, ns, job)
  59. framework.ExpectNoError(err)
  60. e2elog.Logf("Wait for the Job to finish")
  61. err = jobutil.WaitForJobFinish(c, ns, job.Name)
  62. framework.ExpectNoError(err)
  63. e2elog.Logf("Wait for TTL after finished controller to delete the Job")
  64. err = jobutil.WaitForJobDeleting(c, ns, job.Name)
  65. framework.ExpectNoError(err)
  66. e2elog.Logf("Check Job's deletionTimestamp and compare with the time when the Job finished")
  67. job, err = jobutil.GetJob(c, ns, job.Name)
  68. framework.ExpectNoError(err)
  69. finishTime := jobutil.FinishTime(job)
  70. finishTimeUTC := finishTime.UTC()
  71. gomega.Expect(finishTime.IsZero()).NotTo(gomega.BeTrue())
  72. deleteAtUTC := job.ObjectMeta.DeletionTimestamp.UTC()
  73. gomega.Expect(deleteAtUTC).NotTo(gomega.BeNil())
  74. expireAtUTC := finishTimeUTC.Add(time.Duration(ttl) * time.Second)
  75. gomega.Expect(deleteAtUTC.Before(expireAtUTC)).To(gomega.BeFalse())
  76. }