Skip to content

Commit 750af59

Browse files
committed
Fix premature attempt to resolve machine resources
We can't resolve machine resources until the cluster is initialised.
1 parent ad00c64 commit 750af59

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

controllers/openstackmachine_controller.go

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -150,29 +150,32 @@ func (r *OpenStackMachineReconciler) Reconcile(ctx context.Context, req ctrl.Req
150150
}
151151
scope := scope.NewWithLogger(clientScope, log)
152152

153+
clusterName := fmt.Sprintf("%s-%s", cluster.Namespace, cluster.Name)
154+
155+
// Handle deleted machines
156+
if !openStackMachine.DeletionTimestamp.IsZero() {
157+
return r.reconcileDelete(scope, clusterName, infraCluster, machine, openStackMachine)
158+
}
159+
160+
// Handle non-deleted clusters
161+
return r.reconcileNormal(ctx, scope, clusterName, infraCluster, machine, openStackMachine)
162+
}
163+
164+
func resolveMachineResources(scope *scope.WithLogger, openStackCluster *infrav1.OpenStackCluster, openStackMachine *infrav1.OpenStackMachine) (bool, error) {
153165
// Resolve and store referenced resources
154-
changed, err := compute.ResolveReferencedMachineResources(scope, infraCluster, &openStackMachine.Spec, &openStackMachine.Status.ReferencedResources)
166+
changed, err := compute.ResolveReferencedMachineResources(scope,
167+
openStackCluster,
168+
&openStackMachine.Spec, &openStackMachine.Status.ReferencedResources)
155169
if err != nil {
156-
return reconcile.Result{}, err
170+
return false, err
157171
}
158172
if changed {
159173
// If the referenced resources have changed, we need to update the OpenStackMachine status now.
160-
return reconcile.Result{}, nil
174+
return true, nil
161175
}
162176

163177
// Adopt any existing dependent resources
164-
err = compute.AdoptDependentMachineResources(scope, openStackMachine.Name, &openStackMachine.Status.ReferencedResources, &openStackMachine.Status.DependentResources)
165-
if err != nil {
166-
return reconcile.Result{}, err
167-
}
168-
169-
// Handle deleted machines
170-
if !openStackMachine.DeletionTimestamp.IsZero() {
171-
return r.reconcileDelete(scope, cluster, infraCluster, machine, openStackMachine)
172-
}
173-
174-
// Handle non-deleted clusters
175-
return r.reconcileNormal(ctx, scope, cluster, infraCluster, machine, openStackMachine)
178+
return false, compute.AdoptDependentMachineResources(scope, openStackMachine.Name, &openStackMachine.Status.ReferencedResources, &openStackMachine.Status.DependentResources)
176179
}
177180

