1 /* 2 Copyright 2021 The Kubernetes Authors. 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 v1 18 19 import ( 20 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 21 "k8s.io/apimachinery/pkg/util/intstr" 22 ) 23 24 // DisruptionBudgetCause is the status cause returned for eviction failures caused by PodDisruptionBudget violations. 25 const DisruptionBudgetCause metav1.CauseType = "DisruptionBudget" 26 27 // PodDisruptionBudgetSpec is a description of a PodDisruptionBudget. 28 type PodDisruptionBudgetSpec struct { 29 // An eviction is allowed if at least "minAvailable" pods selected by 30 // "selector" will still be available after the eviction, i.e. even in the 31 // absence of the evicted pod. So for example you can prevent all voluntary 32 // evictions by specifying "100%". 33 // +optional 34 MinAvailable *intstr.IntOrString `json:"minAvailable,omitempty" protobuf:"bytes,1,opt,name=minAvailable"` 35 36 // Label query over pods whose evictions are managed by the disruption 37 // budget. 38 // A null selector will match no pods, while an empty ({}) selector will select 39 // all pods within the namespace. 40 // +patchStrategy=replace 41 // +optional 42 Selector *metav1.LabelSelector `json:"selector,omitempty" patchStrategy:"replace" protobuf:"bytes,2,opt,name=selector"` 43 44 // An eviction is allowed if at most "maxUnavailable" pods selected by 45 // "selector" are unavailable after the eviction, i.e. even in absence of 46 // the evicted pod. For example, one can prevent all voluntary evictions 47 // by specifying 0. This is a mutually exclusive setting with "minAvailable". 48 // +optional 49 MaxUnavailable *intstr.IntOrString `json:"maxUnavailable,omitempty" protobuf:"bytes,3,opt,name=maxUnavailable"` 50 51 // UnhealthyPodEvictionPolicy defines the criteria for when unhealthy pods 52 // should be considered for eviction. Current implementation considers healthy pods, 53 // as pods that have status.conditions item with type="Ready",status="True". 54 // 55 // Valid policies are IfHealthyBudget and AlwaysAllow. 56 // If no policy is specified, the default behavior will be used, 57 // which corresponds to the IfHealthyBudget policy. 58 // 59 // IfHealthyBudget policy means that running pods (status.phase="Running"), 60 // but not yet healthy can be evicted only if the guarded application is not 61 // disrupted (status.currentHealthy is at least equal to status.desiredHealthy). 62 // Healthy pods will be subject to the PDB for eviction. 63 // 64 // AlwaysAllow policy means that all running pods (status.phase="Running"), 65 // but not yet healthy are considered disrupted and can be evicted regardless 66 // of whether the criteria in a PDB is met. This means perspective running 67 // pods of a disrupted application might not get a chance to become healthy. 68 // Healthy pods will be subject to the PDB for eviction. 69 // 70 // Additional policies may be added in the future. 71 // Clients making eviction decisions should disallow eviction of unhealthy pods 72 // if they encounter an unrecognized policy in this field. 73 // 74 // This field is beta-level. The eviction API uses this field when 75 // the feature gate PDBUnhealthyPodEvictionPolicy is enabled (enabled by default). 76 // +optional 77 UnhealthyPodEvictionPolicy *UnhealthyPodEvictionPolicyType `json:"unhealthyPodEvictionPolicy,omitempty" protobuf:"bytes,4,opt,name=unhealthyPodEvictionPolicy"` 78 } 79 80 // UnhealthyPodEvictionPolicyType defines the criteria for when unhealthy pods 81 // should be considered for eviction. 82 // +enum 83 type UnhealthyPodEvictionPolicyType string 84 85 const ( 86 // IfHealthyBudget policy means that running pods (status.phase="Running"), 87 // but not yet healthy can be evicted only if the guarded application is not 88 // disrupted (status.currentHealthy is at least equal to status.desiredHealthy). 89 // Healthy pods will be subject to the PDB for eviction. 90 IfHealthyBudget UnhealthyPodEvictionPolicyType = "IfHealthyBudget" 91 92 // AlwaysAllow policy means that all running pods (status.phase="Running"), 93 // but not yet healthy are considered disrupted and can be evicted regardless 94 // of whether the criteria in a PDB is met. This means perspective running 95 // pods of a disrupted application might not get a chance to become healthy. 96 // Healthy pods will be subject to the PDB for eviction. 97 AlwaysAllow UnhealthyPodEvictionPolicyType = "AlwaysAllow" 98 ) 99 100 // PodDisruptionBudgetStatus represents information about the status of a 101 // PodDisruptionBudget. Status may trail the actual state of a system. 102 type PodDisruptionBudgetStatus struct { 103 // Most recent generation observed when updating this PDB status. DisruptionsAllowed and other 104 // status information is valid only if observedGeneration equals to PDB's object generation. 105 // +optional 106 ObservedGeneration int64 `json:"observedGeneration,omitempty" protobuf:"varint,1,opt,name=observedGeneration"` 107 108 // DisruptedPods contains information about pods whose eviction was 109 // processed by the API server eviction subresource handler but has not 110 // yet been observed by the PodDisruptionBudget controller. 111 // A pod will be in this map from the time when the API server processed the 112 // eviction request to the time when the pod is seen by PDB controller 113 // as having been marked for deletion (or after a timeout). The key in the map is the name of the pod 114 // and the value is the time when the API server processed the eviction request. If 115 // the deletion didn't occur and a pod is still there it will be removed from 116 // the list automatically by PodDisruptionBudget controller after some time. 117 // If everything goes smooth this map should be empty for the most of the time. 118 // Large number of entries in the map may indicate problems with pod deletions. 119 // +optional 120 DisruptedPods map[string]metav1.Time `json:"disruptedPods,omitempty" protobuf:"bytes,2,rep,name=disruptedPods"` 121 122 // Number of pod disruptions that are currently allowed. 123 DisruptionsAllowed int32 `json:"disruptionsAllowed" protobuf:"varint,3,opt,name=disruptionsAllowed"` 124 125 // current number of healthy pods 126 CurrentHealthy int32 `json:"currentHealthy" protobuf:"varint,4,opt,name=currentHealthy"` 127 128 // minimum desired number of healthy pods 129 DesiredHealthy int32 `json:"desiredHealthy" protobuf:"varint,5,opt,name=desiredHealthy"` 130 131 // total number of pods counted by this disruption budget 132 ExpectedPods int32 `json:"expectedPods" protobuf:"varint,6,opt,name=expectedPods"` 133 134 // Conditions contain conditions for PDB. The disruption controller sets the 135 // DisruptionAllowed condition. The following are known values for the reason field 136 // (additional reasons could be added in the future): 137 // - SyncFailed: The controller encountered an error and wasn't able to compute 138 // the number of allowed disruptions. Therefore no disruptions are 139 // allowed and the status of the condition will be False. 140 // - InsufficientPods: The number of pods are either at or below the number 141 // required by the PodDisruptionBudget. No disruptions are 142 // allowed and the status of the condition will be False. 143 // - SufficientPods: There are more pods than required by the PodDisruptionBudget. 144 // The condition will be True, and the number of allowed 145 // disruptions are provided by the disruptionsAllowed property. 146 // 147 // +optional 148 // +patchMergeKey=type 149 // +patchStrategy=merge 150 // +listType=map 151 // +listMapKey=type 152 Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,7,rep,name=conditions"` 153 } 154 155 const ( 156 // DisruptionAllowedCondition is a condition set by the disruption controller 157 // that signal whether any of the pods covered by the PDB can be disrupted. 158 DisruptionAllowedCondition = "DisruptionAllowed" 159 160 // SyncFailedReason is set on the DisruptionAllowed condition if reconcile 161 // of the PDB failed and therefore disruption of pods are not allowed. 162 SyncFailedReason = "SyncFailed" 163 // SufficientPodsReason is set on the DisruptionAllowed condition if there are 164 // more pods covered by the PDB than required and at least one can be disrupted. 165 SufficientPodsReason = "SufficientPods" 166 // InsufficientPodsReason is set on the DisruptionAllowed condition if the number 167 // of pods are equal to or fewer than required by the PDB. 168 InsufficientPodsReason = "InsufficientPods" 169 ) 170 171 // +genclient 172 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 173 174 // PodDisruptionBudget is an object to define the max disruption that can be caused to a collection of pods 175 type PodDisruptionBudget struct { 176 metav1.TypeMeta `json:",inline"` 177 // Standard object's metadata. 178 // More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata 179 // +optional 180 metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"` 181 182 // Specification of the desired behavior of the PodDisruptionBudget. 183 // +optional 184 Spec PodDisruptionBudgetSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"` 185 // Most recently observed status of the PodDisruptionBudget. 186 // +optional 187 Status PodDisruptionBudgetStatus `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"` 188 } 189 190 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 191 192 // PodDisruptionBudgetList is a collection of PodDisruptionBudgets. 193 type PodDisruptionBudgetList struct { 194 metav1.TypeMeta `json:",inline"` 195 // Standard object's metadata. 196 // More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata 197 // +optional 198 metav1.ListMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"` 199 // Items is a list of PodDisruptionBudgets 200 Items []PodDisruptionBudget `json:"items" protobuf:"bytes,2,rep,name=items"` 201 } 202 203 // +genclient 204 // +genclient:noVerbs 205 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 206 207 // Eviction evicts a pod from its node subject to certain policies and safety constraints. 208 // This is a subresource of Pod. A request to cause such an eviction is 209 // created by POSTing to .../pods/<pod name>/evictions. 210 type Eviction struct { 211 metav1.TypeMeta `json:",inline"` 212 213 // ObjectMeta describes the pod that is being evicted. 214 // +optional 215 metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"` 216 217 // DeleteOptions may be provided 218 // +optional 219 DeleteOptions *metav1.DeleteOptions `json:"deleteOptions,omitempty" protobuf:"bytes,2,opt,name=deleteOptions"` 220 } 221