...

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 2021 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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package v1alpha1
    17  
    18  import (
    19  	"context"
    20  	"encoding/json"
    21  	"net/http"
    22  
    23  	ctrl "sigs.k8s.io/controller-runtime"
    24  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    25  	"sigs.k8s.io/controller-runtime/pkg/webhook"
    26  	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
    27  )
    28  
    29  // log is for logging in this package.
    30  var podhttpchaoslog = logf.Log.WithName("rawpodhttp-resource")
    31  
    32  // +kubebuilder:object:generate=false
    33  
    34  // PodHttpChaosHandler represents the implementation of podhttpchaos
    35  type PodHttpChaosHandler interface {
    36  	Apply(context.Context, *PodHttpChaos) (int32, error)
    37  }
    38  
    39  var podHttpChaosHandler PodHttpChaosHandler
    40  
    41  // RegisterPodHttpHandler registers handler into webhook
    42  func RegisterPodHttpHandler(newHandler PodHttpChaosHandler) {
    43  	podHttpChaosHandler = newHandler
    44  }
    45  
    46  // SetupWebhookWithManager setup PodHttpChaos's webhook with manager
    47  func (in *PodHttpChaos) SetupWebhookWithManager(mgr ctrl.Manager) error {
    48  	mgr.GetWebhookServer().
    49  		Register("/mutate-chaos-mesh-org-v1alpha1-podhttpchaos", &webhook.Admission{Handler: &PodHttpChaosWebhookRunner{}})
    50  	return nil
    51  }
    52  
    53  // +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
    54  
    55  // +kubebuilder:object:generate=false
    56  
    57  // PodHttpChaosWebhookRunner runs webhook for podhttpchaos
    58  type PodHttpChaosWebhookRunner struct {
    59  	decoder *admission.Decoder
    60  }
    61  
    62  // Handle will run podhttpchaoshandler for this resource
    63  func (r *PodHttpChaosWebhookRunner) Handle(ctx context.Context, req admission.Request) admission.Response {
    64  	chaos := &PodHttpChaos{}
    65  	err := r.decoder.Decode(req, chaos)
    66  	if err != nil {
    67  		return admission.Errored(http.StatusBadRequest, err)
    68  	}
    69  
    70  	if podHttpChaosHandler != nil {
    71  		statusCode, err := podHttpChaosHandler.Apply(ctx, chaos)
    72  		if err != nil {
    73  			return admission.Errored(statusCode, err)
    74  		}
    75  	}
    76  
    77  	// mutate the fields in pod
    78  	marshaledPodHttpChaos, err := json.Marshal(chaos)
    79  	if err != nil {
    80  		return admission.Errored(http.StatusInternalServerError, err)
    81  	}
    82  
    83  	return admission.PatchResponseFromRaw(req.Object.Raw, marshaledPodHttpChaos)
    84  }
    85  
    86  // InjectDecoder injects decoder into webhook runner
    87  func (r *PodHttpChaosWebhookRunner) InjectDecoder(d *admission.Decoder) error {
    88  	r.decoder = d
    89  	return nil
    90  }
    91