k8s_cluster_api/v1beta1/machine/
machineset.rs

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