-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mengqi Yu
committed
Feb 25, 2019
1 parent
b51eb44
commit 8c836d7
Showing
7 changed files
with
198 additions
and
196 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 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,90 @@ | ||
/* | ||
Copyright 2018 The Kubernetes Authors. | ||
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 ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/runtime/inject" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
) | ||
|
||
// Defaulter defines functions for setting defaults on resources | ||
type Defaulter interface { | ||
runtime.Object | ||
Default() | ||
} | ||
|
||
// newDefaultingWebhookFor creates a new admission.Webhook for Defaulting the provided type. | ||
// Returns nil if o does not implement the Defaulter interface. | ||
func newDefaultingWebhookFor(o runtime.Object) *admission.Webhook { | ||
d, ok := o.(Defaulter) | ||
if !ok { | ||
// Type doesn't implement function for mutating webhook | ||
return nil | ||
} | ||
return &admission.Webhook{ | ||
Handler: &mutatingHandler{defaulter: d}, | ||
} | ||
} | ||
|
||
type mutatingHandler struct { | ||
defaulter Defaulter | ||
scheme *runtime.Scheme | ||
decoder *admission.Decoder | ||
} | ||
|
||
var _ inject.Scheme = &mutatingHandler{} | ||
|
||
// InjectScheme injects the scheme into mutatingHandler and initialize a decoder using the scheme. | ||
func (h *mutatingHandler) InjectScheme(s *runtime.Scheme) error { | ||
h.scheme = s | ||
var err error | ||
h.decoder, err = admission.NewDecoder(h.scheme) | ||
if err != nil { | ||
log.Error(err, "unable to get a decoder from the scheme for mutatingHandler") | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// Handle handles admission requests. | ||
func (h *mutatingHandler) Handle(ctx context.Context, req admission.Request) admission.Response { | ||
if h.defaulter == nil { | ||
return admission.Allowed("") | ||
} | ||
|
||
// Get the object in the request | ||
obj := h.defaulter.DeepCopyObject().(Defaulter) | ||
err := h.decoder.Decode(req.Object, obj) | ||
if err != nil { | ||
return admission.Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
// Default the object | ||
obj.Default() | ||
marshalled, err := json.Marshal(obj) | ||
if err != nil { | ||
return admission.Errored(http.StatusInternalServerError, err) | ||
} | ||
|
||
// Create the patch | ||
return admission.PatchResponseFromRaw(req.Object.Raw, marshalled) | ||
} |
Oops, something went wrong.