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