178181
func patchMachine(ctx context.Context, patchHelper *patch.Helper, openStackMachine *infrav1.OpenStackMachine, machine *clusterv1.Machine, options ...patch.Option) error {
@@ -227,11 +230,9 @@ func (r *OpenStackMachineReconciler) SetupWithManager(ctx context.Context, mgr c
227230
Complete(r)
228231
}
229232

230-
func (r *OpenStackMachineReconciler) reconcileDelete(scope *scope.WithLogger, cluster *clusterv1.Cluster, openStackCluster *infrav1.OpenStackCluster, machine *clusterv1.Machine, openStackMachine *infrav1.OpenStackMachine) (ctrl.Result, error) { //nolint:unparam
233+
func (r *OpenStackMachineReconciler) reconcileDelete(scope *scope.WithLogger, clusterName string, openStackCluster *infrav1.OpenStackCluster, machine *clusterv1.Machine, openStackMachine *infrav1.OpenStackMachine) (ctrl.Result, error) { //nolint:unparam
231234
scope.Logger().Info("Reconciling Machine delete")
232235

233-
clusterName := fmt.Sprintf("%s-%s", cluster.ObjectMeta.Namespace, cluster.Name)
234-
235236
computeService, err := compute.NewService(scope)
236237
if err != nil {
237238
return ctrl.Result{}, err
@@ -242,6 +243,13 @@ func (r *OpenStackMachineReconciler) reconcileDelete(scope *scope.WithLogger, cl
242243
return ctrl.Result{}, err
243244
}
244245

246+
// We may have resources to adopt if the cluster is ready
247+
if openStackCluster.Status.Ready && openStackCluster.Status.Network != nil {
248+
if _, err := resolveMachineResources(scope, openStackCluster, openStackMachine); err != nil {
249+
return ctrl.Result{}, err
250+
}
251+
}
252+
245253
if openStackCluster.Spec.APIServerLoadBalancer.IsEnabled() {
246254
loadBalancerService, err := loadbalancer.NewService(scope)
247255
if err != nil {
@@ -454,7 +462,7 @@ func (r *OpenStackMachineReconciler) reconcileDeleteFloatingAddressFromPool(scop
454462
return r.Client.Update(context.Background(), claim)
455463
}
456464

457-
func (r *OpenStackMachineReconciler) reconcileNormal(ctx context.Context, scope *scope.WithLogger, cluster *clusterv1.Cluster, openStackCluster *infrav1.OpenStackCluster, machine *clusterv1.Machine, openStackMachine *infrav1.OpenStackMachine) (_ ctrl.Result, reterr error) {
465+
func (r *OpenStackMachineReconciler) reconcileNormal(ctx context.Context, scope *scope.WithLogger, clusterName string, openStackCluster *infrav1.OpenStackCluster, machine *clusterv1.Machine, openStackMachine *infrav1.OpenStackMachine) (_ ctrl.Result, reterr error) {
458466
var err error
459467

460468
// If the OpenStackMachine is in an error state, return early.
@@ -469,12 +477,16 @@ func (r *OpenStackMachineReconciler) reconcileNormal(ctx context.Context, scope
469477
return ctrl.Result{}, nil
470478
}
471479

472-
if !cluster.Status.InfrastructureReady {
480+
if !openStackCluster.Status.Ready {
473481
scope.Logger().Info("Cluster infrastructure is not ready yet, re-queuing machine")
474482
conditions.MarkFalse(openStackMachine, infrav1.InstanceReadyCondition, infrav1.WaitingForClusterInfrastructureReason, clusterv1.ConditionSeverityInfo, "")
475483
return ctrl.Result{RequeueAfter: waitForClusterInfrastructureReadyDuration}, nil
476484
}
477485

486+
if changed, err := resolveMachineResources(scope, openStackCluster, openStackMachine); changed || err != nil {
487+
return ctrl.Result{}, err
488+
}
489+
478490
// Make sure bootstrap data is available and populated.
479491
if machine.Spec.Bootstrap.DataSecretName == nil {
480492
scope.Logger().Info("Bootstrap data secret reference is not yet available")
@@ -487,8 +499,6 @@ func (r *OpenStackMachineReconciler) reconcileNormal(ctx context.Context, scope
487499
}
488500
scope.Logger().Info("Reconciling Machine")
489501

490-
clusterName := fmt.Sprintf("%s-%s", cluster.ObjectMeta.Namespace, cluster.Name)
491-
492502
computeService, err := compute.NewService(scope)
493503
if err != nil {
494504
return ctrl.Result{}, err

pkg/cloud/services/compute/referenced_resources.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package compute
1818

1919
import (
20+
"fmt"
21+
2022
infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1"
2123
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/networking"
2224
"sigs.k8s.io/cluster-api-provider-openstack/pkg/scope"
@@ -61,8 +63,15 @@ func ResolveReferencedMachineResources(scope *scope.WithLogger, openStackCluster
6163
changed = true
6264
}
6365

66+
// ConstructPorts requires the cluster network to have been set. We only
67+
// call this from places where we know it should have been set, but the
68+
// cluster status is externally-provided data so we check it anyway.
69+
if openStackCluster.Status.Network == nil {
70+
return changed, fmt.Errorf("called ResolveReferencedMachineResources with nil OpenStackCluster.Status.Network")
71+
}
72+
6473
// Network resources are required in order to get ports options.
65-
if len(resources.Ports) == 0 && openStackCluster.Status.Network != nil {
74+
if len(resources.Ports) == 0 {
6675
// For now we put this here but realistically an OpenStack administrator could enable/disable trunk
6776
// support at any time, so we should probably check this on every reconcile.
6877
trunkSupported, err := networkingService.IsTrunkExtSupported()

0 commit comments

Comments
 (0)