...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/awschaos_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  	"reflect"
    20  
    21  	"github.com/pkg/errors"
    22  	"k8s.io/apimachinery/pkg/util/validation/field"
    23  
    24  	"github.com/chaos-mesh/chaos-mesh/api/genericwebhook"
    25  )
    26  
    27  type EbsVolume string
    28  type AWSDeviceName string
    29  
    30  func (in *EbsVolume) Validate(root interface{}, path *field.Path) field.ErrorList {
    31  	allErrs := field.ErrorList{}
    32  
    33  	awsChaos := root.(*AWSChaos)
    34  	if awsChaos.Spec.Action == DetachVolume {
    35  		if in == nil {
    36  			err := errors.Wrapf(errInvalidValue, "the ID of EBS volume is required on %s action", awsChaos.Spec.Action)
    37  			allErrs = append(allErrs, field.Invalid(path, in, err.Error()))
    38  		}
    39  	}
    40  
    41  	return allErrs
    42  }
    43  
    44  func (in *AWSDeviceName) Validate(root interface{}, path *field.Path) field.ErrorList {
    45  	allErrs := field.ErrorList{}
    46  
    47  	awsChaos := root.(*AWSChaos)
    48  	if awsChaos.Spec.Action == DetachVolume {
    49  		if in == nil {
    50  			err := errors.Wrapf(errInvalidValue, "the name of device is required on %s action", awsChaos.Spec.Action)
    51  			allErrs = append(allErrs, field.Invalid(path, in, err.Error()))
    52  		}
    53  	}
    54  
    55  	return allErrs
    56  }
    57  
    58  // Validate validates aws chaos actions
    59  func (in *AWSChaosAction) Validate(root interface{}, path *field.Path) field.ErrorList {
    60  	allErrs := field.ErrorList{}
    61  
    62  	// in cannot be nil
    63  	switch *in {
    64  	case Ec2Stop, DetachVolume:
    65  	case Ec2Restart:
    66  	default:
    67  		err := errors.WithStack(errUnknownAction)
    68  		log.Error(err, "Wrong AWSChaos Action type")
    69  
    70  		allErrs = append(allErrs, field.Invalid(path, in, err.Error()))
    71  	}
    72  	return allErrs
    73  }
    74  
    75  func init() {
    76  	genericwebhook.Register("EbsVolume", reflect.PtrTo(reflect.TypeOf(EbsVolume(""))))
    77  	genericwebhook.Register("AWSDeviceName", reflect.PtrTo(reflect.TypeOf(AWSDeviceName(""))))
    78  }
    79