Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add labels for workload parent #1032

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/controller/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,11 @@ const (
// ignores this Job from admission, and takes control of its suspension
// status based on the admission status of the parent workload.
ParentWorkloadAnnotation = "kueue.x-k8s.io/parent-workload"

// ParentGroupKindLabel is the label key in the workload that holds the Kind
// and Group of a parent.
ParentGroupKindLabel = "kueue.x-k8s.io/parent-group-kind"
// ParentNameLabel is the label key in the workload that holds the Name of a
// parent.
ParentNameLabel = "kueue.x-k8s.io/parent-name"
)
14 changes: 13 additions & 1 deletion pkg/controller/jobframework/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"errors"
"fmt"
"unicode/utf8"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -458,17 +459,28 @@ func (r *JobReconciler) stopJob(ctx context.Context, job GenericJob, object clie
func (r *JobReconciler) constructWorkload(ctx context.Context, job GenericJob, object client.Object) (*kueue.Workload, error) {
podSets := job.PodSets()

jobGVK := job.GetGVK()

wl := &kueue.Workload{
ObjectMeta: metav1.ObjectMeta{
Name: GetWorkloadNameForOwnerWithGVK(object.GetName(), job.GetGVK()),
Name: GetWorkloadNameForOwnerWithGVK(object.GetName(), jobGVK),
Namespace: object.GetNamespace(),
Labels: map[string]string{},
},
Spec: kueue.WorkloadSpec{
PodSets: resetMinCounts(podSets),
QueueName: QueueName(job),
},
}

if utf8.RuneCountInString(job.Object().GetName()) < 64 {
wl.Labels[controllerconsts.ParentNameLabel] = job.Object().GetName()
achernevskii marked this conversation as resolved.
Show resolved Hide resolved
}

if utf8.RuneCountInString(jobGVK.GroupKind().String()) < 64 {
achernevskii marked this conversation as resolved.
Show resolved Hide resolved
wl.Labels[controllerconsts.ParentGroupKindLabel] = jobGVK.GroupKind().String()
}

priorityClassName, p, err := r.extractPriority(ctx, podSets, job)
if err != nil {
return nil, err
Expand Down
37 changes: 36 additions & 1 deletion pkg/controller/jobs/job/job_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package job

import (
controllerconsts "sigs.k8s.io/kueue/pkg/controller/constants"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -307,7 +309,10 @@ var (
cmpopts.SortSlices(func(a, b kueue.Workload) bool {
return a.Name < b.Name
}),
cmpopts.IgnoreFields(kueue.Workload{}, "TypeMeta", "ObjectMeta"),
cmpopts.IgnoreFields(
kueue.Workload{}, "TypeMeta", "ObjectMeta.OwnerReferences",
"ObjectMeta.Name", "ObjectMeta.ResourceVersion",
),
cmpopts.IgnoreFields(metav1.Condition{}, "LastTransitionTime"),
}
)
Expand Down Expand Up @@ -434,6 +439,36 @@ func TestReconciler(t *testing.T) {
PodSets(*utiltesting.MakePodSet(kueue.DefaultPodSetName, 10).Request(corev1.ResourceCPU, "1").Obj()).
Queue("test-queue").
Priority(0).
SetLabels(map[string]string{
controllerconsts.ParentGroupKindLabel: "Job.batch",
controllerconsts.ParentNameLabel: "job",
}).
Obj(),
},
},
"the workload without parent name label is created when job's name is longer than 63 characters": {
job: *utiltestingjob.MakeJob(strings.Repeat("long-name", 8), "ns").
Suspend(false).
Parallelism(10).
Request(corev1.ResourceCPU, "1").
Image("", nil).
Queue("test-queue").
Obj(),
wantJob: *utiltestingjob.MakeJob(strings.Repeat("long-name", 8), "ns").
Suspend(true).
Parallelism(10).
Request(corev1.ResourceCPU, "1").
Image("", nil).
Queue("test-queue").
Obj(),
wantWorkloads: []kueue.Workload{
*utiltesting.MakeWorkload("job", "ns").
PodSets(*utiltesting.MakePodSet(kueue.DefaultPodSetName, 10).Request(corev1.ResourceCPU, "1").Obj()).
Queue("test-queue").
Priority(0).
SetLabels(map[string]string{
controllerconsts.ParentGroupKindLabel: "Job.batch",
}).
Obj(),
},
},
Expand Down
5 changes: 5 additions & 0 deletions pkg/util/testing/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ func (w *WorkloadWrapper) ReclaimablePods(rps ...kueue.ReclaimablePod) *Workload
return w
}

func (w *WorkloadWrapper) SetLabels(l map[string]string) *WorkloadWrapper {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (w *WorkloadWrapper) SetLabels(l map[string]string) *WorkloadWrapper {
func (w *WorkloadWrapper) Labels(l map[string]string) *WorkloadWrapper {

nit, but we generally don't use for functions like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed this method in 0350ad8.

w.Labels = l
return w
}

type PodSetWrapper struct{ kueue.PodSet }

func MakePodSet(name string, count int) *PodSetWrapper {
Expand Down