...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/podiochaos_webhook.go

Documentation: github.com/chaos-mesh/chaos-mesh/api/v1alpha1

     1  // Copyright 2020 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package v1alpha1
    15  
    16  import (
    17  	"context"
    18  	"encoding/json"
    19  	"net/http"
    20  
    21  	ctrl "sigs.k8s.io/controller-runtime"
    22  	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
    23  	"sigs.k8s.io/controller-runtime/pkg/webhook"
    24  	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
    25  )
    26  
    27  // log is for logging in this package.
    28  var podiochaoslog = logf.Log.WithName("rawpodio-resource")
    29  
    30  // +kubebuilder:object:generate=false
    31  
    32  // PodIoChaosHandler represents the implementation of podiochaos
    33  type PodIoChaosHandler interface {
    34  	Apply(context.Context, *PodIoChaos) error
    35  }
    36  
    37  var podIoChaosHandler PodIoChaosHandler
    38  
    39  // RegisterPodIoHandler registers handler into webhook
    40  func RegisterPodIoHandler(newHandler PodIoChaosHandler) {
    41  	podIoChaosHandler = newHandler
    42  }
    43  
    44  // SetupWebhookWithManager setup PodIoChaos's webhook with manager
    45  func (in *PodIoChaos) SetupWebhookWithManager(mgr ctrl.Manager) error {
    46  	mgr.GetWebhookServer().
    47  		Register("/mutate-chaos-mesh-org-v1alpha1-podiochaos", &webhook.Admission{Handler: &PodIoChaosWebhookRunner{}})
    48  	return nil
    49  }
    50  
    51  // +kubebuilder:webhook:path=/mutate-chaos-mesh-org-v1alpha1-podiochaos,mutating=true,failurePolicy=fail,groups=chaos-mesh.org,resources=podiochaos,verbs=create;update,versions=v1alpha1,name=mpodiochaos.kb.io
    52  
    53  // +kubebuilder:object:generate=false
    54  
    55  // PodIoChaosWebhookRunner runs webhook for podiochaos
    56  type PodIoChaosWebhookRunner struct {
    57  	decoder *admission.Decoder
    58  }
    59  
    60  // Handle will run poiochaoshandler for this resource
    61  func (r *PodIoChaosWebhookRunner) Handle(ctx context.Context, req admission.Request) admission.Response {
    62  	chaos := &PodIoChaos{}
    63  	err := r.decoder.Decode(req, chaos)
    64  	if err != nil {
    65  		return admission.Errored(http.StatusBadRequest, err)
    66  	}
    67  
    68  	if podIoChaosHandler != nil {
    69  		err = podIoChaosHandler.Apply(ctx, chaos)
    70  		if err != nil {
    71  			// TODO: refine the http status code
    72  			return admission.Errored(http.StatusInternalServerError, err)
    73  		}
    74  	}
    75  
    76  	// mutate the fields in pod
    77  
    78  	marshaledPodIoChaos, err := json.Marshal(chaos)
    79  	if err != nil {
    80  		return admission.Errored(http.StatusInternalServerError, err)
    81  	}
    82  	return admission.PatchResponseFromRaw(req.Object.Raw, marshaledPodIoChaos)
    83  }
    84  
    85  // InjectDecoder injects decoder into webhook runner
    86  func (r *PodIoChaosWebhookRunner) InjectDecoder(d *admission.Decoder) error {
    87  	r.decoder = d
    88  	return nil
    89  }
    90