@@ -16,6 +16,7 @@ import (
16
16
apierrors "k8s.io/apimachinery/pkg/api/errors"
17
17
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18
18
"k8s.io/apimachinery/pkg/fields"
19
+ apitypes "k8s.io/apimachinery/pkg/types"
19
20
utilnet "k8s.io/apimachinery/pkg/util/net"
20
21
"k8s.io/apimachinery/pkg/util/wait"
21
22
"k8s.io/apimachinery/pkg/watch"
@@ -34,7 +35,8 @@ type Handler struct {
34
35
name string
35
36
namespace string
36
37
37
- spec * types.RunnerGroupSpec
38
+ spec * types.RunnerGroupSpec
39
+ ownerRef * metav1.OwnerReference
38
40
39
41
// FIXME(weifu): should we migrate this field into RunnerGroupSpec?
40
42
imageRef string
@@ -49,10 +51,16 @@ func NewHandler(
49
51
spec * types.RunnerGroupSpec ,
50
52
imageRef string ,
51
53
) (* Handler , error ) {
54
+ ownRef , err := buildOwnerReference (spec .OwnerReference )
55
+ if err != nil {
56
+ return nil , err
57
+ }
58
+
52
59
return & Handler {
53
60
name : name ,
54
61
namespace : namespace ,
55
62
spec : spec ,
63
+ ownerRef : ownRef ,
56
64
imageRef : imageRef ,
57
65
clientset : clientset ,
58
66
}, nil
@@ -108,6 +116,9 @@ func (h *Handler) uploadLoadProfileAsConfigMap(ctx context.Context) error {
108
116
configMapDataKeyLoadProfile : string (raw ),
109
117
},
110
118
}
119
+ if h .ownerRef != nil {
120
+ cm .OwnerReferences = append (cm .OwnerReferences , * h .ownerRef )
121
+ }
111
122
_ , err = cli .Create (ctx , cm , metav1.CreateOptions {})
112
123
return err
113
124
}
@@ -322,6 +333,10 @@ func (h *Handler) buildBatchJobObject(uploadURL string) *batchv1.Job {
322
333
},
323
334
}
324
335
336
+ if h .ownerRef != nil {
337
+ job .OwnerReferences = append (job .OwnerReferences , * h .ownerRef )
338
+ }
339
+
325
340
job .Spec .Template .Spec = corev1.PodSpec {
326
341
Affinity : & corev1.Affinity {},
327
342
Containers : []corev1.Container {
@@ -424,6 +439,25 @@ func (h *Handler) buildBatchJobObject(uploadURL string) *batchv1.Job {
424
439
return job
425
440
}
426
441
442
+ func buildOwnerReference (ref * string ) (* metav1.OwnerReference , error ) {
443
+ if ref == nil {
444
+ return nil , nil
445
+ }
446
+
447
+ tokens := strings .SplitN (* ref , ":" , 4 )
448
+ if len (tokens ) != 4 {
449
+ return nil , fmt .Errorf ("%s own reference is not apiVersion:kind:name:uid format" , * ref )
450
+ }
451
+
452
+ return & metav1.OwnerReference {
453
+ APIVersion : tokens [0 ],
454
+ Kind : tokens [1 ],
455
+ Name : tokens [2 ],
456
+ UID : apitypes .UID (tokens [3 ]),
457
+ Controller : toPtr (true ),
458
+ }, nil
459
+ }
460
+
427
461
func jobFinished (job * batchv1.Job ) bool {
428
462
return job .Status .Failed + job .Status .Succeeded == * job .Spec .Completions
429
463
}
0 commit comments