kubesphere.io/api@v0.0.0-20231107125330-c9a03957060c/notification/v2beta2/silence_types.go (about) 1 /* 2 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package v2beta2 18 19 import ( 20 "time" 21 22 "github.com/robfig/cron/v3" 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 ) 25 26 // SilenceSpec defines the desired state of Silence 27 type SilenceSpec struct { 28 // whether the silence is enabled 29 Enabled *bool `json:"enabled,omitempty"` 30 Matcher *metav1.LabelSelector `json:"matcher"` 31 // The start time during which the silence is active. 32 // 33 // +kubebuilder:validation:Format: date-time 34 StartsAt *metav1.Time `json:"startsAt,omitempty"` 35 // The schedule in Cron format. 36 // If set the silence will be active periodicity, and the startsAt will be invalid. 37 Schedule string `json:"schedule,omitempty"` 38 // The time range during which the silence is active. 39 // If not set, the silence will be active ever. 40 Duration *metav1.Duration `json:"duration,omitempty"` 41 } 42 43 // SilenceStatus defines the observed state of Silence 44 type SilenceStatus struct { 45 } 46 47 // +kubebuilder:object:root=true 48 // +kubebuilder:resource:scope=Cluster,categories=notification-manager 49 // +kubebuilder:subresource:status 50 // +kubebuilder:storageversion 51 // +genclient 52 // +genclient:nonNamespaced 53 54 // Silence is the Schema for the Silence API 55 type Silence struct { 56 metav1.TypeMeta `json:",inline"` 57 metav1.ObjectMeta `json:"metadata,omitempty"` 58 59 Spec SilenceSpec `json:"spec,omitempty"` 60 Status SilenceStatus `json:"status,omitempty"` 61 } 62 63 // +kubebuilder:object:root=true 64 65 // SilenceList contains a list of Silence 66 type SilenceList struct { 67 metav1.TypeMeta `json:",inline"` 68 metav1.ListMeta `json:"metadata,omitempty"` 69 Items []Silence `json:"items"` 70 } 71 72 func init() { 73 SchemeBuilder.Register(&Silence{}, &SilenceList{}) 74 } 75 76 func (s *Silence) IsActive() bool { 77 78 if s.Spec.Enabled != nil && !*s.Spec.Enabled { 79 return false 80 } 81 82 if s.Spec.Schedule != "" { 83 84 if s.Spec.Duration == nil { 85 return true 86 } 87 88 schedule, _ := cron.ParseStandard(s.Spec.Schedule) 89 if schedule.Next(time.Now()) == schedule.Next(time.Now().Add(-(*s.Spec.Duration).Duration)) { 90 return false 91 } else { 92 return true 93 } 94 } else if s.Spec.StartsAt != nil { 95 if s.Spec.StartsAt.After(time.Now()) { 96 return false 97 } 98 99 if s.Spec.Duration == nil { 100 return true 101 } 102 103 if s.Spec.StartsAt.Add((*s.Spec.Duration).Duration).After(time.Now()) { 104 return true 105 } 106 107 return false 108 } else { 109 return true 110 } 111 }