Skip to content

Commit 9867d5a

Browse files
feat(api): add Conditions field and update for CAPI v1.11
Add Conditions to AWSMachineTemplateStatus and update controller for CAPI v1.11 API changes. Squashed from 2 commits: - ffdf7db Fix review comments 4 - 6493363 rebase #5720
1 parent b1258dc commit 9867d5a

8 files changed

+226
-78
lines changed

api/v1beta1/awsmachine_conversion.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ func (r *AWSMachineTemplate) ConvertTo(dstRaw conversion.Hub) error {
138138

139139
// Restore Status fields that don't exist in v1beta1.
140140
dst.Status.NodeInfo = restored.Status.NodeInfo
141+
dst.Status.Conditions = restored.Status.Conditions
141142

142143
return nil
143144
}

api/v1beta1/conversion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,6 @@ func Convert_v1beta2_AWSMachineStatus_To_v1beta1_AWSMachineStatus(in *v1beta2.AW
110110
}
111111

112112
func Convert_v1beta2_AWSMachineTemplateStatus_To_v1beta1_AWSMachineTemplateStatus(in *v1beta2.AWSMachineTemplateStatus, out *AWSMachineTemplateStatus, s conversion.Scope) error {
113-
// NodeInfo field is ignored (dropped) as it doesn't exist in v1beta1
113+
// NodeInfo and Conditions fields are ignored (dropped) as they don't exist in v1beta1
114114
return autoConvert_v1beta2_AWSMachineTemplateStatus_To_v1beta1_AWSMachineTemplateStatus(in, out, s)
115115
}

api/v1beta1/zz_generated.conversion.go

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1beta2/awsmachinetemplate_types.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ type AWSMachineTemplateStatus struct {
7272
// https://github.com/kubernetes-sigs/cluster-api/blob/main/docs/proposals/20210310-opt-in-autoscaling-from-zero.md
7373
// +optional
7474
NodeInfo *NodeInfo `json:"nodeInfo,omitempty"`
75+
76+
// Conditions defines current service state of the AWSMachineTemplate.
77+
// +optional
78+
Conditions clusterv1beta1.Conditions `json:"conditions,omitempty"`
7579
}
7680

7781
// AWSMachineTemplateSpec defines the desired state of AWSMachineTemplate.
@@ -114,6 +118,16 @@ type AWSMachineTemplateResource struct {
114118
Spec AWSMachineSpec `json:"spec"`
115119
}
116120

121+
// GetConditions returns the observations of the operational state of the AWSMachineTemplate resource.
122+
func (r *AWSMachineTemplate) GetConditions() clusterv1beta1.Conditions {
123+
return r.Status.Conditions
124+
}
125+
126+
// SetConditions sets the underlying service state of the AWSMachineTemplate to the predescribed clusterv1beta1.Conditions.
127+
func (r *AWSMachineTemplate) SetConditions(conditions clusterv1beta1.Conditions) {
128+
r.Status.Conditions = conditions
129+
}
130+
117131
func init() {
118132
SchemeBuilder.Register(&AWSMachineTemplate{}, &AWSMachineTemplateList{})
119133
}

api/v1beta2/zz_generated.deepcopy.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/infrastructure.cluster.x-k8s.io_awsmachinetemplates.yaml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,58 @@ spec:
11561156
This value is used for autoscaling from zero operations as defined in:
11571157
https://github.com/kubernetes-sigs/cluster-api/blob/main/docs/proposals/20210310-opt-in-autoscaling-from-zero.md
11581158
type: object
1159+
conditions:
1160+
description: Conditions defines current service state of the AWSMachineTemplate.
1161+
items:
1162+
description: Condition defines an observation of a Cluster API resource
1163+
operational state.
1164+
properties:
1165+
lastTransitionTime:
1166+
description: |-
1167+
lastTransitionTime is the last time the condition transitioned from one status to another.
1168+
This should be when the underlying condition changed. If that is not known, then using the time when
1169+
the API field changed is acceptable.
1170+
format: date-time
1171+
type: string
1172+
message:
1173+
description: |-
1174+
message is a human readable message indicating details about the transition.
1175+
This field may be empty.
1176+
maxLength: 10240
1177+
minLength: 1
1178+
type: string
1179+
reason:
1180+
description: |-
1181+
reason is the reason for the condition's last transition in CamelCase.
1182+
The specific API may choose whether or not this field is considered a guaranteed API.
1183+
This field may be empty.
1184+
maxLength: 256
1185+
minLength: 1
1186+
type: string
1187+
severity:
1188+
description: |-
1189+
severity provides an explicit classification of Reason code, so the users or machines can immediately
1190+
understand the current situation and act accordingly.
1191+
The Severity field MUST be set only when Status=False.
1192+
maxLength: 32
1193+
type: string
1194+
status:
1195+
description: status of the condition, one of True, False, Unknown.
1196+
type: string
1197+
type:
1198+
description: |-
1199+
type of condition in CamelCase or in foo.example.com/CamelCase.
1200+
Many .condition.type values are consistent across resources like Available, but because arbitrary conditions
1201+
can be useful (see .node.status.conditions), the ability to deconflict is important.
1202+
maxLength: 256
1203+
minLength: 1
1204+
type: string
1205+
required:
1206+
- lastTransitionTime
1207+
- status
1208+
- type
1209+
type: object
1210+
type: array
11591211
nodeInfo:
11601212
description: |-
11611213
NodeInfo contains information about the node's architecture and operating system.

