@@ -29,6 +29,7 @@ import (
2929 "github.com/linode/linodego"
3030 corev1 "k8s.io/api/core/v1"
3131 apierrors "k8s.io/apimachinery/pkg/api/errors"
32+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3233 utilerrors "k8s.io/apimachinery/pkg/util/errors"
3334 "k8s.io/client-go/tools/record"
3435 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
@@ -59,6 +60,8 @@ const (
5960 defaultDiskFilesystem = string (linodego .FilesystemExt4 )
6061
6162 // conditions for preflight instance creation
63+ ConditionPreflightBootstrapDataSecretReady clusterv1.ConditionType = "PreflightBootstrapDataSecretReady"
64+ ConditionPreflightLinodeFirewallReady clusterv1.ConditionType = "PreflightLinodeFirewallReady"
6265 ConditionPreflightMetadataSupportConfigured clusterv1.ConditionType = "PreflightMetadataSupportConfigured"
6366 ConditionPreflightCreated clusterv1.ConditionType = "PreflightCreated"
6467 ConditionPreflightRootDiskResizing clusterv1.ConditionType = "PreflightRootDiskResizing"
@@ -212,10 +215,12 @@ func (r *LinodeMachineReconciler) reconcile(ctx context.Context, logger logr.Log
212215 }
213216
214217 // Make sure bootstrap data is available and populated.
215- if machineScope .Machine .Spec .Bootstrap .DataSecretName == nil {
218+ if ! reconciler . ConditionTrue ( machineScope . LinodeMachine , ConditionPreflightBootstrapDataSecretReady ) && machineScope .Machine .Spec .Bootstrap .DataSecretName == nil {
216219 logger .Info ("Bootstrap data secret is not yet available" )
217- conditions .MarkFalse (machineScope .LinodeMachine , ConditionPreflightMetadataSupportConfigured , WaitingForBootstrapDataReason , clusterv1 .ConditionSeverityInfo , "" )
220+ conditions .MarkFalse (machineScope .LinodeMachine , ConditionPreflightBootstrapDataSecretReady , WaitingForBootstrapDataReason , clusterv1 .ConditionSeverityInfo , "" )
218221 return ctrl.Result {}, nil
222+ } else {
223+ conditions .MarkTrue (machineScope .LinodeMachine , ConditionPreflightBootstrapDataSecretReady )
219224 }
220225
221226 // Update
@@ -229,7 +234,7 @@ func (r *LinodeMachineReconciler) reconcile(ctx context.Context, logger logr.Log
229234 return r .reconcileCreate (ctx , logger , machineScope )
230235}
231236
232- //nolint:cyclop // can't make it simpler with existing API
237+ //nolint:cyclop,gocognit // can't make it simpler with existing API
233238func (r * LinodeMachineReconciler ) reconcileCreate (
234239 ctx context.Context ,
235240 logger logr.Logger ,
@@ -242,6 +247,16 @@ func (r *LinodeMachineReconciler) reconcileCreate(
242247 return ctrl.Result {}, err
243248 }
244249
250+ if machineScope .LinodeMachine .Spec .FirewallRef != nil {
251+ if ! reconciler .ConditionTrue (machineScope .LinodeMachine , ConditionPreflightLinodeFirewallReady ) && machineScope .LinodeMachine .Spec .ProviderID == nil {
252+ res , err := r .reconcilePreflightLinodeFirewallCheck (ctx , logger , machineScope )
253+ if err != nil || ! res .IsZero () {
254+ conditions .MarkFalse (machineScope .LinodeMachine , ConditionPreflightLinodeFirewallReady , string ("linode firewall not yet available" ), clusterv1 .ConditionSeverityError , "" )
255+ return res , err
256+ }
257+ }
258+ }
259+
245260 if ! reconciler .ConditionTrue (machineScope .LinodeMachine , ConditionPreflightMetadataSupportConfigured ) && machineScope .LinodeMachine .Spec .ProviderID == nil {
246261 res , err := r .reconcilePreflightMetadataSupportConfigure (ctx , logger , machineScope )
247262 if err != nil || ! res .IsZero () {
@@ -287,6 +302,34 @@ func (r *LinodeMachineReconciler) reconcileCreate(
287302 return ctrl.Result {}, nil
288303}
289304
305+ func (r * LinodeMachineReconciler ) reconcilePreflightLinodeFirewallCheck (ctx context.Context , logger logr.Logger , machineScope * scope.MachineScope ) (ctrl.Result , error ) {
306+ name := machineScope .LinodeMachine .Spec .FirewallRef .Name
307+ namespace := machineScope .LinodeMachine .Spec .FirewallRef .Namespace
308+ if namespace == "" {
309+ namespace = machineScope .LinodeMachine .Namespace
310+ }
311+ linodeFirewall := infrav1alpha2.LinodeFirewall {
312+ ObjectMeta : metav1.ObjectMeta {
313+ Namespace : namespace ,
314+ Name : name ,
315+ },
316+ }
317+ if err := machineScope .Client .Get (ctx , client .ObjectKeyFromObject (& linodeFirewall ), & linodeFirewall ); err != nil {
318+ logger .Error (err , "Failed to find linode Firewall" )
319+ if reconciler .RecordDecayingCondition (machineScope .LinodeMachine ,
320+ ConditionPreflightLinodeFirewallReady , string (cerrs .CreateMachineError ), err .Error (),
321+ reconciler .DefaultTimeout (r .ReconcileTimeout , reconciler .DefaultMachineControllerWaitForPreflightTimeout )) {
322+ return ctrl.Result {}, err
323+ }
324+ return ctrl.Result {RequeueAfter : reconciler .DefaultMachineControllerRetryDelay }, nil
325+ } else if ! linodeFirewall .Status .Ready {
326+ logger .Info ("Linode firewall not yet ready" )
327+ return ctrl.Result {RequeueAfter : reconciler .DefaultMachineControllerRetryDelay }, nil
328+ }
329+ conditions .MarkTrue (machineScope .LinodeMachine , ConditionPreflightLinodeFirewallReady )
330+ return ctrl.Result {}, nil
331+ }
332+
290333func (r * LinodeMachineReconciler ) reconcilePreflightMetadataSupportConfigure (ctx context.Context , logger logr.Logger , machineScope * scope.MachineScope ) (ctrl.Result , error ) {
291334 region , err := machineScope .LinodeClient .GetRegion (ctx , machineScope .LinodeMachine .Spec .Region )
292335 if err != nil {
0 commit comments