...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package v1alpha1
17
18 import (
19 "fmt"
20
21 v1 "k8s.io/api/core/v1"
22 "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
23 )
24
25 const (
26
27 TemplateTypeLabelKey = "template.chaos-mesh.org/type"
28
29
30 ManagedByLabelKey = "app.kubernetes.io/managed-by"
31
32
33 ManagedByLabelValue = "chaos-mesh"
34
35
36
37 TemplateNameAnnotationKey = "template.chaos-mesh.org/name"
38
39
40 TemplateDescriptionAnnotationKey = "template.chaos-mesh.org/description"
41
42
43 PrefixStatusCheckTemplate = "template-status-check"
44
45
46 StatusCheckTemplateKey = "spec"
47 )
48
49
50
51
52 type StatusCheckTemplate struct {
53 StatusCheckSpec `json:",inline"`
54 }
55
56 func GetTemplateName(cm v1.ConfigMap) string {
57 return cm.Annotations[TemplateNameAnnotationKey]
58 }
59
60 func GetTemplateDescription(cm v1.ConfigMap) string {
61 return cm.Annotations[TemplateDescriptionAnnotationKey]
62 }
63
64 func GenerateTemplateName(name string) string {
65 return fmt.Sprintf("%s-%s", PrefixStatusCheckTemplate, name)
66 }
67
68 func IsStatusCheckTemplate(cm v1.ConfigMap) bool {
69 return cm.Labels[ManagedByLabelKey] == ManagedByLabelValue &&
70 cm.Labels[TemplateTypeLabelKey] == KindStatusCheck &&
71 cm.Name == GenerateTemplateName(cm.Annotations[TemplateNameAnnotationKey])
72 }
73
74 func (in *StatusCheckTemplate) Validate() (admission.Warnings, error) {
75 statusCheck := &StatusCheck{
76 Spec: in.StatusCheckSpec,
77 }
78 return statusCheck.Validate()
79 }
80
81 func (in *StatusCheckTemplate) Default() {
82 if in == nil {
83 return
84 }
85
86 statusCheck := &StatusCheck{
87 Spec: in.StatusCheckSpec,
88 }
89 statusCheck.Default()
90 in.StatusCheckSpec = *statusCheck.Spec.DeepCopy()
91 }
92