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

commonize job name validation #2315

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
46 changes: 9 additions & 37 deletions pkg/webhooks/jax/jaxjob_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package jax
import (
"context"
"fmt"
"slices"
"strings"

apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
Expand All @@ -31,6 +30,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

trainingoperator "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1"
"github.com/kubeflow/training-operator/pkg/webhooks/utils"
)

var (
Expand Down Expand Up @@ -84,41 +84,13 @@ func validateSpec(spec trainingoperator.JAXJobSpec) field.ErrorList {
}

func validateJAXReplicaSpecs(rSpecs map[trainingoperator.ReplicaType]*trainingoperator.ReplicaSpec) field.ErrorList {
var allErrs field.ErrorList

if rSpecs == nil {
allErrs = append(allErrs, field.Required(jaxReplicaSpecPath, "must be required"))
}
for rType, rSpec := range rSpecs {
rolePath := jaxReplicaSpecPath.Key(string(rType))
containersPath := rolePath.Child("template").Child("spec").Child("containers")

// Make sure the replica type is valid.
validRoleTypes := []trainingoperator.ReplicaType{
trainingoperator.JAXJobReplicaTypeWorker,
}
if !slices.Contains(validRoleTypes, rType) {
allErrs = append(allErrs, field.NotSupported(rolePath, rType, validRoleTypes))
}

if rSpec == nil || len(rSpec.Template.Spec.Containers) == 0 {
allErrs = append(allErrs, field.Required(containersPath, "must be specified"))
}

// Make sure the image is defined in the container
defaultContainerPresent := false
for idx, container := range rSpec.Template.Spec.Containers {
if container.Image == "" {
allErrs = append(allErrs, field.Required(containersPath.Index(idx).Child("image"), "must be required"))
}
if container.Name == trainingoperator.JAXJobDefaultContainerName {
defaultContainerPresent = true
}
}
// Make sure there has at least one container named "jax"
if !defaultContainerPresent {
allErrs = append(allErrs, field.Required(containersPath, fmt.Sprintf("must have at least one container with name %s", trainingoperator.JAXJobDefaultContainerName)))
}
// Make sure the replica type is valid.
validReplicaTypes := []trainingoperator.ReplicaType{
trainingoperator.JAXJobReplicaTypeWorker,
}
return allErrs

return utils.ValidateReplicaSpecs(rSpecs,
trainingoperator.JAXJobDefaultContainerName,
validReplicaTypes,
jaxReplicaSpecPath)
}
50 changes: 11 additions & 39 deletions pkg/webhooks/paddlepaddle/paddlepaddle_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ package paddlepaddle
import (
"context"
"fmt"
"slices"
"strings"

apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/klog/v2"
Expand All @@ -32,6 +30,8 @@ import (

trainingoperator "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1"
"github.com/kubeflow/training-operator/pkg/common/util"
"github.com/kubeflow/training-operator/pkg/webhooks/utils"
apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
)

var (
Expand Down Expand Up @@ -85,42 +85,14 @@ func validatePaddleJob(oldJob, newJob *trainingoperator.PaddleJob) field.ErrorLi
}

func validateSpec(rSpecs map[trainingoperator.ReplicaType]*trainingoperator.ReplicaSpec) field.ErrorList {
var allErrs field.ErrorList

if rSpecs == nil {
allErrs = append(allErrs, field.Required(paddleReplicaSpecPath, "must be required"))
// Make sure the replica type is valid.
validReplicaTypes := []trainingoperator.ReplicaType{
trainingoperator.PaddleJobReplicaTypeMaster,
trainingoperator.PaddleJobReplicaTypeWorker,
}
for rType, rSpec := range rSpecs {
rolePath := paddleReplicaSpecPath.Key(string(rType))
containersPath := rolePath.Child("template").Child("spec").Child("containers")

// Make sure the replica type is valid.
validReplicaTypes := []trainingoperator.ReplicaType{
trainingoperator.PaddleJobReplicaTypeMaster,
trainingoperator.PaddleJobReplicaTypeWorker,
}
if !slices.Contains(validReplicaTypes, rType) {
allErrs = append(allErrs, field.NotSupported(rolePath, rType, validReplicaTypes))
}

if rSpec == nil || len(rSpec.Template.Spec.Containers) == 0 {
allErrs = append(allErrs, field.Required(containersPath, "must be specified"))
}

// Make sure the image is defined in the container
defaultContainerPresent := false
for idx, container := range rSpec.Template.Spec.Containers {
if container.Image == "" {
allErrs = append(allErrs, field.Required(containersPath.Index(idx).Child("image"), "must be required"))
}
if container.Name == trainingoperator.PaddleJobDefaultContainerName {
defaultContainerPresent = true
}
}
// Make sure there has at least one container named "paddle"
if !defaultContainerPresent {
allErrs = append(allErrs, field.Required(containersPath, fmt.Sprintf("must have at least one container with name %q", trainingoperator.PaddleJobDefaultContainerName)))
}
}
return allErrs

return utils.ValidateReplicaSpecs(rSpecs,
trainingoperator.PaddleJobDefaultContainerName,
validReplicaTypes,
paddleReplicaSpecPath)
}
48 changes: 12 additions & 36 deletions pkg/webhooks/pytorch/pytorchjob_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ package pytorch
import (
"context"
"fmt"
"slices"
"strings"

apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/klog/v2"
Expand All @@ -32,6 +30,8 @@ import (

trainingoperator "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1"
"github.com/kubeflow/training-operator/pkg/common/util"
"github.com/kubeflow/training-operator/pkg/webhooks/utils"
apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
)

var (
Expand Down Expand Up @@ -107,45 +107,21 @@ func validateSpec(spec trainingoperator.PyTorchJobSpec) (admission.Warnings, fie
}

func validatePyTorchReplicaSpecs(rSpecs map[trainingoperator.ReplicaType]*trainingoperator.ReplicaSpec) field.ErrorList {
var allErrs field.ErrorList

if rSpecs == nil {
allErrs = append(allErrs, field.Required(pytorchReplicaSpecPath, "must be required"))
// Make sure the replica type is valid.
validReplicaTypes := []trainingoperator.ReplicaType{
trainingoperator.PyTorchJobReplicaTypeMaster,
trainingoperator.PyTorchJobReplicaTypeWorker,
}
for rType, rSpec := range rSpecs {
rolePath := pytorchReplicaSpecPath.Key(string(rType))
containersPath := rolePath.Child("template").Child("spec").Child("containers")

// Make sure the replica type is valid.
validRoleTypes := []trainingoperator.ReplicaType{
trainingoperator.PyTorchJobReplicaTypeMaster,
trainingoperator.PyTorchJobReplicaTypeWorker,
}
if !slices.Contains(validRoleTypes, rType) {
allErrs = append(allErrs, field.NotSupported(rolePath, rType, validRoleTypes))
}

if rSpec == nil || len(rSpec.Template.Spec.Containers) == 0 {
allErrs = append(allErrs, field.Required(containersPath, "must be specified"))
}
allErrs := utils.ValidateReplicaSpecs(rSpecs,
trainingoperator.PyTorchJobDefaultContainerName,
validReplicaTypes,
pytorchReplicaSpecPath)

// Make sure the image is defined in the container
defaultContainerPresent := false
for idx, container := range rSpec.Template.Spec.Containers {
if container.Image == "" {
allErrs = append(allErrs, field.Required(containersPath.Index(idx).Child("image"), "must be required"))
}
if container.Name == trainingoperator.PyTorchJobDefaultContainerName {
defaultContainerPresent = true
}
}
// Make sure there has at least one container named "pytorch"
if !defaultContainerPresent {
allErrs = append(allErrs, field.Required(containersPath, fmt.Sprintf("must have at least one container with name %s", trainingoperator.PyTorchJobDefaultContainerName)))
}
for rType, rSpec := range rSpecs {
if rType == trainingoperator.PyTorchJobReplicaTypeMaster {
if rSpec.Replicas == nil || int(*rSpec.Replicas) != 1 {
allErrs = append(allErrs, field.Forbidden(rolePath.Child("replicas"), "must be 1"))
allErrs = append(allErrs, field.Forbidden(pytorchReplicaSpecPath.Key(string(rType)).Child("replicas"), "must be 1"))
}
}
}
Expand Down
34 changes: 7 additions & 27 deletions pkg/webhooks/tensorflow/tfjob_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"strings"

apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/klog/v2"
Expand All @@ -31,6 +30,8 @@ import (

trainingoperator "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1"
"github.com/kubeflow/training-operator/pkg/common/util"
"github.com/kubeflow/training-operator/pkg/webhooks/utils"
apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
)

var (
Expand Down Expand Up @@ -88,37 +89,16 @@ func validateSpec(spec trainingoperator.TFJobSpec) field.ErrorList {
}

func validateTFReplicaSpecs(rSpecs map[trainingoperator.ReplicaType]*trainingoperator.ReplicaSpec) field.ErrorList {
var allErrs field.ErrorList

if rSpecs == nil {
allErrs = append(allErrs, field.Required(tfReplicaSpecPath, "must be required"))
}
allErrs := utils.ValidateReplicaSpecs(rSpecs,
trainingoperator.TFJobDefaultContainerName,
nil,
tfReplicaSpecPath)

chiefOrMaster := 0
for rType, rSpec := range rSpecs {
rolePath := tfReplicaSpecPath.Key(string(rType))
containerPath := rolePath.Child("template").Child("spec").Child("containers")

if rSpec == nil || len(rSpec.Template.Spec.Containers) == 0 {
allErrs = append(allErrs, field.Required(containerPath, "must be specified"))
}
for rType := range rSpecs {
if trainingoperator.IsChiefOrMaster(rType) {
chiefOrMaster++
}
// Make sure the image is defined in the container.
defaultContainerPresent := false
for idx, container := range rSpec.Template.Spec.Containers {
if container.Image == "" {
allErrs = append(allErrs, field.Required(containerPath.Index(idx).Child("image"), "must be required"))
}
if container.Name == trainingoperator.TFJobDefaultContainerName {
defaultContainerPresent = true
}
}
// Make sure there has at least one container named "tensorflow".
if !defaultContainerPresent {
allErrs = append(allErrs, field.Required(containerPath, fmt.Sprintf("must have at least one container with name %s", trainingoperator.TFJobDefaultContainerName)))
}
}
if chiefOrMaster > 1 {
allErrs = append(allErrs, field.Forbidden(tfReplicaSpecPath, "must not have more than 1 Chief or Master role"))
Expand Down
55 changes: 55 additions & 0 deletions pkg/webhooks/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package utils
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add some unit tests for this function.


import (
"fmt"
"slices"

trainingoperator "github.com/kubeflow/training-operator/pkg/apis/kubeflow.org/v1"

"k8s.io/apimachinery/pkg/util/validation/field"
)

func ValidateReplicaSpecs(rSpecs map[trainingoperator.ReplicaType]*trainingoperator.ReplicaSpec,
defaultContainerName string,
validReplicaTypes []trainingoperator.ReplicaType,
replicaSpecPath *field.Path) field.ErrorList {

var allErrs field.ErrorList

if rSpecs == nil {
allErrs = append(allErrs, field.Required(replicaSpecPath, "must be required"))
}

for rType, rSpec := range rSpecs {
rolePath := replicaSpecPath.Key(string(rType))
containersPath := rolePath.Child("template").Child("spec").Child("containers")

if len(validReplicaTypes) > 0 {
if !slices.Contains(validReplicaTypes, rType) {
allErrs = append(allErrs, field.NotSupported(rolePath, rType, validReplicaTypes))
}
}

if rSpec == nil || len(rSpec.Template.Spec.Containers) == 0 {
allErrs = append(allErrs, field.Required(containersPath, "must be specified"))
}

// Make sure the image is defined in the container
defaultContainerPresent := false
for idx, container := range rSpec.Template.Spec.Containers {
if container.Image == "" {
allErrs = append(allErrs, field.Required(containersPath.Index(idx).Child("image"), "must be required"))
}
if container.Name == defaultContainerName {
defaultContainerPresent = true
}
}

if !defaultContainerPresent {
allErrs = append(allErrs, field.Required(containersPath,
fmt.Sprintf("must have at least one container with name %s", defaultContainerName)))
}
}

return allErrs
}
Loading