-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mutating and validating webhook for CloneSet (#209)
Signed-off-by: Siyu Wang <[email protected]>
- Loading branch information
Showing
15 changed files
with
967 additions
and
3 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
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,53 @@ | ||
/* | ||
Copyright 2019 The Kruise 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 defaultserver | ||
|
||
import ( | ||
"fmt" | ||
|
||
appsv1alpha1 "github.com/openkruise/kruise/pkg/apis/apps/v1alpha1" | ||
"github.com/openkruise/kruise/pkg/util/gate" | ||
"github.com/openkruise/kruise/pkg/webhook/default_server/cloneset/mutating" | ||
) | ||
|
||
func init() { | ||
if !gate.ResourceEnabled(&appsv1alpha1.CloneSet{}) { | ||
return | ||
} | ||
for k, v := range mutating.Builders { | ||
_, found := builderMap[k] | ||
if found { | ||
log.V(1).Info(fmt.Sprintf( | ||
"conflicting webhook builder names in builder map: %v", k)) | ||
} | ||
builderMap[k] = v | ||
} | ||
for k, v := range mutating.HandlerMap { | ||
_, found := HandlerMap[k] | ||
if found { | ||
log.V(1).Info(fmt.Sprintf( | ||
"conflicting webhook builder names in handler map: %v", k)) | ||
} | ||
_, found = builderMap[k] | ||
if !found { | ||
log.V(1).Info(fmt.Sprintf( | ||
"can't find webhook builder name %q in builder map", k)) | ||
continue | ||
} | ||
HandlerMap[k] = v | ||
} | ||
} |
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,53 @@ | ||
/* | ||
Copyright 2019 The Kruise 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 defaultserver | ||
|
||
import ( | ||
"fmt" | ||
|
||
appsv1alpha1 "github.com/openkruise/kruise/pkg/apis/apps/v1alpha1" | ||
"github.com/openkruise/kruise/pkg/util/gate" | ||
"github.com/openkruise/kruise/pkg/webhook/default_server/cloneset/validating" | ||
) | ||
|
||
func init() { | ||
if !gate.ResourceEnabled(&appsv1alpha1.CloneSet{}) { | ||
return | ||
} | ||
for k, v := range validating.Builders { | ||
_, found := builderMap[k] | ||
if found { | ||
log.V(1).Info(fmt.Sprintf( | ||
"conflicting webhook builder names in builder map: %v", k)) | ||
} | ||
builderMap[k] = v | ||
} | ||
for k, v := range validating.HandlerMap { | ||
_, found := HandlerMap[k] | ||
if found { | ||
log.V(1).Info(fmt.Sprintf( | ||
"conflicting webhook builder names in handler map: %v", k)) | ||
} | ||
_, found = builderMap[k] | ||
if !found { | ||
log.V(1).Info(fmt.Sprintf( | ||
"can't find webhook builder name %q in builder map", k)) | ||
continue | ||
} | ||
HandlerMap[k] = v | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
pkg/webhook/default_server/cloneset/mutating/cloneset_create_update_handler.go
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,92 @@ | ||
/* | ||
Copyright 2019 The Kruise 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 mutating | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/openkruise/kruise/pkg/util" | ||
patchutil "github.com/openkruise/kruise/pkg/util/patch" | ||
"k8s.io/klog" | ||
|
||
appsv1alpha1 "github.com/openkruise/kruise/pkg/apis/apps/v1alpha1" | ||
"sigs.k8s.io/controller-runtime/pkg/runtime/inject" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/types" | ||
) | ||
|
||
func init() { | ||
webhookName := "mutating-create-update-cloneset" | ||
if HandlerMap[webhookName] == nil { | ||
HandlerMap[webhookName] = []admission.Handler{} | ||
} | ||
HandlerMap[webhookName] = append(HandlerMap[webhookName], &CloneSetCreateUpdateHandler{}) | ||
} | ||
|
||
// CloneSetCreateUpdateHandler handles CloneSet | ||
type CloneSetCreateUpdateHandler struct { | ||
// To use the client, you need to do the following: | ||
// - uncomment it | ||
// - import sigs.k8s.io/controller-runtime/pkg/client | ||
// - uncomment the InjectClient method at the bottom of this file. | ||
// Client client.Client | ||
|
||
// Decoder decodes objects | ||
Decoder types.Decoder | ||
} | ||
|
||
var _ admission.Handler = &CloneSetCreateUpdateHandler{} | ||
|
||
// Handle handles admission requests. | ||
func (h *CloneSetCreateUpdateHandler) Handle(ctx context.Context, req types.Request) types.Response { | ||
obj := &appsv1alpha1.CloneSet{} | ||
|
||
err := h.Decoder.Decode(req, obj) | ||
if err != nil { | ||
return admission.ErrorResponse(http.StatusBadRequest, err) | ||
} | ||
|
||
appsv1alpha1.SetDefaults_CloneSet(obj) | ||
|
||
marshaled, err := json.Marshal(obj) | ||
if err != nil { | ||
return admission.ErrorResponse(http.StatusInternalServerError, err) | ||
} | ||
resp := patchutil.ResponseFromRaw(req.AdmissionRequest.Object.Raw, marshaled) | ||
if len(resp.Patches) > 0 { | ||
klog.V(5).Infof("Admit CloneSet %s/%s patches: %v", obj.Namespace, obj.Name, util.DumpJSON(resp.Patches)) | ||
} | ||
return resp | ||
} | ||
|
||
//var _ inject.Client = &CloneSetCreateUpdateHandler{} | ||
// | ||
//// InjectClient injects the client into the CloneSetCreateUpdateHandler | ||
//func (h *CloneSetCreateUpdateHandler) InjectClient(c client.Client) error { | ||
// h.Client = c | ||
// return nil | ||
//} | ||
|
||
var _ inject.Decoder = &CloneSetCreateUpdateHandler{} | ||
|
||
// InjectDecoder injects the decoder into the CloneSetCreateUpdateHandler | ||
func (h *CloneSetCreateUpdateHandler) InjectDecoder(d types.Decoder) error { | ||
h.Decoder = d | ||
return nil | ||
} |
35 changes: 35 additions & 0 deletions
35
pkg/webhook/default_server/cloneset/mutating/create_update_webhook.go
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,35 @@ | ||
/* | ||
Copyright 2019 The Kruise 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 mutating | ||
|
||
import ( | ||
appsv1alpha1 "github.com/openkruise/kruise/pkg/apis/apps/v1alpha1" | ||
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/builder" | ||
) | ||
|
||
func init() { | ||
builderName := "mutating-create-update-cloneset" | ||
Builders[builderName] = builder. | ||
NewWebhookBuilder(). | ||
Name(builderName+".kruise.io"). | ||
Path("/"+builderName). | ||
Mutating(). | ||
Operations(admissionregistrationv1beta1.Create, admissionregistrationv1beta1.Update). | ||
FailurePolicy(admissionregistrationv1beta1.Fail). | ||
ForType(&appsv1alpha1.CloneSet{}) | ||
} |
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,29 @@ | ||
/* | ||
Copyright 2019 The Kruise 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 mutating | ||
|
||
import ( | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/builder" | ||
) | ||
|
||
var ( | ||
// Builders contain admission webhook builders | ||
Builders = map[string]*builder.WebhookBuilder{} | ||
// HandlerMap contains admission webhook handlers | ||
HandlerMap = map[string][]admission.Handler{} | ||
) |
Oops, something went wrong.