...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/schedule_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  
    21  	"k8s.io/apimachinery/pkg/runtime"
    22  	"k8s.io/apimachinery/pkg/util/validation/field"
    23  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    24  	"sigs.k8s.io/controller-runtime/pkg/webhook"
    25  )
    26  
    27  // log is for logging in this package.
    28  var schedulelog = logf.Log.WithName("schedule-resource")
    29  
    30  // +kubebuilder:webhook:path=/mutate-chaos-mesh-org-v1alpha1-schedule,mutating=true,failurePolicy=fail,groups=chaos-mesh.org,resources=schedule,verbs=create;update,versions=v1alpha1,name=mschedule.kb.io
    31  
    32  var _ webhook.Defaulter = &Schedule{}
    33  
    34  // Default implements webhook.Defaulter so a webhook will be registered for the type
    35  func (in *Schedule) Default() {
    36  	schedulelog.Info("default", "name", in.Name)
    37  }
    38  
    39  // +kubebuilder:webhook:verbs=create;update,path=/validate-chaos-mesh-org-v1alpha1-schedule,mutating=false,failurePolicy=fail,groups=chaos-mesh.org,resources=schedule,versions=v1alpha1,name=vschedule.kb.io
    40  
    41  var _ webhook.Validator = &Schedule{}
    42  
    43  // ValidateCreate implements webhook.Validator so a webhook will be registered for the type
    44  func (in *Schedule) ValidateCreate() error {
    45  	schedulelog.Info("validate create", "name", in.Name)
    46  	return in.Validate()
    47  }
    48  
    49  // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
    50  func (in *Schedule) ValidateUpdate(old runtime.Object) error {
    51  	schedulelog.Info("validate update", "name", in.Name)
    52  	return in.Validate()
    53  }
    54  
    55  // ValidateDelete implements webhook.Validator so a webhook will be registered for the type
    56  func (in *Schedule) ValidateDelete() error {
    57  	schedulelog.Info("validate delete", "name", in.Name)
    58  	return nil
    59  }
    60  
    61  // Validate validates chaos object
    62  func (in *Schedule) Validate() error {
    63  	allErrs := in.Spec.Validate()
    64  	if len(allErrs) > 0 {
    65  		return fmt.Errorf(allErrs.ToAggregate().Error())
    66  	}
    67  	return nil
    68  }
    69  
    70  func (in *ScheduleSpec) Validate() field.ErrorList {
    71  	specField := field.NewPath("spec")
    72  	allErrs := field.ErrorList{}
    73  	allErrs = append(allErrs, in.validateSchedule(specField.Child("schedule"))...)
    74  	allErrs = append(allErrs, in.validateChaos(specField)...)
    75  	return allErrs
    76  }
    77  
    78  // validateSchedule validates the cron
    79  func (in *ScheduleSpec) validateSchedule(schedule *field.Path) field.ErrorList {
    80  	allErrs := field.ErrorList{}
    81  	_, err := StandardCronParser.Parse(in.Schedule)
    82  	if err != nil {
    83  		allErrs = append(allErrs, field.Invalid(schedule,
    84  			in.Schedule,
    85  			fmt.Sprintf("parse schedule field error:%s", err)))
    86  	}
    87  
    88  	return allErrs
    89  }
    90  
    91  // validateChaos validates the chaos
    92  func (in *ScheduleSpec) validateChaos(chaos *field.Path) field.ErrorList {
    93  	allErrs := field.ErrorList{}
    94  	if in.Type != ScheduleTypeWorkflow {
    95  		allErrs = append(allErrs, in.EmbedChaos.Validate(chaos, string(in.Type))...)
    96  	}
    97  	return allErrs
    98  }
    99