Skip to content

Commit

Permalink
Add Validate Algorithm Settings in v1alpha2 (#574)
Browse files Browse the repository at this point in the history
* Add Validate Algorithm Settings

* Integrate ValidateAlgorithmSettings in ManagerClient

* Run dep ensure
  • Loading branch information
andreyvelich authored and k8s-ci-robot committed May 23, 2019
1 parent 200c59d commit d4d87ca
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 32 deletions.
11 changes: 11 additions & 0 deletions pkg/common/v1alpha2/katib_manager_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,14 @@ func GetObservationLog(request *api_pb.GetObservationLogRequest) (*api_pb.GetObs
kc := kcc.KatibManagerClient
return kc.GetObservationLog(ctx, request)
}

func ValidateAlgorithmSettings(request *api_pb.ValidateAlgorithmSettingsRequest) (*api_pb.ValidateAlgorithmSettingsReply, error) {
ctx := context.Background()
kcc, err := getKatibManagerClientAndConn()
if err != nil {
return nil, err
}
defer closeKatibManagerConnection(kcc)
kc := kcc.KatibManagerClient
return kc.ValidateAlgorithmSettings(ctx, request)
}
90 changes: 59 additions & 31 deletions pkg/controller/v1alpha2/experiment/managerclient/managerclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type ManagerClient interface {
DeleteExperimentInDB(instance *experimentsv1alpha2.Experiment) error
UpdateExperimentStatusInDB(instance *experimentsv1alpha2.Experiment) error
PreCheckRegisterExperimentInDB(inst *experimentsv1alpha2.Experiment) (*api_pb.PreCheckRegisterExperimentReply, error)
ValidateAlgorithmSettings(inst *experimentsv1alpha2.Experiment) (*api_pb.ValidateAlgorithmSettingsReply, error)
}

// DefaultClient implements the Client interface.
Expand Down Expand Up @@ -70,6 +71,17 @@ func (d *DefaultClient) PreCheckRegisterExperimentInDB(inst *experimentsv1alpha2
return commonv1alpha2.PreCheckRegisterExperiment(request)
}

func (d *DefaultClient) ValidateAlgorithmSettings(inst *experimentsv1alpha2.Experiment) (*api_pb.ValidateAlgorithmSettingsReply, error) {
algorithmName := inst.Spec.Algorithm.AlgorithmName
request := &api_pb.ValidateAlgorithmSettingsRequest{
AlgorithmName: algorithmName,
ExperimentSpec: getExperimentSpec(inst),
}

return commonv1alpha2.ValidateAlgorithmSettings(request)

}

func getExperimentConf(instance *experimentsv1alpha2.Experiment) *api_pb.Experiment {
experiment := &api_pb.Experiment{
Spec: &api_pb.ExperimentSpec{
Expand All @@ -89,28 +101,63 @@ func getExperimentConf(instance *experimentsv1alpha2.Experiment) *api_pb.Experim

experiment.Name = instance.Name

experiment.Spec = getExperimentSpec(instance)

return experiment

}

func getCondition(inst *experimentsv1alpha2.Experiment) api_pb.ExperimentStatus_ExperimentConditionType {
condition, _ := inst.GetLastConditionType()
switch condition {
case experimentsv1alpha2.ExperimentCreated:
return api_pb.ExperimentStatus_CREATED
case experimentsv1alpha2.ExperimentRunning:
return api_pb.ExperimentStatus_RUNNING
case experimentsv1alpha2.ExperimentRestarting:
return api_pb.ExperimentStatus_RESTARTING
case experimentsv1alpha2.ExperimentSucceeded:
return api_pb.ExperimentStatus_SUCCEEDED
case experimentsv1alpha2.ExperimentFailed:
return api_pb.ExperimentStatus_FAILED
default:
return api_pb.ExperimentStatus_UNKNOWN
}
}

func getExperimentSpec(instance *experimentsv1alpha2.Experiment) *api_pb.ExperimentSpec {

experimentSpec := &api_pb.ExperimentSpec{
Objective: &api_pb.ObjectiveSpec{
AdditionalMetricNames: []string{},
},
Algorithm: &api_pb.AlgorithmSpec{
AlgorithmSetting: []*api_pb.AlgorithmSetting{},
},
}

//Populate Objective
switch instance.Spec.Objective.Type {
case commonapiv1alpha2.ObjectiveTypeMaximize:
experiment.Spec.Objective.Type = api_pb.ObjectiveType_MAXIMIZE
experimentSpec.Objective.Type = api_pb.ObjectiveType_MAXIMIZE
case commonapiv1alpha2.ObjectiveTypeMinimize:
experiment.Spec.Objective.Type = api_pb.ObjectiveType_MINIMIZE
experimentSpec.Objective.Type = api_pb.ObjectiveType_MINIMIZE
default:
experiment.Spec.Objective.Type = api_pb.ObjectiveType_UNKNOWN
experimentSpec.Objective.Type = api_pb.ObjectiveType_UNKNOWN

}
experiment.Spec.Objective.Goal = float32(*instance.Spec.Objective.Goal)
experiment.Spec.Objective.ObjectiveMetricName = instance.Spec.Objective.ObjectiveMetricName
experimentSpec.Objective.Goal = float32(*instance.Spec.Objective.Goal)
experimentSpec.Objective.ObjectiveMetricName = instance.Spec.Objective.ObjectiveMetricName
for _, m := range instance.Spec.Objective.AdditionalMetricNames {
experiment.Spec.Objective.AdditionalMetricNames = append(experiment.Spec.Objective.AdditionalMetricNames, m)
experimentSpec.Objective.AdditionalMetricNames = append(experimentSpec.Objective.AdditionalMetricNames, m)
}

//Populate Algorithm Spec
experiment.Spec.Algorithm.AlgorithmName = instance.Spec.Algorithm.AlgorithmName
experimentSpec.Algorithm.AlgorithmName = instance.Spec.Algorithm.AlgorithmName

for _, as := range instance.Spec.Algorithm.AlgorithmSettings {
experiment.Spec.Algorithm.AlgorithmSetting = append(
experiment.Spec.Algorithm.AlgorithmSetting,
experimentSpec.Algorithm.AlgorithmSetting = append(
experimentSpec.Algorithm.AlgorithmSetting,
&api_pb.AlgorithmSetting{
Name: as.Name,
Value: as.Value,
Expand Down Expand Up @@ -146,7 +193,7 @@ func getExperimentConf(instance *experimentsv1alpha2.Experiment) *api_pb.Experim
}
parameterSpecs.Parameters = append(parameterSpecs.Parameters, parameter)
}
experiment.Spec.ParameterSpecs = parameterSpecs
experimentSpec.ParameterSpecs = parameterSpecs
}

//Populate NAS Experiment
Expand Down Expand Up @@ -205,27 +252,8 @@ func getExperimentConf(instance *experimentsv1alpha2.Experiment) *api_pb.Experim
nasConfig.Operations.Operation = append(nasConfig.Operations.Operation, operation)
}

experiment.Spec.NasConfig = nasConfig
experimentSpec.NasConfig = nasConfig
}

return experiment

}

func getCondition(inst *experimentsv1alpha2.Experiment) api_pb.ExperimentStatus_ExperimentConditionType {
condition, _ := inst.GetLastConditionType()
switch condition {
case experimentsv1alpha2.ExperimentCreated:
return api_pb.ExperimentStatus_CREATED
case experimentsv1alpha2.ExperimentRunning:
return api_pb.ExperimentStatus_RUNNING
case experimentsv1alpha2.ExperimentRestarting:
return api_pb.ExperimentStatus_RESTARTING
case experimentsv1alpha2.ExperimentSucceeded:
return api_pb.ExperimentStatus_SUCCEEDED
case experimentsv1alpha2.ExperimentFailed:
return api_pb.ExperimentStatus_FAILED
default:
return api_pb.ExperimentStatus_UNKNOWN
}
return experimentSpec
}
15 changes: 14 additions & 1 deletion pkg/controller/v1alpha2/experiment/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"fmt"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
batchv1beta "k8s.io/api/batch/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
Expand Down Expand Up @@ -77,8 +79,19 @@ func (g *DefaultValidator) ValidateExperiment(instance *experimentsv1alpha2.Expe
}

func (g *DefaultValidator) validateAlgorithmSettings(inst *experimentsv1alpha2.Experiment) error {
// TODO: it need call ValidateAlgorithmSettings API of vizier-core manager, implement it when vizier-core done

_, err := g.ValidateAlgorithmSettings(inst)
statusCode, _ := status.FromError(err)

if statusCode.Code() == codes.Unknown {
return fmt.Errorf("Method ValidateAlgorithmSettings not found inside Suggestion service: %s", inst.Spec.Algorithm.AlgorithmName)
}

if statusCode.Code() == codes.InvalidArgument || statusCode.Code() == codes.Unavailable {
return fmt.Errorf("ValidateAlgorithmSettings Error: %v", statusCode.Message())
}
return nil

}

func (g *DefaultValidator) validateObjective(obj *commonapiv1alpha2.ObjectiveSpec) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ spec:
CanRegister: true,
}, nil).AnyTimes()

mc.EXPECT().ValidateAlgorithmSettings(gomock.Any()).Return(
&api_pb.ValidateAlgorithmSettingsReply{}, nil).AnyTimes()

tcs := []struct {
Instance *experimentsv1alpha2.Experiment
Err bool
Expand Down
15 changes: 15 additions & 0 deletions pkg/mock/v1alpha2/experiment/managerclient/managerclient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d4d87ca

Please sign in to comment.