Files
kubevela/pkg/webhook/standard.oam.dev/v1alpha1/podspecworkload/mutating_handler.go
yangsoon 447ab4a35a Improve the logging system (#1735)
* change to klog/v2

* add logfilepath opt in helm chart

* replace klog.InfoF with klog.InfoS and improve messages

Remove string formatting from log message, improve messages in klog.InfoS and use lowerCamelCase to fmt Name arguments in klog.InfoS

* fix klog.Error

for expected errors (errors that can happen during routine operations) use klog.InfoS and pass error in err key instead.

* use klog.KObj and klog.KRef for Kubernetes objects

ensure that kubernetes objects references are consistent within the codebase

* enable set logdebug level

* add log-file-max-size
2021-06-03 12:07:30 +08:00

89 lines
2.5 KiB
Go

/*
Copyright 2021 The KubeVela 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 podspecworkload
import (
"context"
"encoding/json"
"net/http"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"github.com/oam-dev/kubevela/apis/standard.oam.dev/v1alpha1"
util "github.com/oam-dev/kubevela/pkg/utils"
)
// MutatingHandler handles PodSpec workload
type MutatingHandler struct {
Client client.Client
// Decoder decodes objects
Decoder *admission.Decoder
}
var _ admission.Handler = &MutatingHandler{}
// Handle handles admission requests.
func (h *MutatingHandler) Handle(ctx context.Context, req admission.Request) admission.Response {
obj := &v1alpha1.PodSpecWorkload{}
err := h.Decoder.Decode(req, obj)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
DefaultPodSpecWorkload(obj)
marshalled, err := json.Marshal(obj)
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}
resp := admission.PatchResponseFromRaw(req.AdmissionRequest.Object.Raw, marshalled)
if len(resp.Patches) > 0 {
klog.V(5).InfoS("Admit PodSpecWorkload", "podSpecWorkload", klog.KObj(obj), "patches", util.DumpJSON(resp.Patches))
}
return resp
}
// DefaultPodSpecWorkload will set the default value for the PodSpecWorkload
func DefaultPodSpecWorkload(obj *v1alpha1.PodSpecWorkload) {
klog.InfoS("Set the default value for the PodSpecWorkload", "podSpecWorkload", klog.KObj(obj))
if obj.Spec.Replicas == nil {
klog.InfoS("Set default replicas as 1")
obj.Spec.Replicas = pointer.Int32Ptr(1)
}
}
var _ inject.Client = &MutatingHandler{}
// InjectClient injects the client into the MutatingHandler
func (h *MutatingHandler) InjectClient(c client.Client) error {
h.Client = c
return nil
}
var _ admission.DecoderInjector = &MutatingHandler{}
// InjectDecoder injects the decoder into the MutatingHandler
func (h *MutatingHandler) InjectDecoder(d *admission.Decoder) error {
h.Decoder = d
return nil
}