Skip to content

Commit

Permalink
fix: VM network bug and events (#41)
Browse files Browse the repository at this point in the history
* fix: VM network bug (#40) and Kubernetes events
  • Loading branch information
alperencelik authored Oct 17, 2024
1 parent 32cce3f commit 4880f68
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 109 deletions.
4 changes: 4 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,15 @@ func main() {
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Watchers: proxmox.NewExternalWatchers(),
Recorder: mgr.GetEventRecorderFor("VirtualMachine"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "VirtualMachine")
os.Exit(1)
}
if err = (&proxmoxcontroller.ManagedVirtualMachineReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("ManagedVirtualMachine"),
Watchers: proxmox.NewExternalWatchers(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ManagedVirtualMachine")
Expand Down Expand Up @@ -135,6 +137,7 @@ func main() {
if err = (&proxmoxcontroller.ContainerReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("Container"),
Watchers: proxmox.NewExternalWatchers(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Container")
Expand All @@ -158,6 +161,7 @@ func main() {
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Watchers: proxmox.NewExternalWatchers(),
Recorder: mgr.GetEventRecorderFor("VirtualMachineTemplate"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "VirtualMachineTemplate")
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion examples/virtualmachineclone.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ spec:
# Deletion protection is whether to delete VM from Proxmox or not
deletionProtection: false
# VM should be started any time found in stopped state
enableAutostart: true
enableAutoStart: true
template:
# Name of the template to be cloned
name: fedora-template
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/onsi/gomega v1.27.7
github.com/prometheus/client_golang v1.15.1
github.com/robfig/cron/v3 v3.0.1
k8s.io/api v0.28.3
k8s.io/apiextensions-apiserver v0.27.2
k8s.io/apimachinery v0.28.3
k8s.io/client-go v0.28.3
Expand Down Expand Up @@ -87,6 +86,7 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.28.3 // indirect
k8s.io/component-base v0.27.2 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
Expand Down
7 changes: 7 additions & 0 deletions internal/controller/proxmox/container_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
Expand All @@ -42,6 +43,7 @@ import (
type ContainerReconciler struct {
client.Client
Scheme *runtime.Scheme
Recorder record.EventRecorder
Watchers *proxmox.ExternalWatchers
}

Expand Down Expand Up @@ -165,6 +167,7 @@ func (r *ContainerReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *ContainerReconciler) CloneContainer(container *proxmoxv1alpha1.Container) error {
containerName := container.Spec.Name
nodeName := container.Spec.NodeName
r.Recorder.Event(container, "Normal", "Creating", fmt.Sprintf("Creating Container %s", containerName))
err := proxmox.CloneContainer(container)
if err != nil {
return err
Expand All @@ -173,11 +176,13 @@ func (r *ContainerReconciler) CloneContainer(container *proxmoxv1alpha1.Containe
if err != nil {
return err
}
r.Recorder.Event(container, "Normal", "Created", fmt.Sprintf("Created Container %s", containerName))
return nil
}

func (r *ContainerReconciler) UpdateContainer(ctx context.Context, container *proxmoxv1alpha1.Container) error {
proxmox.UpdateContainer(container)
r.Recorder.Event(container, "Normal", "Updated", fmt.Sprintf("Updated Container %s", container.Name))
err := r.Update(ctx, container)
if err != nil {
return err
Expand Down Expand Up @@ -316,10 +321,12 @@ func (r *ContainerReconciler) handleContainerDeletion(ctx context.Context, conta
logger := log.FromContext(ctx)
containerName := container.Spec.Name
nodeName := container.Spec.NodeName
r.Recorder.Event(container, "Normal", "Deleting", fmt.Sprintf("Deleting Container %s", containerName))
if container.Spec.DeletionProtection {
logger.Info(fmt.Sprintf("Container %s is protected from deletion", containerName))
return
} else {
proxmox.DeleteContainer(containerName, nodeName)
}
r.Recorder.Event(container, "Normal", "Deleted", fmt.Sprintf("Deleted Container %s", containerName))
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand All @@ -34,7 +35,6 @@ import (
"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"
)
Expand All @@ -57,6 +57,7 @@ type ManagedVirtualMachineReconciler struct {
client.Client
Scheme *runtime.Scheme
Watchers *proxmox.ExternalWatchers
Recorder record.EventRecorder
}

//+kubebuilder:rbac:groups=proxmox.alperen.cloud,resources=managedvirtualmachines,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -119,20 +120,18 @@ func (r *ManagedVirtualMachineReconciler) Reconcile(ctx context.Context, req ctr
}

// Delete the VM
r.Recorder.Event(managedVM, "Normal", "Deleting", fmt.Sprintf("Deleting ManagedVirtualMachine %s", managedVM.Name))
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()
}
metrics.DecManagedVirtualMachineCount()
}
logger.Info("Removing finalizer from ManagedVirtualMachine", "name", managedVM.Name)
// Remove finalizer
Expand Down
16 changes: 9 additions & 7 deletions internal/controller/proxmox/virtualmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand Down Expand Up @@ -62,8 +63,7 @@ type VirtualMachineReconciler struct {
client.Client
Scheme *runtime.Scheme
Watchers *proxmox.ExternalWatchers
// Watchers map[string]chan struct{}
// mu sync.Mutex
Recorder record.EventRecorder
}

//+kubebuilder:rbac:groups=proxmox.alperen.cloud,resources=virtualmachines,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -210,7 +210,7 @@ func (r *VirtualMachineReconciler) CreateVirtualMachine(ctx context.Context, vm

switch vmType {
case "template":
kubernetes.CreateVMKubernetesEvent(vm, Clientset, "Creating")
r.Recorder.Event(vm, "Normal", "Creating", fmt.Sprintf("VirtualMachine %s is being created", vmName))
proxmox.CreateVMFromTemplate(vm)
if err := r.Status().Update(context.Background(), vm); err != nil {
return err
Expand All @@ -221,17 +221,17 @@ func (r *VirtualMachineReconciler) CreateVirtualMachine(ctx context.Context, vm
} else {
logger.Info(startResult)
}
kubernetes.CreateVMKubernetesEvent(vm, Clientset, "Created")
r.Recorder.Event(vm, "Normal", "Created", fmt.Sprintf("VirtualMachine %s has been created", vmName))
case "scratch":
kubernetes.CreateVMKubernetesEvent(vm, Clientset, "Creating")
r.Recorder.Event(vm, "Normal", "Creating", fmt.Sprintf("VirtualMachine %s is being created", vmName))
proxmox.CreateVMFromScratch(vm)
startResult, err := proxmox.StartVM(vmName, nodeName)
if err != nil {
return err
} else {
logger.Info(startResult)
}
kubernetes.CreateVMKubernetesEvent(vm, Clientset, "Created")
r.Recorder.Event(vm, "Normal", "Created", fmt.Sprintf("VirtualMachine %s has been created", vmName))
default:
return fmt.Errorf("VM %s doesn't have any template or vmSpec defined", vmName)
}
Expand All @@ -241,7 +241,7 @@ func (r *VirtualMachineReconciler) CreateVirtualMachine(ctx context.Context, vm
func (r *VirtualMachineReconciler) DeleteVirtualMachine(ctx context.Context, vm *proxmoxv1alpha1.VirtualMachine) {
logger := log.FromContext(ctx)
// Delete the VM
kubernetes.CreateVMKubernetesEvent(vm, kubernetes.Clientset, "Deleting")
r.Recorder.Event(vm, "Normal", "Deleting", fmt.Sprintf("VirtualMachine %s is being deleted", vm.Spec.Name))
if vm.Spec.DeletionProtection {
metrics.DecVirtualMachineCount()
logger.Info(fmt.Sprintf("VirtualMachine %s is protected from deletion", vm.Spec.Name))
Expand Down Expand Up @@ -303,11 +303,13 @@ func (r *VirtualMachineReconciler) handleAutoStart(ctx context.Context,

func (r *VirtualMachineReconciler) UpdateVirtualMachine(ctx context.Context, vm *proxmoxv1alpha1.VirtualMachine) error {
logger := log.FromContext(ctx)
// UpdateVM is checks the delta for CPU and Memory and updates the VM with a restart
updateStatus := proxmox.UpdateVM(vm)
err := r.UpdateVirtualMachineStatus(ctx, vm)
if err != nil {
return err
}
// ConfigureVirtualMachine is checks the delta for Disk and Network and updates the VM without a restart
err = proxmox.ConfigureVirtualMachine(vm)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand All @@ -43,6 +44,7 @@ type VirtualMachineTemplateReconciler struct {
client.Client
Scheme *runtime.Scheme
Watchers *proxmox.ExternalWatchers
Recorder record.EventRecorder
}

const (
Expand Down Expand Up @@ -256,6 +258,7 @@ func (r *VirtualMachineTemplateReconciler) handleVMCreation(ctx context.Context,
logger.Error(err, "Failed to create VM for template")
return err
}
r.Recorder.Event(vmTemplate, "Normal", "VMTemplateCreated", fmt.Sprintf("VirtualMachine template %s is created", templateVMName))
}
return nil
}
Expand All @@ -278,6 +281,7 @@ func (r *VirtualMachineTemplateReconciler) deleteVirtualMachineTemplate(ctx cont
logger := log.FromContext(ctx)
logger.Info(fmt.Sprintf("Deleting VirtualMachineTemplate %s", vmTemplate.Name))
// Delete the VM
r.Recorder.Event(vmTemplate, "Normal", "VMTemplateDeleting", fmt.Sprintf("VirtualMachine template %s is deleting", vmTemplate.Name))
if vmTemplate.Spec.DeletionProtection {
logger.Info("Deletion protection is enabled, skipping the deletion of VM")
return
Expand Down
94 changes: 0 additions & 94 deletions pkg/kubernetes/events.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/proxmox/virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ func configureVirtualMachineNetwork(vm *proxmoxv1alpha1.VirtualMachine) error {
}
for i := 0; i < len(virtualMachineNetworksParsed); i++ {
// Check if the network configuration is different
if !reflect.DeepEqual(networks[i], virtualMachineNetworksParsed[i]) {
if len(networks) != 0 && !reflect.DeepEqual(networks[i], virtualMachineNetworksParsed[i]) {
// Update the network configuration
log.Log.Info(fmt.Sprintf("Updating the network configuration for net%d of VM %s", i, vm.Spec.Name))
// Get the network model&bridge name
Expand Down

0 comments on commit 4880f68

Please sign in to comment.