Skip to content

Commit

Permalink
feat(experiment-validator): Inject client (#561)
Browse files Browse the repository at this point in the history
* feat: Inject client to generator in validator

Signed-off-by: Ce Gao <gaoce@caicloud.io>

* fix: mockgen

Signed-off-by: Ce Gao <gaoce@caicloud.io>
  • Loading branch information
gaocegege authored and k8s-ci-robot committed May 21, 2019
1 parent d2c64a7 commit 07b1343
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/controller/v1alpha2/experiment/manifest/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"text/template"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

experimentsv1alpha2 "github.com/kubeflow/katib/pkg/api/operators/apis/experiment/v1alpha2"
apiv1alpha2 "github.com/kubeflow/katib/pkg/api/v1alpha2"
commonv1alpha2 "github.com/kubeflow/katib/pkg/common/v1alpha2"
"github.com/kubeflow/katib/pkg/util/v1alpha2/katibclient"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
Expand All @@ -22,6 +22,7 @@ const (

// Generator is the type for manifests Generator.
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)
Expand All @@ -40,6 +41,10 @@ func New(c client.Client) Generator {
}
}

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) {
var mtp *template.Template = nil
var err error
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/v1alpha2/experiment/validation_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ var _ inject.Client = &experimentValidator{}
// InjectClient injects the client.
func (v *experimentValidator) InjectClient(c client.Client) error {
v.client = c
v.Validator.InjectClient(c)
return nil
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/controller/v1alpha2/experiment/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"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"

commonapiv1alpha2 "github.com/kubeflow/katib/pkg/api/operators/apis/common/v1alpha2"
experimentsv1alpha2 "github.com/kubeflow/katib/pkg/api/operators/apis/experiment/v1alpha2"
Expand All @@ -21,6 +22,7 @@ var log = logf.Log.WithName("experiment-controller")

type Validator interface {
ValidateExperiment(instance *experimentsv1alpha2.Experiment) error
InjectClient(c client.Client)
}

type DefaultValidator struct {
Expand All @@ -35,6 +37,10 @@ func New(generator manifest.Generator, managerClient managerclient.ManagerClient
}
}

func (g *DefaultValidator) InjectClient(c client.Client) {
g.Generator.InjectClient(c)
}

func (g *DefaultValidator) ValidateExperiment(instance *experimentsv1alpha2.Experiment) error {
if !instance.IsCreated() {
if err := g.validateForCreate(instance); err != nil {
Expand Down
13 changes: 13 additions & 0 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.

13 changes: 13 additions & 0 deletions pkg/mock/v1alpha2/util/katibclient/katibclient.go

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

5 changes: 5 additions & 0 deletions pkg/util/v1alpha2/katibclient/katib_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
)

type Client interface {
InjectClient(c client.Client)
GetExperimentList(namespace ...string) (*experimentsv1alpha2.ExperimentList, error)
CreateExperiment(experiment *experimentsv1alpha2.Experiment, namespace ...string) error
GetConfigMap(name string, namespace ...string) (map[string]string, error)
Expand Down Expand Up @@ -58,6 +59,10 @@ func NewClient(options client.Options) (Client, error) {
}, nil
}

func (k *KatibClient) InjectClient(c client.Client) {
k.client = c
}

func (k *KatibClient) GetExperimentList(namespace ...string) (*experimentsv1alpha2.ExperimentList, error) {
ns := getNamespace(namespace...)
expList := &experimentsv1alpha2.ExperimentList{}
Expand Down

0 comments on commit 07b1343

Please sign in to comment.