Skip to content

Commit

Permalink
refactor vmi reconcile to pass codefactor check
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrokethecloud committed Sep 13, 2024
1 parent 45f4685 commit b6c8e04
Showing 1 changed file with 64 additions and 49 deletions.
113 changes: 64 additions & 49 deletions pkg/controller/virtualmachineinstance/virtualmachineinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,36 +117,11 @@ func (h *Handler) OnVMIChange(name string, vmi *kubevirtv1.VirtualMachineInstanc

// trackDevices reconciles GPU and HostDevices info
func (h *Handler) trackDevices(vmi *kubevirtv1.VirtualMachineInstance) error {
logrus.WithFields(logrus.Fields{
"name": vmi.Name,
"namespace": vmi.Namespace,
}).Debug("looking up pod associated with vmi")

pod, err := h.findPodForVMI(vmi)
if err != nil {
return err
}

logrus.WithFields(logrus.Fields{
"name": pod.Name,
"namespace": pod.Namespace,
}).Debug("looking up pod env")

podEnv, err := h.getPodEnv(pod)
if err != nil {
return err
}

envMap, err := convertEnvToMap(podEnv)
envMap, err := h.generatePodEnvMap(vmi)
if err != nil {
return err
}

logrus.WithFields(logrus.Fields{
"name": vmi.Name,
"namespace": vmi.Namespace,
}).Debugf("found envMap: %v", envMap)

var pciDeviceMap, vGPUMap map[string]string
selector := map[string]string{
"nodename": vmi.Status.NodeName,
Expand Down Expand Up @@ -199,29 +174,7 @@ func (h *Handler) trackDevices(vmi *kubevirtv1.VirtualMachineInstance) error {
return fmt.Errorf("error marshalling deviceDetails: %v", err)
}

logrus.WithFields(logrus.Fields{
"name": vmi.Name,
"namespace": vmi.Namespace,
}).Debugf("device allocation details: %s", deviceDetailsBytes)

vmObj, err := h.vmCache.Get(vmi.Namespace, vmi.Name)
if err != nil {
return fmt.Errorf("error fetching vm %s from cache: %v", vmi.Name, err)
}

var currentAnnotationValue string
// update device allocation details
if vmObj.Annotations == nil {
vmObj.Annotations = make(map[string]string)
} else {
currentAnnotationValue = vmObj.Annotations[DeviceAllocationKey]
}

if currentAnnotationValue != string(deviceDetailsBytes) {
vmObj.Annotations[DeviceAllocationKey] = string(deviceDetailsBytes)
_, err = h.vmClient.Update(vmObj)
}
return err
return h.reconcileVMResourceAllocationAnnotation(vmi, string(deviceDetailsBytes))
}

// findPodForVMI leverages the fact that each pod associated with a VMI a label vm.kubevirt.io/name: $vmName
Expand Down Expand Up @@ -351,3 +304,65 @@ func dedupDevices(deviceMap map[string][]string) map[string][]string {
}
return deviceMap
}

// generatePodEnvMap attempts to find the pod associated with vmi, exec into pod to fetch `env` output
// and converts the same to the map, to allow the controller to identify device allocated to pod by kubelet
// which can differ from the name in the vmi devices spec, since allocation is only performed by resourceName
func (h *Handler) generatePodEnvMap(vmi *kubevirtv1.VirtualMachineInstance) (map[string]string, error) {
logrus.WithFields(logrus.Fields{
"name": vmi.Name,
"namespace": vmi.Namespace,
}).Debug("looking up pod associated with vmi")

pod, err := h.findPodForVMI(vmi)
if err != nil {
return nil, err
}

logrus.WithFields(logrus.Fields{
"name": pod.Name,
"namespace": pod.Namespace,
}).Debug("looking up pod env")

podEnv, err := h.getPodEnv(pod)
if err != nil {
return nil, err
}

envMap, err := convertEnvToMap(podEnv)
if err != nil {
return nil, err
}

logrus.WithFields(logrus.Fields{
"name": vmi.Name,
"namespace": vmi.Namespace,
}).Debugf("found envMap: %v", envMap)
return envMap, nil
}

func (h *Handler) reconcileVMResourceAllocationAnnotation(vmi *kubevirtv1.VirtualMachineInstance, deviceDetails string) error {
logrus.WithFields(logrus.Fields{
"name": vmi.Name,
"namespace": vmi.Namespace,
}).Debugf("device allocation details: %s", deviceDetails)

vmObj, err := h.vmCache.Get(vmi.Namespace, vmi.Name)
if err != nil {
return fmt.Errorf("error fetching vm %s from cache: %v", vmi.Name, err)
}

var currentAnnotationValue string
// update device allocation details
if vmObj.Annotations == nil {
vmObj.Annotations = make(map[string]string)
} else {
currentAnnotationValue = vmObj.Annotations[DeviceAllocationKey]
}

if currentAnnotationValue != deviceDetails {
vmObj.Annotations[DeviceAllocationKey] = deviceDetails
_, err = h.vmClient.Update(vmObj)
}
return err
}

0 comments on commit b6c8e04

Please sign in to comment.