...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/httpchaos_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  	"fmt"
    20  	"net/http"
    21  	"reflect"
    22  	"time"
    23  
    24  	"k8s.io/apimachinery/pkg/util/validation/field"
    25  
    26  	"github.com/chaos-mesh/chaos-mesh/api/genericwebhook"
    27  )
    28  
    29  type Delay string
    30  
    31  func (in *Delay) Validate(root interface{}, path *field.Path) field.ErrorList {
    32  	allErrs := field.ErrorList{}
    33  	if in != nil {
    34  		_, err := time.ParseDuration(string(*in))
    35  		if err != nil {
    36  			allErrs = append(allErrs, field.Invalid(path, in, fmt.Sprintf("invalid duration %s", *in)))
    37  		}
    38  	}
    39  	return allErrs
    40  }
    41  
    42  type Port int32
    43  
    44  func (in *Port) Validate(root interface{}, path *field.Path) field.ErrorList {
    45  	allErrs := field.ErrorList{}
    46  	// in cannot be zero or negative
    47  	if *in <= 0 {
    48  		allErrs = append(allErrs, field.Invalid(path, in, fmt.Sprintf("port %d is not supported", *in)))
    49  	}
    50  	return allErrs
    51  }
    52  
    53  type HTTPMethod string
    54  
    55  func (in *HTTPMethod) Validate(root interface{}, path *field.Path) field.ErrorList {
    56  	allErrs := field.ErrorList{}
    57  	if in != nil && root.(*HTTPChaos).Spec.Target == PodHttpRequest {
    58  		switch *in {
    59  		case http.MethodGet:
    60  		case http.MethodPost:
    61  		case http.MethodPut:
    62  		case http.MethodDelete:
    63  		case http.MethodPatch:
    64  		case http.MethodHead:
    65  		case http.MethodOptions:
    66  		case http.MethodTrace:
    67  		case http.MethodConnect:
    68  		default:
    69  			allErrs = append(allErrs, field.Invalid(path, in, fmt.Sprintf("method %s is not supported", *in)))
    70  		}
    71  	}
    72  	return allErrs
    73  }
    74  
    75  func (in *PodHttpChaosTarget) Validate(root interface{}, path *field.Path) field.ErrorList {
    76  	allErrs := field.ErrorList{}
    77  	switch *in {
    78  	case PodHttpRequest:
    79  	case PodHttpResponse:
    80  	default:
    81  		allErrs = append(allErrs, field.Invalid(path, in, fmt.Sprintf("target %s is not supported", *in)))
    82  	}
    83  	return allErrs
    84  }
    85  
    86  func init() {
    87  	genericwebhook.Register("Delay", reflect.PtrTo(reflect.TypeOf(Delay(""))))
    88  	genericwebhook.Register("Port", reflect.PtrTo(reflect.TypeOf(Port(0))))
    89  	genericwebhook.Register("HTTPMethod", reflect.PtrTo(reflect.TypeOf(HTTPMethod(""))))
    90  	genericwebhook.Register("PodHttpChaosTarget", reflect.PtrTo(reflect.TypeOf(PodHttpChaosTarget(""))))
    91  }
    92