@@ -36,7 +36,6 @@ import (
36
36
37
37
proxmoxv1alpha1 "github.com/alperencelik/kubemox/api/proxmox/v1alpha1"
38
38
"github.com/alperencelik/kubemox/pkg/kubernetes"
39
- "github.com/alperencelik/kubemox/pkg/metrics"
40
39
"github.com/alperencelik/kubemox/pkg/proxmox"
41
40
)
42
41
@@ -81,6 +80,33 @@ func (r *VirtualMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reque
81
80
if err != nil {
82
81
return ctrl.Result {}, r .handleResourceNotFound (ctx , err )
83
82
}
83
+ reconcileMode := r .getReconcileMode (vm )
84
+
85
+ switch reconcileMode {
86
+ case proxmoxv1alpha1 .ReconcileModeWatchOnly :
87
+ logger .Info (fmt .Sprintf ("Reconciliation is watch only for VirtualMachine %s" , vm .Name ))
88
+ r .handleWatcher (ctx , req , vm )
89
+ return ctrl.Result {}, nil
90
+ case proxmoxv1alpha1 .ReconcileModeEnsureExists :
91
+ logger .Info (fmt .Sprintf ("Reconciliation is ensure exists for VirtualMachine %s" , vm .Name ))
92
+ vmExists := proxmox .CheckVM (vm .Spec .Name , vm .Spec .NodeName )
93
+ if ! vmExists {
94
+ // If not exists, create the VM
95
+ logger .Info ("Creating VirtualMachine" , "name" , vm .Spec .Name )
96
+ err = r .CreateVirtualMachine (ctx , vm )
97
+ if err != nil {
98
+ logger .Error (err , "Error creating VirtualMachine" )
99
+ }
100
+ }
101
+ return ctrl.Result {}, nil
102
+ case proxmoxv1alpha1 .ReconcileModeDisable :
103
+ // Disable the reconciliation
104
+ logger .Info (fmt .Sprintf ("Reconciliation is disabled for VirtualMachine %s" , vm .Name ))
105
+ return ctrl.Result {}, nil
106
+ default :
107
+ // Normal mode
108
+ break
109
+ }
84
110
85
111
logger .Info (fmt .Sprintf ("Reconciling VirtualMachine %s" , vm .Name ))
86
112
@@ -98,37 +124,10 @@ func (r *VirtualMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reque
98
124
// The object is being deleted
99
125
if controllerutil .ContainsFinalizer (vm , virtualMachineFinalizerName ) {
100
126
// Delete the VM
101
- logger .Info (fmt .Sprintf ("Deleting VirtualMachine %s" , vm .Spec .Name ))
102
-
103
- // Update the condition for the VirtualMachine if it is not already deleting
104
- if ! meta .IsStatusConditionPresentAndEqual (vm .Status .Conditions , typeDeletingVirtualMachine , metav1 .ConditionUnknown ) {
105
- meta .SetStatusCondition (& vm .Status .Conditions , metav1.Condition {
106
- Type : typeDeletingVirtualMachine ,
107
- Status : metav1 .ConditionUnknown ,
108
- Reason : "Deleting" ,
109
- Message : "Deleting VirtualMachine" ,
110
- })
111
- if err = r .Status ().Update (ctx , vm ); err != nil {
112
- logger .Error (err , "Error updating VirtualMachine status" )
113
- return ctrl.Result {Requeue : true }, client .IgnoreNotFound (err )
114
- }
115
- } else {
116
- return ctrl.Result {}, nil
117
- }
118
- // Stop the watcher if resource is being deleted
119
- if stopChan , exists := r .Watchers .Watchers [req .Name ]; exists {
120
- close (stopChan )
121
- delete (r .Watchers .Watchers , req .Name )
122
- }
123
- // Perform all operations to delete the VM if the VM is not marked as deleting
124
- // TODO: Evaluate the requirement of check mechanism for VM whether it's already deleting
125
- r .DeleteVirtualMachine (ctx , vm )
126
-
127
- // Remove finalizer
128
- logger .Info ("Removing finalizer from VirtualMachine" , "name" , vm .Spec .Name )
129
- controllerutil .RemoveFinalizer (vm , virtualMachineFinalizerName )
130
- if err = r .Update (ctx , vm ); err != nil {
131
- return ctrl.Result {}, nil
127
+ res , delErr := r .handleDelete (ctx , req , vm )
128
+ if delErr != nil {
129
+ logger .Error (err , "Error handling VirtualMachine deletion" )
130
+ return res , client .IgnoreNotFound (delErr )
132
131
}
133
132
}
134
133
// Stop reconciliation as the item is being deleted
@@ -166,7 +165,8 @@ func (r *VirtualMachineReconciler) SetupWithManager(mgr ctrl.Manager) error {
166
165
newVM := e .ObjectNew .(* proxmoxv1alpha1.VirtualMachine )
167
166
condition1 := ! reflect .DeepEqual (oldVM .Spec , newVM .Spec )
168
167
condition2 := newVM .ObjectMeta .GetDeletionTimestamp ().IsZero ()
169
- return condition1 || ! condition2
168
+ condition3 := ! reflect .DeepEqual (oldVM .GetAnnotations (), newVM .GetAnnotations ())
169
+ return condition1 || ! condition2 || condition3
170
170
},
171
171
}).
172
172
WithOptions (controller.Options {MaxConcurrentReconciles : VMmaxConcurrentReconciles }).
@@ -197,7 +197,6 @@ func (r *VirtualMachineReconciler) handleVirtualMachineOperations(ctx context.Co
197
197
}
198
198
return ctrl.Result {Requeue : true }, client .IgnoreNotFound (err )
199
199
}
200
- metrics .IncVirtualMachineCount ()
201
200
} else {
202
201
// Check if auto start is enabled
203
202
_ , err = r .handleAutoStart (ctx , vm )
@@ -273,12 +272,10 @@ func (r *VirtualMachineReconciler) DeleteVirtualMachine(ctx context.Context, vm
273
272
// Delete the VM
274
273
r .Recorder .Event (vm , "Normal" , "Deleting" , fmt .Sprintf ("VirtualMachine %s is being deleted" , vm .Spec .Name ))
275
274
if vm .Spec .DeletionProtection {
276
- metrics .DecVirtualMachineCount ()
277
275
logger .Info (fmt .Sprintf ("VirtualMachine %s is protected from deletion" , vm .Spec .Name ))
278
276
return
279
277
} else {
280
278
proxmox .DeleteVM (vm .Spec .Name , vm .Spec .NodeName )
281
- metrics .DecVirtualMachineCount ()
282
279
}
283
280
}
284
281
@@ -365,6 +362,45 @@ func (r *VirtualMachineReconciler) handleFinalizer(ctx context.Context, vm *prox
365
362
return nil
366
363
}
367
364
365
+ func (r * VirtualMachineReconciler ) handleDelete (ctx context.Context , req ctrl.Request ,
366
+ vm * proxmoxv1alpha1.VirtualMachine ) (ctrl.Result , error ) {
367
+ logger := log .FromContext (ctx )
368
+ // Delete the VM
369
+ logger .Info (fmt .Sprintf ("Deleting VirtualMachine %s" , vm .Spec .Name ))
370
+
371
+ // Update the condition for the VirtualMachine if it is not already deleting
372
+ if ! meta .IsStatusConditionPresentAndEqual (vm .Status .Conditions , typeDeletingVirtualMachine , metav1 .ConditionUnknown ) {
373
+ meta .SetStatusCondition (& vm .Status .Conditions , metav1.Condition {
374
+ Type : typeDeletingVirtualMachine ,
375
+ Status : metav1 .ConditionUnknown ,
376
+ Reason : "Deleting" ,
377
+ Message : "Deleting VirtualMachine" ,
378
+ })
379
+ if err := r .Status ().Update (ctx , vm ); err != nil {
380
+ logger .Error (err , "Error updating VirtualMachine status" )
381
+ return ctrl.Result {Requeue : true }, client .IgnoreNotFound (err )
382
+ }
383
+ } else {
384
+ return ctrl.Result {}, nil
385
+ }
386
+ // Stop the watcher if resource is being deleted
387
+ if stopChan , exists := r .Watchers .Watchers [req .Name ]; exists {
388
+ close (stopChan )
389
+ delete (r .Watchers .Watchers , req .Name )
390
+ }
391
+ // Perform all operations to delete the VM if the VM is not marked as deleting
392
+ // TODO: Evaluate the requirement of check mechanism for VM whether it's already deleting
393
+ r .DeleteVirtualMachine (ctx , vm )
394
+
395
+ // Remove finalizer
396
+ logger .Info ("Removing finalizer from VirtualMachine" , "name" , vm .Spec .Name )
397
+ controllerutil .RemoveFinalizer (vm , virtualMachineFinalizerName )
398
+ if err := r .Update (ctx , vm ); err != nil {
399
+ return ctrl.Result {}, nil
400
+ }
401
+ return ctrl.Result {}, nil
402
+ }
403
+
368
404
func (r * VirtualMachineReconciler ) handleCloudInitOperations (ctx context.Context ,
369
405
vm * proxmoxv1alpha1.VirtualMachine ) error {
370
406
if proxmox .CheckVMType (vm ) == proxmox .VirtualMachineTemplateType {
@@ -416,6 +452,18 @@ func (r *VirtualMachineReconciler) handleAdditionalConfig(ctx context.Context, v
416
452
return nil
417
453
}
418
454
455
+ func (r * VirtualMachineReconciler ) getReconcileMode (vm * proxmoxv1alpha1.VirtualMachine ) string {
456
+ // Get the annotations and find out the reconcile mode
457
+ annotations := vm .GetAnnotations ()
458
+ if annotations == nil {
459
+ return "Normal"
460
+ }
461
+ if mode , ok := annotations [proxmoxv1alpha1 .ReconcileModeAnnotation ]; ok {
462
+ return mode
463
+ }
464
+ return "Normal"
465
+ }
466
+
419
467
func (r * VirtualMachineReconciler ) handleWatcher (ctx context.Context , req ctrl.Request , vm * proxmoxv1alpha1.VirtualMachine ) {
420
468
r .Watchers .HandleWatcher (ctx , req , func (ctx context.Context , stopChan chan struct {}) (ctrl.Result , error ) {
421
469
return proxmox .StartWatcher (ctx , vm , stopChan , r .fetchResource , r .updateStatus ,
0 commit comments