-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: wenqi <[email protected]>
- Loading branch information
wenqi
authored and
wenqi
committed
Apr 8, 2019
1 parent
8d01af0
commit f964c88
Showing
7 changed files
with
202 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Copyright (c) 2019 Dell Inc., or its subsidiaries. All Rights Reserved. | ||
* | ||
* 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 | ||
*/ | ||
|
||
package webhook | ||
|
||
import ( | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
) | ||
|
||
// AddToManagerFuncs is a list of functions to add all Webhooks to the Manager | ||
var AddToManagerFuncs []func(manager.Manager) error | ||
|
||
func init() { | ||
// AddToManagerFuncs is a list of functions to create Webhooks and add them to a manager. | ||
AddToManagerFuncs = append(AddToManagerFuncs, Add) | ||
} | ||
|
||
// AddToManager adds all Webhooks to the Manager | ||
func AddToManager(m manager.Manager) error { | ||
for _, f := range AddToManagerFuncs { | ||
if err := f(m); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/** | ||
* Copyright (c) 2019 Dell Inc., or its subsidiaries. All Rights Reserved. | ||
* | ||
* 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 | ||
*/ | ||
|
||
package webhook | ||
|
||
import ( | ||
"context" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"net/http" | ||
"os" | ||
|
||
pravegav1alpha1 "github.com/pravega/pravega-operator/pkg/apis/pravega/v1alpha1" | ||
|
||
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1" | ||
"k8s.io/apimachinery/pkg/types" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/builder" | ||
admissiontypes "sigs.k8s.io/controller-runtime/pkg/webhook/admission/types" | ||
"sigs.k8s.io/controller-runtime/pkg/runtime/inject" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
const ( | ||
CertDir = "/var/run/secrets/kubernetes.io/" | ||
) | ||
|
||
// Create webhook server and register webhook to it | ||
func Add(mgr manager.Manager) error { | ||
log.Printf("Initializing webhook") | ||
svr, err := newWebhookServer(mgr) | ||
if err != nil { | ||
log.Printf("Failed to create webhook server: %v", err) | ||
return err | ||
} | ||
|
||
wh, err := newValidatingWebhook(mgr) | ||
if err != nil { | ||
log.Printf("Failed to create validating webhook: %v", err) | ||
return err | ||
} | ||
|
||
svr.Register(wh) | ||
return nil | ||
} | ||
|
||
func newValidatingWebhook(mgr manager.Manager) (*admission.Webhook, error){ | ||
return builder.NewWebhookBuilder(). | ||
Mutating(). | ||
Operations(admissionregistrationv1beta1.Create). | ||
ForType(&pravegav1alpha1.PravegaCluster{}). | ||
Handlers(&pravegaWebhookHandler{}). | ||
WithManager(mgr). | ||
Build() | ||
} | ||
|
||
func newWebhookServer(mgr manager.Manager) (*webhook.Server, error) { | ||
return webhook.NewServer("admission-webhook-server", mgr, webhook.ServerOptions{ | ||
CertDir: CertDir, | ||
BootstrapOptions: &webhook.BootstrapOptions{ | ||
Secret: &types.NamespacedName{ | ||
Namespace: os.Getenv("WATCH_NAMESPACE"), | ||
Name: os.Getenv("SECRET_NAME"), | ||
}, | ||
|
||
// TODO: garbage collect webhook service | ||
Service: &webhook.Service{ | ||
Namespace: os.Getenv("WATCH_NAMESPACE"), | ||
Name: "admission-webhook-server-service", | ||
Selectors: map[string]string{ | ||
"component": "operator", | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
type pravegaWebhookHandler struct { | ||
client client.Client | ||
scheme *runtime.Scheme | ||
decoder admissiontypes.Decoder | ||
} | ||
|
||
var _ admission.Handler = &pravegaWebhookHandler{} | ||
|
||
func (pwh *pravegaWebhookHandler) Handle(ctx context.Context, req admissiontypes.Request) admissiontypes.Response { | ||
log.Printf("Webhook is handling incoming requests") | ||
pravega := &pravegav1alpha1.PravegaCluster{} | ||
|
||
if err := pwh.decoder.Decode(req, pravega); err != nil { | ||
return admission.ErrorResponse(http.StatusBadRequest, err) | ||
} | ||
copy := pravega.DeepCopy() | ||
|
||
err := pwh.validatePravegaManifest(ctx, copy) | ||
if err != nil { | ||
return admission.ErrorResponse(http.StatusInternalServerError, err) | ||
} | ||
|
||
return admission.ValidationResponse(true, "") | ||
} | ||
|
||
|
||
func (pwh *pravegaWebhookHandler) validatePravegaManifest(ctx context.Context, pod *pravegav1alpha1.PravegaCluster) error { | ||
// TODO: implement logic to validate a upgrade version | ||
return nil | ||
} | ||
|
||
// pravegaWebhookHandler implements inject.Client. | ||
var _ inject.Client = &pravegaWebhookHandler{} | ||
|
||
// InjectClient injects the client into the pravegaWebhookHandler | ||
func (pwh *pravegaWebhookHandler) InjectClient(c client.Client) error { | ||
pwh.client = c | ||
return nil | ||
} | ||
|
||
// pravegaWebhookHandler implements inject.Decoder. | ||
var _ inject.Decoder = &pravegaWebhookHandler{} | ||
|
||
// InjectDecoder injects the decoder into the pravegaWebhookHandler | ||
func (pwh *pravegaWebhookHandler) InjectDecoder(d admissiontypes.Decoder) error { | ||
pwh.decoder = d | ||
return nil | ||
} | ||
|
||
// pravegaWebhookHandler implements inject.Scheme. | ||
var _ inject.Scheme = &pravegaWebhookHandler{} | ||
|
||
// InjectClient injects the client into the pravegaWebhookHandler | ||
func (pwh *pravegaWebhookHandler) InjectScheme(s *runtime.Scheme) error { | ||
pwh.scheme = s | ||
return nil | ||
} |