Skip to content

Commit

Permalink
Include cluster scoped resources while handling resource updates
Browse files Browse the repository at this point in the history
Signed-off-by: Chetan Banavikalmutt <chetanrns1997@gmail.com>
  • Loading branch information
chetan-rns committed Nov 3, 2023
1 parent 1c10a44 commit 78e42e9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 10 deletions.
43 changes: 33 additions & 10 deletions pkg/cache/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type OnEventHandler func(event watch.EventType, un *unstructured.Unstructured)
type OnPopulateResourceInfoHandler func(un *unstructured.Unstructured, isRoot bool) (info interface{}, cacheManifest bool)

// OnResourceUpdatedHandler handlers resource update event
type OnResourceUpdatedHandler func(newRes *Resource, oldRes *Resource, namespaceResources map[kube.ResourceKey]*Resource)
type OnResourceUpdatedHandler func(newRes *Resource, oldRes *Resource, resources map[kube.ResourceKey]*Resource)
type Unsubscribe func()

type ClusterCache interface {
Expand Down Expand Up @@ -405,6 +405,7 @@ func (c *clusterCache) newResource(un *unstructured.Unstructured) *Resource {
}

func (c *clusterCache) setNode(n *Resource) {
c.updateChildrenByParentMap(n)
key := n.ResourceKey()
c.resources[key] = n
ns, ok := c.nsIndex[key.Namespace]
Expand Down Expand Up @@ -983,11 +984,13 @@ func (c *clusterCache) IterateHierarchy(key kube.ResourceKey, action func(resour
nsNodes := c.nsIndex[key.Namespace]
if key.Namespace == "" && c.showClusterResourceChildren {
childRefs := c.childrenByParent[key]
childResources := map[kube.ResourceKey]*Resource{}
for _, childRef := range childRefs {
if c.resources[childRef] != nil {
nsNodes[childRef] = c.resources[childRef]
childResources[childRef] = c.resources[childRef]
}
}
nsNodes = mergeResourceMaps(nsNodes, childResources)
}
if !action(res, nsNodes) {
return
Expand Down Expand Up @@ -1149,13 +1152,25 @@ func (c *clusterCache) processEvent(event watch.EventType, un *unstructured.Unst
}

func (c *clusterCache) onNodeUpdated(oldRes *Resource, newRes *Resource) {
c.updateChildrenByParentMap(newRes)
c.setNode(newRes)
for _, h := range c.getResourceUpdatedHandlers() {
h(newRes, oldRes, c.nsIndex[newRes.Ref.Namespace])
h(newRes, oldRes, getNSResourcesWithClusterReources(c, newRes.Ref.Namespace))
}
}

// Returns all resources in a particular namespace. It also includes cluster scoped resources
// if showClusterResourceChildren is enabled
func getNSResourcesWithClusterReources(c *clusterCache, ns string) map[kube.ResourceKey]*Resource {
resources := c.nsIndex[ns]
if c.showClusterResourceChildren {
// We include cluster scoped resources because a namespace scoped resource could be owned
// either by a cluster scoped or a namespace scoped parent.
resources = mergeResourceMaps(resources, c.nsIndex[""])
}

return resources
}

func (c *clusterCache) onNodeRemoved(key kube.ResourceKey) {
c.removeFromChildrenByParentMap(key)
existing, ok := c.resources[key]
Expand All @@ -1168,11 +1183,6 @@ func (c *clusterCache) onNodeRemoved(key kube.ResourceKey) {
delete(c.nsIndex, key.Namespace)
}

nsAll, ok := c.nsIndex[""]
if ok {
delete(nsAll, key)
}

// remove ownership references from children with inferred references
if existing.isInferredParentOf != nil {
for k, v := range ns {
Expand All @@ -1183,11 +1193,24 @@ func (c *clusterCache) onNodeRemoved(key kube.ResourceKey) {
}
}
for _, h := range c.getResourceUpdatedHandlers() {
h(nil, existing, ns)
h(nil, existing, getNSResourcesWithClusterReources(c, key.Namespace))
}
}
}

func mergeResourceMaps(a, b map[kube.ResourceKey]*Resource) map[kube.ResourceKey]*Resource {
mergedMap := map[kube.ResourceKey]*Resource{}
for k, v := range a {
mergedMap[k] = v
}

for k, v := range b {
mergedMap[k] = v
}

return mergedMap
}

func (c *clusterCache) updateChildrenByParentMap(res *Resource) {
if res == nil || !c.showClusterResourceChildren {
return
Expand Down
37 changes: 37 additions & 0 deletions pkg/cache/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,43 @@ func TestIterateHierachy(t *testing.T) {
})
}

func TestMergeResourceMaps(t *testing.T) {
mapA := map[kube.ResourceKey]*Resource{
kube.GetResourceKey(kube.MustToUnstructured(testPod())): {
ResourceVersion: "resource-a",
},
kube.GetResourceKey(kube.MustToUnstructured(testClusterRole())): {
ResourceVersion: "resource-b",
},
}
mapB := map[kube.ResourceKey]*Resource{
kube.GetResourceKey(kube.MustToUnstructured(testDeploy())): {
ResourceVersion: "resource-c",
},
}

mergedMap := mergeResourceMaps(mapA, mapB)
assert.Equal(t, len(mapA)+len(mapB), len(mergedMap))

containsKeysAndValues := func(t *testing.T, testMap map[kube.ResourceKey]*Resource) {
t.Helper()
for k, expectedVal := range testMap {
val, ok := mergedMap[k]
assert.True(t, ok, "expected key to be present in map", k)
assert.Equal(t, expectedVal, val)
}
}

containsKeysAndValues(t, mapA)
containsKeysAndValues(t, mapB)

// update the merged map and verify that the original map doesn't change
testKey := kube.GetResourceKey(kube.MustToUnstructured(testPod()))
expectedVal := mapA[testKey]
mergedMap[testKey] = nil
assert.Equal(t, expectedVal, mapA[testKey])
}

func testSA() *corev1.ServiceAccount {
return &corev1.ServiceAccount{
TypeMeta: metav1.TypeMeta{
Expand Down

0 comments on commit 78e42e9

Please sign in to comment.