Skip to content

Commit d590fe5

Browse files
chore: Cleanup IterateHierarchy v1 (#748)
* chore: Cleanup IterateHierarchy v1 Part of argoproj/argo-cd#23854 Remove the unused code in gitops-engine. Signed-off-by: Andrii Korotkov <[email protected]> * Revert some deleted tests Signed-off-by: Andrii Korotkov <[email protected]> --------- Signed-off-by: Andrii Korotkov <[email protected]>
1 parent 093aef0 commit d590fe5

File tree

5 files changed

+2
-176
lines changed

5 files changed

+2
-176
lines changed

pkg/cache/cluster.go

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"runtime/debug"
7-
"sort"
87
"strings"
98
"sync"
109
"time"
@@ -130,9 +129,6 @@ type ClusterCache interface {
130129
Invalidate(opts ...UpdateSettingsFunc)
131130
// FindResources returns resources that matches given list of predicates from specified namespace or everywhere if specified namespace is empty
132131
FindResources(namespace string, predicates ...func(r *Resource) bool) map[kube.ResourceKey]*Resource
133-
// IterateHierarchy iterates resource tree starting from the specified top level resource and executes callback for each resource in the tree.
134-
// The action callback returns true if iteration should continue and false otherwise.
135-
IterateHierarchy(key kube.ResourceKey, action func(resource *Resource, namespaceResources map[kube.ResourceKey]*Resource) bool)
136132
// IterateHierarchyV2 iterates resource tree starting from the specified top level resources and executes callback for each resource in the tree.
137133
// The action callback returns true if iteration should continue and false otherwise.
138134
IterateHierarchyV2(keys []kube.ResourceKey, action func(resource *Resource, namespaceResources map[kube.ResourceKey]*Resource) bool)
@@ -1055,46 +1051,6 @@ func (c *clusterCache) FindResources(namespace string, predicates ...func(r *Res
10551051
return result
10561052
}
10571053

1058-
// IterateHierarchy iterates resource tree starting from the specified top level resource and executes callback for each resource in the tree
1059-
func (c *clusterCache) IterateHierarchy(key kube.ResourceKey, action func(resource *Resource, namespaceResources map[kube.ResourceKey]*Resource) bool) {
1060-
c.lock.RLock()
1061-
defer c.lock.RUnlock()
1062-
if res, ok := c.resources[key]; ok {
1063-
nsNodes := c.nsIndex[key.Namespace]
1064-
if !action(res, nsNodes) {
1065-
return
1066-
}
1067-
childrenByUID := make(map[types.UID][]*Resource)
1068-
for _, child := range nsNodes {
1069-
if res.isParentOf(child) {
1070-
childrenByUID[child.Ref.UID] = append(childrenByUID[child.Ref.UID], child)
1071-
}
1072-
}
1073-
// make sure children has no duplicates
1074-
for _, children := range childrenByUID {
1075-
if len(children) > 0 {
1076-
// The object might have multiple children with the same UID (e.g. replicaset from apps and extensions group). It is ok to pick any object but we need to make sure
1077-
// we pick the same child after every refresh.
1078-
sort.Slice(children, func(i, j int) bool {
1079-
key1 := children[i].ResourceKey()
1080-
key2 := children[j].ResourceKey()
1081-
return strings.Compare(key1.String(), key2.String()) < 0
1082-
})
1083-
child := children[0]
1084-
if action(child, nsNodes) {
1085-
child.iterateChildren(nsNodes, map[kube.ResourceKey]bool{res.ResourceKey(): true}, func(err error, child *Resource, namespaceResources map[kube.ResourceKey]*Resource) bool {
1086-
if err != nil {
1087-
c.log.V(2).Info(err.Error())
1088-
return false
1089-
}
1090-
return action(child, namespaceResources)
1091-
})
1092-
}
1093-
}
1094-
}
1095-
}
1096-
}
1097-
10981054
// IterateHierarchy iterates resource tree starting from the specified top level resources and executes callback for each resource in the tree
10991055
func (c *clusterCache) IterateHierarchyV2(keys []kube.ResourceKey, action func(resource *Resource, namespaceResources map[kube.ResourceKey]*Resource) bool) {
11001056
c.lock.RLock()

pkg/cache/cluster_test.go

Lines changed: 1 addition & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (c *clusterCache) WithAPIResources(newApiResources []kube.APIResourceInfo)
140140

141141
func getChildren(cluster *clusterCache, un *unstructured.Unstructured) []*Resource {
142142
hierarchy := make([]*Resource, 0)
143-
cluster.IterateHierarchy(kube.GetResourceKey(un), func(child *Resource, _ map[kube.ResourceKey]*Resource) bool {
143+
cluster.IterateHierarchyV2([]kube.ResourceKey{kube.GetResourceKey(un)}, func(child *Resource, _ map[kube.ResourceKey]*Resource) bool {
144144
hierarchy = append(hierarchy, child)
145145
return true
146146
})
@@ -1045,92 +1045,6 @@ func testDeploy() *appsv1.Deployment {
10451045
}
10461046
}
10471047

1048-
func TestIterateHierachy(t *testing.T) {
1049-
cluster := newCluster(t, testPod1(), testPod2(), testRS(), testExtensionsRS(), testDeploy())
1050-
err := cluster.EnsureSynced()
1051-
require.NoError(t, err)
1052-
1053-
t.Run("IterateAll", func(t *testing.T) {
1054-
keys := []kube.ResourceKey{}
1055-
cluster.IterateHierarchy(kube.GetResourceKey(mustToUnstructured(testDeploy())), func(child *Resource, _ map[kube.ResourceKey]*Resource) bool {
1056-
keys = append(keys, child.ResourceKey())
1057-
return true
1058-
})
1059-
1060-
assert.ElementsMatch(t,
1061-
[]kube.ResourceKey{
1062-
kube.GetResourceKey(mustToUnstructured(testPod1())),
1063-
kube.GetResourceKey(mustToUnstructured(testPod2())),
1064-
kube.GetResourceKey(mustToUnstructured(testRS())),
1065-
kube.GetResourceKey(mustToUnstructured(testDeploy())),
1066-
},
1067-
keys)
1068-
})
1069-
1070-
t.Run("ExitAtRoot", func(t *testing.T) {
1071-
keys := []kube.ResourceKey{}
1072-
cluster.IterateHierarchy(kube.GetResourceKey(mustToUnstructured(testDeploy())), func(child *Resource, _ map[kube.ResourceKey]*Resource) bool {
1073-
keys = append(keys, child.ResourceKey())
1074-
return false
1075-
})
1076-
1077-
assert.ElementsMatch(t,
1078-
[]kube.ResourceKey{
1079-
kube.GetResourceKey(mustToUnstructured(testDeploy())),
1080-
},
1081-
keys)
1082-
})
1083-
1084-
t.Run("ExitAtSecondLevelChild", func(t *testing.T) {
1085-
keys := []kube.ResourceKey{}
1086-
cluster.IterateHierarchy(kube.GetResourceKey(mustToUnstructured(testDeploy())), func(child *Resource, _ map[kube.ResourceKey]*Resource) bool {
1087-
keys = append(keys, child.ResourceKey())
1088-
return child.ResourceKey().Kind != kube.ReplicaSetKind
1089-
})
1090-
1091-
assert.ElementsMatch(t,
1092-
[]kube.ResourceKey{
1093-
kube.GetResourceKey(mustToUnstructured(testDeploy())),
1094-
kube.GetResourceKey(mustToUnstructured(testRS())),
1095-
},
1096-
keys)
1097-
})
1098-
1099-
t.Run("ExitAtThirdLevelChild", func(t *testing.T) {
1100-
keys := []kube.ResourceKey{}
1101-
cluster.IterateHierarchy(kube.GetResourceKey(mustToUnstructured(testDeploy())), func(child *Resource, _ map[kube.ResourceKey]*Resource) bool {
1102-
keys = append(keys, child.ResourceKey())
1103-
return child.ResourceKey().Kind != kube.PodKind
1104-
})
1105-
1106-
assert.ElementsMatch(t,
1107-
[]kube.ResourceKey{
1108-
kube.GetResourceKey(mustToUnstructured(testDeploy())),
1109-
kube.GetResourceKey(mustToUnstructured(testRS())),
1110-
kube.GetResourceKey(mustToUnstructured(testPod1())),
1111-
kube.GetResourceKey(mustToUnstructured(testPod2())),
1112-
},
1113-
keys)
1114-
})
1115-
1116-
// After uid is backfilled for owner of pod2, it should appear in results here as well.
1117-
t.Run("IterateStartFromExtensionsRS", func(t *testing.T) {
1118-
keys := []kube.ResourceKey{}
1119-
cluster.IterateHierarchy(kube.GetResourceKey(mustToUnstructured(testExtensionsRS())), func(child *Resource, _ map[kube.ResourceKey]*Resource) bool {
1120-
keys = append(keys, child.ResourceKey())
1121-
return true
1122-
})
1123-
1124-
assert.ElementsMatch(t,
1125-
[]kube.ResourceKey{
1126-
kube.GetResourceKey(mustToUnstructured(testPod1())),
1127-
kube.GetResourceKey(mustToUnstructured(testPod2())),
1128-
kube.GetResourceKey(mustToUnstructured(testExtensionsRS())),
1129-
},
1130-
keys)
1131-
})
1132-
}
1133-
11341048
func TestIterateHierachyV2(t *testing.T) {
11351049
cluster := newCluster(t, testPod1(), testPod2(), testRS(), testExtensionsRS(), testDeploy())
11361050
err := cluster.EnsureSynced()
@@ -1378,18 +1292,3 @@ func BenchmarkIterateHierarchyV2(b *testing.B) {
13781292
})
13791293
}
13801294
}
1381-
1382-
// func BenchmarkIterateHierarchy(b *testing.B) {
1383-
// cluster := newCluster(b)
1384-
// for _, resource := range testResources {
1385-
// cluster.setNode(resource)
1386-
// }
1387-
// b.ResetTimer()
1388-
// for n := 0; n < b.N; n++ {
1389-
// cluster.IterateHierarchy(kube.ResourceKey{
1390-
// Namespace: "default", Name: "test-1", Kind: "Pod",
1391-
// }, func(child *Resource, _ map[kube.ResourceKey]*Resource) bool {
1392-
// return true
1393-
// })
1394-
// }
1395-
//}

pkg/cache/mocks/ClusterCache.go

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cache/predicates_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func ExampleNewClusterCache_inspectNamespaceResources() {
115115
}
116116
// Iterate default namespace resources tree
117117
for _, root := range clusterCache.FindResources("default", TopLevelResource) {
118-
clusterCache.IterateHierarchy(root.ResourceKey(), func(resource *Resource, _ map[kube.ResourceKey]*Resource) bool {
118+
clusterCache.IterateHierarchyV2([]kube.ResourceKey{root.ResourceKey()}, func(resource *Resource, _ map[kube.ResourceKey]*Resource) bool {
119119
fmt.Printf("resource: %s, info: %v\n", resource.Ref.String(), resource.Info)
120120
return true
121121
})

pkg/cache/resource.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,30 +75,6 @@ func (r *Resource) toOwnerRef() metav1.OwnerReference {
7575
return metav1.OwnerReference{UID: r.Ref.UID, Name: r.Ref.Name, Kind: r.Ref.Kind, APIVersion: r.Ref.APIVersion}
7676
}
7777

78-
func newResourceKeySet(set map[kube.ResourceKey]bool, keys ...kube.ResourceKey) map[kube.ResourceKey]bool {
79-
newSet := make(map[kube.ResourceKey]bool)
80-
for k, v := range set {
81-
newSet[k] = v
82-
}
83-
for i := range keys {
84-
newSet[keys[i]] = true
85-
}
86-
return newSet
87-
}
88-
89-
func (r *Resource) iterateChildren(ns map[kube.ResourceKey]*Resource, parents map[kube.ResourceKey]bool, action func(err error, child *Resource, namespaceResources map[kube.ResourceKey]*Resource) bool) {
90-
for childKey, child := range ns {
91-
if r.isParentOf(ns[childKey]) {
92-
if parents[childKey] {
93-
key := r.ResourceKey()
94-
_ = action(fmt.Errorf("circular dependency detected. %s is child and parent of %s", childKey.String(), key.String()), child, ns)
95-
} else if action(nil, child, ns) {
96-
child.iterateChildren(ns, newResourceKeySet(parents, r.ResourceKey()), action)
97-
}
98-
}
99-
}
100-
}
101-
10278
// iterateChildrenV2 is a depth-first traversal of the graph of resources starting from the current resource.
10379
func (r *Resource) iterateChildrenV2(graph map[kube.ResourceKey]map[types.UID]*Resource, ns map[kube.ResourceKey]*Resource, visited map[kube.ResourceKey]int, action func(err error, child *Resource, namespaceResources map[kube.ResourceKey]*Resource) bool) {
10480
key := r.ResourceKey()

0 commit comments

Comments
 (0)