Skip to content

Commit 0ab4e3c

Browse files
committed
worked on PR comments: change in logic validation
Signed-off-by: Bharath Nallapeta <[email protected]>
1 parent f7dfd16 commit 0ab4e3c

File tree

4 files changed

+148
-239
lines changed

4 files changed

+148
-239
lines changed

controllers/openstackcluster_controller.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,10 @@ func (r *OpenStackClusterReconciler) reconcileBastionServer(ctx context.Context,
497497
}
498498

499499
// If the bastion is found but the spec has changed, we need to delete it and reconcile.
500-
bastionServerSpec := bastionToOpenStackServerSpec(openStackCluster)
500+
bastionServerSpec, err := bastionToOpenStackServerSpec(openStackCluster)
501+
if err != nil {
502+
return nil, true, err
503+
}
501504
if !bastionNotFound && server != nil && !apiequality.Semantic.DeepEqual(bastionServerSpec, &server.Spec) {
502505
scope.Logger().Info("Bastion spec has changed, re-creating the OpenStackServer object")
503506
if err := r.deleteBastion(ctx, scope, cluster, openStackCluster); err != nil {
@@ -543,7 +546,10 @@ func (r *OpenStackClusterReconciler) getBastionServer(ctx context.Context, openS
543546
// createBastionServer creates the OpenStackServer object for the bastion server.
544547
// It returns the OpenStackServer object and an error if any.
545548
func (r *OpenStackClusterReconciler) createBastionServer(ctx context.Context, openStackCluster *infrav1.OpenStackCluster, cluster *clusterv1.Cluster) (*infrav1alpha1.OpenStackServer, error) {
546-
bastionServerSpec := bastionToOpenStackServerSpec(openStackCluster)
549+
bastionServerSpec, err := bastionToOpenStackServerSpec(openStackCluster)
550+
if err != nil {
551+
return nil, err
552+
}
547553
bastionServer := &infrav1alpha1.OpenStackServer{
548554
ObjectMeta: metav1.ObjectMeta{
549555
Labels: map[string]string{
@@ -571,7 +577,7 @@ func (r *OpenStackClusterReconciler) createBastionServer(ctx context.Context, op
571577

572578
// bastionToOpenStackServerSpec converts the OpenStackMachineSpec for the bastion to an OpenStackServerSpec.
573579
// It returns the OpenStackServerSpec and an error if any.
574-
func bastionToOpenStackServerSpec(openStackCluster *infrav1.OpenStackCluster) *infrav1alpha1.OpenStackServerSpec {
580+
func bastionToOpenStackServerSpec(openStackCluster *infrav1.OpenStackCluster) (*infrav1alpha1.OpenStackServerSpec, error) {
575581
bastion := openStackCluster.Spec.Bastion
576582
if bastion == nil {
577583
bastion = &infrav1.Bastion{}
@@ -586,9 +592,12 @@ func bastionToOpenStackServerSpec(openStackCluster *infrav1.OpenStackCluster) *i
586592
if bastion.AvailabilityZone != nil {
587593
az = *bastion.AvailabilityZone
588594
}
589-
openStackServerSpec := openStackMachineSpecToOpenStackServerSpec(bastion.Spec, openStackCluster.Spec.IdentityRef, compute.InstanceTags(bastion.Spec, openStackCluster), az, nil, getBastionSecurityGroupID(openStackCluster), openStackCluster.Status.Network.ID)
595+
openStackServerSpec, err := openStackMachineSpecToOpenStackServerSpec(bastion.Spec, openStackCluster.Spec.IdentityRef, compute.InstanceTags(bastion.Spec, openStackCluster), az, nil, getBastionSecurityGroupID(openStackCluster), openStackCluster.Status.Network)
596+
if err != nil {
597+
return nil, err
598+
}
590599

591-
return openStackServerSpec
600+
return openStackServerSpec, nil
592601
}
593602

594603
func bastionName(clusterResourceName string) string {

controllers/openstackmachine_controller.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,18 @@ func (r *OpenStackMachineReconciler) getMachineServer(ctx context.Context, openS
479479

480480
// openStackMachineSpecToOpenStackServerSpec converts an OpenStackMachineSpec to an OpenStackServerSpec.
481481
// It returns the OpenStackServerSpec object and an error if there is any.
482-
func openStackMachineSpecToOpenStackServerSpec(openStackMachineSpec *infrav1.OpenStackMachineSpec, identityRef infrav1.OpenStackIdentityReference, tags []string, failureDomain string, userDataRef *corev1.LocalObjectReference, defaultSecGroup *string, defaultNetworkID string) *infrav1alpha1.OpenStackServerSpec {
482+
func openStackMachineSpecToOpenStackServerSpec(openStackMachineSpec *infrav1.OpenStackMachineSpec, identityRef infrav1.OpenStackIdentityReference, tags []string, failureDomain string, userDataRef *corev1.LocalObjectReference, defaultSecGroup *string, clusterNetwork *infrav1.NetworkStatusWithSubnets) (*infrav1alpha1.OpenStackServerSpec, error) {
483+
// Determine default network ID if the cluster status exposes one.
484+
var defaultNetworkID string
485+
if clusterNetwork != nil {
486+
defaultNetworkID = clusterNetwork.ID
487+
}
488+
489+
// If no cluster network is available AND the machine spec did not define any ports with a network, we cannot choose a network.
490+
if defaultNetworkID == "" && len(openStackMachineSpec.Ports) == 0 {
491+
return nil, capoerrors.Terminal(infrav1.InvalidMachineSpecReason, "no network configured: cluster network is missing and machine spec does not define ports with a network")
492+
}
493+
483494
openStackServerSpec := &infrav1alpha1.OpenStackServerSpec{
484495
AdditionalBlockDevices: openStackMachineSpec.AdditionalBlockDevices,
485496
ConfigDrive: openStackMachineSpec.ConfigDrive,
@@ -535,7 +546,7 @@ func openStackMachineSpecToOpenStackServerSpec(openStackMachineSpec *infrav1.Ope
535546
}
536547
openStackServerSpec.Ports = serverPorts
537548

538-
return openStackServerSpec
549+
return openStackServerSpec, nil
539550
}
540551

541552
// reconcileMachineServer reconciles the OpenStackServer object for the OpenStackMachine.
@@ -584,18 +595,10 @@ func (r *OpenStackMachineReconciler) getOrCreateMachineServer(ctx context.Contex
584595
}
585596
return openStackCluster.Spec.IdentityRef
586597
}()
587-
// Determine default network ID if the cluster status exposes one.
588-
var defaultNetworkID string
589-
if openStackCluster.Status.Network != nil {
590-
defaultNetworkID = openStackCluster.Status.Network.ID
591-
}
592-
593-
// If no cluster network is available AND the machine spec did not define any ports with a network, we cannot choose a network.
594-
if defaultNetworkID == "" && len(openStackMachine.Spec.Ports) == 0 {
595-
return nil, capoerrors.Terminal(infrav1.InvalidMachineSpecReason, "no network configured: cluster network is missing and machine spec does not define ports with a network")
598+
machineServerSpec, err := openStackMachineSpecToOpenStackServerSpec(&openStackMachine.Spec, identityRef, compute.InstanceTags(&openStackMachine.Spec, openStackCluster), failureDomain, userDataRef, getManagedSecurityGroup(openStackCluster, machine), openStackCluster.Status.Network)
599+
if err != nil {
600+
return nil, err
596601
}
597-
598-
machineServerSpec := openStackMachineSpecToOpenStackServerSpec(&openStackMachine.Spec, identityRef, compute.InstanceTags(&openStackMachine.Spec, openStackCluster), failureDomain, userDataRef, getManagedSecurityGroup(openStackCluster, machine), defaultNetworkID)
599602
machineServer = &infrav1alpha1.OpenStackServer{
600603
ObjectMeta: metav1.ObjectMeta{
601604
Labels: map[string]string{

0 commit comments

Comments
 (0)