...

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  	"github.com/pkg/errors"
    22  	"k8s.io/apimachinery/pkg/runtime"
    23  	"k8s.io/apimachinery/pkg/util/validation/field"
    24  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    25  	"sigs.k8s.io/controller-runtime/pkg/webhook"
    26  	"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
    27  )
    28  
    29  // log is for logging in this package.
    30  var schedulelog = logf.Log.WithName("schedule-resource")
    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  	in.Spec.ConcurrencyPolicy.Default()
    38  }
    39  
    40  func (in *ConcurrencyPolicy) Default() {
    41  	if *in == "" {
    42  		*in = ForbidConcurrent
    43  	}
    44  }
    45  
    46  var _ webhook.Validator = &Schedule{}
    47  
    48  // ValidateCreate implements webhook.Validator so a webhook will be registered for the type
    49  func (in *Schedule) ValidateCreate() (admission.Warnings, error) {
    50  	schedulelog.Info("validate create", "name", in.Name)
    51  	return in.Validate()
    52  }
    53  
    54  // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
    55  func (in *Schedule) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
    56  	schedulelog.Info("validate update", "name", in.Name)
    57  	return in.Validate()
    58  }
    59  
    60  // ValidateDelete implements webhook.Validator so a webhook will be registered for the type
    61  func (in *Schedule) ValidateDelete() (admission.Warnings, error) {
    62  	schedulelog.Info("validate delete", "name", in.Name)
    63  	return nil, nil
    64  }
    65  
    66  // Validate validates chaos object
    67  func (in *Schedule) Validate() ([]string, error) {
    68  	allErrs := in.Spec.Validate()
    69  	if len(allErrs) > 0 {
    70  		return nil, errors.New(allErrs.ToAggregate().Error())
    71  	}
    72  	return nil, nil
    73  }
    74  
    75  func (in *ScheduleSpec) Validate() field.ErrorList {
    76  	specField := field.NewPath("spec")
    77  	allErrs := field.ErrorList{}
    78  	allErrs = append(allErrs, in.validateSchedule(specField.Child("schedule"))...)
    79  	allErrs = append(allErrs, in.validateChaos(specField)...)
    80  	return allErrs
    81  }
    82  
    83  // validateSchedule validates the cron
    84  func (in *ScheduleSpec) validateSchedule(schedule *field.Path) field.ErrorList {
    85  	allErrs := field.ErrorList{}
    86  	_, err := StandardCronParser.Parse(in.Schedule)
    87  	if err != nil {
    88  		allErrs = append(allErrs, field.Invalid(schedule,
    89  			in.Schedule,
    90  			fmt.Sprintf("parse schedule field error:%s", err)))
    91  	}
    92  
    93  	return allErrs
    94  }
    95  
    96  // validateChaos validates the chaos
    97  func (in *ScheduleSpec) validateChaos(chaos *field.Path) field.ErrorList {
    98  	allErrs := field.ErrorList{}
    99  	if in.Type != ScheduleTypeWorkflow {
   100  		allErrs = append(allErrs, in.EmbedChaos.Validate(chaos, string(in.Type))...)
   101  	}
   102  	return allErrs
   103  }
   104