-
Notifications
You must be signed in to change notification settings - Fork 4
/
managedvirtualmachine_controller.go
235 lines (215 loc) · 9.5 KB
/
managedvirtualmachine_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
/*
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 is the name of the finalizer
managedvirtualMachineFinalizerName = "managedvirtualmachine.proxmox.alperen.cloud/finalizer"
ManagedVMreconcilationPeriod = 15
ManagedVMmaxConcurrentReconciles = 5
// Status conditions
typeAvailableManagedVirtualMachine = "Available"
typeCreatingManagedVirtualMachine = "Creating"
typeDeletingManagedVirtualMachine = "Deleting"
typeErrorManagedVirtualMachine = "Error"
)
// ManagedVirtualMachineReconciler reconciles a ManagedVirtualMachine object
type ManagedVirtualMachineReconciler struct {
client.Client
Scheme *runtime.Scheme
}
//+kubebuilder:rbac:groups=proxmox.alperen.cloud,resources=managedvirtualmachines,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=proxmox.alperen.cloud,resources=managedvirtualmachines/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=proxmox.alperen.cloud,resources=managedvirtualmachines/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.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the ManagedVirtualMachine object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.15.0/pkg/reconcile
func (r *ManagedVirtualMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx)
// TODO(user): your logic here
managedVM := &proxmoxv1alpha1.ManagedVirtualMachine{}
err := r.Get(ctx, req.NamespacedName, managedVM)
if err != nil {
return ctrl.Result{}, r.handleResourceNotFound(ctx, err)
}
logger.Info(fmt.Sprintf("Reconciling ManagedVirtualMachine %s", managedVM.Name))
// Check if the ManagedVM instance is marked to be deleted, which is indicated by the deletion timestamp being set.
if managedVM.ObjectMeta.DeletionTimestamp.IsZero() {
if !controllerutil.ContainsFinalizer(managedVM, managedvirtualMachineFinalizerName) {
controllerutil.AddFinalizer(managedVM, managedvirtualMachineFinalizerName)
if err = r.Update(ctx, managedVM); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
}
} else {
// The object is being deleted
if controllerutil.ContainsFinalizer(managedVM, managedvirtualMachineFinalizerName) {
logger.Info(fmt.Sprintf("Deleting ManagedVirtualMachine %s", managedVM.Name))
if !meta.IsStatusConditionPresentAndEqual(managedVM.Status.Conditions, typeDeletingManagedVirtualMachine, metav1.ConditionTrue) {
meta.SetStatusCondition(&managedVM.Status.Conditions, metav1.Condition{
Type: typeDeletingManagedVirtualMachine,
Status: metav1.ConditionTrue,
Reason: "Deleting",
Message: "Deleting ManagedVirtualMachine",
})
if err = r.Status().Update(ctx, managedVM); err != nil {
logger.Error(err, "Error updating ManagedVirtualMachine status")
return ctrl.Result{Requeue: true}, client.IgnoreNotFound(err)
}
}
// Delete the VM
if !managedVM.Spec.DeletionProtection {
kubernetes.CreateManagedVMKubernetesEvent(managedVM, Clientset, "Deleting")
proxmox.DeleteVM(managedVM.Name, managedVM.Spec.NodeName)
metrics.DecManagedVirtualMachineCount()
} else {
kubernetes.CreateManagedVMKubernetesEvent(managedVM, Clientset, "Deleting")
// Remove managedVirtualMachineTag from Managed Virtual Machine to not manage it anymore
err = proxmox.RemoveVirtualMachineTag(managedVM.Name, managedVM.Spec.NodeName, proxmox.ManagedVirtualMachineTag)
if err != nil {
logger.Error(err, "Error removing managedVirtualMachineTag from Managed Virtual Machine")
return ctrl.Result{Requeue: true}, client.IgnoreNotFound(err)
}
metrics.DecManagedVirtualMachineCount()
}
}
logger.Info("Removing finalizer from ManagedVirtualMachine", "name", managedVM.Name)
// Remove finalizer
controllerutil.RemoveFinalizer(managedVM, managedvirtualMachineFinalizerName)
if err = r.Update(ctx, managedVM); err != nil {
logger.Info(fmt.Sprintf("Error updating ManagedVirtualMachine %s", managedVM.Name))
}
return ctrl.Result{}, nil
}
// If EnableAutoStart is true, start the VM if it's stopped
r.handleAutoStart(ctx, managedVM)
// Update ManagedVM
proxmox.UpdateManagedVM(ctx, managedVM)
err = r.Update(context.Background(), managedVM)
if err != nil {
logger.Info(fmt.Sprintf("ManagedVM %v could not be updated", managedVM.Name))
return ctrl.Result{}, client.IgnoreNotFound(err)
}
// Update ManagedVMStatus
managedVMName := managedVM.Name
nodeName := proxmox.GetNodeOfVM(managedVMName)
ManagedVMStatus, _ := proxmox.UpdateVMStatus(managedVMName, nodeName)
managedVM.Status.Status = *ManagedVMStatus
err = r.Status().Update(context.Background(), managedVM)
if err != nil {
logger.Info(fmt.Sprintf("ManagedVMStatus %v could not be updated", managedVM.Name))
}
return ctrl.Result{}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *ManagedVirtualMachineReconciler) SetupWithManager(mgr ctrl.Manager) error {
// Get list of VMs that tagged with managedVirtualMachineTag
managedVMs := proxmox.GetManagedVMs()
// Handle ManagedVM creation
err := r.handleManagedVMCreation(context.Background(), managedVMs)
if err != nil {
return err
}
return ctrl.NewControllerManagedBy(mgr).
For(&proxmoxv1alpha1.ManagedVirtualMachine{}).
WithEventFilter(predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
oldManagedVM := e.ObjectOld.(*proxmoxv1alpha1.ManagedVirtualMachine)
newManagedVM := e.ObjectNew.(*proxmoxv1alpha1.ManagedVirtualMachine)
condition1 := !reflect.DeepEqual(oldManagedVM.Spec, newManagedVM.Spec)
condition2 := newManagedVM.ObjectMeta.GetDeletionTimestamp().IsZero()
return condition1 || !condition2
},
}).
WithOptions(controller.Options{MaxConcurrentReconciles: ManagedVMmaxConcurrentReconciles}).
Complete(r)
}
func (r *ManagedVirtualMachineReconciler) handleResourceNotFound(ctx context.Context, err error) error {
logger := log.FromContext(ctx)
if errors.IsNotFound(err) {
logger.Info("ManagedVirtualMachine resource not found. Ignoring since object must be deleted")
return nil
}
logger.Error(err, "Failed to get VirtualMachine")
return err
}
func (r *ManagedVirtualMachineReconciler) handleManagedVMCreation(ctx context.Context, managedVMs []string) error {
logger := log.FromContext(ctx)
for _, managedVM := range managedVMs {
// Create ManagedVM that matches with tag of managedVirtualMachineTag value
logger.Info(fmt.Sprintf("ManagedVM %v found!", managedVM))
if !proxmox.CheckManagedVMExists(managedVM) {
logger.Info(fmt.Sprintf("ManagedVM %v does not exist so creating it", managedVM))
managedVM := proxmox.CreateManagedVM(managedVM)
err := r.Create(context.Background(), managedVM)
if err != nil {
logger.Info(fmt.Sprintf("ManagedVM %v could not be created", managedVM.Name))
return err
}
// Add metrics
metrics.SetManagedVirtualMachineCPUCores(managedVM.Name, managedVM.Namespace, float64(managedVM.Spec.Cores))
metrics.SetManagedVirtualMachineMemory(managedVM.Name, managedVM.Namespace, float64(managedVM.Spec.Memory))
metrics.IncManagedVirtualMachineCount()
} else {
logger.Info(fmt.Sprintf("ManagedVM %v already exists", managedVM))
}
}
return nil
}
func (r *ManagedVirtualMachineReconciler) handleAutoStart(ctx context.Context,
managedVM *proxmoxv1alpha1.ManagedVirtualMachine) ctrl.Result {
logger := log.FromContext(ctx)
if managedVM.Spec.EnableAutoStart {
vmName := managedVM.Name
nodeName := managedVM.Spec.NodeName
vmState := proxmox.GetVMState(vmName, nodeName)
if vmState == proxmox.VirtualMachineStoppedState {
startResult, err := proxmox.StartVM(vmName, nodeName)
if err != nil {
logger.Info(fmt.Sprintf("ManagedVirtualMachine %s could not be started", vmName))
return ctrl.Result{Requeue: true}
} else {
logger.Info(startResult)
}
logger.Info(fmt.Sprintf("ManagedVirtualMachine %s started", vmName))
return ctrl.Result{Requeue: true}
}
}
return ctrl.Result{}
}