controllers/awsmachinetemplate_controller.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ import (
4040
ec2service "sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/services/ec2"
4141
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/logger"
4242
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/record"
43-
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
44-
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
43+
"sigs.k8s.io/cluster-api-provider-aws/v2/util/paused"
44+
controlplanev1 "sigs.k8s.io/cluster-api/api/controlplane/kubeadm/v1beta2"
45+
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
4546
"sigs.k8s.io/cluster-api/util"
4647
"sigs.k8s.io/cluster-api/util/predicates"
4748
)
@@ -101,8 +102,22 @@ func (r *AWSMachineTemplateReconciler) Reconcile(ctx context.Context, req ctrl.R
101102
return ctrl.Result{}, nil
102103
}
103104

105+
// Get the owner cluster
106+
cluster, err := util.GetOwnerCluster(ctx, r.Client, awsMachineTemplate.ObjectMeta)
107+
if err != nil {
108+
return ctrl.Result{}, err
109+
}
110+
if cluster == nil {
111+
return ctrl.Result{}, nil
112+
}
113+
114+
// Check if the resource is paused
115+
if isPaused, conditionChanged, err := paused.EnsurePausedCondition(ctx, r.Client, cluster, awsMachineTemplate); err != nil || isPaused || conditionChanged {
116+
return ctrl.Result{}, err
117+
}
118+
104119
// Find the region by checking ownerReferences
105-
region, err := r.getRegion(ctx, awsMachineTemplate)
120+
region, err := r.getRegion(ctx, cluster)
106121
if err != nil {
107122
return ctrl.Result{}, err
108123
}
@@ -155,18 +170,13 @@ func (r *AWSMachineTemplateReconciler) Reconcile(ctx context.Context, req ctrl.R
155170
}
156171

157172
// getRegion finds the region by checking the template's owner cluster reference.
158-
func (r *AWSMachineTemplateReconciler) getRegion(ctx context.Context, template *infrav1.AWSMachineTemplate) (string, error) {
159-
// Get the owner cluster
160-
cluster, err := util.GetOwnerCluster(ctx, r.Client, template.ObjectMeta)
161-
if err != nil {
162-
return "", err
163-
}
173+
func (r *AWSMachineTemplateReconciler) getRegion(ctx context.Context, cluster *clusterv1.Cluster) (string, error) {
164174
if cluster == nil {
165175
return "", errors.New("no owner cluster found")
166176
}
167177

168178
// Get region from AWSCluster (standard EC2-based cluster)
169-
if cluster.Spec.InfrastructureRef != nil && cluster.Spec.InfrastructureRef.Kind == "AWSCluster" {
179+
if cluster.Spec.InfrastructureRef.IsDefined() && cluster.Spec.InfrastructureRef.Kind == "AWSCluster" {
170180
awsCluster := &infrav1.AWSCluster{}
171181
if err := r.Get(ctx, client.ObjectKey{
172182
Namespace: cluster.Namespace,
@@ -181,7 +191,7 @@ func (r *AWSMachineTemplateReconciler) getRegion(ctx context.Context, template *
181191
}
182192

183193
// Get region from AWSManagedControlPlane (EKS cluster)
184-
if cluster.Spec.ControlPlaneRef != nil && cluster.Spec.ControlPlaneRef.Kind == "AWSManagedControlPlane" {
194+
if cluster.Spec.ControlPlaneRef.IsDefined() && cluster.Spec.ControlPlaneRef.Kind == "AWSManagedControlPlane" {
185195
awsManagedCP := &ekscontrolplanev1.AWSManagedControlPlane{}
186196
if err := r.Get(ctx, client.ObjectKey{
187197
Namespace: cluster.Namespace,
@@ -363,9 +373,10 @@ func (r *AWSMachineTemplateReconciler) getKubernetesVersion(ctx context.Context,
363373

364374
// Find MachineDeployments that reference this AWSMachineTemplate
365375
for _, md := range machineDeploymentList.Items {
366-
if version := ptr.Deref(md.Spec.Template.Spec.Version, ""); md.Spec.Template.Spec.InfrastructureRef.Kind == "AWSMachineTemplate" &&
367-
md.Spec.Template.Spec.InfrastructureRef.Name == template.Name && version != "" {
368-
return version, nil
376+
if md.Spec.Template.Spec.InfrastructureRef.Kind == "AWSMachineTemplate" &&
377+
md.Spec.Template.Spec.InfrastructureRef.Name == template.Name &&
378+
md.Spec.Template.Spec.Version != "" {
379+
return md.Spec.Template.Spec.Version, nil
369380
}
370381
}
371382

@@ -377,8 +388,8 @@ func (r *AWSMachineTemplateReconciler) getKubernetesVersion(ctx context.Context,
377388

378389
// Find KubeadmControlPlanes that reference this AWSMachineTemplate
379390
for _, kcp := range kcpList.Items {
380-
if kcp.Spec.MachineTemplate.InfrastructureRef.Kind == "AWSMachineTemplate" &&
381-
kcp.Spec.MachineTemplate.InfrastructureRef.Name == template.Name &&
391+
if kcp.Spec.MachineTemplate.Spec.InfrastructureRef.Kind == "AWSMachineTemplate" &&
392+
kcp.Spec.MachineTemplate.Spec.InfrastructureRef.Name == template.Name &&
382393
kcp.Spec.Version != "" {
383394
return kcp.Spec.Version, nil
384395
}

0 commit comments

Comments
 (0)