Skip to content

Commit

Permalink
Remove InstanceID from machine spec
Browse files Browse the repository at this point in the history
This is a status field and should not be included in the spec.
ProviderID is the same, but cannot be removed as it is required by
Cluster API.
  • Loading branch information
mdbooth committed Mar 20, 2024
1 parent c4e5c2a commit d29b359
Show file tree
Hide file tree
Showing 22 changed files with 341 additions and 206 deletions.
4 changes: 4 additions & 0 deletions api/v1alpha5/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,10 @@ func Convert_v1beta1_OpenStackMachineSpec_To_v1alpha5_OpenStackMachineSpec(in *i
return nil
}

func Convert_v1alpha5_OpenStackMachineStatus_To_v1beta1_OpenStackMachineStatus(in *OpenStackMachineStatus, out *infrav1.OpenStackMachineStatus, s conversion.Scope) error {
return autoConvert_v1alpha5_OpenStackMachineStatus_To_v1beta1_OpenStackMachineStatus(in, out, s)
}

func Convert_v1beta1_OpenStackMachineStatus_To_v1alpha5_OpenStackMachineStatus(in *infrav1.OpenStackMachineStatus, out *OpenStackMachineStatus, s conversion.Scope) error {
// ReferencedResources have no equivalent in v1alpha5
return autoConvert_v1beta1_OpenStackMachineStatus_To_v1alpha5_OpenStackMachineStatus(in, out, s)
Expand Down
21 changes: 7 additions & 14 deletions api/v1alpha5/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion api/v1alpha6/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,10 @@ func TestMachineConversionControllerSpecFields(t *testing.T) {
{
name: "Set InstanceID",
modifyUp: func(up *infrav1.OpenStackMachine) {
up.Spec.InstanceID = pointer.String("new-instance-id")
up.Status.DependentResources.Server = &infrav1.ServerStatus{
ID: "new-instance-id",
State: "ACTIVE",
}
},
testAfter: func(after *OpenStackMachine) {
g.Expect(after.Spec.InstanceID).To(gomega.Equal(pointer.String("new-instance-id")))
Expand Down
9 changes: 5 additions & 4 deletions api/v1alpha6/openstackcluster_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func restorev1alpha6ClusterSpec(previous *OpenStackClusterSpec, dst *OpenStackCl
dstBastion := dst.Bastion
if prevBastion != nil && dstBastion != nil {
restorev1alpha6MachineSpec(&prevBastion.Instance, &dstBastion.Instance)
dstBastion.Instance.InstanceID = prevBastion.Instance.InstanceID
}

// To avoid lossy conversion, we need to restore AllowAllInClusterTraffic
Expand Down Expand Up @@ -369,9 +370,8 @@ func restorev1beta1ClusterStatus(previous *infrav1.OpenStackClusterStatus, dst *

if previous.Bastion != nil {
dst.Bastion.ReferencedResources = previous.Bastion.ReferencedResources
}
if previous.Bastion != nil && previous.Bastion.DependentResources.Ports != nil {
dst.Bastion.DependentResources.Ports = previous.Bastion.DependentResources.Ports
dst.Bastion.DependentResources.Server = previous.Bastion.DependentResources.Server
}
}

Expand Down Expand Up @@ -412,9 +412,10 @@ func Convert_v1alpha6_OpenStackClusterStatus_To_v1beta1_OpenStackClusterStatus(i
/* Bastion */

func restorev1beta1Bastion(previous **infrav1.Bastion, dst **infrav1.Bastion) {
if *previous != nil && *dst != nil {
restorev1beta1MachineSpec(&(*previous).Instance, &(*dst).Instance)
if *previous == nil || *dst == nil {
return
}
restorev1beta1MachineSpec(&(*previous).Instance, &(*dst).Instance)
}

func Convert_v1alpha6_Instance_To_v1beta1_BastionStatus(in *Instance, out *infrav1.BastionStatus, _ apiconversion.Scope) error {
Expand Down
50 changes: 49 additions & 1 deletion api/v1alpha6/openstackmachine_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ var v1alpha6OpenStackMachineRestorer = conversion.RestorerFor[*OpenStackMachine]
return s
}),
),
"status": conversion.HashedFieldRestorer(
func(c *OpenStackMachine) *OpenStackMachineStatus {
return &c.Status
},
restorev1alpha6MachineStatus,
),
}

var v1beta1OpenStackMachineRestorer = conversion.RestorerFor[*infrav1.OpenStackMachine]{
Expand All @@ -102,6 +108,39 @@ var v1beta1OpenStackMachineRestorer = conversion.RestorerFor[*infrav1.OpenStackM
),
}

/* OpenStackMachine */

func Convert_v1alpha6_OpenStackMachine_To_v1beta1_OpenStackMachine(in *OpenStackMachine, out *infrav1.OpenStackMachine, s apiconversion.Scope) error {
if err := autoConvert_v1alpha6_OpenStackMachine_To_v1beta1_OpenStackMachine(in, out, s); err != nil {
return err
}

if in.Spec.InstanceID != nil {
serverStatus := &infrav1.ServerStatus{
ID: *in.Spec.InstanceID,
}
if in.Status.InstanceState != nil {
serverStatus.State = (infrav1.InstanceState)(*in.Status.InstanceState)
}
out.Status.DependentResources.Server = serverStatus
}
return nil
}

func Convert_v1beta1_OpenStackMachine_To_v1alpha6_OpenStackMachine(in *infrav1.OpenStackMachine, out *OpenStackMachine, s apiconversion.Scope) error {
if err := autoConvert_v1beta1_OpenStackMachine_To_v1alpha6_OpenStackMachine(in, out, s); err != nil {
return err
}

if in.Status.DependentResources.Server != nil {
out.Spec.InstanceID = &in.Status.DependentResources.Server.ID
state := InstanceState(in.Status.DependentResources.Server.State)
out.Status.InstanceState = &state
}

return nil
}

/* OpenStackMachineSpec */

func restorev1alpha6MachineSpec(previous *OpenStackMachineSpec, dst *OpenStackMachineSpec) {
Expand Down Expand Up @@ -339,7 +378,16 @@ func Convert_v1beta1_OpenStackMachineSpec_To_v1alpha6_OpenStackMachineSpec(in *i

/* OpenStackMachineStatus */

func restorev1alpha6MachineStatus(previous *OpenStackMachineStatus, dst *OpenStackMachineStatus) {
if dst.InstanceState == nil || *dst.InstanceState == "" {
dst.InstanceState = previous.InstanceState
}
}

func Convert_v1alpha6_OpenStackMachineStatus_To_v1beta1_OpenStackMachineStatus(in *OpenStackMachineStatus, out *infrav1.OpenStackMachineStatus, s apiconversion.Scope) error {
return autoConvert_v1alpha6_OpenStackMachineStatus_To_v1beta1_OpenStackMachineStatus(in, out, s)
}

func Convert_v1beta1_OpenStackMachineStatus_To_v1alpha6_OpenStackMachineStatus(in *infrav1.OpenStackMachineStatus, out *OpenStackMachineStatus, s apiconversion.Scope) error {
// ReferencedResources have no equivalent in v1alpha6
return autoConvert_v1beta1_OpenStackMachineStatus_To_v1alpha6_OpenStackMachineStatus(in, out, s)
}
8 changes: 7 additions & 1 deletion api/v1alpha6/openstackmachinetemplate_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var v1alpha6OpenStackMachineTemplateRestorer = conversion.RestorerFor[*OpenStack
func(c *OpenStackMachineTemplate) *OpenStackMachineSpec {
return &c.Spec.Template.Spec
},
restorev1alpha6MachineSpec,
restorev1alpha6MachineSpecFromTemplate,
),
}

Expand All @@ -76,3 +76,9 @@ var v1beta1OpenStackMachineTemplateRestorer = conversion.RestorerFor[*infrav1.Op
restorev1beta1MachineSpec,
),
}

func restorev1alpha6MachineSpecFromTemplate(previous *OpenStackMachineSpec, dst *OpenStackMachineSpec) {
restorev1alpha6MachineSpec(previous, dst)

dst.InstanceID = previous.InstanceID
}
51 changes: 17 additions & 34 deletions api/v1alpha6/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion api/v1alpha7/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,10 @@ func TestMachineConversionControllerSpecFields(t *testing.T) {
{
name: "Set InstanceID",
modifyUp: func(up *infrav1.OpenStackMachine) {
up.Spec.InstanceID = pointer.String("new-instance-id")
up.Status.DependentResources.Server = &infrav1.ServerStatus{
ID: "new-instance-id",
State: "ACTIVE",
}
},
testAfter: func(after *OpenStackMachine) {
g.Expect(after.Spec.InstanceID).To(gomega.Equal(pointer.String("new-instance-id")))
Expand Down
Loading

0 comments on commit d29b359

Please sign in to comment.