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 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 19 20 // LabelSelectorRequirements is list of LabelSelectorRequirement 21 type LabelSelectorRequirements []metav1.LabelSelectorRequirement 22 23 // SelectorMode represents the mode to run chaos action. 24 type SelectorMode string 25 26 const ( 27 // OneMode represents that the system will do the chaos action on one object selected randomly. 28 OneMode SelectorMode = "one" 29 // AllMode represents that the system will do the chaos action on all objects 30 // regardless of status (not ready or not running pods includes). 31 // Use this label carefully. 32 AllMode SelectorMode = "all" 33 // FixedMode represents that the system will do the chaos action on a specific number of running objects. 34 FixedMode SelectorMode = "fixed" 35 // FixedPercentMode to specify a fixed % that can be inject chaos action. 36 FixedPercentMode SelectorMode = "fixed-percent" 37 // RandomMaxPercentMode to specify a maximum % that can be inject chaos action. 38 RandomMaxPercentMode SelectorMode = "random-max-percent" 39 ) 40 41 // GenericSelectorSpec defines some selectors to select objects. 42 type GenericSelectorSpec struct { 43 // Namespaces is a set of namespace to which objects belong. 44 // +optional 45 Namespaces []string `json:"namespaces,omitempty"` 46 47 // Map of string keys and values that can be used to select objects. 48 // A selector based on fields. 49 // +optional 50 FieldSelectors map[string]string `json:"fieldSelectors,omitempty"` 51 52 // Map of string keys and values that can be used to select objects. 53 // A selector based on labels. 54 // +optional 55 LabelSelectors map[string]string `json:"labelSelectors,omitempty"` 56 57 // a slice of label selector expressions that can be used to select objects. 58 // A list of selectors based on set-based label expressions. 59 // +optional 60 ExpressionSelectors LabelSelectorRequirements `json:"expressionSelectors,omitempty" swaggerignore:"true"` 61 62 // Map of string keys and values that can be used to select objects. 63 // A selector based on annotations. 64 // +optional 65 AnnotationSelectors map[string]string `json:"annotationSelectors,omitempty"` 66 } 67 68 // PodSelectorSpec defines the some selectors to select objects. 69 // If the all selectors are empty, all objects will be used in chaos experiment. 70 type PodSelectorSpec struct { 71 GenericSelectorSpec `json:",inline"` 72 73 // Nodes is a set of node name and objects must belong to these nodes. 74 // +optional 75 Nodes []string `json:"nodes,omitempty"` 76 77 // Pods is a map of string keys and a set values that used to select pods. 78 // The key defines the namespace which pods belong, 79 // and the each values is a set of pod names. 80 // +optional 81 Pods map[string][]string `json:"pods,omitempty"` 82 83 // Map of string keys and values that can be used to select nodes. 84 // Selector which must match a node's labels, 85 // and objects must belong to these selected nodes. 86 // +optional 87 NodeSelectors map[string]string `json:"nodeSelectors,omitempty"` 88 89 // PodPhaseSelectors is a set of condition of a pod at the current time. 90 // supported value: Pending / Running / Succeeded / Failed / Unknown 91 // +optional 92 PodPhaseSelectors []string `json:"podPhaseSelectors,omitempty"` 93 } 94 95 func (in *PodSelectorSpec) DefaultNamespace(namespace string) { 96 if len(in.Namespaces) == 0 { 97 in.Namespaces = []string{namespace} 98 } 99 } 100 101 type PodSelector struct { 102 // Selector is used to select pods that are used to inject chaos action. 103 Selector PodSelectorSpec `json:"selector"` 104 105 // Mode defines the mode to run chaos action. 106 // Supported mode: one / all / fixed / fixed-percent / random-max-percent 107 // +kubebuilder:validation:Enum=one;all;fixed;fixed-percent;random-max-percent 108 Mode SelectorMode `json:"mode"` 109 110 // Value is required when the mode is set to `FixedMode` / `FixedPercentMode` / `RandomMaxPercentMode`. 111 // If `FixedMode`, provide an integer of pods to do chaos action. 112 // If `FixedPercentMode`, provide a number from 0-100 to specify the percent of pods the server can do chaos action. 113 // IF `RandomMaxPercentMode`, provide a number from 0-100 to specify the max percent of pods to do chaos action 114 // +optional 115 Value string `json:"value,omitempty"` 116 } 117 118 type ContainerSelector struct { 119 PodSelector `json:",inline"` 120 121 // ContainerNames indicates list of the name of affected container. 122 // If not set, the first container will be injected 123 // +optional 124 ContainerNames []string `json:"containerNames,omitempty"` 125 } 126 127 // ClusterScoped returns true if the selector selects Pods in the cluster 128 func (in PodSelectorSpec) ClusterScoped() bool { 129 // in fact, this will never happened, will add namespace if it is empty, so len(s.Namespaces) can not be 0, 130 // but still add judgentment here for safe 131 // https://github.com/chaos-mesh/chaos-mesh/blob/478d00d01bb0f9fb08a1085428a7da8c8f9df4e8/api/v1alpha1/common_webhook.go#L22 132 if len(in.Namespaces) == 0 && len(in.Pods) == 0 { 133 return true 134 } 135 136 return false 137 } 138