Skip to content

Commit

Permalink
Remove unused BastionStatus fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mdbooth committed Mar 22, 2024
1 parent 4d71381 commit 084caa7
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 106 deletions.
10 changes: 2 additions & 8 deletions api/v1alpha5/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,23 +448,17 @@ func Convert_v1alpha5_PortOpts_To_v1beta1_PortOpts(in *PortOpts, out *infrav1.Po
}

func Convert_v1alpha5_Instance_To_v1beta1_BastionStatus(in *Instance, out *infrav1.BastionStatus, _ conversion.Scope) error {
// BastionStatus is the same as Spec with unused fields removed
out.ID = in.ID
// BastionStatus is the same as Instance with unused fields removed
out.Name = in.Name
out.SSHKeyName = in.SSHKeyName
out.State = infrav1.InstanceState(in.State)
out.IP = in.IP
out.FloatingIP = in.FloatingIP
out.Resolved.ServerGroupID = in.ServerGroupID
return nil
}

func Convert_v1beta1_BastionStatus_To_v1alpha5_Instance(in *infrav1.BastionStatus, out *Instance, _ conversion.Scope) error {
// BastionStatus is the same as Spec with unused fields removed
out.ID = in.ID
// BastionStatus is the same as Instance with unused fields removed
out.Name = in.Name
out.SSHKeyName = in.SSHKeyName
out.State = InstanceState(in.State)
out.IP = in.IP
out.FloatingIP = in.FloatingIP
out.ServerGroupID = in.Resolved.ServerGroupID
Expand Down
24 changes: 18 additions & 6 deletions api/v1alpha6/openstackcluster_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ func Convert_v1beta1_OpenStackClusterSpec_To_v1alpha6_OpenStackClusterSpec(in *i
/* OpenStackClusterStatus */

func restorev1alpha6ClusterStatus(previous *OpenStackClusterStatus, dst *OpenStackClusterStatus) {
if previous == nil || dst == nil {
return
}

// PortOpts.SecurityGroups have been removed in v1beta1
// We restore the whole PortOpts/Networks since they are anyway immutable.
if previous.ExternalNetwork != nil {
Expand All @@ -354,6 +358,8 @@ func restorev1alpha6ClusterStatus(previous *OpenStackClusterStatus, dst *OpenSta
restorev1alpha6SecurityGroup(previous.ControlPlaneSecurityGroup, dst.ControlPlaneSecurityGroup)
restorev1alpha6SecurityGroup(previous.WorkerSecurityGroup, dst.WorkerSecurityGroup)
restorev1alpha6SecurityGroup(previous.BastionSecurityGroup, dst.BastionSecurityGroup)

restorev1alpha6BastionStatus(previous.Bastion, dst.Bastion)
}

func restorev1beta1ClusterStatus(previous *infrav1.OpenStackClusterStatus, dst *infrav1.OpenStackClusterStatus) {
Expand Down Expand Up @@ -422,21 +428,27 @@ func restorev1beta1Bastion(previous **infrav1.Bastion, dst **infrav1.Bastion) {

func Convert_v1alpha6_Instance_To_v1beta1_BastionStatus(in *Instance, out *infrav1.BastionStatus, _ apiconversion.Scope) error {
// BastionStatus is the same as Spec with unused fields removed
out.ID = in.ID
out.Name = in.Name
out.SSHKeyName = in.SSHKeyName
out.State = infrav1.InstanceState(in.State)
out.IP = in.IP
out.FloatingIP = in.FloatingIP
return nil
}

/* Bastion status */

func restorev1alpha6BastionStatus(previous *Instance, dst *Instance) {
if previous == nil || dst == nil {
return
}

dst.ID = previous.ID
dst.SSHKeyName = previous.SSHKeyName
dst.State = previous.State
}

func Convert_v1beta1_BastionStatus_To_v1alpha6_Instance(in *infrav1.BastionStatus, out *Instance, _ apiconversion.Scope) error {
// BastionStatus is the same as Spec with unused fields removed
out.ID = in.ID
out.Name = in.Name
out.SSHKeyName = in.SSHKeyName
out.State = InstanceState(in.State)
out.IP = in.IP
out.FloatingIP = in.FloatingIP
return nil
Expand Down
22 changes: 22 additions & 0 deletions api/v1alpha7/openstackcluster_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,15 @@ func Convert_v1beta1_OpenStackClusterSpec_To_v1alpha7_OpenStackClusterSpec(in *i
/* OpenStackClusterStatus */

func restorev1alpha7ClusterStatus(previous *OpenStackClusterStatus, dst *OpenStackClusterStatus) {
if previous == nil || dst == nil {
return
}

restorev1alpha7SecurityGroup(previous.ControlPlaneSecurityGroup, dst.ControlPlaneSecurityGroup)
restorev1alpha7SecurityGroup(previous.WorkerSecurityGroup, dst.WorkerSecurityGroup)
restorev1alpha7SecurityGroup(previous.BastionSecurityGroup, dst.BastionSecurityGroup)

restorev1alpha7BastionStatus(previous.Bastion, dst.Bastion)
}

func restorev1beta1ClusterStatus(previous *infrav1.OpenStackClusterStatus, dst *infrav1.OpenStackClusterStatus) {
Expand Down Expand Up @@ -432,6 +438,22 @@ func Convert_v1beta1_Bastion_To_v1alpha7_Bastion(in *infrav1.Bastion, out *Basti
return optional.Convert_optional_String_To_string(&in.FloatingIP, &out.Instance.FloatingIP, s)
}

/* Bastion status */

func restorev1alpha7BastionStatus(previous *BastionStatus, dst *BastionStatus) {
if previous == nil || dst == nil {
return
}

dst.ID = previous.ID
dst.SSHKeyName = previous.SSHKeyName
dst.State = previous.State
}

func Convert_v1alpha7_BastionStatus_To_v1beta1_BastionStatus(in *BastionStatus, out *infrav1.BastionStatus, s apiconversion.Scope) error {
return autoConvert_v1alpha7_BastionStatus_To_v1beta1_BastionStatus(in, out, s)
}

func Convert_v1beta1_BastionStatus_To_v1alpha7_BastionStatus(in *infrav1.BastionStatus, out *BastionStatus, s apiconversion.Scope) error {
// ReferencedResources have no equivalent in v1alpha7
return autoConvert_v1beta1_BastionStatus_To_v1alpha7_BastionStatus(in, out, s)
Expand Down
24 changes: 8 additions & 16 deletions api/v1alpha7/zz_generated.conversion.go

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

10 changes: 7 additions & 3 deletions api/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,7 @@ type AddressPair struct {
}

type BastionStatus struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
SSHKeyName string `json:"sshKeyName,omitempty"`
State InstanceState `json:"state,omitempty"`
IP string `json:"ip,omitempty"`
FloatingIP string `json:"floatingIP,omitempty"`
Resolved ResolvedMachineSpec `json:"resolved,omitempty"`
Expand Down Expand Up @@ -683,6 +680,13 @@ type MachineResources struct {
Server *ServerStatus `json:"server,omitempty"`
}

func (r *MachineResources) GetServerID() *string {
if r == nil || r.Server == nil {
return nil
}
return &r.Server.ID
}

type ServerStatus struct {
// ID is the uuid of the server
// +kubebuilder:validation:Required
Expand Down

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

33 changes: 23 additions & 10 deletions controllers/openstackcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,22 @@ func deleteBastion(scope *scope.WithLogger, cluster *clusterv1.Cluster, openStac
return err
}

if openStackCluster.Status.Bastion != nil && openStackCluster.Status.Bastion.FloatingIP != "" {
var instanceStatus *compute.InstanceStatus
bastionStatus := openStackCluster.Status.Bastion
if bastionStatus != nil && bastionStatus.FloatingIP != "" {
if err = networkingService.DeleteFloatingIP(openStackCluster, openStackCluster.Status.Bastion.FloatingIP); err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to delete floating IP: %w", err))
return fmt.Errorf("failed to delete floating IP: %w", err)
}
}

var instanceStatus *compute.InstanceStatus
if openStackCluster.Status.Bastion != nil && openStackCluster.Status.Bastion.ID != "" {
instanceStatus, err = computeService.GetInstanceStatus(openStackCluster.Status.Bastion.ID)
var serverID *string
if bastionStatus != nil {
serverID = bastionStatus.Resources.GetServerID()
}

if serverID != nil {
instanceStatus, err = computeService.GetInstanceStatus(*serverID)
if err != nil {
return err
}
Expand Down Expand Up @@ -308,18 +314,18 @@ func deleteBastion(scope *scope.WithLogger, cluster *clusterv1.Cluster, openStac
}
}

if openStackCluster.Status.Bastion != nil && len(openStackCluster.Status.Bastion.Resources.Ports) > 0 {
if bastionStatus != nil && len(bastionStatus.Resources.Ports) > 0 {
trunkSupported, err := networkingService.IsTrunkExtSupported()
if err != nil {
return err
}
for _, port := range openStackCluster.Status.Bastion.Resources.Ports {
for _, port := range bastionStatus.Resources.Ports {
if err := networkingService.DeleteInstanceTrunkAndPort(openStackCluster, port, trunkSupported); err != nil {
handleUpdateOSCError(openStackCluster, fmt.Errorf("failed to delete port: %w", err))
return fmt.Errorf("failed to delete port: %w", err)
}
}
openStackCluster.Status.Bastion.Resources.Ports = nil
bastionStatus.Resources.Ports = nil
}

scope.Logger().Info("Deleted Bastion")
Expand Down Expand Up @@ -448,9 +454,12 @@ func reconcileBastion(scope *scope.WithLogger, cluster *clusterv1.Cluster, openS
bastionPortIDs := GetPortIDs(openStackCluster.Status.Bastion.Resources.Ports)

var instanceStatus *compute.InstanceStatus
if openStackCluster.Status.Bastion != nil && openStackCluster.Status.Bastion.ID != "" {
if instanceStatus, err = computeService.GetInstanceStatus(openStackCluster.Status.Bastion.ID); err != nil {
return nil, err
bastionStatus := openStackCluster.Status.Bastion
if bastionStatus != nil {
if serverID := bastionStatus.Resources.GetServerID(); serverID != nil {
if instanceStatus, err = computeService.GetInstanceStatus(*serverID); err != nil {
return nil, err
}
}
}
if instanceStatus == nil {
Expand All @@ -467,6 +476,10 @@ func reconcileBastion(scope *scope.WithLogger, cluster *clusterv1.Cluster, openS
}

// Save hash & status as soon as we know we have an instance
openStackCluster.Status.Bastion.Resources.Server = &infrav1.ServerStatus{
ID: instanceStatus.ID(),
State: instanceStatus.State(),
}
instanceStatus.UpdateBastionStatus(openStackCluster)

// Make sure that bastion instance has a valid state
Expand Down
Loading

0 comments on commit 084caa7

Please sign in to comment.