| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You 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 webhooks |
| |
| import ( |
| "context" |
| "encoding/json" |
| "net/http" |
| ) |
| |
| import ( |
| "k8s.io/apimachinery/pkg/runtime" |
| |
| "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| ) |
| |
| import ( |
| core_model "github.com/apache/dubbo-kubernetes/pkg/core/resources/model" |
| "github.com/apache/dubbo-kubernetes/pkg/core/resources/registry" |
| k8s_common "github.com/apache/dubbo-kubernetes/pkg/plugins/common/k8s" |
| "github.com/apache/dubbo-kubernetes/pkg/plugins/runtime/k8s/metadata" |
| ) |
| |
| type Defaulter interface { |
| core_model.Resource |
| Default() error |
| } |
| |
| func DefaultingWebhookFor(scheme *runtime.Scheme, converter k8s_common.Converter) *admission.Webhook { |
| return &admission.Webhook{ |
| Handler: &defaultingHandler{ |
| converter: converter, |
| decoder: admission.NewDecoder(scheme), |
| }, |
| } |
| } |
| |
| type defaultingHandler struct { |
| converter k8s_common.Converter |
| decoder *admission.Decoder |
| } |
| |
| func (h *defaultingHandler) Handle(ctx context.Context, req admission.Request) admission.Response { |
| resource, err := registry.Global().NewObject(core_model.ResourceType(req.Kind.Kind)) |
| if err != nil { |
| return admission.Errored(http.StatusBadRequest, err) |
| } |
| |
| obj, err := h.converter.ToKubernetesObject(resource) |
| if err != nil { |
| return admission.Errored(http.StatusInternalServerError, err) |
| } |
| |
| err = h.decoder.Decode(req, obj) |
| if err != nil { |
| return admission.Errored(http.StatusBadRequest, err) |
| } |
| |
| if err := h.converter.ToCoreResource(obj, resource); err != nil { |
| return admission.Errored(http.StatusInternalServerError, err) |
| } |
| |
| if defaulter, ok := resource.(Defaulter); ok { |
| if err := defaulter.Default(); err != nil { |
| return admission.Errored(http.StatusInternalServerError, err) |
| } |
| } |
| |
| obj, err = h.converter.ToKubernetesObject(resource) |
| if err != nil { |
| return admission.Errored(http.StatusInternalServerError, err) |
| } |
| |
| if resource.Descriptor().Scope == core_model.ScopeMesh { |
| labels := obj.GetLabels() |
| if _, ok := labels[metadata.DubboMeshLabel]; !ok { |
| if len(labels) == 0 { |
| labels = map[string]string{} |
| } |
| labels[metadata.DubboMeshLabel] = core_model.DefaultMesh |
| obj.SetLabels(labels) |
| } |
| } |
| |
| marshaled, err := json.Marshal(obj) |
| if err != nil { |
| return admission.Errored(http.StatusInternalServerError, err) |
| } |
| |
| return admission.PatchResponseFromRaw(req.Object.Raw, marshaled) |
| } |