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(suggestion): Use interface #502

Merged
merged 8 commits into from
May 14, 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
85 changes: 85 additions & 0 deletions Gopkg.lock

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

55 changes: 38 additions & 17 deletions cmd/katib-controller/v1alpha2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,70 @@ limitations under the License.
package main

import (
"log"
"flag"
"os"

"github.com/kubeflow/katib/pkg/api/operators/apis"
controller "github.com/kubeflow/katib/pkg/controller/v1alpha2"
"github.com/spf13/viper"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/runtime/signals"

"github.com/kubeflow/katib/pkg/api/operators/apis"
controller "github.com/kubeflow/katib/pkg/controller/v1alpha2"
"github.com/kubeflow/katib/pkg/controller/v1alpha2/consts"
)

func main() {
logf.SetLogger(logf.ZapLogger(false))
log := logf.Log.WithName("entrypoint")

var experimentSuggestionName string

flag.StringVar(&experimentSuggestionName, "experiment-suggestion-name",
"default", "The implementation of suggestion interface in experiment controller (default|fake)")

flag.Parse()

viper.Set(consts.ConfigExperimentSuggestionName, experimentSuggestionName)
log.Info("Config:",
consts.ConfigExperimentSuggestionName,
viper.GetString(consts.ConfigExperimentSuggestionName))

// Get a config to talk to the apiserver
cfg, err := config.GetConfig()
if err != nil {
log.Printf("config.GetConfig()")
log.Fatal(err)
log.Error(err, "Fail to get the config")
os.Exit(1)
}

// Create a new katib controller to provide shared dependencies and start components
mgr, err := manager.New(cfg, manager.Options{})
if err != nil {
log.Printf("manager.New")
log.Fatal(err)
log.Error(err, "unable add APIs to scheme")
os.Exit(1)
}

log.Printf("Registering Components.")
log.Info("Registering Components.")

// Setup Scheme for all resources
if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
log.Printf("apis.AddToScheme")
log.Fatal(err)
log.Error(err, "Fail to create the manager")
os.Exit(1)
}

// Setup katib controller
// Setup all Controllers
log.Info("Setting up controller")
if err := controller.AddToManager(mgr); err != nil {
log.Printf("controller.AddToManager(mgr)")
log.Fatal(err)
log.Error(err, "unable to register controllers to the manager")
os.Exit(1)
}

log.Printf("Starting the Cmd.")

// Starting the katib controller
log.Fatal(mgr.Start(signals.SetupSignalHandler()))
// Start the Cmd
log.Info("Starting the Cmd.")
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
log.Error(err, "unable to run the manager")
os.Exit(1)
}
}
5 changes: 5 additions & 0 deletions pkg/controller/v1alpha2/consts/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package consts

const (
ConfigExperimentSuggestionName string = "experiment-suggestion-name"
)
43 changes: 34 additions & 9 deletions pkg/controller/v1alpha2/experiment/experiment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"os"

"github.com/spf13/viper"
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -38,10 +39,13 @@ import (

experimentsv1alpha2 "github.com/kubeflow/katib/pkg/api/operators/apis/experiment/v1alpha2"
trialsv1alpha2 "github.com/kubeflow/katib/pkg/api/operators/apis/trial/v1alpha2"
"github.com/kubeflow/katib/pkg/controller/v1alpha2/consts"
"github.com/kubeflow/katib/pkg/controller/v1alpha2/experiment/suggestion"
suggestionfake "github.com/kubeflow/katib/pkg/controller/v1alpha2/experiment/suggestion/fake"
"github.com/kubeflow/katib/pkg/controller/v1alpha2/experiment/util"
)

const KatibController = "katib-controller"
const katibControllerName = "katib-controller"

var log = logf.Log.WithName("experiment-controller")

