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

refactor: Unify the interface #568

Merged
merged 1 commit into from
May 21, 2019
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
2 changes: 1 addition & 1 deletion pkg/controller/v1alpha2/experiment/experiment_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (r *ReconcileExperiment) createTrialInstance(expInstance *experimentsv1alph
logger.Error(err, "Error getting metrics collector manifest")
return err
}
trial.Spec.MetricsCollectorSpec = mcSpec.String()
trial.Spec.MetricsCollectorSpec = mcSpec

if err := r.Create(context.TODO(), trial); err != nil {
logger.Error(err, "Trial create error", "Trial name", trial.Name)
Expand Down
14 changes: 7 additions & 7 deletions pkg/controller/v1alpha2/experiment/manifest/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Generator interface {
InjectClient(c client.Client)
GetRunSpec(e *experimentsv1alpha2.Experiment, experiment, trial, namespace string) (string, error)
GetRunSpecWithHyperParameters(e *experimentsv1alpha2.Experiment, experiment, trial, namespace string, hps []*apiv1alpha2.ParameterAssignment) (string, error)
GetMetricsCollectorManifest(experimentName string, trialName string, jobKind string, namespace string, metricNames []string, mcs *experimentsv1alpha2.MetricsCollectorSpec) (*bytes.Buffer, error)
GetMetricsCollectorManifest(experimentName string, trialName string, jobKind string, namespace string, metricNames []string, mcs *experimentsv1alpha2.MetricsCollectorSpec) (string, error)
}

// DefaultGenerator is the default implementation of Generator.
Expand All @@ -45,7 +45,7 @@ func (g *DefaultGenerator) InjectClient(c client.Client) {
g.client.InjectClient(c)
}

func (g *DefaultGenerator) GetMetricsCollectorManifest(experimentName string, trialName string, jobKind string, namespace string, metricNames []string, mcs *experimentsv1alpha2.MetricsCollectorSpec) (*bytes.Buffer, error) {
func (g *DefaultGenerator) GetMetricsCollectorManifest(experimentName string, trialName string, jobKind string, namespace string, metricNames []string, mcs *experimentsv1alpha2.MetricsCollectorSpec) (string, error) {
var mtp *template.Template = nil
var err error
tmpValues := map[string]string{
Expand All @@ -65,23 +65,23 @@ func (g *DefaultGenerator) GetMetricsCollectorManifest(experimentName string, tr
}
mtl, err := g.client.GetMetricsCollectorTemplates()
if err != nil {
return nil, err
return "", err
}
if mt, ok := mtl[mctp]; !ok {
return nil, fmt.Errorf("No MetricsCollector template name %s", mctp)
return "", fmt.Errorf("No MetricsCollector template name %s", mctp)
} else {
mtp, err = template.New("MetricsCollector").Parse(mt)
}
}
if err != nil {
return nil, err
return "", err
}
var b bytes.Buffer
err = mtp.Execute(&b, tmpValues)
if err != nil {
return nil, err
return "", err
}
return &b, nil
return b.String(), nil
}

// GetRunSpec get the specification for trial.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ data:
- "-mn"
- "test"`

if expected != actual.String() {
if expected != actual {
t.Errorf("Expected %s, got %s", expected, actual)
}
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/controller/v1alpha2/experiment/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
batchv1beta "k8s.io/api/batch/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"

commonapiv1alpha2 "github.com/kubeflow/katib/pkg/api/operators/apis/common/v1alpha2"
experimentsv1alpha2 "github.com/kubeflow/katib/pkg/api/operators/apis/experiment/v1alpha2"
Expand Down Expand Up @@ -179,15 +179,16 @@ func (g *DefaultValidator) validateMetricsCollector(inst *experimentsv1alpha2.Ex
return fmt.Errorf("Invalid spec.trialTemplate: %v.", err)
}

var mcjob batchv1beta.CronJob
mcm, err := g.GetMetricsCollectorManifest(experimentName, trialName, job.GetKind(), namespace, metricNames, inst.Spec.MetricsCollectorSpec)
if err != nil {
log.Info("getMetricsCollectorManifest error", "err", err)
return err
}

log.Info("1", "m", mcm, "instance", inst)
if err := k8syaml.NewYAMLOrJSONDecoder(mcm, BUFSIZE).Decode(&mcjob); err != nil {
buf = bytes.NewBufferString(mcm)

mcjob := &batchv1beta.CronJob{}
if err := k8syaml.NewYAMLOrJSONDecoder(buf, BUFSIZE).Decode(&mcjob); err != nil {
log.Info("MetricsCollector Yaml decode error", "err", err)
return err
}
Expand Down
11 changes: 1 addition & 10 deletions pkg/controller/v1alpha2/experiment/validator/validator_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package validator

import (
"bytes"
"database/sql"
"testing"

Expand Down Expand Up @@ -104,15 +103,7 @@ spec:
p.EXPECT().GetMetricsCollectorManifest(
gomock.Any(), gomock.Any(), gomock.Any(),
gomock.Any(), gomock.Any(), gomock.Any()).
Return(func() *bytes.Buffer {
var b bytes.Buffer
_, err := b.WriteString(metricsCollectorTemplate)
if err != nil {
t.Errorf("Expected nil, got %v", err)
}
println(b.String())
return &b
}(), nil).AnyTimes()
Return(metricsCollectorTemplate, nil).AnyTimes()
mc.EXPECT().GetExperimentFromDB(gomock.Any()).Return(nil, sql.ErrNoRows).AnyTimes()

tcs := []struct {
Expand Down
5 changes: 2 additions & 3 deletions pkg/mock/v1alpha2/experiment/manifest/producer.go

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