Repository: vllm-project/aibrix
File: pkg/cache/informers.go (Go)

Code before:
		}
	}

	// ignore worker pods
	oldNodeType := oldPod.Labels[nodeType]
	newNodeType := newPod.Labels[nodeType]
	if oldNodeType == nodeWorker || newNodeType == nodeWorker {
		klog.InfoS("ignored ray worker pod", "old pod", oldPod.Name, "new pod", newPod.Name)
		return
	}

	// Add new mappings if present
	if newOk {
		metaPod := c.addPodLocked(newPod)
		c.addPodAndModelMappingLocked(metaPod, newModelName)
	}

	klog.V(4).Infof("POD UPDATED: %s/%s %s", newPod.Namespace, newPod.Name, newPod.Status.Phase)
	c.debugInfo()
}

func (c *Store) deletePod(obj interface{}) {
	var namespace, name string
	pod, ok := obj.(*v1.Pod)
	if !ok {
		tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
		if !ok {
			return
		}
		pod, ok = tombstone.Obj.(*v1.Pod)
		if !ok {
			// We can usually ignore cases where the tombstone isn't a *v1.Pod. However,
			// since the following logic in this function still works fine with just the namespace and name,
			// let's try parsing the tombstone.Key here for added robustness.
			var err error
			namespace, name, err = cache.SplitMetaNamespaceKey(tombstone.Key)
			if err != nil {
				klog.ErrorS(err, "couldn't get pod's namespace and name from tombstone", "key", tombstone.Key)
				return
			}
		}
	}
	var hasModelLabel bool
	if pod != nil {
		namespace, name = pod.Namespace, pod.Name
		_, hasModelLabel = pod.Labels[modelIdentifier]
	}

	_, existed := c.metaPods.Load(name)
	if !hasModelLabel && !existed {
		return

Review comment (performance):
that's a good catch. I am considering whether it's better to use switch case for `cache.DeletedFinalStateUnknown` and `v1.Pod` which makes the code cleaner?

something like this. otherwise the v1.pod-> cache.DeletedFinalStateUnknown -> v1.pod chain is kind of messy.

```
switch obj := obj.(type) {
case *v1.Pod:
	namespace, name = obj.Namespace, obj.Name
	_, hasModelLabel = obj.Labels[modelIdentifier]

case cache.DeletedFinalStateUnknown:
	if pod, ok := obj.Obj.(*v1.Pod); ok {
		namespace, name = pod.Namespace, pod.Name
		_, hasModelLabel = pod.Labels[modelIdentifier]

```

Code after:
		}
	}

	// ignore worker pods
	oldNodeType := oldPod.Labels[nodeType]
	newNodeType := newPod.Labels[nodeType]
	if oldNodeType == nodeWorker || newNodeType == nodeWorker {
		klog.InfoS("ignored ray worker pod", "old pod", oldPod.Name, "new pod", newPod.Name)
		return
	}

	// Add new mappings if present
	if newOk {
		metaPod := c.addPodLocked(newPod)
		c.addPodAndModelMappingLocked(metaPod, newModelName)
	}

	klog.V(4).Infof("POD UPDATED: %s/%s %s", newPod.Namespace, newPod.Name, newPod.Status.Phase)
	c.debugInfo()
}

func (c *Store) deletePod(obj interface{}) {
	var namespace, name string
	var hasModelLabel bool
	switch obj := obj.(type) {
	case *v1.Pod:
		namespace, name = obj.Namespace, obj.Name
		_, hasModelLabel = obj.Labels[modelIdentifier]
	case cache.DeletedFinalStateUnknown:
		if pod, ok := obj.Obj.(*v1.Pod); ok {
			namespace, name = pod.Namespace, pod.Name
			_, hasModelLabel = pod.Labels[modelIdentifier]
			break
		}

		// We can usually ignore cases where the object contained in the tombstone isn't a *v1.Pod. However,
		// since the following logic in this function still works fine with just the namespace and name,
		// let's try parsing the tombstone.Key here for added robustness.
		var err error
		namespace, name, err = cache.SplitMetaNamespaceKey(obj.Key)
		if err != nil {
			klog.ErrorS(err, "couldn't get pod's namespace and name from tombstone", "key", obj.Key)
			return
		}
	}

	_, existed := c.metaPods.Load(name)
	if !hasModelLabel && !existed {
		return
	}