Skip to content
Permalink
Browse files

Refactor webhook organization (#711)

  • Loading branch information...
hougangliu authored and k8s-ci-robot committed Aug 5, 2019
1 parent de96a52 commit 6d4dd4750cb15b159fb1b353f1db6fdf3ce72152
@@ -30,6 +30,7 @@ import (
"github.com/kubeflow/katib/pkg/api/operators/apis"
controller "github.com/kubeflow/katib/pkg/controller/v1alpha2"
"github.com/kubeflow/katib/pkg/controller/v1alpha2/consts"
webhook "github.com/kubeflow/katib/pkg/webhook/v1alpha2"
)

func main() {
@@ -77,6 +78,12 @@ func main() {
os.Exit(1)
}

log.Info("Setting up webhooks")
if err := webhook.AddToManager(mgr); err != nil {
log.Error(err, "unable to register webhooks to the manager")
os.Exit(1)
}

// Start the Cmd
log.Info("Starting the Cmd.")
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
@@ -18,11 +18,9 @@ package experiment

import (
"context"
"os"
"sort"

"github.com/spf13/viper"
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -35,8 +33,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/source"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/builder"

experimentsv1alpha2 "github.com/kubeflow/katib/pkg/api/operators/apis/experiment/v1alpha2"
trialsv1alpha2 "github.com/kubeflow/katib/pkg/api/operators/apis/trial/v1alpha2"
@@ -48,8 +44,6 @@ import (
"github.com/kubeflow/katib/pkg/controller/v1alpha2/experiment/util"
)

const katibControllerName = "katib-controller"

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

/**
@@ -108,10 +102,6 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
log.Error(err, "Trial watch failed")
return err
}
if err = addWebhook(mgr); err != nil {
log.Error(err, "Failed to create webhook")
return err
}

log.Info("Experiment controller created")
return nil
@@ -141,57 +131,6 @@ func addWatch(mgr manager.Manager, c controller.Controller) error {
return nil
}

func addWebhook(mgr manager.Manager) error {
mutatingWebhook, err := builder.NewWebhookBuilder().
Name("mutating.experiment.kubeflow.org").
Mutating().
Operations(admissionregistrationv1beta1.Create, admissionregistrationv1beta1.Update).
WithManager(mgr).
ForType(&experimentsv1alpha2.Experiment{}).
Handlers(&experimentDefaulter{}).
Build()
if err != nil {
return err
}
validatingWebhook, err := builder.NewWebhookBuilder().
Name("validating.experiment.kubeflow.org").
Validating().
Operations(admissionregistrationv1beta1.Create, admissionregistrationv1beta1.Update).
WithManager(mgr).
ForType(&experimentsv1alpha2.Experiment{}).
Handlers(newExperimentValidator(mgr.GetClient())).
Build()
if err != nil {
return err
}
as, err := webhook.NewServer("experiment-admission-server", mgr, webhook.ServerOptions{
CertDir: "/tmp/cert",
BootstrapOptions: &webhook.BootstrapOptions{
Secret: &types.NamespacedName{
Namespace: os.Getenv(experimentsv1alpha2.DefaultKatibNamespaceEnvName),
Name: katibControllerName,
},
Service: &webhook.Service{
Namespace: os.Getenv(experimentsv1alpha2.DefaultKatibNamespaceEnvName),
Name: katibControllerName,
Selectors: map[string]string{
"app": katibControllerName,
},
},
ValidatingWebhookConfigName: "experiment-validating-webhook-config",
MutatingWebhookConfigName: "experiment-mutating-webhook-config",
},
})
if err != nil {
return err
}
err = as.Register(mutatingWebhook, validatingWebhook)
if err != nil {
return err
}
return nil
}

var _ reconcile.Reconciler = &ReconcileExperiment{}

// ReconcileExperiment reconciles a Experiment object
@@ -62,3 +62,9 @@ func (e *experimentDefaulter) InjectDecoder(d types.Decoder) error {
e.decoder = d
return nil
}

func NewExperimentDefaulter(c client.Client) *experimentDefaulter {
return &experimentDefaulter{
client: c,
}
}
@@ -28,7 +28,7 @@ import (
experimentsv1alpha2 "github.com/kubeflow/katib/pkg/api/operators/apis/experiment/v1alpha2"
"github.com/kubeflow/katib/pkg/controller/v1alpha2/experiment/managerclient"
"github.com/kubeflow/katib/pkg/controller/v1alpha2/experiment/manifest"
"github.com/kubeflow/katib/pkg/controller/v1alpha2/experiment/validator"
"github.com/kubeflow/katib/pkg/webhook/v1alpha2/experiment/validator"
)

// experimentValidator validates Pods
@@ -39,7 +39,7 @@ type experimentValidator struct {
validator.Validator
}

func newExperimentValidator(c client.Client) *experimentValidator {
func NewExperimentValidator(c client.Client) *experimentValidator {
p := manifest.New(c)
mc := managerclient.New()
return &experimentValidator{
@@ -19,7 +19,7 @@ import (
"github.com/kubeflow/katib/pkg/controller/v1alpha2/experiment/manifest"
)

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

type Validator interface {
ValidateExperiment(instance *experimentsv1alpha2.Experiment) error
@@ -0,0 +1,88 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package webhook

import (
"os"

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

admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
"k8s.io/apimachinery/pkg/types"

"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/builder"
)

const katibControllerName = "katib-controller"

func AddToManager(m manager.Manager) error {
server, err := webhook.NewServer("katib-admission-server", m, webhook.ServerOptions{
CertDir: "/tmp/cert",
BootstrapOptions: &webhook.BootstrapOptions{
Secret: &types.NamespacedName{
Namespace: os.Getenv(experimentsv1alpha2.DefaultKatibNamespaceEnvName),
Name: katibControllerName,
},
Service: &webhook.Service{
Namespace: os.Getenv(experimentsv1alpha2.DefaultKatibNamespaceEnvName),
Name: katibControllerName,
Selectors: map[string]string{
"app": katibControllerName,
},
},
ValidatingWebhookConfigName: "katib-validating-webhook-config",
MutatingWebhookConfigName: "katib-mutating-webhook-config",
},
})
if err != nil {
return err
}

if err := register(m, server); err != nil {
return err
}

return nil
}

func register(manager manager.Manager, server *webhook.Server) error {
mutatingWebhook, err := builder.NewWebhookBuilder().
Name("mutating.experiment.kubeflow.org").
Mutating().
Operations(admissionregistrationv1beta1.Create, admissionregistrationv1beta1.Update).
WithManager(manager).
ForType(&experimentsv1alpha2.Experiment{}).
Handlers(experiment.NewExperimentDefaulter(manager.GetClient())).
Build()
if err != nil {
return err
}
validatingWebhook, err := builder.NewWebhookBuilder().
Name("validating.experiment.kubeflow.org").
Validating().
Operations(admissionregistrationv1beta1.Create, admissionregistrationv1beta1.Update).
WithManager(manager).
ForType(&experimentsv1alpha2.Experiment{}).
Handlers(experiment.NewExperimentValidator(manager.GetClient())).
Build()
if err != nil {
return err
}
return server.Register(mutatingWebhook, validatingWebhook)
}

0 comments on commit 6d4dd47

Please sign in to comment.
You can’t perform that action at this time.