Skip to content

Commit

Permalink
Add some metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
alperencelik committed Nov 22, 2023
1 parent 0f181af commit 4f3c651
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

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 Down Expand Up @@ -85,6 +86,7 @@ func (r *ManagedVirtualMachineReconciler) Reconcile(ctx context.Context, req ctr
// Create the event
kubernetes.CreateManagedVMKubernetesEvent(managedVM, Clientset, "Deleting")
proxmox.DeleteVM(managedVM.Name, managedVM.Spec.NodeName)
metrics.DecManagedVirtualMachineCount()
}
// Delete VM from Proxmox

Expand Down Expand Up @@ -133,6 +135,7 @@ func (r *ManagedVirtualMachineReconciler) SetupWithManager(mgr ctrl.Manager) err
log.Log.Info(fmt.Sprintf("ManagedVM %v could not be created", ManagedVM))
}
}
metrics.IncManagedVirtualMachineCount()
}

return ctrl.NewControllerManagedBy(mgr).
Expand Down
3 changes: 3 additions & 0 deletions internal/controller/proxmox/virtualmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

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 Down Expand Up @@ -88,6 +89,7 @@ func (r *VirtualMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reque
kubernetes.CreateVMKubernetesEvent(vm, kubernetes.Clientset, "Deleting")
proxmox.DeleteVM(vm.Spec.Name, vm.Spec.NodeName)
processedResources[deletionKey] = true
metrics.DecVirtualMachineCount()
}
// Remove finalizer
controllerutil.RemoveFinalizer(vm, virtualMachineFinalizerName)
Expand Down Expand Up @@ -117,6 +119,7 @@ func (r *VirtualMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reque
Log.Info(fmt.Sprintf("VirtualMachine %s already exists and running", vmName))
// Mark it as processed
processedResources[resourceKey] = true
metrics.IncVirtualMachineCount()
}
proxmox.UpdateVM(vmName, nodeName, vm)
err = r.Update(context.Background(), vm)
Expand Down
74 changes: 74 additions & 0 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package metrics

import (
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)

var (
virtualMachineCount = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "kubemox_virtual_machine_count",
Help: "Number of virtualMachines registered in the cluster",
})
virtualMachineCPUCores = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "kubemox_virtual_machine_cpu_cores",
Help: "Number of CPU cores of virtualMachine",
}, []string{"name", "namespace"})
virtualMachineMemory = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "kubemox_virtual_machine_memory",
Help: "Memory of virtualMachine as MB",
}, []string{"name", "namespace"})
ManagedVirtualMachineCount = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "kubemox_managed_virtual_machine_count",
Help: "Number of managedVirtualMachines exists in Proxmox",
})
ManagedVirtualMachineCPUCores = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "kubemox_managed_virtual_machine_cpu_cores",
Help: "Number of CPU cores of managedVirtualMachine",
}, []string{"name", "namespace"})
ManagedVirtualMachineMemory = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "kubemox_managed_virtual_machine_memory",
Help: "Memory of managedVirtualMachine as MB",
}, []string{"name", "namespace"})
)

func init() {
metrics.Registry.MustRegister(virtualMachineCount)
metrics.Registry.MustRegister(virtualMachineCPUCores)
metrics.Registry.MustRegister(virtualMachineMemory)
metrics.Registry.MustRegister(ManagedVirtualMachineCount)
metrics.Registry.MustRegister(ManagedVirtualMachineCPUCores)
metrics.Registry.MustRegister(ManagedVirtualMachineMemory)
}

func IncVirtualMachineCount() {
virtualMachineCount.Inc()
}

func DecVirtualMachineCount() {
virtualMachineCount.Dec()
}

func SetVirtualMachineCPUCores(name, namespace string, cores float64) {
virtualMachineCPUCores.WithLabelValues(name, namespace).Set(cores)
}

func SetVirtualMachineMemory(name, namespace string, memory float64) {
virtualMachineMemory.WithLabelValues(name, namespace).Set(memory)
}

func IncManagedVirtualMachineCount() {
ManagedVirtualMachineCount.Inc()
}

func DecManagedVirtualMachineCount() {
ManagedVirtualMachineCount.Dec()
}

func SetManagedVirtualMachineCPUCores(name, namespace string, cores float64) {
ManagedVirtualMachineCPUCores.WithLabelValues(name, namespace).Set(cores)
}

func SetManagedVirtualMachineMemory(name, namespace string, memory float64) {
ManagedVirtualMachineMemory.WithLabelValues(name, namespace).Set(memory)
}

0 comments on commit 4f3c651

Please sign in to comment.