Skip to content

Commit

Permalink
add ValidateAlpha2TFJobSpec to check v1alpha2.TFJobSpec is valid
Browse files Browse the repository at this point in the history
Signed-off-by: Pengyu Chen <[email protected]>
  • Loading branch information
Pengyu Chen committed Jun 28, 2018
1 parent 9360614 commit c4e0564
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 12 additions & 1 deletion pkg/apis/tensorflow/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller.v2/informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c4e0564

Please sign in to comment.