Skip to content

Commit 64864e4

Browse files
⚠️ Drop unnecessary fields from Machine status.nodeRef (#12352)
* Refactor machine nodeRef # Conflicts: # api/core/v1beta1/conversion.go * Address comments * More comments
1 parent 01ff70b commit 64864e4

39 files changed

+292
-206
lines changed

api/core/v1beta1/conversion.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,18 @@ func Convert_v1beta2_ExternalPatchDefinition_To_v1beta1_ExternalPatchDefinition(
11401140
return nil
11411141
}
11421142

1143+
func Convert_v1_ObjectReference_To_v1beta2_MachineNodeReference(in *corev1.ObjectReference, out *clusterv1.MachineNodeReference, _ apimachineryconversion.Scope) error {
1144+
out.Name = in.Name
1145+
return nil
1146+
}
1147+
1148+
func Convert_v1beta2_MachineNodeReference_To_v1_ObjectReference(in *clusterv1.MachineNodeReference, out *corev1.ObjectReference, _ apimachineryconversion.Scope) error {
1149+
out.Name = in.Name
1150+
out.APIVersion = corev1.SchemeGroupVersion.String()
1151+
out.Kind = "Node"
1152+
return nil
1153+
}
1154+
11431155
func Convert_v1beta1_LocalObjectTemplate_To_v1beta2_ClusterClassTemplate(in *LocalObjectTemplate, out *clusterv1.ClusterClassTemplate, _ apimachineryconversion.Scope) error {
11441156
if in.Ref == nil {
11451157
return nil

api/core/v1beta1/conversion_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,15 @@ func spokeMachineStatus(in *MachineStatus, c randfill.Continue) {
321321
in.V1Beta2 = nil
322322
}
323323
}
324+
325+
if in.NodeRef != nil {
326+
// Drop everything except name
327+
in.NodeRef = &corev1.ObjectReference{
328+
Name: in.NodeRef.Name,
329+
APIVersion: corev1.SchemeGroupVersion.String(),
330+
Kind: "Node",
331+
}
332+
}
324333
}
325334

326335
func MachineSetFuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {

api/core/v1beta1/zz_generated.conversion.go

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

api/core/v1beta2/index/machine_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"testing"
2121

2222
. "github.com/onsi/gomega"
23-
corev1 "k8s.io/api/core/v1"
2423
"k8s.io/utils/ptr"
2524
"sigs.k8s.io/controller-runtime/pkg/client"
2625

@@ -42,7 +41,7 @@ func TestIndexMachineByNodeName(t *testing.T) {
4241
name: "when the machine has valid a NodeRef",
4342
object: &clusterv1.Machine{
4443
Status: clusterv1.MachineStatus{
45-
NodeRef: &corev1.ObjectReference{
44+
NodeRef: &clusterv1.MachineNodeReference{
4645
Name: "node1",
4746
},
4847
},

api/core/v1beta2/machine_types.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ type MachineStatus struct {
516516

517517
// nodeRef will point to the corresponding Node if it exists.
518518
// +optional
519-
NodeRef *corev1.ObjectReference `json:"nodeRef,omitempty"`
519+
NodeRef *MachineNodeReference `json:"nodeRef,omitempty"`
520520

521521
// nodeInfo is a set of ids/uuids to uniquely identify the node.
522522
// More info: https://kubernetes.io/docs/concepts/nodes/node/#info
@@ -556,6 +556,17 @@ type MachineStatus struct {
556556
Deprecated *MachineDeprecatedStatus `json:"deprecated,omitempty"`
557557
}
558558

559+
// MachineNodeReference is a reference to the node running on the machine.
560+
type MachineNodeReference struct {
561+
// name of the node.
562+
// name must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character.
563+
// +required
564+
// +kubebuilder:validation:MinLength=1
565+
// +kubebuilder:validation:MaxLength=253
566+
// +kubebuilder:validation:Pattern=`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`
567+
Name string `json:"name"`
568+
}
569+
559570
// MachineInitializationStatus provides observations of the Machine initialization process.
560571
// NOTE: Fields in this struct are part of the Cluster API contract and are used to orchestrate initial Machine provisioning.
561572
type MachineInitializationStatus struct {

api/core/v1beta2/zz_generated.deepcopy.go

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/core/v1beta2/zz_generated.openapi.go

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

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,19 +1213,15 @@ func TestBootstrapTokenTTLExtension(t *testing.T) {
12131213

12141214
patchHelper, err = patch.NewHelper(workerMachine, myclient)
12151215
g.Expect(err).ShouldNot(HaveOccurred())
1216-
workerMachine.Status.NodeRef = &corev1.ObjectReference{
1217-
APIVersion: "v1",
1218-
Kind: "Node",
1219-
Name: "worker-node",
1216+
workerMachine.Status.NodeRef = &clusterv1.MachineNodeReference{
1217+
Name: "worker-node",
12201218
}
12211219
g.Expect(patchHelper.Patch(ctx, workerMachine)).To(Succeed())
12221220

12231221
patchHelper, err = patch.NewHelper(controlPlaneJoinMachine, myclient)
12241222
g.Expect(err).ShouldNot(HaveOccurred())
1225-
controlPlaneJoinMachine.Status.NodeRef = &corev1.ObjectReference{
1226-
APIVersion: "v1",
1227-
Kind: "Node",
1228-
Name: "control-plane-node",
1223+
controlPlaneJoinMachine.Status.NodeRef = &clusterv1.MachineNodeReference{
1224+
Name: "control-plane-node",
12291225
}
12301226
g.Expect(patchHelper.Patch(ctx, controlPlaneJoinMachine)).To(Succeed())
12311227

bootstrap/util/configowner_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,8 @@ func TestHasNodeRefs(t *testing.T) {
221221
Initialization: &clusterv1.MachineInitializationStatus{
222222
InfrastructureProvisioned: true,
223223
},
224-
NodeRef: &corev1.ObjectReference{
225-
Kind: "Node",
226-
Namespace: metav1.NamespaceDefault,
227-
Name: "node-0",
224+
NodeRef: &clusterv1.MachineNodeReference{
225+
Name: "node-0",
228226
},
229227
},
230228
}

cmd/clusterctl/client/cluster/mover_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,7 @@ func Test_objectMover_checkProvisioningCompleted(t *testing.T) {
16261626
},
16271627
},
16281628
Status: clusterv1.MachineStatus{
1629-
NodeRef: &corev1.ObjectReference{},
1629+
NodeRef: &clusterv1.MachineNodeReference{},
16301630
},
16311631
},
16321632
},

config/crd/bases/cluster.x-k8s.io_machines.yaml

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

controlplane/kubeadm/internal/control_plane_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,23 +149,23 @@ func TestHasMachinesToBeRemediated(t *testing.T) {
149149
// healthy machine (without MachineHealthCheckSucceded condition)
150150
healthyMachineNotProvisioned := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Name: "healthyMachine1"}}
151151
// healthy machine (with MachineHealthCheckSucceded == true)
152-
healthyMachineProvisioned := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Name: "healthyMachine2"}, Status: clusterv1.MachineStatus{NodeRef: &corev1.ObjectReference{Kind: "Node", Name: "node1"}}}
152+
healthyMachineProvisioned := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Name: "healthyMachine2"}, Status: clusterv1.MachineStatus{NodeRef: &clusterv1.MachineNodeReference{Name: "node1"}}}
153153
healthyMachineProvisioned.SetConditions([]metav1.Condition{
154154
{
155155
Type: clusterv1.MachineHealthCheckSucceededCondition,
156156
Status: metav1.ConditionTrue,
157157
},
158158
})
159159
// unhealthy machine NOT eligible for KCP remediation (with MachineHealthCheckSucceded == False, but without MachineOwnerRemediated condition)
160-
unhealthyMachineNOTOwnerRemediated := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Name: "unhealthyMachineNOTOwnerRemediated"}, Status: clusterv1.MachineStatus{NodeRef: &corev1.ObjectReference{Kind: "Node", Name: "node2"}}}
160+
unhealthyMachineNOTOwnerRemediated := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Name: "unhealthyMachineNOTOwnerRemediated"}, Status: clusterv1.MachineStatus{NodeRef: &clusterv1.MachineNodeReference{Name: "node2"}}}
161161
unhealthyMachineNOTOwnerRemediated.SetConditions([]metav1.Condition{
162162
{
163163
Type: clusterv1.MachineHealthCheckSucceededCondition,
164164
Status: metav1.ConditionFalse,
165165
},
166166
})
167167
// unhealthy machine eligible for KCP remediation (with MachineHealthCheckSucceded == False, with MachineOwnerRemediated condition)
168-
unhealthyMachineOwnerRemediated := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Name: "unhealthyMachineOwnerRemediated"}, Status: clusterv1.MachineStatus{NodeRef: &corev1.ObjectReference{Kind: "Node", Name: "node3"}}}
168+
unhealthyMachineOwnerRemediated := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Name: "unhealthyMachineOwnerRemediated"}, Status: clusterv1.MachineStatus{NodeRef: &clusterv1.MachineNodeReference{Name: "node3"}}}
169169
unhealthyMachineOwnerRemediated.SetConditions([]metav1.Condition{
170170
{
171171
Type: clusterv1.MachineHealthCheckSucceededCondition,
@@ -232,7 +232,7 @@ func TestHasHealthyMachineStillProvisioning(t *testing.T) {
232232

233233
// healthy machine (without MachineHealthCheckSucceded condition) provisioned (with NodeRef)
234234
healthyMachineProvisioned1 := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Name: "healthyMachineProvisioned1"}}
235-
healthyMachineProvisioned1.Status.NodeRef = &corev1.ObjectReference{}
235+
healthyMachineProvisioned1.Status.NodeRef = &clusterv1.MachineNodeReference{}
236236

237237
// unhealthy machine (with MachineHealthCheckSucceded condition) still provisioning (without NodeRef)
238238
unhealthyMachineStillProvisioning1 := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Name: "unhealthyMachineStillProvisioning1"}}
@@ -249,7 +249,7 @@ func TestHasHealthyMachineStillProvisioning(t *testing.T) {
249249

250250
// unhealthy machine (with MachineHealthCheckSucceded condition) provisioned (with NodeRef)
251251
unhealthyMachineProvisioned1 := &clusterv1.Machine{ObjectMeta: metav1.ObjectMeta{Name: "unhealthyMachineProvisioned1"}}
252-
unhealthyMachineProvisioned1.Status.NodeRef = &corev1.ObjectReference{}
252+
unhealthyMachineProvisioned1.Status.NodeRef = &clusterv1.MachineNodeReference{}
253253
unhealthyMachineProvisioned1.SetConditions([]metav1.Condition{
254254
{
255255
Type: clusterv1.MachineHealthCheckSucceededCondition,
@@ -292,7 +292,7 @@ func TestStatusToLogKeyAndValues(t *testing.T) {
292292
healthyMachine := &clusterv1.Machine{
293293
ObjectMeta: metav1.ObjectMeta{Name: "healthy"},
294294
Status: clusterv1.MachineStatus{
295-
NodeRef: &corev1.ObjectReference{Name: "healthy-node"},
295+
NodeRef: &clusterv1.MachineNodeReference{Name: "healthy-node"},
296296
Conditions: []metav1.Condition{
297297
{Type: controlplanev1.KubeadmControlPlaneMachineAPIServerPodHealthyCondition, Status: metav1.ConditionTrue},
298298
{Type: controlplanev1.KubeadmControlPlaneMachineControllerManagerPodHealthyCondition, Status: metav1.ConditionTrue},

0 commit comments

Comments
 (0)