rest.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 job
  14. import (
  15. "fmt"
  16. batch "k8s.io/api/batch/v1"
  17. v1 "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/labels"
  20. "k8s.io/apimachinery/pkg/util/wait"
  21. clientset "k8s.io/client-go/kubernetes"
  22. "k8s.io/kubernetes/test/e2e/framework"
  23. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  24. )
  25. // GetJob uses c to get the Job in namespace ns named name. If the returned error is nil, the returned Job is valid.
  26. func GetJob(c clientset.Interface, ns, name string) (*batch.Job, error) {
  27. return c.BatchV1().Jobs(ns).Get(name, metav1.GetOptions{})
  28. }
  29. // GetJobPods returns a list of Pods belonging to a Job.
  30. func GetJobPods(c clientset.Interface, ns, jobName string) (*v1.PodList, error) {
  31. label := labels.SelectorFromSet(labels.Set(map[string]string{JobSelectorKey: jobName}))
  32. options := metav1.ListOptions{LabelSelector: label.String()}
  33. return c.CoreV1().Pods(ns).List(options)
  34. }
  35. // CreateJob uses c to create job in namespace ns. If the returned error is nil, the returned Job is valid and has
  36. // been created.
  37. func CreateJob(c clientset.Interface, ns string, job *batch.Job) (*batch.Job, error) {
  38. return c.BatchV1().Jobs(ns).Create(job)
  39. }
  40. // UpdateJob uses c to updated job in namespace ns. If the returned error is nil, the returned Job is valid and has
  41. // been updated.
  42. func UpdateJob(c clientset.Interface, ns string, job *batch.Job) (*batch.Job, error) {
  43. return c.BatchV1().Jobs(ns).Update(job)
  44. }
  45. // UpdateJobWithRetries updates job with retries.
  46. func UpdateJobWithRetries(c clientset.Interface, namespace, name string, applyUpdate func(*batch.Job)) (job *batch.Job, err error) {
  47. jobs := c.BatchV1().Jobs(namespace)
  48. var updateErr error
  49. pollErr := wait.PollImmediate(framework.Poll, JobTimeout, func() (bool, error) {
  50. if job, err = jobs.Get(name, metav1.GetOptions{}); err != nil {
  51. return false, err
  52. }
  53. // Apply the update, then attempt to push it to the apiserver.
  54. applyUpdate(job)
  55. if job, err = jobs.Update(job); err == nil {
  56. e2elog.Logf("Updating job %s", name)
  57. return true, nil
  58. }
  59. updateErr = err
  60. return false, nil
  61. })
  62. if pollErr == wait.ErrWaitTimeout {
  63. pollErr = fmt.Errorf("couldn't apply the provided updated to job %q: %v", name, updateErr)
  64. }
  65. return job, pollErr
  66. }
  67. // DeleteJob uses c to delete the Job named name in namespace ns. If the returned error is nil, the Job has been
  68. // deleted.
  69. func DeleteJob(c clientset.Interface, ns, name string) error {
  70. return c.BatchV1().Jobs(ns).Delete(name, nil)
  71. }