sigs.k8s.io/cluster-api@v1.7.1/api/v1beta1/machinehealthcheck_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  	"time"
    21  
    22  	corev1 "k8s.io/api/core/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/util/intstr"
    25  )
    26  
    27  var (
    28  	// DefaultNodeStartupTimeout is the time allowed for a node to start up.
    29  	// Can be made longer as part of spec if required for particular provider.
    30  	// 10 minutes should allow the instance to start and the node to join the
    31  	// cluster on most providers.
    32  	DefaultNodeStartupTimeout = metav1.Duration{Duration: 10 * time.Minute}
    33  )
    34  
    35  // ANCHOR: MachineHealthCheckSpec
    36  
    37  // MachineHealthCheckSpec defines the desired state of MachineHealthCheck.
    38  type MachineHealthCheckSpec struct {
    39  	// ClusterName is the name of the Cluster this object belongs to.
    40  	// +kubebuilder:validation:MinLength=1
    41  	ClusterName string `json:"clusterName"`
    42  
    43  	// Label selector to match machines whose health will be exercised
    44  	Selector metav1.LabelSelector `json:"selector"`
    45  
    46  	// UnhealthyConditions contains a list of the conditions that determine
    47  	// whether a node is considered unhealthy.  The conditions are combined in a
    48  	// logical OR, i.e. if any of the conditions is met, the node is unhealthy.
    49  	//
    50  	// +kubebuilder:validation:MinItems=1
    51  	UnhealthyConditions []UnhealthyCondition `json:"unhealthyConditions"`
    52  
    53  	// Any further remediation is only allowed if at most "MaxUnhealthy" machines selected by
    54  	// "selector" are not healthy.
    55  	// +optional
    56  	MaxUnhealthy *intstr.IntOrString `json:"maxUnhealthy,omitempty"`
    57  
    58  	// Any further remediation is only allowed if the number of machines selected by "selector" as not healthy
    59  	// is within the range of "UnhealthyRange". Takes precedence over MaxUnhealthy.
    60  	// Eg. "[3-5]" - This means that remediation will be allowed only when:
    61  	// (a) there are at least 3 unhealthy machines (and)
    62  	// (b) there are at most 5 unhealthy machines
    63  	// +optional
    64  	// +kubebuilder:validation:Pattern=^\[[0-9]+-[0-9]+\]$
    65  	UnhealthyRange *string `json:"unhealthyRange,omitempty"`
    66  
    67  	// Machines older than this duration without a node will be considered to have
    68  	// failed and will be remediated.
    69  	// If not set, this value is defaulted to 10 minutes.
    70  	// If you wish to disable this feature, set the value explicitly to 0.
    71  	// +optional
    72  	NodeStartupTimeout *metav1.Duration `json:"nodeStartupTimeout,omitempty"`
    73  
    74  	// RemediationTemplate is a reference to a remediation template
    75  	// provided by an infrastructure provider.
    76  	//
    77  	// This field is completely optional, when filled, the MachineHealthCheck controller
    78  	// creates a new object from the template referenced and hands off remediation of the machine to
    79  	// a controller that lives outside of Cluster API.
    80  	// +optional
    81  	RemediationTemplate *corev1.ObjectReference `json:"remediationTemplate,omitempty"`
    82  }
    83  
    84  // ANCHOR_END: MachineHealthCHeckSpec
    85  
    86  // ANCHOR: UnhealthyCondition
    87  
    88  // UnhealthyCondition represents a Node condition type and value with a timeout
    89  // specified as a duration.  When the named condition has been in the given
    90  // status for at least the timeout value, a node is considered unhealthy.
    91  type UnhealthyCondition struct {
    92  	// +kubebuilder:validation:Type=string
    93  	// +kubebuilder:validation:MinLength=1
    94  	Type corev1.NodeConditionType `json:"type"`
    95  
    96  	// +kubebuilder:validation:Type=string
    97  	// +kubebuilder:validation:MinLength=1
    98  	Status corev1.ConditionStatus `json:"status"`
    99  
   100  	Timeout metav1.Duration `json:"timeout"`
   101  }
   102  
   103  // ANCHOR_END: UnhealthyCondition
   104  
   105  // ANCHOR: MachineHealthCheckStatus
   106  
   107  // MachineHealthCheckStatus defines the observed state of MachineHealthCheck.
   108  type MachineHealthCheckStatus struct {
   109  	// total number of machines counted by this machine health check
   110  	// +kubebuilder:validation:Minimum=0
   111  	// +optional
   112  	ExpectedMachines int32 `json:"expectedMachines"`
   113  
   114  	// total number of healthy machines counted by this machine health check
   115  	// +kubebuilder:validation:Minimum=0
   116  	// +optional
   117  	CurrentHealthy int32 `json:"currentHealthy"`
   118  
   119  	// RemediationsAllowed is the number of further remediations allowed by this machine health check before
   120  	// maxUnhealthy short circuiting will be applied
   121  	// +kubebuilder:validation:Minimum=0
   122  	// +optional
   123  	RemediationsAllowed int32 `json:"remediationsAllowed"`
   124  
   125  	// ObservedGeneration is the latest generation observed by the controller.
   126  	// +optional
   127  	ObservedGeneration int64 `json:"observedGeneration,omitempty"`
   128  
   129  	// Targets shows the current list of machines the machine health check is watching
   130  	// +optional
   131  	Targets []string `json:"targets,omitempty"`
   132  
   133  	// Conditions defines current service state of the MachineHealthCheck.
   134  	// +optional
   135  	Conditions Conditions `json:"conditions,omitempty"`
   136  }
   137  
   138  // ANCHOR_END: MachineHealthCheckStatus
   139  
   140  // +kubebuilder:object:root=true
   141  // +kubebuilder:resource:path=machinehealthchecks,shortName=mhc;mhcs,scope=Namespaced,categories=cluster-api
   142  // +kubebuilder:storageversion
   143  // +kubebuilder:subresource:status
   144  // +kubebuilder:printcolumn:name="Cluster",type="string",JSONPath=".spec.clusterName",description="Cluster"
   145  // +kubebuilder:printcolumn:name="ExpectedMachines",type="integer",JSONPath=".status.expectedMachines",description="Number of machines currently monitored"
   146  // +kubebuilder:printcolumn:name="MaxUnhealthy",type="string",JSONPath=".spec.maxUnhealthy",description="Maximum number of unhealthy machines allowed"
   147  // +kubebuilder:printcolumn:name="CurrentHealthy",type="integer",JSONPath=".status.currentHealthy",description="Current observed healthy machines"
   148  // +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Time duration since creation of MachineHealthCheck"
   149  
   150  // MachineHealthCheck is the Schema for the machinehealthchecks API.
   151  type MachineHealthCheck struct {
   152  	metav1.TypeMeta   `json:",inline"`
   153  	metav1.ObjectMeta `json:"metadata,omitempty"`
   154  
   155  	// Specification of machine health check policy
   156  	Spec MachineHealthCheckSpec `json:"spec,omitempty"`
   157  
   158  	// Most recently observed status of MachineHealthCheck resource
   159  	Status MachineHealthCheckStatus `json:"status,omitempty"`
   160  }
   161  
   162  // GetConditions returns the set of conditions for this object.
   163  func (m *MachineHealthCheck) GetConditions() Conditions {
   164  	return m.Status.Conditions
   165  }
   166  
   167  // SetConditions sets the conditions on this object.
   168  func (m *MachineHealthCheck) SetConditions(conditions Conditions) {
   169  	m.Status.Conditions = conditions
   170  }
   171  
   172  // +kubebuilder:object:root=true
   173  
   174  // MachineHealthCheckList contains a list of MachineHealthCheck.
   175  type MachineHealthCheckList struct {
   176  	metav1.TypeMeta `json:",inline"`
   177  	metav1.ListMeta `json:"metadata,omitempty"`
   178  	Items           []MachineHealthCheck `json:"items"`
   179  }
   180  
   181  func init() {
   182  	objectTypes = append(objectTypes, &MachineHealthCheck{}, &MachineHealthCheckList{})
   183  }