-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: delete in reverse order of sync waves (#3959)
* feat: delete in reverse order of sync waves Signed-off-by: darshanime <[email protected]> * feat: add tests for deletion in order Signed-off-by: darshanime <[email protected]> * feat: fix lint for appcontroller.go Signed-off-by: darshanime <[email protected]> * feat: add comment to explain early return Signed-off-by: darshanime <[email protected]>
- Loading branch information
1 parent
dfd7457
commit 53a9222
Showing
3 changed files
with
90 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package controller | ||
|
||
import ( | ||
"sort" | ||
|
||
"github.com/argoproj/gitops-engine/pkg/sync/syncwaves" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
type syncWaveSorter []*unstructured.Unstructured | ||
|
||
func (s syncWaveSorter) Len() int { | ||
return len(s) | ||
} | ||
|
||
func (s syncWaveSorter) Swap(i, j int) { | ||
s[i], s[j] = s[j], s[i] | ||
} | ||
|
||
func (s syncWaveSorter) Less(i, j int) bool { | ||
return syncwaves.Wave(s[i]) < syncwaves.Wave(s[j]) | ||
} | ||
|
||
func FilterObjectsForDeletion(objs []*unstructured.Unstructured) []*unstructured.Unstructured { | ||
if len(objs) <= 1 { | ||
return objs | ||
} | ||
|
||
sort.Sort(sort.Reverse(syncWaveSorter(objs))) | ||
|
||
currentSyncWave := syncwaves.Wave(objs[0]) | ||
filteredObjs := make([]*unstructured.Unstructured, 0) | ||
for _, obj := range objs { | ||
if syncwaves.Wave(obj) != currentSyncWave { | ||
break | ||
} | ||
filteredObjs = append(filteredObjs, obj) | ||
} | ||
return filteredObjs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package controller | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/argoproj/gitops-engine/pkg/sync/common" | ||
. "github.com/argoproj/gitops-engine/pkg/utils/testing" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
func TestFilterObjectsForDeletion(t *testing.T) { | ||
tests := []struct { | ||
input []string | ||
want []string | ||
}{ | ||
{[]string{"1", "5", "7", "7", "4"}, []string{"7", "7"}}, | ||
{[]string{"1", "5", "2", "2", "4"}, []string{"5"}}, | ||
{[]string{"1"}, []string{"1"}}, | ||
{[]string{}, []string{}}, | ||
} | ||
for _, tt := range tests { | ||
in := sliceOfObjectsWithSyncWaves(tt.input) | ||
need := sliceOfObjectsWithSyncWaves(tt.want) | ||
if got := FilterObjectsForDeletion(in); !reflect.DeepEqual(got, need) { | ||
t.Errorf("Received unexpected objects for deletion = %v, want %v", got, need) | ||
} | ||
} | ||
} | ||
|
||
func podWithSyncWave(wave string) *unstructured.Unstructured { | ||
return Annotate(NewPod(), common.AnnotationSyncWave, wave) | ||
} | ||
|
||
func sliceOfObjectsWithSyncWaves(waves []string) []*unstructured.Unstructured { | ||
objects := make([]*unstructured.Unstructured, 0) | ||
for _, wave := range waves { | ||
objects = append(objects, podWithSyncWave(wave)) | ||
} | ||
return objects | ||
} |