...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/timechaos_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  	"reflect"
    21  	"time"
    22  
    23  	"k8s.io/apimachinery/pkg/util/validation/field"
    24  
    25  	"github.com/chaos-mesh/chaos-mesh/api/genericwebhook"
    26  )
    27  
    28  type ClockIds []string
    29  
    30  // DefaultClockIds will set default value for empty ClockIds fields
    31  func (in *ClockIds) Default(root interface{}, field *reflect.StructField) {
    32  	// in cannot be nil
    33  	if *in == nil || len(*in) == 0 {
    34  		*in = []string{"CLOCK_REALTIME"}
    35  	}
    36  }
    37  
    38  type TimeOffset string
    39  
    40  func (in *TimeOffset) Validate(root interface{}, path *field.Path) field.ErrorList {
    41  	allErrs := field.ErrorList{}
    42  
    43  	_, err := time.ParseDuration(string(*in))
    44  	if err != nil {
    45  		allErrs = append(allErrs, field.Invalid(path,
    46  			in,
    47  			fmt.Sprintf("parse timeOffset field error:%s", err)))
    48  	}
    49  
    50  	return allErrs
    51  }
    52  
    53  func init() {
    54  	genericwebhook.Register("ClockIds", reflect.PtrTo(reflect.TypeOf(ClockIds{})))
    55  	genericwebhook.Register("TimeOffset", reflect.PtrTo(reflect.TypeOf(TimeOffset(""))))
    56  }
    57