Issue #1374 - Add k8s objects circular dependency protection to getApp method (#1379)

This commit is contained in:
Alexander Matyushentsev 2019-04-04 17:52:30 -07:00 committed by GitHub
parent ac7906fdea
commit 86f6b657e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View file

@ -375,6 +375,11 @@ func TestCircularReference(t *testing.T) {
children := getChildren(cluster, dep)
assert.Len(t, children, 2)
node := cluster.nodes[kube.GetResourceKey(dep)]
assert.NotNil(t, node)
app := node.getApp(cluster.nodes)
assert.Equal(t, "", app)
}
func TestWatchCacheUpdated(t *testing.T) {

View file

@ -53,13 +53,24 @@ func ownerRefGV(ownerRef metav1.OwnerReference) schema.GroupVersion {
}
func (n *node) getApp(ns map[kube.ResourceKey]*node) string {
return n.getAppRecursive(ns, map[kube.ResourceKey]bool{})
}
func (n *node) getAppRecursive(ns map[kube.ResourceKey]*node, visited map[kube.ResourceKey]bool) string {
if !visited[n.resourceKey()] {
visited[n.resourceKey()] = true
} else {
log.Warnf("Circular dependency detected: %v.", visited)
return n.appName
}
if n.appName != "" {
return n.appName
}
for _, ownerRef := range n.ownerRefs {
gv := ownerRefGV(ownerRef)
if parent, ok := ns[kube.NewResourceKey(gv.Group, ownerRef.Kind, n.ref.Namespace, ownerRef.Name)]; ok {
app := parent.getApp(ns)
app := parent.getAppRecursive(ns, visited)
if app != "" {
return app
}