sigs.k8s.io/cluster-api@v1.6.3/api/v1alpha4/machineset_types.go (about) 1 /* 2 Copyright 2020 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 v1alpha4 18 19 import ( 20 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 21 metav1validation "k8s.io/apimachinery/pkg/apis/meta/v1/validation" 22 "k8s.io/apimachinery/pkg/labels" 23 "k8s.io/apimachinery/pkg/util/validation/field" 24 25 capierrors "sigs.k8s.io/cluster-api/errors" 26 ) 27 28 const ( 29 // MachineSetTopologyFinalizer is the finalizer used by the topology MachineDeployment controller to 30 // clean up referenced template resources if necessary when a MachineSet is being deleted. 31 MachineSetTopologyFinalizer = "machineset.topology.cluster.x-k8s.io" 32 ) 33 34 // ANCHOR: MachineSetSpec 35 36 // MachineSetSpec defines the desired state of MachineSet. 37 type MachineSetSpec struct { 38 // ClusterName is the name of the Cluster this object belongs to. 39 // +kubebuilder:validation:MinLength=1 40 ClusterName string `json:"clusterName"` 41 42 // Replicas is the number of desired replicas. 43 // This is a pointer to distinguish between explicit zero and unspecified. 44 // Defaults to 1. 45 // +optional 46 // +kubebuilder:default=1 47 Replicas *int32 `json:"replicas,omitempty"` 48 49 // MinReadySeconds is the minimum number of seconds for which a newly created machine should be ready. 50 // Defaults to 0 (machine will be considered available as soon as it is ready) 51 // +optional 52 MinReadySeconds int32 `json:"minReadySeconds,omitempty"` 53 54 // DeletePolicy defines the policy used to identify nodes to delete when downscaling. 55 // Defaults to "Random". Valid values are "Random, "Newest", "Oldest" 56 // +kubebuilder:validation:Enum=Random;Newest;Oldest 57 DeletePolicy string `json:"deletePolicy,omitempty"` 58 59 // Selector is a label query over machines that should match the replica count. 60 // Label keys and values that must match in order to be controlled by this MachineSet. 61 // It must match the machine template's labels. 62 // More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors 63 Selector metav1.LabelSelector `json:"selector"` 64 65 // Template is the object that describes the machine that will be created if 66 // insufficient replicas are detected. 67 // Object references to custom resources are treated as templates. 68 // +optional 69 Template MachineTemplateSpec `json:"template,omitempty"` 70 } 71 72 // ANCHOR_END: MachineSetSpec 73 74 // ANCHOR: MachineTemplateSpec 75 76 // MachineTemplateSpec describes the data needed to create a Machine from a template. 77 type MachineTemplateSpec struct { 78 // Standard object's metadata. 79 // More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata 80 // +optional 81 ObjectMeta `json:"metadata,omitempty"` 82 83 // Specification of the desired behavior of the machine. 84 // More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status 85 // +optional 86 Spec MachineSpec `json:"spec,omitempty"` 87 } 88 89 // ANCHOR_END: MachineTemplateSpec 90 91 // MachineSetDeletePolicy defines how priority is assigned to nodes to delete when 92 // downscaling a MachineSet. Defaults to "Random". 93 type MachineSetDeletePolicy string 94 95 const ( 96 // RandomMachineSetDeletePolicy prioritizes both Machines that have the annotation 97 // "cluster.x-k8s.io/delete-machine=yes" and Machines that are unhealthy 98 // (Status.FailureReason or Status.FailureMessage are set to a non-empty value). 99 // Finally, it picks Machines at random to delete. 100 RandomMachineSetDeletePolicy MachineSetDeletePolicy = "Random" 101 102 // NewestMachineSetDeletePolicy prioritizes both Machines that have the annotation 103 // "cluster.x-k8s.io/delete-machine=yes" and Machines that are unhealthy 104 // (Status.FailureReason or Status.FailureMessage are set to a non-empty value). 105 // It then prioritizes the newest Machines for deletion based on the Machine's CreationTimestamp. 106 NewestMachineSetDeletePolicy MachineSetDeletePolicy = "Newest" 107 108 // OldestMachineSetDeletePolicy prioritizes both Machines that have the annotation 109 // "cluster.x-k8s.io/delete-machine=yes" and Machines that are unhealthy 110 // (Status.FailureReason or Status.FailureMessage are set to a non-empty value). 111 // It then prioritizes the oldest Machines for deletion based on the Machine's CreationTimestamp. 112 OldestMachineSetDeletePolicy MachineSetDeletePolicy = "Oldest" 113 ) 114 115 // ANCHOR: MachineSetStatus 116 117 // MachineSetStatus defines the observed state of MachineSet. 118 type MachineSetStatus struct { 119 // Selector is the same as the label selector but in the string format to avoid introspection 120 // by clients. The string will be in the same format as the query-param syntax. 121 // More info about label selectors: http://kubernetes.io/docs/user-guide/labels#label-selectors 122 // +optional 123 Selector string `json:"selector,omitempty"` 124 125 // Replicas is the most recently observed number of replicas. 126 // +optional 127 Replicas int32 `json:"replicas,omitempty"` 128 129 // The number of replicas that have labels matching the labels of the machine template of the MachineSet. 130 // +optional 131 FullyLabeledReplicas int32 `json:"fullyLabeledReplicas,omitempty"` 132 133 // The number of ready replicas for this MachineSet. A machine is considered ready when the node has been created and is "Ready". 134 // +optional 135 ReadyReplicas int32 `json:"readyReplicas,omitempty"` 136 137 // The number of available replicas (ready for at least minReadySeconds) for this MachineSet. 138 // +optional 139 AvailableReplicas int32 `json:"availableReplicas,omitempty"` 140 141 // ObservedGeneration reflects the generation of the most recently observed MachineSet. 142 // +optional 143 ObservedGeneration int64 `json:"observedGeneration,omitempty"` 144 145 // In the event that there is a terminal problem reconciling the 146 // replicas, both FailureReason and FailureMessage will be set. FailureReason 147 // will be populated with a succinct value suitable for machine 148 // interpretation, while FailureMessage will contain a more verbose 149 // string suitable for logging and human consumption. 150 // 151 // These fields should not be set for transitive errors that a 152 // controller faces that are expected to be fixed automatically over 153 // time (like service outages), but instead indicate that something is 154 // fundamentally wrong with the MachineTemplate's spec or the configuration of 155 // the machine controller, and that manual intervention is required. Examples 156 // of terminal errors would be invalid combinations of settings in the 157 // spec, values that are unsupported by the machine controller, or the 158 // responsible machine controller itself being critically misconfigured. 159 // 160 // Any transient errors that occur during the reconciliation of Machines 161 // can be added as events to the MachineSet object and/or logged in the 162 // controller's output. 163 // +optional 164 FailureReason *capierrors.MachineSetStatusError `json:"failureReason,omitempty"` 165 // +optional 166 FailureMessage *string `json:"failureMessage,omitempty"` 167 // Conditions defines current service state of the MachineSet. 168 // +optional 169 Conditions Conditions `json:"conditions,omitempty"` 170 } 171 172 // ANCHOR_END: MachineSetStatus 173 174 // Validate validates the MachineSet fields. 175 func (m *MachineSet) Validate() field.ErrorList { 176 errors := field.ErrorList{} 177 178 // validate spec.selector and spec.template.labels 179 fldPath := field.NewPath("spec") 180 errors = append(errors, metav1validation.ValidateLabelSelector(&m.Spec.Selector, metav1validation.LabelSelectorValidationOptions{}, fldPath.Child("selector"))...) 181 if len(m.Spec.Selector.MatchLabels)+len(m.Spec.Selector.MatchExpressions) == 0 { 182 errors = append(errors, field.Invalid(fldPath.Child("selector"), m.Spec.Selector, "empty selector is not valid for MachineSet.")) 183 } 184 selector, err := metav1.LabelSelectorAsSelector(&m.Spec.Selector) 185 if err != nil { 186 errors = append(errors, field.Invalid(fldPath.Child("selector"), m.Spec.Selector, "invalid label selector.")) 187 } else { 188 labels := labels.Set(m.Spec.Template.Labels) 189 if !selector.Matches(labels) { 190 errors = append(errors, field.Invalid(fldPath.Child("template", "metadata", "labels"), m.Spec.Template.Labels, "`selector` does not match template `labels`")) 191 } 192 } 193 194 return errors 195 } 196 197 // +kubebuilder:object:root=true 198 // +kubebuilder:unservedversion 199 // +kubebuilder:deprecatedversion 200 // +kubebuilder:resource:path=machinesets,shortName=ms,scope=Namespaced,categories=cluster-api 201 // +kubebuilder:subresource:status 202 // +kubebuilder:subresource:scale:specpath=.spec.replicas,statuspath=.status.replicas,selectorpath=.status.selector 203 // +kubebuilder:printcolumn:name="Cluster",type="string",JSONPath=".spec.clusterName",description="Cluster" 204 // +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Time duration since creation of MachineSet" 205 // +kubebuilder:printcolumn:name="Replicas",type="integer",JSONPath=".status.replicas",description="Total number of non-terminated machines targeted by this machineset" 206 // +kubebuilder:printcolumn:name="Available",type="integer",JSONPath=".status.availableReplicas",description="Total number of available machines (ready for at least minReadySeconds)" 207 // +kubebuilder:printcolumn:name="Ready",type="integer",JSONPath=".status.readyReplicas",description="Total number of ready machines targeted by this machineset." 208 209 // MachineSet is the Schema for the machinesets API. 210 // 211 // Deprecated: This type will be removed in one of the next releases. 212 type MachineSet struct { 213 metav1.TypeMeta `json:",inline"` 214 metav1.ObjectMeta `json:"metadata,omitempty"` 215 216 Spec MachineSetSpec `json:"spec,omitempty"` 217 Status MachineSetStatus `json:"status,omitempty"` 218 } 219 220 // +kubebuilder:object:root=true 221 222 // MachineSetList contains a list of MachineSet. 223 // 224 // Deprecated: This type will be removed in one of the next releases. 225 type MachineSetList struct { 226 metav1.TypeMeta `json:",inline"` 227 metav1.ListMeta `json:"metadata,omitempty"` 228 Items []MachineSet `json:"items"` 229 } 230 231 func init() { 232 objectTypes = append(objectTypes, &MachineSet{}, &MachineSetList{}) 233 } 234 235 // GetConditions returns the set of conditions for the MachineSet. 236 func (m *MachineSet) GetConditions() Conditions { 237 return m.Status.Conditions 238 } 239 240 // SetConditions updates the set of conditions on the MachineSet. 241 func (m *MachineSet) SetConditions(conditions Conditions) { 242 m.Status.Conditions = conditions 243 }