diff --git a/pkg/apis/tensorflow/validation/validation.go b/pkg/apis/tensorflow/validation/validation.go index fa0f012319..bc80c90e36 100644 --- a/pkg/apis/tensorflow/validation/validation.go +++ b/pkg/apis/tensorflow/validation/validation.go @@ -19,10 +19,21 @@ import ( "fmt" tfv1 "github.com/kubeflow/tf-operator/pkg/apis/tensorflow/v1alpha1" + tfv2 "github.com/kubeflow/tf-operator/pkg/apis/tensorflow/v1alpha2" "github.com/kubeflow/tf-operator/pkg/util" ) -// ValidateTFJobSpec checks that the TFJobSpec is valid. +// ValidateTFJobSpec checks that the v1alpha2.TFJobSpec is valid. +func ValidateAlpha2TFJobSpec(c *tfv2.TFJobSpec) error { + for _, value := range c.TFReplicaSpecs { + if &value.Template == nil || len(value.Template.Spec.Containers) == 0 { + return fmt.Errorf("TFJobSpec validate failed") + } + } + return nil +} + +// ValidateTFJobSpec checks that the v1alpha1.TFJobSpec is valid. func ValidateTFJobSpec(c *tfv1.TFJobSpec) error { if c.TerminationPolicy == nil || c.TerminationPolicy.Chief == nil { return fmt.Errorf("invalid termination policy: %v", c.TerminationPolicy) diff --git a/pkg/controller.v2/informer.go b/pkg/controller.v2/informer.go index 9965e2a2a3..cf0ea66f3e 100644 --- a/pkg/controller.v2/informer.go +++ b/pkg/controller.v2/informer.go @@ -16,6 +16,7 @@ import ( tfjobinformers "github.com/kubeflow/tf-operator/pkg/client/informers/externalversions" tfjobinformersv1alpha2 "github.com/kubeflow/tf-operator/pkg/client/informers/externalversions/kubeflow/v1alpha2" "github.com/kubeflow/tf-operator/pkg/util/unstructured" + "github.com/kubeflow/tf-operator/pkg/apis/tensorflow/validation" ) const ( @@ -92,7 +93,8 @@ func tfJobFromUnstructured(obj interface{}) (*tfv1alpha2.TFJob, error) { // This is a simple validation for TFJob to close // https://github.com/kubeflow/tf-operator/issues/641 // TODO(gaocegege): Add more validation here. - if err != nil || tfjob.Spec.TFReplicaSpecs == nil { + validationErr := validation.ValidateAlpha2TFJobSpec(&tfjob.Spec) + if err != nil || tfjob.Spec.TFReplicaSpecs == nil || validationErr != nil { return &tfjob, errFailedMarshal } return &tfjob, nil