Skip to content

Commit

Permalink
Allow manageJobsWithoutQueueName on StatefulSet.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbobrovskyi committed Nov 13, 2024
1 parent 43b7374 commit f9aa14c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
16 changes: 5 additions & 11 deletions pkg/controller/jobs/deployment/deployment_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,11 @@ import (
)

type Webhook struct {
client client.Client
manageJobsWithoutQueueName bool
client client.Client
}

func SetupWebhook(mgr ctrl.Manager, opts ...jobframework.Option) error {
options := jobframework.ProcessOptions(opts...)
wh := &Webhook{
client: mgr.GetClient(),
manageJobsWithoutQueueName: options.ManageJobsWithoutQueueName,
}
func SetupWebhook(mgr ctrl.Manager, _ ...jobframework.Option) error {
wh := &Webhook{client: mgr.GetClient()}
obj := &appsv1.Deployment{}
return webhook.WebhookManagedBy(mgr).
For(obj).
Expand All @@ -61,12 +56,11 @@ func (wh *Webhook) Default(ctx context.Context, obj runtime.Object) error {
log := ctrl.LoggerFrom(ctx).WithName("deployment-webhook").WithValues("deployment", klog.KObj(d))
log.V(5).Info("Applying defaults")

cqLabel, ok := d.Labels[constants.QueueLabel]
if ok {
if queueName := jobframework.QueueNameForObject(d.Object()); queueName != "" {
if d.Spec.Template.Labels == nil {
d.Spec.Template.Labels = make(map[string]string, 1)
}
d.Spec.Template.Labels[constants.QueueLabel] = cqLabel
d.Spec.Template.Labels[constants.QueueLabel] = queueName
}

return nil
Expand Down
19 changes: 11 additions & 8 deletions pkg/controller/jobs/deployment/deployment_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ import (

func TestDefault(t *testing.T) {
testCases := map[string]struct {
deployment *appsv1.Deployment
manageJobsWithoutQueueName bool
enableIntegrations []string
want *appsv1.Deployment
deployment *appsv1.Deployment
enableIntegrations []string
want *appsv1.Deployment
}{
"pod without queue": {
enableIntegrations: []string{"pod"},
deployment: testingdeployment.MakeDeployment("test-pod", "").
Obj(),
want: testingdeployment.MakeDeployment("test-pod", "").
Obj(),
},
"pod with queue": {
enableIntegrations: []string{"pod"},
deployment: testingdeployment.MakeDeployment("test-pod", "").
Expand All @@ -52,10 +58,7 @@ func TestDefault(t *testing.T) {
builder := utiltesting.NewClientBuilder()
cli := builder.Build()

w := &Webhook{
client: cli,
manageJobsWithoutQueueName: tc.manageJobsWithoutQueueName,
}
w := &Webhook{client: cli}

ctx, _ := utiltesting.ContextWithLog(t)

Expand Down
16 changes: 12 additions & 4 deletions pkg/controller/jobs/statefulset/statefulset_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,24 @@ func (wh *Webhook) Default(ctx context.Context, obj runtime.Object) error {
log := ctrl.LoggerFrom(ctx).WithName("statefulset-webhook")
log.V(5).Info("Applying defaults")

cqLabel, ok := ss.Labels[constants.QueueLabel]
if !ok {
queueName := jobframework.QueueNameForObject(ss.Object())

// Skip not managed by Kueue.
if queueName == "" && !wh.manageJobsWithoutQueueName {
return nil
}

if queueName != "" {
if ss.Spec.Template.Labels == nil {
ss.Spec.Template.Labels = make(map[string]string, 2)
}
ss.Spec.Template.Labels[constants.QueueLabel] = queueName
}

if ss.Spec.Template.Labels == nil {
ss.Spec.Template.Labels = make(map[string]string, 2)
ss.Spec.Template.Labels = make(map[string]string, 1)
}

ss.Spec.Template.Labels[constants.QueueLabel] = cqLabel
ss.Spec.Template.Labels[pod.GroupNameLabel] = GetWorkloadName(ss.Name)

if ss.Spec.Template.Annotations == nil {
Expand Down
22 changes: 22 additions & 0 deletions pkg/controller/jobs/statefulset/statefulset_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@ func TestDefault(t *testing.T) {
enableIntegrations []string
want *appsv1.StatefulSet
}{
"statefulset not managed by kueue": {
enableIntegrations: []string{"pod"},
statefulset: testingstatefulset.MakeStatefulSet("test-pod", "").
Replicas(10).
Obj(),
want: testingstatefulset.MakeStatefulSet("test-pod", "").
Replicas(10).
Obj(),
},
"statefulset managed by kueue without queue name": {
enableIntegrations: []string{"pod"},
manageJobsWithoutQueueName: true,
statefulset: testingstatefulset.MakeStatefulSet("test-pod", "").
Replicas(10).
Obj(),
want: testingstatefulset.MakeStatefulSet("test-pod", "").
Replicas(10).
PodTemplateSpecPodGroupNameLabel("test-pod", "", gvk).
PodTemplateSpecPodGroupTotalCountAnnotation(10).
PodTemplateSpecPodGroupFastAdmissionAnnotation(true).
Obj(),
},
"statefulset with queue": {
enableIntegrations: []string{"pod"},
statefulset: testingstatefulset.MakeStatefulSet("test-pod", "").
Expand Down

0 comments on commit f9aa14c

Please sign in to comment.