...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/azurechaos_webhook.go

Documentation: github.com/chaos-mesh/chaos-mesh/api/v1alpha1

     1  // Copyright 2022 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  	"reflect"
    21  
    22  	"k8s.io/apimachinery/pkg/util/validation/field"
    23  
    24  	"github.com/chaos-mesh/chaos-mesh/api/genericwebhook"
    25  )
    26  
    27  type DiskName string
    28  type LUN int
    29  
    30  func (in *DiskName) Validate(root interface{}, path *field.Path) field.ErrorList {
    31  	allErrs := field.ErrorList{}
    32  
    33  	azurechaos := root.(*AzureChaos)
    34  	if azurechaos.Spec.Action == AzureDiskDetach {
    35  		if in == nil {
    36  			err := fmt.Errorf("the name of data disk should not be empty on %s action", azurechaos.Spec.Action)
    37  			allErrs = append(allErrs, field.Invalid(path, in, err.Error()))
    38  		}
    39  	}
    40  
    41  	return allErrs
    42  }
    43  
    44  func (in *LUN) Validate(root interface{}, path *field.Path) field.ErrorList {
    45  	allErrs := field.ErrorList{}
    46  
    47  	azurechaos := root.(*AzureChaos)
    48  	if azurechaos.Spec.Action == AzureDiskDetach {
    49  		if in == nil {
    50  			err := fmt.Errorf("the LUN of data disk should not be empty on %s action", azurechaos.Spec.Action)
    51  			allErrs = append(allErrs, field.Invalid(path, in, err.Error()))
    52  		}
    53  	}
    54  
    55  	return allErrs
    56  }
    57  
    58  // Validate validates the azure chaos actions
    59  func (in *AzureChaosAction) Validate(root interface{}, path *field.Path) field.ErrorList {
    60  	allErrs := field.ErrorList{}
    61  
    62  	// in cannot be nil
    63  	switch *in {
    64  	case AzureVmStop, AzureDiskDetach:
    65  	case AzureVmRestart:
    66  	default:
    67  		err := fmt.Errorf("azurechaos have unknown action type")
    68  		log.Error(err, "Wrong AzureChaos 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("DiskName", reflect.PtrTo(reflect.TypeOf(DiskName(""))))
    77  	genericwebhook.Register("LUN", reflect.PtrTo(reflect.TypeOf(LUN(0))))
    78  }
    79