Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 20 additions & 0 deletions pkg/util/testing/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,26 @@ func (j *JobSetWrapper) Container(rJobName, containerName, image string, command
return j
}

func (j *JobSetWrapper) ReplaceContainer(rJobName, containerName, newContainerName, image string, command, args []string, res corev1.ResourceList) *JobSetWrapper {
for i, rJob := range j.Spec.ReplicatedJobs {
if rJob.Name == rJobName {
for k, container := range rJob.Template.Spec.Template.Spec.Containers {
if container.Name == containerName {
j.Spec.ReplicatedJobs[i].Template.Spec.Template.Spec.Containers[k] = corev1.Container{
Name: newContainerName,
Image: image,
Command: command,
Args: args,
Resources: corev1.ResourceRequirements{Requests: res},
}
return j
}
}
}
}
return j
}

func (j *JobSetWrapper) ContainerTrainerPorts(ports []corev1.ContainerPort) *JobSetWrapper {
for i, rJob := range j.Spec.ReplicatedJobs {
if rJob.Name == constants.Node {
Expand Down
43 changes: 35 additions & 8 deletions pkg/webhooks/trainingruntime_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package webhooks

import (
"context"
"fmt"

apiruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
Expand All @@ -34,7 +35,16 @@ import (
)

const (
rJobReplicasErrorMsg = "always must be 1"
rJobReplicasErrorMsg = "always must be 1"
rJobContainerNamesErrorMsg = "must contain the required container for the ancestor: %s"
)

var (
expectedContainerNames = map[string]string{
constants.AncestorTrainer: constants.Node,
constants.ModelInitializer: constants.ModelInitializer,
constants.DatasetInitializer: constants.DatasetInitializer,
}
)

type TrainingRuntimeWebhook struct {
Expand Down Expand Up @@ -67,18 +77,35 @@ func validateReplicatedJobs(rJobs []jobsetv1alpha2.ReplicatedJob) field.ErrorLis
Child("replicatedJobs")
var allErrs field.ErrorList
for idx, rJob := range rJobs {
if rJob.Name == constants.Launcher && rJob.Replicas != 1 {
allErrs = append(allErrs, field.Invalid(rJobsPath.Index(idx).Child("replicas"), rJob.Replicas, rJobReplicasErrorMsg))
}

if rJob.Template.Labels == nil {
continue
}

if labelAncestor, ok := rJob.Template.Labels[constants.LabelTrainJobAncestor]; ok && ancestors.Has(labelAncestor) && rJob.Replicas != 1 {
allErrs = append(allErrs, field.Invalid(rJobsPath.Index(idx).Child("replicas"), rJob.Replicas, rJobReplicasErrorMsg))
if labelAncestor, ok := rJob.Template.Labels[constants.LabelTrainJobAncestor]; ok && ancestors.Has(labelAncestor) {
if rJob.Replicas != 1 {
allErrs = append(allErrs, field.Invalid(rJobsPath.Index(idx).Child("replicas"), rJob.Replicas, rJobReplicasErrorMsg))
}

// Validate replicated job contains the required containers.
// Mapping of the ancestor labels to the containers:
// 1. dataset-initializer - dataset-initializer
// 2. model-initializer - model-initializer
// 3. trainer - node
hasRequiredContainer := false
for _, container := range rJob.Template.Spec.Template.Spec.Containers {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder how difficult that would be to implement this validation in CEL?

Copy link
Member Author

@Electronic-Waste Electronic-Waste Oct 18, 2025

Choose a reason for hiding this comment

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

Hmm. These fields are defined in JobSetSpec:

// spec of the desired JobSet which will be created from TrainJob.
// +optional
Spec jobsetv1alpha2.JobSetSpec `json:"spec,omitempty"`

I'm not sure whether can we reference the container name in it (detecting corresponding label) and verify its value. In the contrast, I think webhook is more convenient and do not need to change the API by adding something like:

// +kubebuilder:validation:XValidation:rule="self > 0 || self in ['auto', 'cpu', 'gpu']", message="NumProcPerNode must be equal to auto, cpu, gpu, or int value"

if container.Name == expectedContainerNames[labelAncestor] {
hasRequiredContainer = true
break
}
}
if !hasRequiredContainer {
allErrs = append(allErrs, field.Invalid(
rJobsPath.Index(idx).Child("template").Child("spec").Child("template").Child("spec").Child("containers"),
rJob.Template.Spec.Template.Spec.Containers,
fmt.Sprintf(rJobContainerNamesErrorMsg, labelAncestor),
))
}
}

}
return allErrs
}
Expand Down
19 changes: 17 additions & 2 deletions pkg/webhooks/trainingruntime_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
jobsetv1alpha2 "sigs.k8s.io/jobset/api/jobset/v1alpha2"

Expand Down Expand Up @@ -55,12 +56,26 @@ func TestValidateReplicatedJobs(t *testing.T) {
"2", ""),
field.Invalid(field.NewPath("spec").Child("template").Child("spec").Child("replicatedJobs").Index(1).Child("replicas"),
"2", ""),
field.Invalid(field.NewPath("spec").Child("template").Child("spec").Child("replicatedJobs").Index(2).Child("replicas"),
"2", ""),
field.Invalid(field.NewPath("spec").Child("template").Child("spec").Child("replicatedJobs").Index(3).Child("replicas"),
"2", ""),
},
},
"missing required container in replicatedJobs": {
rJobs: testingutil.MakeJobSetWrapper("ns", "valid").
Replicas(1, constants.Launcher, constants.Node, constants.DatasetInitializer, constants.ModelInitializer).
ReplaceContainer(constants.DatasetInitializer, constants.DatasetInitializer, "test", "", []string{}, []string{}, corev1.ResourceList{}).
ReplaceContainer(constants.ModelInitializer, constants.ModelInitializer, "test", "", []string{}, []string{}, corev1.ResourceList{}).
ReplaceContainer(constants.Node, constants.Node, "test", "", []string{}, []string{}, corev1.ResourceList{}).
Obj().Spec.ReplicatedJobs,
wantError: field.ErrorList{
field.Invalid(field.NewPath("spec").Child("template").Child("spec").Child("replicatedJobs").Index(0).Child("template").Child("spec").Child("template").Child("spec").Child("containers"),
[]corev1.Container{{Name: "test"}}, ""),
field.Invalid(field.NewPath("spec").Child("template").Child("spec").Child("replicatedJobs").Index(1).Child("template").Child("spec").Child("template").Child("spec").Child("containers"),
[]corev1.Container{{Name: "test"}}, ""),
field.Invalid(field.NewPath("spec").Child("template").Child("spec").Child("replicatedJobs").Index(2).Child("template").Child("spec").Child("template").Child("spec").Child("containers"),
[]corev1.Container{{Name: "test"}}, ""),
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
Expand Down
Loading