func()

in pkg/handler/warm.go [143:183]


func (w *warmResourceHandler) HandleDelete(pod *v1.Pod) (ctrl.Result, error) {
	resourcePool, err := w.getResourcePool(pod.Spec.NodeName)
	if err != nil {
		w.log.Error(err, "failed to find resource pool for node",
			"node", pod.Spec.NodeName)
		return ctrl.Result{}, nil
	}
	resourceID, present := pod.Annotations[w.resourceName]
	if !present {
		// When a Pod with TerminationGracePeriodSeconds set to 0 is created and
		// deleted immediately, the delete event doesnt' contain the resource
		// annotation, in such cases, query the data store to get the assigned resource
		resourceID, present = resourcePool.GetAssignedResource(string(pod.UID))
		if !present {
			return ctrl.Result{}, nil
		}
		w.log.Info("resource ID was not found in annotation, fetched from pool",
			"resource from data store", resourceID)
	}
	log := w.log.WithValues("UID", string(pod.UID), "namespace", pod.Namespace,
		"name", pod.Name, "resource id", resourceID)

	// Handle Delete can be invoked multiple times for same object. For instance
	// Once a Pod has Succeeded/Failed and once the object is actually deleted
	shouldReconcile, err := resourcePool.FreeResource(string(pod.UID), resourceID)
	if err != nil {
		if pod.Status.Phase == v1.PodSucceeded || pod.Status.Phase == v1.PodFailed {
			log.V(1).Info("failed to free resource, resource likely freed when pod succeed/failed")
			return ctrl.Result{}, nil
		}
		// Only Log the error, since this error is not retryable
		log.Error(err, "failed to free resource")
		return ctrl.Result{}, nil
	}

	w.reconcilePool(shouldReconcile, resourcePool)

	log.Info("successfully freed resource")

	return ctrl.Result{}, nil
}