@@ -24,6 +24,7 @@ import (
2424 "github.com/argoproj-labs/argocd-autopilot/pkg/kube"
2525 "github.com/codefresh-io/cli-v2/pkg/store"
2626 authv1 "k8s.io/api/authorization/v1"
27+ batchv1 "k8s.io/api/batch/v1"
2728 v1 "k8s.io/api/core/v1"
2829 "k8s.io/apimachinery/pkg/api/resource"
2930 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -54,13 +55,6 @@ type (
5455 RestartPolicy v1.RestartPolicy
5556 BackOffLimit int32
5657 }
57-
58- RunPodOptions struct {
59- Namespace string
60- GenerateName string
61- Image string
62- Env []v1.EnvVar
63- }
6458)
6559
6660func EnsureClusterRequirements (ctx context.Context , kubeFactory kube.Factory , namespace string ) error {
@@ -235,24 +229,61 @@ func testNode(n v1.Node, req validationRequest) []string {
235229 return result
236230}
237231
238- func RunPod (ctx context.Context , client kubernetes.Interface , opts RunPodOptions ) (* v1. Pod , error ) {
239- podSpec := & v1. Pod {
232+ func LaunchJob (ctx context.Context , client kubernetes.Interface , opts LaunchJobOptions ) (* batchv1. Job , error ) {
233+ jobSpec := & batchv1. Job {
240234 ObjectMeta : metav1.ObjectMeta {
241- Namespace : opts .Namespace ,
242- GenerateName : opts .GenerateName ,
235+ Name : * opts .JobName ,
236+ Namespace : opts .Namespace ,
243237 },
244- Spec : v1.PodSpec {
245- Containers : []v1.Container {
246- {
247- Name : opts .GenerateName ,
248- Image : opts .Image ,
249- Env : opts .Env ,
238+ Spec : batchv1.JobSpec {
239+ Template : v1.PodTemplateSpec {
240+ Spec : v1.PodSpec {
241+ Containers : []v1.Container {
242+ {
243+ Name : * opts .JobName ,
244+ Image : * opts .Image ,
245+ Env : opts .Env ,
246+ },
247+ },
248+ RestartPolicy : opts .RestartPolicy ,
250249 },
251250 },
251+ BackoffLimit : & opts .BackOffLimit ,
252252 },
253253 }
254254
255- return client .CoreV1 ().Pods (opts .Namespace ).Create (ctx , podSpec , metav1.CreateOptions {})
255+ return client .BatchV1 ().Jobs (opts .Namespace ).Create (ctx , jobSpec , metav1.CreateOptions {})
256+ }
257+
258+ func DeleteJob (ctx context.Context , client kubernetes.Interface , job * batchv1.Job ) error {
259+ err := client .BatchV1 ().Jobs (job .Namespace ).Delete (ctx , job .Name , metav1.DeleteOptions {})
260+ if err != nil {
261+ return fmt .Errorf ("fail to delete job resource \" %s\" : %s" , job .Name , err .Error ())
262+ }
263+
264+ err = client .CoreV1 ().Pods (job .Namespace ).DeleteCollection (ctx , metav1.DeleteOptions {}, metav1.ListOptions {
265+ LabelSelector : "controller-uid=" + job .GetLabels ()["controller-uid" ],
266+ })
267+ if err != nil {
268+ return fmt .Errorf ("fail to delete tester pod: %s" , err .Error ())
269+ }
270+
271+ return nil
272+ }
273+
274+ func GetPodByJob (ctx context.Context , client kubernetes.Interface , job * batchv1.Job ) (* v1.Pod , error ) {
275+ pods , err := client .CoreV1 ().Pods (store .Get ().DefaultNamespace ).List (ctx , metav1.ListOptions {
276+ LabelSelector : "controller-uid=" + job .GetLabels ()["controller-uid" ],
277+ })
278+ if err != nil {
279+ return nil , err
280+ }
281+
282+ if len (pods .Items ) == 0 {
283+ return nil , nil
284+ }
285+
286+ return & pods .Items [0 ], nil
256287}
257288
258289func GetPodLogs (ctx context.Context , client kubernetes.Interface , namespace , name string ) (string , error ) {
0 commit comments