@@ -19,14 +19,15 @@ package controllers
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "math/rand"
22
23
"reflect"
23
24
24
-
25
25
"github.com/pkg/errors"
26
26
corev1 "k8s.io/api/core/v1"
27
27
"k8s.io/apimachinery/pkg/types"
28
28
"sigs.k8s.io/cluster-api/util"
29
29
ctrl "sigs.k8s.io/controller-runtime"
30
+ "sigs.k8s.io/controller-runtime/pkg/client"
30
31
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
31
32
"sigs.k8s.io/controller-runtime/pkg/event"
32
33
"sigs.k8s.io/controller-runtime/pkg/handler"
@@ -74,7 +75,7 @@ func NewCSMachineReconciliationRunner() *CloudStackMachineReconciliationRunner {
74
75
r .AffinityGroup = & infrav1.CloudStackAffinityGroup {}
75
76
r .FailureDomain = & infrav1.CloudStackFailureDomain {}
76
77
// Setup the base runner. Initializes pointers and links reconciliation methods.
77
- r .ReconciliationRunner = utils .NewRunner (r , r .ReconciliationSubject )
78
+ r .ReconciliationRunner = utils .NewRunner (r , r .ReconciliationSubject , "CloudStackMachine" )
78
79
return r
79
80
}
80
81
@@ -90,13 +91,18 @@ func (reconciler *CloudStackMachineReconciler) Reconcile(ctx context.Context, re
90
91
91
92
func (r * CloudStackMachineReconciliationRunner ) Reconcile () (retRes ctrl.Result , reterr error ) {
92
93
return r .RunReconciliationStages (
93
- r .AsFailureDomainUser (r .ReconciliationSubject .Spec .FailureDomain ),
94
- r .GetZonesAndRequeueIfMissing (r .Zones ),
95
94
r .GetParent (r .ReconciliationSubject , r .CAPIMachine ),
96
95
r .RequeueIfCloudStackClusterNotReady ,
97
- r .ConsiderAffinity ,
98
96
r .SetFailureDomainOnCSMachine ,
99
- r .GetObjectByName ("placeholder" , r .IsoNet , func () string { return r .IsoNetMetaName (r .FailureDomain .Spec .Zone .Network .Name ) }),
97
+ r .GetObjectByName ("placeholder" , r .FailureDomain ,
98
+ func () string { return r .ReconciliationSubject .Spec .FailureDomain .Name }),
99
+ r .CheckPresent (map [string ]client.Object {"CloudStackFailureDomain" : r .FailureDomain }),
100
+ r .AsFailureDomainUser (& r .FailureDomain .Spec ),
101
+ r .GetObjectByName ("placeholder" , r .IsoNet ,
102
+ func () string { return r .IsoNetMetaName (r .FailureDomain .Spec .Zone .Network .Name ) }),
103
+ r .RunIf (func () bool { return r .FailureDomain .Spec .Zone .Network .Type == cloud .NetworkTypeIsolated },
104
+ r .CheckPresent (map [string ]client.Object {"CloudStackIsolatedNetwork" : r .IsoNet })),
105
+ r .ConsiderAffinity ,
100
106
r .GetOrCreateVMInstance ,
101
107
r .RequeueIfInstanceNotRunning ,
102
108
r .AddToLBIfNeeded ,
@@ -128,40 +134,14 @@ func (r *CloudStackMachineReconciliationRunner) ConsiderAffinity() (ctrl.Result,
128
134
129
135
// SetFailureDomainOnCSMachine sets the failure domain the machine should launch in.
130
136
func (r * CloudStackMachineReconciliationRunner ) SetFailureDomainOnCSMachine () (retRes ctrl.Result , reterr error ) {
131
- // // Set ZoneID on csMachine.
132
- // if util.IsControlPlaneMachine(r.CAPIMachine) { // Use failure domain zone.
133
- // r.ReconciliationSubject.Status.ZoneID = *r.CAPIMachine.Spec.FailureDomain
134
- // } else { // Specified by Machine Template or Random zone.
135
- // if r.ReconciliationSubject.Spec.FailureDomain.Name != "" {
136
- // if zone, foundZone := r.CSCluster.Status.Zones[r.ReconciliationSubject.Spec.ZoneID]; foundZone { // ZoneID Specified.
137
- // r.ReconciliationSubject.Status.ZoneID = zone.ID
138
- // } else {
139
- // return ctrl.Result{}, errors.Errorf("could not find zone by zoneID: %s", r.ReconciliationSubject.Spec.ZoneID)
140
- // }
141
- // } else if r.ReconciliationSubject.Spec.ZoneName != "" {
142
- // for _, zone := range r.Zones.Items {
143
- // if zone.Spec.Name == r.ReconciliationSubject.Spec.ZoneName {
144
- // r.ReconciliationSubject.Status.ZoneID = zone.Spec.ID
145
- // break
146
- // }
147
- // }
148
- // if r.ReconciliationSubject.Status.ZoneID == "" {
149
- // return ctrl.Result{}, errors.Errorf("could not find zone by zoneName: %s", r.ReconciliationSubject.Spec.ZoneName)
150
- // }
151
- // } else { // No Zone Specified, pick a Random Zone.
152
- // if len(r.Zones.Items) < 1 { // Double check that zones are present.
153
- // return r.RequeueWithMessage("no zones found, requeueing")
154
- // }
155
- // randNum := (rand.Int() % len(r.Zones.Items)) // #nosec G404 -- weak crypt rand doesn't matter here.
156
- // r.ReconciliationSubject.Status.ZoneID = r.Zones.Items[randNum].Spec.ID
157
- // }
158
- // }
159
- // for idx, zone := range r.Zones.Items {
160
- // if zone.Spec.ID == r.ReconciliationSubject.Status.ZoneID {
161
- // r.FailureDomain = &r.Zones.Items[idx]
162
- // break
163
- // }
164
- // }
137
+ if r .ReconciliationSubject .Spec .FailureDomain .Name == "" { // Needs random FD, but not yet set.
138
+ if util .IsControlPlaneMachine (r .CAPIMachine ) { // Is control plane machine -- CAPI will specify.
139
+ r .ReconciliationSubject .Spec .FailureDomain .Name = * r .CAPIMachine .Spec .FailureDomain
140
+ } else { // Not a control plane machine. Place randomly.
141
+ randNum := (rand .Int () % len (r .CSCluster .Spec .FailureDomains )) // #nosec G404 -- weak crypt rand doesn't matter here.
142
+ r .ReconciliationSubject .Spec .FailureDomain .Name = r .CSCluster .Spec .FailureDomains [randNum ].Name
143
+ }
144
+ }
165
145
return ctrl.Result {}, nil
166
146
}
167
147
@@ -173,15 +153,6 @@ func (r *CloudStackMachineReconciliationRunner) GetOrCreateVMInstance() (retRes
173
153
}
174
154
r .Log .Info ("Got Bootstrap DataSecretName." )
175
155
176
- // Get the CloudStackZone for the Machine.
177
- var machineZone infrav1.CloudStackZone
178
- for _ , zone := range r .Zones .Items {
179
- machineZone = zone
180
- if zone .Spec .ID == r .ReconciliationSubject .Status .ZoneID {
181
- break
182
- }
183
- }
184
-
185
156
// Get the kubeadm bootstrap secret for this machine.
186
157
secret := & corev1.Secret {}
187
158
key := types.NamespacedName {Namespace : r .CAPIMachine .Namespace , Name : * r .CAPIMachine .Spec .Bootstrap .DataSecretName }
@@ -193,7 +164,7 @@ func (r *CloudStackMachineReconciliationRunner) GetOrCreateVMInstance() (retRes
193
164
return ctrl.Result {}, errors .New ("bootstrap secret data not yet set" )
194
165
}
195
166
196
- err := r .CSUser .GetOrCreateVMInstance (r .ReconciliationSubject , r .CAPIMachine , r .CSCluster , & machineZone , r .AffinityGroup , string (data ))
167
+ err := r .CSUser .GetOrCreateVMInstance (r .ReconciliationSubject , r .CAPIMachine , r .CSCluster , r . FailureDomain , r .AffinityGroup , string (data ))
197
168
198
169
if err == nil && ! controllerutil .ContainsFinalizer (r .ReconciliationSubject , infrav1 .MachineFinalizer ) { // Fetched or Created?
199
170
r .Log .Info ("CloudStack instance Created" , "instanceStatus" , r .ReconciliationSubject .Status )
@@ -253,7 +224,7 @@ func (r *CloudStackMachineReconciliationRunner) GetOrCreateMachineStateChecker()
253
224
}
254
225
255
226
func (r * CloudStackMachineReconciliationRunner ) ReconcileDelete () (retRes ctrl.Result , reterr error ) {
256
- if res , err := r .AsFailureDomainUser (r .ReconciliationSubject .Spec .FailureDomain )(); r .ShouldReturn (res , err ) {
227
+ if res , err := r .AsFailureDomainUser (& r .ReconciliationSubject .Spec .FailureDomain )(); r .ShouldReturn (res , err ) {
257
228
return res , err
258
229
}
259
230
if r .ReconciliationSubject .Spec .InstanceID != nil {
0 commit comments