sigs.k8s.io/cluster-api@v1.6.3/api/v1beta1/machine_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  	corev1 "k8s.io/api/core/v1"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  
    23  	capierrors "sigs.k8s.io/cluster-api/errors"
    24  )
    25  
    26  const (
    27  	// MachineFinalizer is set on PrepareForCreate callback.
    28  	MachineFinalizer = "machine.cluster.x-k8s.io"
    29  
    30  	// MachineControlPlaneLabel is the label set on machines or related objects that are part of a control plane.
    31  	MachineControlPlaneLabel = "cluster.x-k8s.io/control-plane"
    32  
    33  	// ExcludeNodeDrainingAnnotation annotation explicitly skips node draining if set.
    34  	ExcludeNodeDrainingAnnotation = "machine.cluster.x-k8s.io/exclude-node-draining"
    35  
    36  	// ExcludeWaitForNodeVolumeDetachAnnotation annotation explicitly skips the waiting for node volume detaching if set.
    37  	ExcludeWaitForNodeVolumeDetachAnnotation = "machine.cluster.x-k8s.io/exclude-wait-for-node-volume-detach"
    38  
    39  	// MachineSetNameLabel is the label set on machines if they're controlled by MachineSet.
    40  	// Note: The value of this label may be a hash if the MachineSet name is longer than 63 characters.
    41  	MachineSetNameLabel = "cluster.x-k8s.io/set-name"
    42  
    43  	// MachineDeploymentNameLabel is the label set on machines if they're controlled by MachineDeployment.
    44  	MachineDeploymentNameLabel = "cluster.x-k8s.io/deployment-name"
    45  
    46  	// MachinePoolNameLabel is the label indicating the name of the MachinePool a Machine is controlled by.
    47  	// Note: The value of this label may be a hash if the MachinePool name is longer than 63 characters.
    48  	MachinePoolNameLabel = "cluster.x-k8s.io/pool-name"
    49  
    50  	// MachineControlPlaneNameLabel is the label set on machines if they're controlled by a ControlPlane.
    51  	// Note: The value of this label may be a hash if the control plane name is longer than 63 characters.
    52  	MachineControlPlaneNameLabel = "cluster.x-k8s.io/control-plane-name"
    53  
    54  	// PreDrainDeleteHookAnnotationPrefix annotation specifies the prefix we
    55  	// search each annotation for during the pre-drain.delete lifecycle hook
    56  	// to pause reconciliation of deletion. These hooks will prevent removal of
    57  	// draining the associated node until all are removed.
    58  	PreDrainDeleteHookAnnotationPrefix = "pre-drain.delete.hook.machine.cluster.x-k8s.io"
    59  
    60  	// PreTerminateDeleteHookAnnotationPrefix annotation specifies the prefix we
    61  	// search each annotation for during the pre-terminate.delete lifecycle hook
    62  	// to pause reconciliation of deletion. These hooks will prevent removal of
    63  	// an instance from an infrastructure provider until all are removed.
    64  	PreTerminateDeleteHookAnnotationPrefix = "pre-terminate.delete.hook.machine.cluster.x-k8s.io"
    65  
    66  	// MachineCertificatesExpiryDateAnnotation annotation specifies the expiry date of the machine certificates in RFC3339 format.
    67  	// This annotation can be used on control plane machines to trigger rollout before certificates expire.
    68  	// This annotation can be set on BootstrapConfig or Machine objects. The value set on the Machine object takes precedence.
    69  	// This annotation can only be used on Control Plane Machines.
    70  	MachineCertificatesExpiryDateAnnotation = "machine.cluster.x-k8s.io/certificates-expiry"
    71  
    72  	// NodeRoleLabelPrefix is one of the CAPI managed Node label prefixes.
    73  	NodeRoleLabelPrefix = "node-role.kubernetes.io"
    74  	// NodeRestrictionLabelDomain is one of the CAPI managed Node label domains.
    75  	NodeRestrictionLabelDomain = "node-restriction.kubernetes.io"
    76  	// ManagedNodeLabelDomain is one of the CAPI managed Node label domains.
    77  	ManagedNodeLabelDomain = "node.cluster.x-k8s.io"
    78  )
    79  
    80  // ANCHOR: MachineSpec
    81  
    82  // MachineSpec defines the desired state of Machine.
    83  type MachineSpec struct {
    84  	// ClusterName is the name of the Cluster this object belongs to.
    85  	// +kubebuilder:validation:MinLength=1
    86  	ClusterName string `json:"clusterName"`
    87  
    88  	// Bootstrap is a reference to a local struct which encapsulates
    89  	// fields to configure the Machine’s bootstrapping mechanism.
    90  	Bootstrap Bootstrap `json:"bootstrap"`
    91  
    92  	// InfrastructureRef is a required reference to a custom resource
    93  	// offered by an infrastructure provider.
    94  	InfrastructureRef corev1.ObjectReference `json:"infrastructureRef"`
    95  
    96  	// Version defines the desired Kubernetes version.
    97  	// This field is meant to be optionally used by bootstrap providers.
    98  	// +optional
    99  	Version *string `json:"version,omitempty"`
   100  
   101  	// ProviderID is the identification ID of the machine provided by the provider.
   102  	// This field must match the provider ID as seen on the node object corresponding to this machine.
   103  	// This field is required by higher level consumers of cluster-api. Example use case is cluster autoscaler
   104  	// with cluster-api as provider. Clean-up logic in the autoscaler compares machines to nodes to find out
   105  	// machines at provider which could not get registered as Kubernetes nodes. With cluster-api as a
   106  	// generic out-of-tree provider for autoscaler, this field is required by autoscaler to be
   107  	// able to have a provider view of the list of machines. Another list of nodes is queried from the k8s apiserver
   108  	// and then a comparison is done to find out unregistered machines and are marked for delete.
   109  	// This field will be set by the actuators and consumed by higher level entities like autoscaler that will
   110  	// be interfacing with cluster-api as generic provider.
   111  	// +optional
   112  	ProviderID *string `json:"providerID,omitempty"`
   113  
   114  	// FailureDomain is the failure domain the machine will be created in.
   115  	// Must match a key in the FailureDomains map stored on the cluster object.
   116  	// +optional
   117  	FailureDomain *string `json:"failureDomain,omitempty"`
   118  
   119  	// NodeDrainTimeout is the total amount of time that the controller will spend on draining a node.
   120  	// The default value is 0, meaning that the node can be drained without any time limitations.
   121  	// NOTE: NodeDrainTimeout is different from `kubectl drain --timeout`
   122  	// +optional
   123  	NodeDrainTimeout *metav1.Duration `json:"nodeDrainTimeout,omitempty"`
   124  
   125  	// NodeVolumeDetachTimeout is the total amount of time that the controller will spend on waiting for all volumes
   126  	// to be detached. The default value is 0, meaning that the volumes can be detached without any time limitations.
   127  	// +optional
   128  	NodeVolumeDetachTimeout *metav1.Duration `json:"nodeVolumeDetachTimeout,omitempty"`
   129  
   130  	// NodeDeletionTimeout defines how long the controller will attempt to delete the Node that the Machine
   131  	// hosts after the Machine is marked for deletion. A duration of 0 will retry deletion indefinitely.
   132  	// Defaults to 10 seconds.
   133  	// +optional
   134  	NodeDeletionTimeout *metav1.Duration `json:"nodeDeletionTimeout,omitempty"`
   135  }
   136  
   137  // ANCHOR_END: MachineSpec
   138  
   139  // ANCHOR: MachineStatus
   140  
   141  // MachineStatus defines the observed state of Machine.
   142  type MachineStatus struct {
   143  	// NodeRef will point to the corresponding Node if it exists.
   144  	// +optional
   145  	NodeRef *corev1.ObjectReference `json:"nodeRef,omitempty"`
   146  
   147  	// NodeInfo is a set of ids/uuids to uniquely identify the node.
   148  	// More info: https://kubernetes.io/docs/concepts/nodes/node/#info
   149  	// +optional
   150  	NodeInfo *corev1.NodeSystemInfo `json:"nodeInfo,omitempty"`
   151  
   152  	// LastUpdated identifies when the phase of the Machine last transitioned.
   153  	// +optional
   154  	LastUpdated *metav1.Time `json:"lastUpdated,omitempty"`
   155  
   156  	// FailureReason will be set in the event that there is a terminal problem
   157  	// reconciling the Machine and will contain a succinct value suitable
   158  	// for machine interpretation.
   159  	//
   160  	// This field should not be set for transitive errors that a controller
   161  	// faces that are expected to be fixed automatically over
   162  	// time (like service outages), but instead indicate that something is
   163  	// fundamentally wrong with the Machine's spec or the configuration of
   164  	// the controller, and that manual intervention is required. Examples
   165  	// of terminal errors would be invalid combinations of settings in the
   166  	// spec, values that are unsupported by the controller, or the
   167  	// responsible controller itself being critically misconfigured.
   168  	//
   169  	// Any transient errors that occur during the reconciliation of Machines
   170  	// can be added as events to the Machine object and/or logged in the
   171  	// controller's output.
   172  	// +optional
   173  	FailureReason *capierrors.MachineStatusError `json:"failureReason,omitempty"`
   174  
   175  	// FailureMessage will be set in the event that there is a terminal problem
   176  	// reconciling the Machine and will contain a more verbose string suitable
   177  	// for logging and human consumption.
   178  	//
   179  	// This field should not be set for transitive errors that a controller
   180  	// faces that are expected to be fixed automatically over
   181  	// time (like service outages), but instead indicate that something is
   182  	// fundamentally wrong with the Machine's spec or the configuration of
   183  	// the controller, and that manual intervention is required. Examples
   184  	// of terminal errors would be invalid combinations of settings in the
   185  	// spec, values that are unsupported by the controller, or the
   186  	// responsible controller itself being critically misconfigured.
   187  	//
   188  	// Any transient errors that occur during the reconciliation of Machines
   189  	// can be added as events to the Machine object and/or logged in the
   190  	// controller's output.
   191  	// +optional
   192  	FailureMessage *string `json:"failureMessage,omitempty"`
   193  
   194  	// Addresses is a list of addresses assigned to the machine.
   195  	// This field is copied from the infrastructure provider reference.
   196  	// +optional
   197  	Addresses MachineAddresses `json:"addresses,omitempty"`
   198  
   199  	// Phase represents the current phase of machine actuation.
   200  	// E.g. Pending, Running, Terminating, Failed etc.
   201  	// +optional
   202  	Phase string `json:"phase,omitempty"`
   203  
   204  	// CertificatesExpiryDate is the expiry date of the machine certificates.
   205  	// This value is only set for control plane machines.
   206  	// +optional
   207  	CertificatesExpiryDate *metav1.Time `json:"certificatesExpiryDate,omitempty"`
   208  
   209  	// BootstrapReady is the state of the bootstrap provider.
   210  	// +optional
   211  	BootstrapReady bool `json:"bootstrapReady"`
   212  
   213  	// InfrastructureReady is the state of the infrastructure provider.
   214  	// +optional
   215  	InfrastructureReady bool `json:"infrastructureReady"`
   216  
   217  	// ObservedGeneration is the latest generation observed by the controller.
   218  	// +optional
   219  	ObservedGeneration int64 `json:"observedGeneration,omitempty"`
   220  
   221  	// Conditions defines current service state of the Machine.
   222  	// +optional
   223  	Conditions Conditions `json:"conditions,omitempty"`
   224  }
   225  
   226  // ANCHOR_END: MachineStatus
   227  
   228  // SetTypedPhase sets the Phase field to the string representation of MachinePhase.
   229  func (m *MachineStatus) SetTypedPhase(p MachinePhase) {
   230  	m.Phase = string(p)
   231  }
   232  
   233  // GetTypedPhase attempts to parse the Phase field and return
   234  // the typed MachinePhase representation as described in `machine_phase_types.go`.
   235  func (m *MachineStatus) GetTypedPhase() MachinePhase {
   236  	switch phase := MachinePhase(m.Phase); phase {
   237  	case
   238  		MachinePhasePending,
   239  		MachinePhaseProvisioning,
   240  		MachinePhaseProvisioned,
   241  		MachinePhaseRunning,
   242  		MachinePhaseDeleting,
   243  		MachinePhaseDeleted,
   244  		MachinePhaseFailed:
   245  		return phase
   246  	default:
   247  		return MachinePhaseUnknown
   248  	}
   249  }
   250  
   251  // ANCHOR: Bootstrap
   252  
   253  // Bootstrap encapsulates fields to configure the Machine’s bootstrapping mechanism.
   254  type Bootstrap struct {
   255  	// ConfigRef is a reference to a bootstrap provider-specific resource
   256  	// that holds configuration details. The reference is optional to
   257  	// allow users/operators to specify Bootstrap.DataSecretName without
   258  	// the need of a controller.
   259  	// +optional
   260  	ConfigRef *corev1.ObjectReference `json:"configRef,omitempty"`
   261  
   262  	// DataSecretName is the name of the secret that stores the bootstrap data script.
   263  	// If nil, the Machine should remain in the Pending state.
   264  	// +optional
   265  	DataSecretName *string `json:"dataSecretName,omitempty"`
   266  }
   267  
   268  // ANCHOR_END: Bootstrap
   269  
   270  // +kubebuilder:object:root=true
   271  // +kubebuilder:resource:path=machines,shortName=ma,scope=Namespaced,categories=cluster-api
   272  // +kubebuilder:subresource:status
   273  // +kubebuilder:storageversion
   274  // +kubebuilder:printcolumn:name="Cluster",type="string",JSONPath=".spec.clusterName",description="Cluster"
   275  // +kubebuilder:printcolumn:name="NodeName",type="string",JSONPath=".status.nodeRef.name",description="Node name associated with this machine"
   276  // +kubebuilder:printcolumn:name="ProviderID",type="string",JSONPath=".spec.providerID",description="Provider ID"
   277  // +kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase",description="Machine status such as Terminating/Pending/Running/Failed etc"
   278  // +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Time duration since creation of Machine"
   279  // +kubebuilder:printcolumn:name="Version",type="string",JSONPath=".spec.version",description="Kubernetes version associated with this Machine"
   280  
   281  // Machine is the Schema for the machines API.
   282  type Machine struct {
   283  	metav1.TypeMeta   `json:",inline"`
   284  	metav1.ObjectMeta `json:"metadata,omitempty"`
   285  
   286  	Spec   MachineSpec   `json:"spec,omitempty"`
   287  	Status MachineStatus `json:"status,omitempty"`
   288  }
   289  
   290  // GetConditions returns the set of conditions for this object.
   291  func (m *Machine) GetConditions() Conditions {
   292  	return m.Status.Conditions
   293  }
   294  
   295  // SetConditions sets the conditions on this object.
   296  func (m *Machine) SetConditions(conditions Conditions) {
   297  	m.Status.Conditions = conditions
   298  }
   299  
   300  // +kubebuilder:object:root=true
   301  
   302  // MachineList contains a list of Machine.
   303  type MachineList struct {
   304  	metav1.TypeMeta `json:",inline"`
   305  	metav1.ListMeta `json:"metadata,omitempty"`
   306  	Items           []Machine `json:"items"`
   307  }
   308  
   309  func init() {
   310  	objectTypes = append(objectTypes, &Machine{}, &MachineList{})
   311  }