Skip to content

Commit

Permalink
Add validation for evaluator
Browse files Browse the repository at this point in the history
Add validation for evaluator so that the number of evaluator
should no more than one.
  • Loading branch information
DeliangFan committed Dec 19, 2018
1 parent 451069f commit c10b155
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pkg/apis/tensorflow/v1alpha2/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ package v1alpha2
func IsChieforMaster(typ TFReplicaType) bool {
return typ == TFReplicaTypeChief || typ == TFReplicaTypeMaster
}

// IsEvaluator returns true if the type is Evaluator.
func IsEvaluator(typ TFReplicaType) bool {
return typ == TFReplicaTypeEval
}
7 changes: 6 additions & 1 deletion pkg/apis/tensorflow/v1beta1/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ func IsChieforMaster(typ TFReplicaType) bool {
return typ == TFReplicaTypeChief || typ == TFReplicaTypeMaster
}

// IsWorker returns true if the type is Worker
// IsWorker returns true if the type is Worker.
func IsWorker(typ TFReplicaType) bool {
return typ == TFReplicaTypeWorker
}

// IsEvaluator returns true if the type is Evaluator.
func IsEvaluator(typ TFReplicaType) bool {
return typ == TFReplicaTypeEval
}
14 changes: 14 additions & 0 deletions pkg/apis/tensorflow/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ func validateBetaOneReplicaSpecs(specs map[tfv1beta1.TFReplicaType]*common.Repli
return fmt.Errorf("TFJobSpec is not valid")
}
foundChief := 0
var foundEvaluator int32 = 0
for rType, value := range specs {
if value == nil || len(value.Template.Spec.Containers) == 0 {
return fmt.Errorf("TFJobSpec is not valid")
}
if tfv1beta1.IsChieforMaster(rType) {
foundChief++
}
if tfv1beta1.IsEvaluator(rType) {
foundEvaluator = foundEvaluator + *value.Replicas
}
// Make sure the image is defined in the container.
numNamedTensorflow := 0
for _, container := range value.Template.Spec.Containers {
Expand All @@ -61,6 +65,9 @@ func validateBetaOneReplicaSpecs(specs map[tfv1beta1.TFReplicaType]*common.Repli
if foundChief > 1 {
return fmt.Errorf("More than 1 chief/master found")
}
if foundEvaluator > 1 {
return fmt.Errorf("More than 1 evaluator found")
}
return nil
}

Expand All @@ -74,13 +81,17 @@ func validateAlphaTwoReplicaSpecs(specs map[tfv2.TFReplicaType]*tfv2.TFReplicaSp
return fmt.Errorf("TFJobSpec is not valid")
}
foundChief := 0
var foundEvaluator int32 = 0
for rType, value := range specs {
if value == nil || len(value.Template.Spec.Containers) == 0 {
return fmt.Errorf("TFJobSpec is not valid")
}
if tfv2.IsChieforMaster(rType) {
foundChief++
}
if tfv2.IsEvaluator(rType) {
foundEvaluator = foundEvaluator + *value.Replicas
}
// Make sure the image is defined in the container.
numNamedTensorflow := 0
for _, container := range value.Template.Spec.Containers {
Expand All @@ -101,5 +112,8 @@ func validateAlphaTwoReplicaSpecs(specs map[tfv2.TFReplicaType]*tfv2.TFReplicaSp
if foundChief > 1 {
return fmt.Errorf("More than 1 chief/master found")
}
if foundEvaluator > 1 {
return fmt.Errorf("More than 1 evaluator found")
}
return nil
}
37 changes: 36 additions & 1 deletion pkg/apis/tensorflow/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ package validation
import (
"testing"

common "github.com/kubeflow/tf-operator/pkg/apis/common/v1beta1"
"github.com/golang/protobuf/proto"
common "github.com/kubeflow/tf-operator/pkg/apis/common/v1beta1"
tfv2 "github.com/kubeflow/tf-operator/pkg/apis/tensorflow/v1alpha2"
tfv1beta1 "github.com/kubeflow/tf-operator/pkg/apis/tensorflow/v1beta1"

Expand Down Expand Up @@ -89,6 +90,23 @@ func TestValidateAlphaTwoTFJobSpec(t *testing.T) {
},
},
},
{
TFReplicaSpecs: map[tfv2.TFReplicaType]*tfv2.TFReplicaSpec{
tfv2.TFReplicaTypeEval: &tfv2.TFReplicaSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Name: "tensorflow",
Image: "kubeflow/tf-dist-mnist-test:1.0",
},
},
},
},
Replicas: proto.Int32(2),
},
},
},
}
for _, c := range testCases {
err := ValidateAlphaTwoTFJobSpec(&c)
Expand Down Expand Up @@ -163,6 +181,23 @@ func TestValidateBetaOneTFJobSpec(t *testing.T) {
},
},
},
{
TFReplicaSpecs: map[tfv1beta1.TFReplicaType]*common.ReplicaSpec{
tfv1beta1.TFReplicaTypeEval: &common.ReplicaSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Name: "tensorflow",
Image: "kubeflow/tf-dist-mnist-test:1.0",
},
},
},
},
Replicas: proto.Int32(2),
},
},
},
}
for _, c := range testCases {
err := ValidateBetaOneTFJobSpec(&c)
Expand Down

0 comments on commit c10b155

Please sign in to comment.