fix(discovery): Handle cache.DeletedFinalStateUnknown in node informers' DeleteFunc

Signed-off-by: machine424 <ayoubmrini424@gmail.com>
This commit is contained in:
machine424 2024-10-15 20:29:07 +02:00
parent d4b1f9eb33
commit b1c356beea
No known key found for this signature in database
GPG key ID: A4B001A4FDEE017D
6 changed files with 43 additions and 7 deletions

View file

@ -167,8 +167,11 @@ func NewEndpoints(l *slog.Logger, eps cache.SharedIndexInformer, svc, pod, node
e.enqueueNode(node.Name) e.enqueueNode(node.Name)
}, },
DeleteFunc: func(o interface{}) { DeleteFunc: func(o interface{}) {
node := o.(*apiv1.Node) nodeName, err := nodeName(o)
e.enqueueNode(node.Name) if err != nil {
l.Error("Error getting Node name", "err", err)
}
e.enqueueNode(nodeName)
}, },
}) })
if err != nil { if err != nil {

View file

@ -145,8 +145,11 @@ func NewEndpointSlice(l *slog.Logger, eps cache.SharedIndexInformer, svc, pod, n
e.enqueueNode(node.Name) e.enqueueNode(node.Name)
}, },
DeleteFunc: func(o interface{}) { DeleteFunc: func(o interface{}) {
node := o.(*apiv1.Node) nodeName, err := nodeName(o)
e.enqueueNode(node.Name) if err != nil {
l.Error("Error getting Node name", "err", err)
}
e.enqueueNode(nodeName)
}, },
}) })
if err != nil { if err != nil {

View file

@ -804,3 +804,13 @@ func addObjectMetaLabels(labelSet model.LabelSet, objectMeta metav1.ObjectMeta,
func namespacedName(namespace, name string) string { func namespacedName(namespace, name string) string {
return namespace + "/" + name return namespace + "/" + name
} }
// nodeName knows how to handle the cache.DeletedFinalStateUnknown tombstone.
// It assumes the MetaNamespaceKeyFunc keyFunc is used, which uses the node name as the tombstone key.
func nodeName(o interface{}) (string, error) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(o)
if err != nil {
return "", err
}
return key, nil
}

View file

@ -23,7 +23,9 @@ import (
prom_testutil "github.com/prometheus/client_golang/prometheus/testutil" prom_testutil "github.com/prometheus/client_golang/prometheus/testutil"
"github.com/prometheus/common/promslog" "github.com/prometheus/common/promslog"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
apiv1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/version" "k8s.io/apimachinery/pkg/version"
"k8s.io/apimachinery/pkg/watch" "k8s.io/apimachinery/pkg/watch"
@ -320,3 +322,18 @@ func TestFailuresCountMetric(t *testing.T) {
}) })
} }
} }
func TestNodeName(t *testing.T) {
node := &apiv1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
}
name, err := nodeName(node)
require.NoError(t, err)
require.Equal(t, "foo", name)
name, err = nodeName(cache.DeletedFinalStateUnknown{Key: "bar"})
require.NoError(t, err)
require.Equal(t, "bar", name)
}

View file

@ -82,7 +82,7 @@ func NewNode(l *slog.Logger, inf cache.SharedInformer, eventCount *prometheus.Co
} }
func (n *Node) enqueue(obj interface{}) { func (n *Node) enqueue(obj interface{}) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj) key, err := nodeName(obj)
if err != nil { if err != nil {
return return
} }

View file

@ -95,8 +95,11 @@ func NewPod(l *slog.Logger, pods cache.SharedIndexInformer, nodes cache.SharedIn
p.enqueuePodsForNode(node.Name) p.enqueuePodsForNode(node.Name)
}, },
DeleteFunc: func(o interface{}) { DeleteFunc: func(o interface{}) {
node := o.(*apiv1.Node) nodeName, err := nodeName(o)
p.enqueuePodsForNode(node.Name) if err != nil {
l.Error("Error getting Node name", "err", err)
}
p.enqueuePodsForNode(nodeName)
}, },
}) })
if err != nil { if err != nil {