...

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