...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/gcpchaos_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  // validateDeviceName validates the gcp chaos actions
    28  func (in GCPChaosAction) Validate(root interface{}, path *field.Path) field.ErrorList {
    29  	allErrs := field.ErrorList{}
    30  
    31  	switch in {
    32  	case NodeStop, DiskLoss:
    33  	case NodeReset:
    34  	default:
    35  		err := errors.WithStack(errUnknownAction)
    36  		log.Error(err, "Wrong GCPChaos Action type")
    37  
    38  		allErrs = append(allErrs, field.Invalid(path, in, err.Error()))
    39  	}
    40  	return allErrs
    41  }
    42  
    43  type GCPDeviceNames []string
    44  
    45  // validateDeviceName validates the DeviceName
    46  func (in *GCPDeviceNames) Validate(root interface{}, path *field.Path) field.ErrorList {
    47  	allErrs := field.ErrorList{}
    48  	obj := root.(*GCPChaos)
    49  	if obj.Spec.Action == DiskLoss {
    50  		if *in == nil {
    51  			err := errors.Errorf("at least one device name is required on %s action", obj.Spec.Action)
    52  			allErrs = append(allErrs, field.Invalid(path, *in, err.Error()))
    53  		}
    54  	}
    55  	return allErrs
    56  }
    57  
    58  func init() {
    59  	genericwebhook.Register("GCPDeviceNames", reflect.PtrTo(reflect.TypeOf(GCPDeviceNames{})))
    60  }
    61