Expand All @@ -58,7 +62,30 @@ func Add(mgr manager.Manager) error {

// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
return &ReconcileExperiment{Client: mgr.GetClient(), scheme: mgr.GetScheme()}
r := &ReconcileExperiment{
Client: mgr.GetClient(),
scheme: mgr.GetScheme(),
}
imp := viper.GetString(consts.ConfigExperimentSuggestionName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to check: Is there a better way to pass flags to the manager than using viper?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea. Kubebuilder does not support a built-in way, thus I use viper here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work for you?

r.Suggestion = newSuggestion(imp)
return r
}

// newSuggestion returns the new Suggestion for the given config.
func newSuggestion(config string) suggestion.Suggestion {
// Use different implementation according to the configuration.
switch config {
case "fake":
log.Info("Using the fake suggestion implementation")
return suggestionfake.New()
case "default":
log.Info("Using the default suggestion implementation")
return suggestion.New()
default:
log.Info("No valid name specified, using the default suggestion implementation",
"implementation", config)
return suggestion.New()
}
}

// add adds a new Controller to mgr with r as the reconcile.Reconciler
Expand Down Expand Up @@ -126,13 +153,13 @@ func addWebhook(mgr manager.Manager) error {
BootstrapOptions: &webhook.BootstrapOptions{
Secret: &types.NamespacedName{
Namespace: os.Getenv(experimentsv1alpha2.DefaultKatibNamespaceEnvName),
Name: KatibController,
Name: katibControllerName,
},
Service: &webhook.Service{
Namespace: os.Getenv(experimentsv1alpha2.DefaultKatibNamespaceEnvName),
Name: KatibController,
Name: katibControllerName,
Selectors: map[string]string{
"app": KatibController,
"app": katibControllerName,
},
},
ValidatingWebhookConfigName: "experiment-validating-webhook-config",
Expand All @@ -155,6 +182,7 @@ var _ reconcile.Reconciler = &ReconcileExperiment{}
type ReconcileExperiment struct {
client.Client
scheme *runtime.Scheme
suggestion.Suggestion
}

// Reconcile reads that state of the cluster for a Experiment object and makes changes based on the state read
Expand Down Expand Up @@ -303,14 +331,11 @@ func (r *ReconcileExperiment) ReconcileTrials(instance *experimentsv1alpha2.Expe
func (r *ReconcileExperiment) createTrials(instance *experimentsv1alpha2.Experiment, addCount int) error {

logger := log.WithValues("Experiment", types.NamespacedName{Name: instance.GetName(), Namespace: instance.GetNamespace()})
trials, err := util.GetSuggestions(instance, addCount)
trials, err := r.GetSuggestions(instance, addCount)
if err != nil {
logger.Error(err, "Get suggestions error")
return err
}
/*trials := []apiv1alpha2.Trial{
apiv1alpha2.Trial{Spec: &apiv1alpha2.TrialSpec{}}, apiv1alpha2.Trial{Spec: &apiv1alpha2.TrialSpec{}},
}*/
for _, trial := range trials {
if err = r.createTrialInstance(instance, trial); err != nil {
logger.Error(err, "Create trial instance error", "trial", trial)
Expand Down
18 changes: 18 additions & 0 deletions pkg/controller/v1alpha2/experiment/suggestion/fake/fake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package fake

import (
experimentsv1alpha2 "github.com/kubeflow/katib/pkg/api/operators/apis/experiment/v1alpha2"
api_pb "github.com/kubeflow/katib/pkg/api/v1alpha2"
"github.com/kubeflow/katib/pkg/controller/v1alpha2/experiment/suggestion"
)

type Fake struct {
}

func New() suggestion.Suggestion {
return &Fake{}
}

func (k *Fake) GetSuggestions(instance *experimentsv1alpha2.Experiment, addCount int) ([]*api_pb.Trial, error) {
return nil, nil
}
23 changes: 23 additions & 0 deletions pkg/controller/v1alpha2/experiment/suggestion/suggestion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package suggestion

import (
"fmt"

experimentsv1alpha2 "github.com/kubeflow/katib/pkg/api/operators/apis/experiment/v1alpha2"
api_pb "github.com/kubeflow/katib/pkg/api/v1alpha2"
)

type Suggestion interface {
GetSuggestions(instance *experimentsv1alpha2.Experiment, addCount int) ([]*api_pb.Trial, error)
}

type General struct {
}

func New() Suggestion {
return &General{}
}

func (g *General) GetSuggestions(instance *experimentsv1alpha2.Experiment, addCount int) ([]*api_pb.Trial, error) {
return nil, fmt.Errorf("Not implemented")
}
Loading