sigs.k8s.io/cluster-api@v1.7.1/api/v1beta1/machinedeployment_types.go (about)

     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 v1beta1
    18  
    19  import (
    20  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    21  	"k8s.io/apimachinery/pkg/util/intstr"
    22  )
    23  
    24  const (
    25  	// MachineDeploymentTopologyFinalizer is the finalizer used by the topology MachineDeployment controller to
    26  	// clean up referenced template resources if necessary when a MachineDeployment is being deleted.
    27  	MachineDeploymentTopologyFinalizer = "machinedeployment.topology.cluster.x-k8s.io"
    28  )
    29  
    30  // MachineDeploymentStrategyType defines the type of MachineDeployment rollout strategies.
    31  type MachineDeploymentStrategyType string
    32  
    33  const (
    34  	// RollingUpdateMachineDeploymentStrategyType replaces the old MachineSet by new one using rolling update
    35  	// i.e. gradually scale down the old MachineSet and scale up the new one.
    36  	RollingUpdateMachineDeploymentStrategyType MachineDeploymentStrategyType = "RollingUpdate"
    37  
    38  	// OnDeleteMachineDeploymentStrategyType replaces old MachineSets when the deletion of the associated machines are completed.
    39  	OnDeleteMachineDeploymentStrategyType MachineDeploymentStrategyType = "OnDelete"
    40  
    41  	// RevisionAnnotation is the revision annotation of a machine deployment's machine sets which records its rollout sequence.
    42  	RevisionAnnotation = "machinedeployment.clusters.x-k8s.io/revision"
    43  
    44  	// RevisionHistoryAnnotation maintains the history of all old revisions that a machine set has served for a machine deployment.
    45  	RevisionHistoryAnnotation = "machinedeployment.clusters.x-k8s.io/revision-history"
    46  
    47  	// DesiredReplicasAnnotation is the desired replicas for a machine deployment recorded as an annotation
    48  	// in its machine sets. Helps in separating scaling events from the rollout process and for
    49  	// determining if the new machine set for a deployment is really saturated.
    50  	DesiredReplicasAnnotation = "machinedeployment.clusters.x-k8s.io/desired-replicas"
    51  
    52  	// MaxReplicasAnnotation is the maximum replicas a deployment can have at a given point, which
    53  	// is machinedeployment.spec.replicas + maxSurge. Used by the underlying machine sets to estimate their
    54  	// proportions in case the deployment has surge replicas.
    55  	MaxReplicasAnnotation = "machinedeployment.clusters.x-k8s.io/max-replicas"
    56  
    57  	// MachineDeploymentUniqueLabel is used to uniquely identify the Machines of a MachineSet.
    58  	// The MachineDeployment controller will set this label on a MachineSet when it is created.
    59  	// The label is also applied to the Machines of the MachineSet and used in the MachineSet selector.
    60  	// Note: For the lifetime of the MachineSet the label's value has to stay the same, otherwise the
    61  	// MachineSet selector would no longer match its Machines.
    62  	// Note: In previous Cluster API versions (< v1.4.0), the label value was the hash of the full machine template.
    63  	// With the introduction of in-place mutation the machine template of the MachineSet can change.
    64  	// Because of that it is impossible that the label's value to always be the hash of the full machine template.
    65  	// (Because the hash changes when the machine template changes).
    66  	// As a result, we use the hash of the machine template while ignoring all in-place mutable fields, i.e. the
    67  	// machine template with only fields that could trigger a rollout for the machine-template-hash, making it
    68  	// independent of the changes to any in-place mutable fields.
    69  	// A random string is appended at the end of the label value (label value format is "<hash>-<random string>"))
    70  	// to distinguish duplicate MachineSets that have the exact same spec but were created as a result of rolloutAfter.
    71  	MachineDeploymentUniqueLabel = "machine-template-hash"
    72  )
    73  
    74  // ANCHOR: MachineDeploymentSpec
    75  
    76  // MachineDeploymentSpec defines the desired state of MachineDeployment.
    77  type MachineDeploymentSpec struct {
    78  	// ClusterName is the name of the Cluster this object belongs to.
    79  	// +kubebuilder:validation:MinLength=1
    80  	ClusterName string `json:"clusterName"`
    81  
    82  	// Number of desired machines.
    83  	// This is a pointer to distinguish between explicit zero and not specified.
    84  	//
    85  	// Defaults to:
    86  	// * if the Kubernetes autoscaler min size and max size annotations are set:
    87  	//   - if it's a new MachineDeployment, use min size
    88  	//   - if the replicas field of the old MachineDeployment is < min size, use min size
    89  	//   - if the replicas field of the old MachineDeployment is > max size, use max size
    90  	//   - if the replicas field of the old MachineDeployment is in the (min size, max size) range, keep the value from the oldMD
    91  	// * otherwise use 1
    92  	// Note: Defaulting will be run whenever the replicas field is not set:
    93  	// * A new MachineDeployment is created with replicas not set.
    94  	// * On an existing MachineDeployment the replicas field was first set and is now unset.
    95  	// Those cases are especially relevant for the following Kubernetes autoscaler use cases:
    96  	// * A new MachineDeployment is created and replicas should be managed by the autoscaler
    97  	// * An existing MachineDeployment which initially wasn't controlled by the autoscaler
    98  	//   should be later controlled by the autoscaler
    99  	// +optional
   100  	Replicas *int32 `json:"replicas,omitempty"`
   101  
   102  	// RolloutAfter is a field to indicate a rollout should be performed
   103  	// after the specified time even if no changes have been made to the
   104  	// MachineDeployment.
   105  	// Example: In the YAML the time can be specified in the RFC3339 format.
   106  	// To specify the rolloutAfter target as March 9, 2023, at 9 am UTC
   107  	// use "2023-03-09T09:00:00Z".
   108  	// +optional
   109  	RolloutAfter *metav1.Time `json:"rolloutAfter,omitempty"`
   110  
   111  	// Label selector for machines. Existing MachineSets whose machines are
   112  	// selected by this will be the ones affected by this deployment.
   113  	// It must match the machine template's labels.
   114  	Selector metav1.LabelSelector `json:"selector"`
   115  
   116  	// Template describes the machines that will be created.
   117  	Template MachineTemplateSpec `json:"template"`
   118  
   119  	// The deployment strategy to use to replace existing machines with
   120  	// new ones.
   121  	// +optional
   122  	Strategy *MachineDeploymentStrategy `json:"strategy,omitempty"`
   123  
   124  	// MinReadySeconds is the minimum number of seconds for which a Node for a newly created machine should be ready before considering the replica available.
   125  	// Defaults to 0 (machine will be considered available as soon as the Node is ready)
   126  	// +optional
   127  	MinReadySeconds *int32 `json:"minReadySeconds,omitempty"`
   128  
   129  	// The number of old MachineSets to retain to allow rollback.
   130  	// This is a pointer to distinguish between explicit zero and not specified.
   131  	// Defaults to 1.
   132  	// +optional
   133  	RevisionHistoryLimit *int32 `json:"revisionHistoryLimit,omitempty"`
   134  
   135  	// Indicates that the deployment is paused.
   136  	// +optional
   137  	Paused bool `json:"paused,omitempty"`
   138  
   139  	// The maximum time in seconds for a deployment to make progress before it
   140  	// is considered to be failed. The deployment controller will continue to
   141  	// process failed deployments and a condition with a ProgressDeadlineExceeded
   142  	// reason will be surfaced in the deployment status. Note that progress will
   143  	// not be estimated during the time a deployment is paused. Defaults to 600s.
   144  	// +optional
   145  	ProgressDeadlineSeconds *int32 `json:"progressDeadlineSeconds,omitempty"`
   146  }
   147  
   148  // ANCHOR_END: MachineDeploymentSpec
   149  
   150  // ANCHOR: MachineDeploymentStrategy
   151  
   152  // MachineDeploymentStrategy describes how to replace existing machines
   153  // with new ones.
   154  type MachineDeploymentStrategy struct {
   155  	// Type of deployment. Allowed values are RollingUpdate and OnDelete.
   156  	// The default is RollingUpdate.
   157  	// +kubebuilder:validation:Enum=RollingUpdate;OnDelete
   158  	// +optional
   159  	Type MachineDeploymentStrategyType `json:"type,omitempty"`
   160  
   161  	// Rolling update config params. Present only if
   162  	// MachineDeploymentStrategyType = RollingUpdate.
   163  	// +optional
   164  	RollingUpdate *MachineRollingUpdateDeployment `json:"rollingUpdate,omitempty"`
   165  }
   166  
   167  // ANCHOR_END: MachineDeploymentStrategy
   168  
   169  // ANCHOR: MachineRollingUpdateDeployment
   170  
   171  // MachineRollingUpdateDeployment is used to control the desired behavior of rolling update.
   172  type MachineRollingUpdateDeployment struct {
   173  	// The maximum number of machines that can be unavailable during the update.
   174  	// Value can be an absolute number (ex: 5) or a percentage of desired
   175  	// machines (ex: 10%).
   176  	// Absolute number is calculated from percentage by rounding down.
   177  	// This can not be 0 if MaxSurge is 0.
   178  	// Defaults to 0.
   179  	// Example: when this is set to 30%, the old MachineSet can be scaled
   180  	// down to 70% of desired machines immediately when the rolling update
   181  	// starts. Once new machines are ready, old MachineSet can be scaled
   182  	// down further, followed by scaling up the new MachineSet, ensuring
   183  	// that the total number of machines available at all times
   184  	// during the update is at least 70% of desired machines.
   185  	// +optional
   186  	MaxUnavailable *intstr.IntOrString `json:"maxUnavailable,omitempty"`
   187  
   188  	// The maximum number of machines that can be scheduled above the
   189  	// desired number of machines.
   190  	// Value can be an absolute number (ex: 5) or a percentage of
   191  	// desired machines (ex: 10%).
   192  	// This can not be 0 if MaxUnavailable is 0.
   193  	// Absolute number is calculated from percentage by rounding up.
   194  	// Defaults to 1.
   195  	// Example: when this is set to 30%, the new MachineSet can be scaled
   196  	// up immediately when the rolling update starts, such that the total
   197  	// number of old and new machines do not exceed 130% of desired
   198  	// machines. Once old machines have been killed, new MachineSet can
   199  	// be scaled up further, ensuring that total number of machines running
   200  	// at any time during the update is at most 130% of desired machines.
   201  	// +optional
   202  	MaxSurge *intstr.IntOrString `json:"maxSurge,omitempty"`
   203  
   204  	// DeletePolicy defines the policy used by the MachineDeployment to identify nodes to delete when downscaling.
   205  	// Valid values are "Random, "Newest", "Oldest"
   206  	// When no value is supplied, the default DeletePolicy of MachineSet is used
   207  	// +kubebuilder:validation:Enum=Random;Newest;Oldest
   208  	// +optional
   209  	DeletePolicy *string `json:"deletePolicy,omitempty"`
   210  }
   211  
   212  // ANCHOR_END: MachineRollingUpdateDeployment
   213  
   214  // ANCHOR: MachineDeploymentStatus
   215  
   216  // MachineDeploymentStatus defines the observed state of MachineDeployment.
   217  type MachineDeploymentStatus struct {
   218  	// The generation observed by the deployment controller.
   219  	// +optional
   220  	ObservedGeneration int64 `json:"observedGeneration,omitempty"`
   221  
   222  	// Selector is the same as the label selector but in the string format to avoid introspection
   223  	// by clients. The string will be in the same format as the query-param syntax.
   224  	// More info about label selectors: http://kubernetes.io/docs/user-guide/labels#label-selectors
   225  	// +optional
   226  	Selector string `json:"selector,omitempty"`
   227  
   228  	// Total number of non-terminated machines targeted by this deployment
   229  	// (their labels match the selector).
   230  	// +optional
   231  	Replicas int32 `json:"replicas"`
   232  
   233  	// Total number of non-terminated machines targeted by this deployment
   234  	// that have the desired template spec.
   235  	// +optional
   236  	UpdatedReplicas int32 `json:"updatedReplicas"`
   237  
   238  	// Total number of ready machines targeted by this deployment.
   239  	// +optional
   240  	ReadyReplicas int32 `json:"readyReplicas"`
   241  
   242  	// Total number of available machines (ready for at least minReadySeconds)
   243  	// targeted by this deployment.
   244  	// +optional
   245  	AvailableReplicas int32 `json:"availableReplicas"`
   246  
   247  	// Total number of unavailable machines targeted by this deployment.
   248  	// This is the total number of machines that are still required for
   249  	// the deployment to have 100% available capacity. They may either
   250  	// be machines that are running but not yet available or machines
   251  	// that still have not been created.
   252  	// +optional
   253  	UnavailableReplicas int32 `json:"unavailableReplicas"`
   254  
   255  	// Phase represents the current phase of a MachineDeployment (ScalingUp, ScalingDown, Running, Failed, or Unknown).
   256  	// +optional
   257  	Phase string `json:"phase,omitempty"`
   258  
   259  	// Conditions defines current service state of the MachineDeployment.
   260  	// +optional
   261  	Conditions Conditions `json:"conditions,omitempty"`
   262  }
   263  
   264  // ANCHOR_END: MachineDeploymentStatus
   265  
   266  // MachineDeploymentPhase indicates the progress of the machine deployment.
   267  type MachineDeploymentPhase string
   268  
   269  const (
   270  	// MachineDeploymentPhaseScalingUp indicates the MachineDeployment is scaling up.
   271  	MachineDeploymentPhaseScalingUp = MachineDeploymentPhase("ScalingUp")
   272  
   273  	// MachineDeploymentPhaseScalingDown indicates the MachineDeployment is scaling down.
   274  	MachineDeploymentPhaseScalingDown = MachineDeploymentPhase("ScalingDown")
   275  
   276  	// MachineDeploymentPhaseRunning indicates scaling has completed and all Machines are running.
   277  	MachineDeploymentPhaseRunning = MachineDeploymentPhase("Running")
   278  
   279  	// MachineDeploymentPhaseFailed indicates there was a problem scaling and user intervention might be required.
   280  	MachineDeploymentPhaseFailed = MachineDeploymentPhase("Failed")
   281  
   282  	// MachineDeploymentPhaseUnknown indicates the state of the MachineDeployment cannot be determined.
   283  	MachineDeploymentPhaseUnknown = MachineDeploymentPhase("Unknown")
   284  )
   285  
   286  // SetTypedPhase sets the Phase field to the string representation of MachineDeploymentPhase.
   287  func (md *MachineDeploymentStatus) SetTypedPhase(p MachineDeploymentPhase) {
   288  	md.Phase = string(p)
   289  }
   290  
   291  // GetTypedPhase attempts to parse the Phase field and return
   292  // the typed MachineDeploymentPhase representation.
   293  func (md *MachineDeploymentStatus) GetTypedPhase() MachineDeploymentPhase {
   294  	switch phase := MachineDeploymentPhase(md.Phase); phase {
   295  	case
   296  		MachineDeploymentPhaseScalingDown,
   297  		MachineDeploymentPhaseScalingUp,
   298  		MachineDeploymentPhaseRunning,
   299  		MachineDeploymentPhaseFailed:
   300  		return phase
   301  	default:
   302  		return MachineDeploymentPhaseUnknown
   303  	}
   304  }
   305  
   306  // +kubebuilder:object:root=true
   307  // +kubebuilder:resource:path=machinedeployments,shortName=md,scope=Namespaced,categories=cluster-api
   308  // +kubebuilder:storageversion
   309  // +kubebuilder:subresource:status
   310  // +kubebuilder:subresource:scale:specpath=.spec.replicas,statuspath=.status.replicas,selectorpath=.status.selector
   311  // +kubebuilder:printcolumn:name="Cluster",type="string",JSONPath=".spec.clusterName",description="Cluster"
   312  // +kubebuilder:printcolumn:name="Desired",type=integer,JSONPath=".spec.replicas",description="Total number of machines desired by this MachineDeployment",priority=10
   313  // +kubebuilder:printcolumn:name="Replicas",type="integer",JSONPath=".status.replicas",description="Total number of non-terminated machines targeted by this MachineDeployment"
   314  // +kubebuilder:printcolumn:name="Ready",type="integer",JSONPath=".status.readyReplicas",description="Total number of ready machines targeted by this MachineDeployment"
   315  // +kubebuilder:printcolumn:name="Updated",type=integer,JSONPath=".status.updatedReplicas",description="Total number of non-terminated machines targeted by this deployment that have the desired template spec"
   316  // +kubebuilder:printcolumn:name="Unavailable",type=integer,JSONPath=".status.unavailableReplicas",description="Total number of unavailable machines targeted by this MachineDeployment"
   317  // +kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase",description="MachineDeployment status such as ScalingUp/ScalingDown/Running/Failed/Unknown"
   318  // +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Time duration since creation of MachineDeployment"
   319  // +kubebuilder:printcolumn:name="Version",type="string",JSONPath=".spec.template.spec.version",description="Kubernetes version associated with this MachineDeployment"
   320  
   321  // MachineDeployment is the Schema for the machinedeployments API.
   322  type MachineDeployment struct {
   323  	metav1.TypeMeta   `json:",inline"`
   324  	metav1.ObjectMeta `json:"metadata,omitempty"`
   325  
   326  	Spec   MachineDeploymentSpec   `json:"spec,omitempty"`
   327  	Status MachineDeploymentStatus `json:"status,omitempty"`
   328  }
   329  
   330  // +kubebuilder:object:root=true
   331  
   332  // MachineDeploymentList contains a list of MachineDeployment.
   333  type MachineDeploymentList struct {
   334  	metav1.TypeMeta `json:",inline"`
   335  	metav1.ListMeta `json:"metadata,omitempty"`
   336  	Items           []MachineDeployment `json:"items"`
   337  }
   338  
   339  func init() {
   340  	objectTypes = append(objectTypes, &MachineDeployment{}, &MachineDeploymentList{})
   341  }
   342  
   343  // GetConditions returns the set of conditions for the machinedeployment.
   344  func (m *MachineDeployment) GetConditions() Conditions {
   345  	return m.Status.Conditions
   346  }
   347  
   348  // SetConditions updates the set of conditions on the machinedeployment.
   349  func (m *MachineDeployment) SetConditions(conditions Conditions) {
   350  	m.Status.Conditions = conditions
   351  }