diff --git a/controller/cache/cluster.go b/controller/cache/cluster.go index 717b476ab3..c887e8c75b 100644 --- a/controller/cache/cluster.go +++ b/controller/cache/cluster.go @@ -87,14 +87,6 @@ func (c *clusterInfo) replaceResourceCache(gk schema.GroupKind, resourceVersion func (c *clusterInfo) createObjInfo(un *unstructured.Unstructured, appInstanceLabel string) *node { ownerRefs := un.GetOwnerReferences() - // Special case for endpoint. Remove after https://github.com/kubernetes/kubernetes/issues/28483 is fixed - if un.GroupVersionKind().Group == "" && un.GetKind() == kube.EndpointsKind && len(un.GetOwnerReferences()) == 0 { - ownerRefs = append(ownerRefs, metav1.OwnerReference{ - Name: un.GetName(), - Kind: kube.ServiceKind, - APIVersion: "", - }) - } nodeInfo := &node{ resourceVersion: un.GetResourceVersion(), ref: kube.GetObjectRef(un), diff --git a/controller/cache/node.go b/controller/cache/node.go index 6326f0dad6..56b709cfc4 100644 --- a/controller/cache/node.go +++ b/controller/cache/node.go @@ -35,6 +35,11 @@ func (n *node) resourceKey() kube.ResourceKey { } func (n *node) isParentOf(child *node) bool { + // Special case for endpoint. Remove after https://github.com/kubernetes/kubernetes/issues/28483 is fixed + if len(child.ownerRefs) == 0 && child.ref.APIVersion == "v1" && child.ref.Kind == kube.EndpointsKind && n.ref.APIVersion == "v1" && n.ref.Kind == kube.ServiceKind && n.ref.Name == child.ref.Name { + child.ownerRefs = []metav1.OwnerReference{{Name: n.ref.Name, Kind: n.ref.Kind, APIVersion: n.ref.APIVersion, UID: n.ref.UID}} + } + for _, ownerRef := range child.ownerRefs { if n.ref.UID == ownerRef.UID { return true diff --git a/controller/cache/node_test.go b/controller/cache/node_test.go index 9812601fd8..67a6d268eb 100644 --- a/controller/cache/node_test.go +++ b/controller/cache/node_test.go @@ -30,3 +30,26 @@ func TestIsParentOfSameKindDifferentGroupAndUID(t *testing.T) { assert.False(t, invalidParent.isParentOf(child)) } + +func TestIsServiceParentOfEndPointWithTheSameName(t *testing.T) { + nonMatchingNameEndPoint := c.createObjInfo(strToUnstructured(` +apiVersion: v1 +kind: Endpoints +metadata: + name: not-matching-name + namespace: default +`), "") + + matchingNameEndPoint := c.createObjInfo(strToUnstructured(` +apiVersion: v1 +kind: Endpoints +metadata: + name: helm-guestbook + namespace: default +`), "") + + parent := c.createObjInfo(testService, "") + + assert.True(t, parent.isParentOf(matchingNameEndPoint)) + assert.False(t, parent.isParentOf(nonMatchingNameEndPoint)) +}