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

validate studyJob when first reconcile it #308

Merged
merged 2 commits into from
Dec 23, 2018
Merged
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
17 changes: 17 additions & 0 deletions Gopkg.lock

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

7 changes: 4 additions & 3 deletions pkg/controller/studyjob/katib_api_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package studyjob

import (
"context"
"fmt"
"log"

"github.com/kubeflow/katib/pkg"
Expand All @@ -26,13 +25,15 @@ import (
)

func initializeStudy(instance *katibv1alpha1.StudyJob, ns string) error {
if instance.Spec.SuggestionSpec == nil {
if validErr := validateStudy(instance, ns); validErr != nil {
instance.Status.Condition = katibv1alpha1.ConditionFailed
return fmt.Errorf("No Spec.SuggestionSpec specified.")
return validErr
}

if instance.Spec.SuggestionSpec.SuggestionAlgorithm == "" {
instance.Spec.SuggestionSpec.SuggestionAlgorithm = "random"
}

instance.Status.Condition = katibv1alpha1.ConditionRunning

conn, err := grpc.Dial(pkg.ManagerAddr, grpc.WithInsecure())
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/studyjob/manifest_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
katibv1alpha1 "github.com/kubeflow/katib/pkg/api/operators/apis/studyjob/v1alpha1"
"github.com/kubeflow/katib/pkg/manager/studyjobclient"

"k8s.io/apimachinery/pkg/util/uuid"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
)

Expand Down Expand Up @@ -103,7 +104,7 @@ func getWorkerManifest(c katibapi.ManagerClient, studyID string, trial *katibapi
}
var wid string
if dryrun {
wid = "validation"
wid = string(uuid.NewUUID())
} else {
cwreq := &katibapi.RegisterWorkerRequest{
Worker: &katibapi.Worker{
Expand Down
66 changes: 66 additions & 0 deletions pkg/controller/studyjob/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@ limitations under the License.
package studyjob

import (
"fmt"
"log"

katibapi "github.com/kubeflow/katib/pkg/api"
katibv1alpha1 "github.com/kubeflow/katib/pkg/api/operators/apis/studyjob/v1alpha1"
pytorchjobv1beta1 "github.com/kubeflow/pytorch-operator/pkg/apis/pytorch/v1beta1"
tfjobv1beta1 "github.com/kubeflow/tf-operator/pkg/apis/tensorflow/v1beta1"

batchv1 "k8s.io/api/batch/v1"
batchv1beta "k8s.io/api/batch/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
)

func createWorkerJobObj(kind string) runtime.Object {
Expand All @@ -30,3 +38,61 @@ func createWorkerJobObj(kind string) runtime.Object {
}
return nil
}

func validateStudy(instance *katibv1alpha1.StudyJob, namespace string) error {
if instance.Spec.SuggestionSpec == nil {
return fmt.Errorf("No Spec.SuggestionSpec specified.")
}
BUFSIZE := 1024
wkind, err := getWorkerKind(instance.Spec.WorkerSpec)
if err != nil {
log.Printf("getWorkerKind error %v", err)
return err
}

studyID := "studyID4Validation"
trialID := "trialID4Validation"
workerID, wm, err := getWorkerManifest(
nil,
studyID,
&katibapi.Trial{
TrialId: trialID,
ParameterSet: []*katibapi.Parameter{},
},
instance.Spec.WorkerSpec,
wkind,
namespace,
true,
)
if err != nil {
return err
}

job := createWorkerJobObj(wkind)
if err := k8syaml.NewYAMLOrJSONDecoder(wm, BUFSIZE).Decode(job); err != nil {
log.Printf("Yaml decode error %v", err)
return err
}

metav1Job := job.(metav1.Object)
if metav1Job.GetNamespace() != namespace || metav1Job.GetName() != workerID {
return fmt.Errorf("Invalid worker template.")
}

var mcjob batchv1beta.CronJob
mcm, err := getMetricsCollectorManifest(studyID, trialID, workerID, wkind, namespace, instance.Spec.MetricsCollectorSpec)
if err != nil {
log.Printf("getMetricsCollectorManifest error %v", err)
return err
}

if err := k8syaml.NewYAMLOrJSONDecoder(mcm, BUFSIZE).Decode(&mcjob); err != nil {
log.Printf("MetricsCollector Yaml decode error %v", err)
return err
}

if mcjob.GetNamespace() != namespace || mcjob.GetName() != workerID {
return fmt.Errorf("Invalid metricsCollector template.")
}
return nil
}