diff --git a/pkg/sync/inventory.go b/pkg/sync/inventory.go index 647a0de..3fcacae 100644 --- a/pkg/sync/inventory.go +++ b/pkg/sync/inventory.go @@ -97,6 +97,7 @@ type Inventory struct { //internal resourcesMap *OrderedMap[bool] + topOrder []string requiredMap map[string]*OrderedMap[bool] dependencyMap map[string]*OrderedMap[bool] @@ -135,7 +136,7 @@ func (i *Inventory) GetResourcesMap() *OrderedMap[bool] { // GetResourcesOrder returns the order of resources in the inventory. func (i *Inventory) GetResourcesOrder() []string { - return i.resourcesMap.Keys() + return i.topOrder } // GetRequiredMap returns the required map, which represents the dependencies between resources in the Inventory. @@ -260,6 +261,7 @@ func (i *Inventory) buildResourcesGraph() error { order[y], order[j] = order[j], order[y] } + i.topOrder = order i.resourcesMap.OrderBy(order) return nil diff --git a/pkg/sync/resource.go b/pkg/sync/resource.go index bcdaf7c..2c5b701 100644 --- a/pkg/sync/resource.go +++ b/pkg/sync/resource.go @@ -271,6 +271,22 @@ func (m *OrderedMap[T]) Keys() []string { // OrderBy updates the order of keys in the [OrderedMap] based on the orderList. func (m *OrderedMap[T]) OrderBy(orderList []string) { var newKeys []string + var remainingKeys []string + +keysLoop: + for _, key := range m.keys { + isInOrderList := false + for _, orderKey := range orderList { + if key == orderKey { + isInOrderList = true + continue keysLoop + } + } + + if !isInOrderList { + remainingKeys = append(remainingKeys, key) + } + } for _, item := range orderList { _, ok := m.Get(item) @@ -279,6 +295,7 @@ func (m *OrderedMap[T]) OrderBy(orderList []string) { } } + newKeys = append(newKeys, remainingKeys...) m.keys = newKeys }