rest.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. "context"
  16. batchv1 "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. clientset "k8s.io/client-go/kubernetes"
  21. )
  22. // GetJob uses c to get the Job in namespace ns named name. If the returned error is nil, the returned Job is valid.
  23. func GetJob(c clientset.Interface, ns, name string) (*batchv1.Job, error) {
  24. return c.BatchV1().Jobs(ns).Get(context.TODO(), name, metav1.GetOptions{})
  25. }
  26. // GetJobPods returns a list of Pods belonging to a Job.
  27. func GetJobPods(c clientset.Interface, ns, jobName string) (*v1.PodList, error) {
  28. label := labels.SelectorFromSet(labels.Set(map[string]string{JobSelectorKey: jobName}))
  29. options := metav1.ListOptions{LabelSelector: label.String()}
  30. return c.CoreV1().Pods(ns).List(context.TODO(), options)
  31. }
  32. // CreateJob uses c to create job in namespace ns. If the returned error is nil, the returned Job is valid and has
  33. // been created.
  34. func CreateJob(c clientset.Interface, ns string, job *batchv1.Job) (*batchv1.Job, error) {
  35. return c.BatchV1().Jobs(ns).Create(context.TODO(), job, metav1.CreateOptions{})
  36. }