@@ -24,6 +24,7 @@ import (
24
24
"github.com/argoproj-labs/argocd-autopilot/pkg/kube"
25
25
"github.com/codefresh-io/cli-v2/pkg/store"
26
26
authv1 "k8s.io/api/authorization/v1"
27
+ batchv1 "k8s.io/api/batch/v1"
27
28
v1 "k8s.io/api/core/v1"
28
29
"k8s.io/apimachinery/pkg/api/resource"
29
30
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -54,13 +55,6 @@ type (
54
55
RestartPolicy v1.RestartPolicy
55
56
BackOffLimit int32
56
57
}
57
-
58
- RunPodOptions struct {
59
- Namespace string
60
- GenerateName string
61
- Image string
62
- Env []v1.EnvVar
63
- }
64
58
)
65
59
66
60
func EnsureClusterRequirements (ctx context.Context , kubeFactory kube.Factory , namespace string ) error {
@@ -235,24 +229,61 @@ func testNode(n v1.Node, req validationRequest) []string {
235
229
return result
236
230
}
237
231
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 {
240
234
ObjectMeta : metav1.ObjectMeta {
241
- Namespace : opts .Namespace ,
242
- GenerateName : opts .GenerateName ,
235
+ Name : * opts .JobName ,
236
+ Namespace : opts .Namespace ,
243
237
},
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 ,
250
249
},
251
250
},
251
+ BackoffLimit : & opts .BackOffLimit ,
252
252
},
253
253
}
254
254
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
256
287
}
257
288
258
289
func GetPodLogs (ctx context.Context , client kubernetes.Interface , namespace , name string ) (string , error ) {
0 commit comments