Skip to content

Commit 141fe6e

Browse files
committed
*: Introduce OwnerReference to RunnerGroup
It's used to cleanup completed runner group. Signed-off-by: Wei Fu <weifu@microsoft.com>
1 parent f05acda commit 141fe6e

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

api/types/runner_group.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ type RunnerGroupSpec struct {
1919
NodeAffinity map[string][]string `json:"nodeAffinity,omitempty" yaml:"nodeAffinity"`
2020
// ServiceAccount is the name of the ServiceAccount to use to run runners.
2121
ServiceAccount *string `json:"serviceAccount,omitempty" yaml:"serviceAccount"`
22+
// OwnerReference is to mark the runner group depending on this object.
23+
//
24+
// FORMAT: APIVersion:Kind:Name:UID
25+
OwnerReference *string `json:"ownerReference,omitempty" yaml:"ownerReference"`
2226
}

cmd/kperf/commands/multirunners/server.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ var serverCommand = cli.Command{
3030
Usage: "The runner's conainer image",
3131
Required: true,
3232
},
33+
cli.StringFlag{
34+
Name: "runner-owner",
35+
Usage: "The runners depend on this object",
36+
},
3337
cli.StringSliceFlag{
3438
Name: "address",
3539
Usage: "Address for the server",
@@ -79,13 +83,22 @@ func buildRunnerGroupHandlers(cliCtx *cli.Context, serverName string) ([]*runner
7983
imgRef := cliCtx.String("runner-image")
8084
namespace := cliCtx.String("namespace")
8185

86+
ownerRef := ""
87+
if cliCtx.IsSet("runner-owner") {
88+
ownerRef = cliCtx.String("runner-owner")
89+
}
90+
8291
groups := make([]*runnergroup.Handler, 0, len(specURIs))
8392
for idx, specURI := range specURIs {
8493
spec, err := runnergroup.NewRunnerGroupSpecFromURI(clientset, specURI)
8594
if err != nil {
8695
return nil, err
8796
}
8897

98+
if ownerRef != "" {
99+
spec.OwnerReference = &ownerRef
100+
}
101+
89102
groupName := fmt.Sprintf("%s-%d", serverName, idx)
90103
g, err := runnergroup.NewHandler(clientset, namespace, groupName, spec, imgRef)
91104
if err != nil {

runner/group/handler.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
apierrors "k8s.io/apimachinery/pkg/api/errors"
1717
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1818
"k8s.io/apimachinery/pkg/fields"
19+
apitypes "k8s.io/apimachinery/pkg/types"
1920
utilnet "k8s.io/apimachinery/pkg/util/net"
2021
"k8s.io/apimachinery/pkg/util/wait"
2122
"k8s.io/apimachinery/pkg/watch"
@@ -34,7 +35,8 @@ type Handler struct {
3435
name string
3536
namespace string
3637

37-
spec *types.RunnerGroupSpec
38+
spec *types.RunnerGroupSpec
39+
ownerRef *metav1.OwnerReference
3840

3941
// FIXME(weifu): should we migrate this field into RunnerGroupSpec?
4042
imageRef string
@@ -49,10 +51,16 @@ func NewHandler(
4951
spec *types.RunnerGroupSpec,
5052
imageRef string,
5153
) (*Handler, error) {
54+
ownRef, err := buildOwnerReference(spec.OwnerReference)
55+
if err != nil {
56+
return nil, err
57+
}
58+
5259
return &Handler{
5360
name: name,
5461
namespace: namespace,
5562
spec: spec,
63+
ownerRef: ownRef,
5664
imageRef: imageRef,
5765
clientset: clientset,
5866
}, nil
@@ -108,6 +116,9 @@ func (h *Handler) uploadLoadProfileAsConfigMap(ctx context.Context) error {
108116
configMapDataKeyLoadProfile: string(raw),
109117
},
110118
}
119+
if h.ownerRef != nil {
120+
cm.OwnerReferences = append(cm.OwnerReferences, *h.ownerRef)
121+
}
111122
_, err = cli.Create(ctx, cm, metav1.CreateOptions{})
112123
return err
113124
}
@@ -322,6 +333,10 @@ func (h *Handler) buildBatchJobObject(uploadURL string) *batchv1.Job {
322333
},
323334
}
324335

336+
if h.ownerRef != nil {
337+
job.OwnerReferences = append(job.OwnerReferences, *h.ownerRef)
338+
}
339+
325340
job.Spec.Template.Spec = corev1.PodSpec{
326341
Affinity: &corev1.Affinity{},
327342
Containers: []corev1.Container{
@@ -424,6 +439,25 @@ func (h *Handler) buildBatchJobObject(uploadURL string) *batchv1.Job {
424439
return job
425440
}
426441

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+
427461
func jobFinished(job *batchv1.Job) bool {
428462
return job.Status.Failed+job.Status.Succeeded == *job.Spec.Completions
429463
}

0 commit comments

Comments
 (0)