-
Notifications
You must be signed in to change notification settings - Fork 4
/
virtualmachine_controller.go
317 lines (284 loc) · 10.5 KB
/
virtualmachine_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
Copyright 2023.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package proxmox
import (
"context"
"fmt"
"reflect"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
proxmoxv1alpha1 "github.com/alperencelik/kubemox/api/proxmox/v1alpha1"
"github.com/alperencelik/kubemox/pkg/kubernetes"
"github.com/alperencelik/kubemox/pkg/metrics"
"github.com/alperencelik/kubemox/pkg/proxmox"
)
const (
virtualMachineFinalizerName = "virtualmachine.proxmox.alperen.cloud/finalizer"
// Controller settings
VMreconcilationPeriod = 10
VMmaxConcurrentReconciles = 10
// Status conditions
typeAvailableVirtualMachine = "Available"
typeCreatingVirtualMachine = "Creating"
typeDeletingVirtualMachine = "Deleting"
typeErrorVirtualMachine = "Error"
)
var (
Clientset, DynamicClient = kubernetes.GetKubeconfig()
)
// VirtualMachineReconciler reconciles a VirtualMachine object
type VirtualMachineReconciler struct {
client.Client
Scheme *runtime.Scheme
}
//+kubebuilder:rbac:groups=proxmox.alperen.cloud,resources=virtualmachines,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=proxmox.alperen.cloud,resources=virtualmachines/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=proxmox.alperen.cloud,resources=virtualmachines/finalizers,verbs=update
//+kubebuilder:rbac:groups="",resources=events,verbs=get;list;watch;create;update;patch;delete
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
func (r *VirtualMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx)
// Get the VirtualMachine resource with this namespace/name
vm := &proxmoxv1alpha1.VirtualMachine{}
err := r.Get(ctx, req.NamespacedName, vm)
if err != nil {
return ctrl.Result{}, r.handleResourceNotFound(ctx, err)
}
logger.Info(fmt.Sprintf("Reconciling VirtualMachine %s", vm.Name))
// Check if the VirtualMachine instance is marked to be deleted, which is indicated by the deletion timestamp being set.
if vm.ObjectMeta.DeletionTimestamp.IsZero() {
if !controllerutil.ContainsFinalizer(vm, virtualMachineFinalizerName) {
controllerutil.AddFinalizer(vm, virtualMachineFinalizerName)
if err = r.Update(ctx, vm); err != nil {
return ctrl.Result{Requeue: true}, client.IgnoreNotFound(err)
}
}
} else {
// The object is being deleted
if controllerutil.ContainsFinalizer(vm, virtualMachineFinalizerName) {
// Delete the VM
logger.Info(fmt.Sprintf("Deleting VirtualMachine %s", vm.Spec.Name))
// Update the condition for the VirtualMachine if it is not already deleting
if !meta.IsStatusConditionPresentAndEqual(vm.Status.Conditions, typeDeletingVirtualMachine, metav1.ConditionUnknown) {
meta.SetStatusCondition(&vm.Status.Conditions, metav1.Condition{
Type: typeDeletingVirtualMachine,
Status: metav1.ConditionUnknown,
Reason: "Deleting",
Message: "Deleting VirtualMachine",
})
if err = r.Status().Update(ctx, vm); err != nil {
logger.Error(err, "Error updating VirtualMachine status")
return ctrl.Result{Requeue: true}, client.IgnoreNotFound(err)
}
} else {
return ctrl.Result{}, nil
}
// Perform all operations to delete the VM if the VM is not marked as deleting
// TODO: Check if the VM is already deleting
r.DeleteVirtualMachine(vm)
// Remove finalizer
logger.Info("Removing finalizer from VirtualMachine", "name", vm.Spec.Name)
// Remove finalizer
controllerutil.RemoveFinalizer(vm, virtualMachineFinalizerName)
if err = r.Update(ctx, vm); err != nil {
return ctrl.Result{}, nil
}
}
// Stop reconciliation as the item is being deleted
return ctrl.Result{}, nil
}
// Check if this VirtualMachine already exists
vmName := vm.Spec.Name
nodeName := vm.Spec.NodeName
vmExists := proxmox.CheckVM(vmName, nodeName)
if !vmExists {
// If not exists, create the VM
logger.Info("Creating VirtualMachine", "name", vmName)
err = r.CreateVirtualMachine(ctx, vm)
if err != nil {
logger.Error(err, "Error creating VirtualMachine")
meta.SetStatusCondition(&vm.Status.Conditions, metav1.Condition{
Type: typeErrorVirtualMachine,
Status: metav1.ConditionTrue,
Reason: "Error",
Message: fmt.Sprintf("Error creating VirtualMachine: %s", err),
})
if err = r.Status().Update(ctx, vm); err != nil {
logger.Error(err, "Error updating VirtualMachine status")
return ctrl.Result{Requeue: true}, client.IgnoreNotFound(err)
}
return ctrl.Result{Requeue: true}, client.IgnoreNotFound(err)
}
metrics.IncVirtualMachineCount()
} else {
// Check if auto start is enabled
_, err = r.handleAutoStart(ctx, vm)
if err != nil {
logger.Error(err, "Error handling auto start")
return ctrl.Result{Requeue: true}, client.IgnoreNotFound(err)
}
err = r.UpdateVirtualMachine(ctx, vm)
if err != nil {
logger.Error(err, "Error updating VirtualMachine")
return ctrl.Result{Requeue: true}, client.IgnoreNotFound(err)
}
}
logger.Info(fmt.Sprintf("VirtualMachine %s already exists", vmName))
// Update the VirtualMachine status
err = r.UpdateVirtualMachineStatus(vm)
if err != nil {
logger.Error(err, "Error updating VirtualMachine status")
return ctrl.Result{Requeue: true}, client.IgnoreNotFound(err)
}
// Configure the VirtualMachine with required network and storage configurations
return ctrl.Result{}, client.IgnoreNotFound(err)
}
// SetupWithManager sets up the controller with the Manager.
func (r *VirtualMachineReconciler) SetupWithManager(mgr ctrl.Manager) error {
logger := log.FromContext(context.Background())
version, err := proxmox.GetProxmoxVersion()
if err != nil {
logger.Error(err, "Error getting Proxmox version")
}
logger.Info(fmt.Sprintf("Connected to the Proxmox, version is: %s", version))
return ctrl.NewControllerManagedBy(mgr).
For(&proxmoxv1alpha1.VirtualMachine{}).
WithEventFilter(predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
oldVM := e.ObjectOld.(*proxmoxv1alpha1.VirtualMachine)
newVM := e.ObjectNew.(*proxmoxv1alpha1.VirtualMachine)
condition1 := !reflect.DeepEqual(oldVM.Spec, newVM.Spec)
condition2 := newVM.ObjectMeta.GetDeletionTimestamp().IsZero()
return condition1 || !condition2
},
}).
WithOptions(controller.Options{MaxConcurrentReconciles: VMmaxConcurrentReconciles}).
Complete(r)
}
func (r *VirtualMachineReconciler) CreateVirtualMachine(ctx context.Context, vm *proxmoxv1alpha1.VirtualMachine) error {
logger := log.FromContext(ctx)
vmName := vm.Spec.Name
nodeName := vm.Spec.NodeName
vmType := proxmox.CheckVMType(vm)
switch vmType {
case "template":
kubernetes.CreateVMKubernetesEvent(vm, Clientset, "Creating")
proxmox.CreateVMFromTemplate(vm)
if err := r.Status().Update(context.Background(), vm); err != nil {
return err
}
startResult, err := proxmox.StartVM(vmName, nodeName)
if err != nil {
return err
} else {
logger.Info(startResult)
}
kubernetes.CreateVMKubernetesEvent(vm, Clientset, "Created")
case "scratch":
kubernetes.CreateVMKubernetesEvent(vm, Clientset, "Creating")
proxmox.CreateVMFromScratch(vm)
startResult, err := proxmox.StartVM(vmName, nodeName)
if err != nil {
return err
} else {
logger.Info(startResult)
}
kubernetes.CreateVMKubernetesEvent(vm, Clientset, "Created")
default:
return fmt.Errorf("VM %s doesn't have any template or vmSpec defined", vmName)
}
return nil
}
func (r *VirtualMachineReconciler) DeleteVirtualMachine(vm *proxmoxv1alpha1.VirtualMachine) {
// Delete the VM
kubernetes.CreateVMKubernetesEvent(vm, kubernetes.Clientset, "Deleting")
if vm.Spec.DeletionProtection {
metrics.DecVirtualMachineCount()
return
} else {
proxmox.DeleteVM(vm.Spec.Name, vm.Spec.NodeName)
metrics.DecVirtualMachineCount()
}
}
func (r *VirtualMachineReconciler) UpdateVirtualMachineStatus(vm *proxmoxv1alpha1.VirtualMachine) error {
meta.SetStatusCondition(&vm.Status.Conditions, metav1.Condition{
Type: typeAvailableVirtualMachine,
Status: metav1.ConditionTrue,
Reason: "Available",
Message: "VirtualMachine status is updated",
})
// Update the QEMU status
qemuStatus, err := proxmox.UpdateVMStatus(vm.Spec.Name, vm.Spec.NodeName)
if err != nil {
return err
}
vm.Status.Status = *qemuStatus
if err := r.Status().Update(context.Background(), vm); err != nil {
return err
}
return nil
}
func (r *VirtualMachineReconciler) handleResourceNotFound(ctx context.Context, err error) error {
logger := log.FromContext(ctx)
if errors.IsNotFound(err) {
logger.Info("VirtualMachine resource not found. Ignoring since object must be deleted")
return nil
}
logger.Error(err, "Failed to get VirtualMachine")
return err
}
func (r *VirtualMachineReconciler) handleAutoStart(ctx context.Context,
vm *proxmoxv1alpha1.VirtualMachine) (ctrl.Result, error) {
logger := log.FromContext(ctx)
if vm.Spec.EnableAutoStart {
vmName := vm.Spec.Name
nodeName := vm.Spec.NodeName
vmState := proxmox.GetVMState(vmName, nodeName)
if vmState == "stopped" {
startResult, err := proxmox.StartVM(vmName, nodeName)
if err != nil {
return ctrl.Result{Requeue: true}, err
} else {
logger.Info(startResult)
}
return ctrl.Result{Requeue: true}, nil
}
}
return ctrl.Result{}, nil
}
func (r *VirtualMachineReconciler) UpdateVirtualMachine(ctx context.Context, vm *proxmoxv1alpha1.VirtualMachine) error {
logger := log.FromContext(ctx)
updateStatus := proxmox.UpdateVM(vm)
err := r.UpdateVirtualMachineStatus(vm)
if err != nil {
return err
}
err = proxmox.ConfigureVirtualMachine(vm)
if err != nil {
return err
}
if updateStatus {
logger.Info(fmt.Sprintf("VirtualMachine %s is updated", vm.Spec.Name))
}
return err
}