...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/schedule_types.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  	"github.com/robfig/cron/v3"
    20  	corev1 "k8s.io/api/core/v1"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  )
    23  
    24  const KindSchedule = "Schedule"
    25  
    26  // +kubebuilder:object:root=true
    27  
    28  // Schedule is the cronly schedule object
    29  type Schedule struct {
    30  	metav1.TypeMeta   `json:",inline"`
    31  	metav1.ObjectMeta `json:"metadata,omitempty"`
    32  
    33  	Spec ScheduleSpec `json:"spec"`
    34  
    35  	// +optional
    36  	Status ScheduleStatus `json:"status,omitempty"`
    37  }
    38  
    39  type ConcurrencyPolicy string
    40  
    41  var (
    42  	ForbidConcurrent ConcurrencyPolicy = "Forbid"
    43  	AllowConcurrent  ConcurrencyPolicy = "Allow"
    44  )
    45  
    46  func (c ConcurrencyPolicy) IsForbid() bool {
    47  	return c == ForbidConcurrent || c == ""
    48  }
    49  
    50  func (c ConcurrencyPolicy) IsAllow() bool {
    51  	return c == AllowConcurrent
    52  }
    53  
    54  // ScheduleSpec is the specification of a schedule object
    55  type ScheduleSpec struct {
    56  	Schedule string `json:"schedule"`
    57  
    58  	// +optional
    59  	// +nullable
    60  	// +kubebuilder:validation:Minimum=0
    61  	// +kubebuilder:validation:ExclusiveMinimum=true
    62  	StartingDeadlineSeconds *int64 `json:"startingDeadlineSeconds"`
    63  
    64  	// +optional
    65  	// +kubebuilder:default=Forbid
    66  	// +kubebuilder:validation:Enum=Forbid;Allow
    67  	ConcurrencyPolicy ConcurrencyPolicy `json:"concurrencyPolicy"`
    68  
    69  	// +optional
    70  	// +kubebuilder:validation:Minimum=1
    71  	HistoryLimit int `json:"historyLimit,omitempty"`
    72  
    73  	Type ScheduleTemplateType `json:"type"`
    74  
    75  	ScheduleItem `json:",inline"`
    76  }
    77  
    78  // ScheduleStatus is the status of a schedule object
    79  type ScheduleStatus struct {
    80  	// +optional
    81  	Active []corev1.ObjectReference `json:"active,omitempty"`
    82  
    83  	// +optional
    84  	// +nullable
    85  	LastScheduleTime metav1.Time `json:"time,omitempty"`
    86  }
    87  
    88  type ScheduleTemplateType string
    89  
    90  func (in *Schedule) IsPaused() bool {
    91  	if in.Annotations == nil || in.Annotations[PauseAnnotationKey] != "true" {
    92  		return false
    93  	}
    94  	return true
    95  }
    96  
    97  // +kubebuilder:object:root=true
    98  
    99  // ScheduleList contains a list of Schedule
   100  type ScheduleList struct {
   101  	metav1.TypeMeta `json:",inline"`
   102  	metav1.ListMeta `json:"metadata,omitempty"`
   103  	Items           []Schedule `json:"items"`
   104  }
   105  
   106  func (in *ScheduleList) GetItems() []GenericChaos {
   107  	var result []GenericChaos
   108  	for _, item := range in.Items {
   109  		item := item
   110  		result = append(result, &item)
   111  	}
   112  	return result
   113  }
   114  
   115  func (in *ScheduleList) DeepCopyList() GenericChaosList {
   116  	return in.DeepCopy()
   117  }
   118  
   119  var StandardCronParser = cron.NewParser(
   120  	cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor,
   121  )
   122  
   123  func init() {
   124  	SchemeBuilder.Register(&Schedule{})
   125  	SchemeBuilder.Register(&ScheduleList{})
   126  }
   127