...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/podhttpchaos_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 podhttpchaoslog = logf.Log.WithName("rawpodhttp-resource")
    29  
    30  // +kubebuilder:object:generate=false
    31  
    32  // PodHttpChaosHandler represents the implementation of podhttpchaos
    33  type PodHttpChaosHandler interface {
    34  	Apply(context.Context, *PodHttpChaos) (int32, error)
    35  }
    36  
    37  var podHttpChaosHandler PodHttpChaosHandler
    38  
    39  // RegisterPodHttpHandler registers handler into webhook
    40  func RegisterPodHttpHandler(newHandler PodHttpChaosHandler) {
    41  	podHttpChaosHandler = newHandler
    42  }
    43  
    44  // SetupWebhookWithManager setup PodHttpChaos's webhook with manager
    45  func (in *PodHttpChaos) SetupWebhookWithManager(mgr ctrl.Manager) error {
    46  	mgr.GetWebhookServer().
    47  		Register("/mutate-chaos-mesh-org-v1alpha1-podhttpchaos", &webhook.Admission{Handler: &PodHttpChaosWebhookRunner{}})
    48  	return nil
    49  }
    50  
    51  // +kubebuilder:webhook:path=/mutate-chaos-mesh-org-v1alpha1-podhttpchaos,mutating=true,failurePolicy=fail,groups=chaos-mesh.org,resources=podhttpchaos,verbs=create;update,versions=v1alpha1,name=mpodhttpchaos.kb.io
    52  
    53  // +kubebuilder:object:generate=false
    54  
    55  // PodHttpChaosWebhookRunner runs webhook for podhttpchaos
    56  type PodHttpChaosWebhookRunner struct {
    57  	decoder *admission.Decoder
    58  }
    59  
    60  // Handle will run podhttpchaoshandler for this resource
    61  func (r *PodHttpChaosWebhookRunner) Handle(ctx context.Context, req admission.Request) admission.Response {
    62  	chaos := &PodHttpChaos{}
    63  	err := r.decoder.Decode(req, chaos)
    64  	if err != nil {
    65  		return admission.Errored(http.StatusBadRequest, err)
    66  	}
    67  
    68  	if podHttpChaosHandler != nil {
    69  		statusCode, err := podHttpChaosHandler.Apply(ctx, chaos)
    70  		if err != nil {
    71  			return admission.Errored(statusCode, err)
    72  		}
    73  	}
    74  
    75  	// mutate the fields in pod
    76  	marshaledPodHttpChaos, err := json.Marshal(chaos)
    77  	if err != nil {
    78  		return admission.Errored(http.StatusInternalServerError, err)
    79  	}
    80  
    81  	return admission.PatchResponseFromRaw(req.Object.Raw, marshaledPodHttpChaos)
    82  }
    83  
    84  // InjectDecoder injects decoder into webhook runner
    85  func (r *PodHttpChaosWebhookRunner) InjectDecoder(d *admission.Decoder) error {
    86  	r.decoder = d
    87  	return nil
    88  }
    89