From c2706efe38b1641e845353b5c2e86d135d70d055 Mon Sep 17 00:00:00 2001 From: pashavictorovich Date: Wed, 16 Mar 2022 23:02:44 +0200 Subject: [PATCH 1/2] add to approvers Signed-off-by: pashavictorovich --- OWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OWNERS b/OWNERS index 37a255f070d76..7a06b21bbcdfc 100644 --- a/OWNERS +++ b/OWNERS @@ -10,6 +10,7 @@ approvers: - jgwest - mayzhang2000 - rbreeze +- pasha-codefresh reviewers: - dthomson25 @@ -20,7 +21,6 @@ reviewers: - hblixt - chetan-rns - wanghong230 -- pasha-codefresh - ciiay - leoluz - crenshaw-dev From abaf3207cb85fcf44c35b0ed7005a408f0a48204 Mon Sep 17 00:00:00 2001 From: pashavictorovich Date: Wed, 23 Mar 2022 20:18:24 +0200 Subject: [PATCH 2/2] test filter resources in app cmd Signed-off-by: pashavictorovich --- cmd/argocd/commands/app.go | 8 ++--- cmd/argocd/commands/app_actions.go | 4 +-- cmd/argocd/commands/app_test.go | 57 ++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 6 deletions(-) diff --git a/cmd/argocd/commands/app.go b/cmd/argocd/commands/app.go index 76487602fd663..04712c78d761c 100644 --- a/cmd/argocd/commands/app.go +++ b/cmd/argocd/commands/app.go @@ -2169,7 +2169,7 @@ func NewApplicationPatchCommand(clientOpts *argocdclient.ClientOptions) *cobra.C return &command } -func filterResources(command *cobra.Command, resources []*argoappv1.ResourceDiff, group, kind, namespace, resourceName string, all bool) []*unstructured.Unstructured { +func filterResources(groupChanged bool, resources []*argoappv1.ResourceDiff, group, kind, namespace, resourceName string, all bool) []*unstructured.Unstructured { liveObjs, err := liveObjects(resources) errors.CheckError(err) filteredObjects := make([]*unstructured.Unstructured, 0) @@ -2179,7 +2179,7 @@ func filterResources(command *cobra.Command, resources []*argoappv1.ResourceDiff continue } gvk := obj.GroupVersionKind() - if command.Flags().Changed("group") && group != gvk.Group { + if groupChanged && group != gvk.Group { continue } if namespace != "" && namespace != obj.GetNamespace() { @@ -2239,7 +2239,7 @@ func NewApplicationPatchResourceCommand(clientOpts *argocdclient.ClientOptions) ctx := context.Background() resources, err := appIf.ManagedResources(ctx, &applicationpkg.ResourcesQuery{ApplicationName: &appName}) errors.CheckError(err) - objectsToPatch := filterResources(command, resources.Items, group, kind, namespace, resourceName, all) + objectsToPatch := filterResources(command.Flags().Changed("group"), resources.Items, group, kind, namespace, resourceName, all) for i := range objectsToPatch { obj := objectsToPatch[i] gvk := obj.GroupVersionKind() @@ -2295,7 +2295,7 @@ func NewApplicationDeleteResourceCommand(clientOpts *argocdclient.ClientOptions) ctx := context.Background() resources, err := appIf.ManagedResources(ctx, &applicationpkg.ResourcesQuery{ApplicationName: &appName}) errors.CheckError(err) - objectsToDelete := filterResources(command, resources.Items, group, kind, namespace, resourceName, all) + objectsToDelete := filterResources(command.Flags().Changed("group"), resources.Items, group, kind, namespace, resourceName, all) for i := range objectsToDelete { obj := objectsToDelete[i] gvk := obj.GroupVersionKind() diff --git a/cmd/argocd/commands/app_actions.go b/cmd/argocd/commands/app_actions.go index a59e1d0612ae6..dc8d2fc8233f0 100644 --- a/cmd/argocd/commands/app_actions.go +++ b/cmd/argocd/commands/app_actions.go @@ -64,7 +64,7 @@ func NewApplicationResourceActionsListCommand(clientOpts *argocdclient.ClientOpt ctx := context.Background() resources, err := appIf.ManagedResources(ctx, &applicationpkg.ResourcesQuery{ApplicationName: &appName}) errors.CheckError(err) - filteredObjects := filterResources(command, resources.Items, group, kind, namespace, resourceName, true) + filteredObjects := filterResources(command.Flags().Changed("group"), resources.Items, group, kind, namespace, resourceName, true) var availableActions []DisplayedAction for i := range filteredObjects { obj := filteredObjects[i] @@ -148,7 +148,7 @@ func NewApplicationResourceActionsRunCommand(clientOpts *argocdclient.ClientOpti ctx := context.Background() resources, err := appIf.ManagedResources(ctx, &applicationpkg.ResourcesQuery{ApplicationName: &appName}) errors.CheckError(err) - filteredObjects := filterResources(command, resources.Items, group, kind, namespace, resourceName, all) + filteredObjects := filterResources(command.Flags().Changed("group"), resources.Items, group, kind, namespace, resourceName, all) var resGroup = filteredObjects[0].GroupVersionKind().Group for i := range filteredObjects[1:] { if filteredObjects[i].GroupVersionKind().Group != resGroup { diff --git a/cmd/argocd/commands/app_test.go b/cmd/argocd/commands/app_test.go index 9f1d8c676e729..db425b6409dc1 100644 --- a/cmd/argocd/commands/app_test.go +++ b/cmd/argocd/commands/app_test.go @@ -191,3 +191,60 @@ func TestFindRevisionHistoryWithPassedIdThatNotExist(t *testing.T) { } } + +func TestFilterResources(t *testing.T) { + + t.Run("Filter by ns", func(t *testing.T) { + + resources := []*v1alpha1.ResourceDiff{ + { + LiveState: "{\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"name\":\"test-helm-guestbook\",\"namespace\":\"argocd\"},\"spec\":{\"selector\":{\"app\":\"helm-guestbook\",\"release\":\"test\"},\"sessionAffinity\":\"None\",\"type\":\"ClusterIP\"},\"status\":{\"loadBalancer\":{}}}", + }, + { + LiveState: "{\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"name\":\"test-helm-guestbook\",\"namespace\":\"ns\"},\"spec\":{\"selector\":{\"app\":\"helm-guestbook\",\"release\":\"test\"},\"sessionAffinity\":\"None\",\"type\":\"ClusterIP\"},\"status\":{\"loadBalancer\":{}}}", + }, + } + + filteredResources := filterResources(false, resources, "g", "Service", "ns", "test-helm-guestbook", true) + if len(filteredResources) != 1 { + t.Fatal("Incorrect number of resources after filter") + } + + }) + + t.Run("Filter by kind", func(t *testing.T) { + + resources := []*v1alpha1.ResourceDiff{ + { + LiveState: "{\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"name\":\"test-helm-guestbook\",\"namespace\":\"argocd\"},\"spec\":{\"selector\":{\"app\":\"helm-guestbook\",\"release\":\"test\"},\"sessionAffinity\":\"None\",\"type\":\"ClusterIP\"},\"status\":{\"loadBalancer\":{}}}", + }, + { + LiveState: "{\"apiVersion\":\"v1\",\"kind\":\"Deployment\",\"metadata\":{\"name\":\"test-helm-guestbook\",\"namespace\":\"argocd\"},\"spec\":{\"selector\":{\"app\":\"helm-guestbook\",\"release\":\"test\"},\"sessionAffinity\":\"None\",\"type\":\"ClusterIP\"},\"status\":{\"loadBalancer\":{}}}", + }, + } + + filteredResources := filterResources(false, resources, "g", "Deployment", "argocd", "test-helm-guestbook", true) + if len(filteredResources) != 1 { + t.Fatal("Incorrect number of resources after filter") + } + + }) + + t.Run("Filter by name", func(t *testing.T) { + + resources := []*v1alpha1.ResourceDiff{ + { + LiveState: "{\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"name\":\"test-helm-guestbook\",\"namespace\":\"argocd\"},\"spec\":{\"selector\":{\"app\":\"helm-guestbook\",\"release\":\"test\"},\"sessionAffinity\":\"None\",\"type\":\"ClusterIP\"},\"status\":{\"loadBalancer\":{}}}", + }, + { + LiveState: "{\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"name\":\"test-helm\",\"namespace\":\"argocd\"},\"spec\":{\"selector\":{\"app\":\"helm-guestbook\",\"release\":\"test\"},\"sessionAffinity\":\"None\",\"type\":\"ClusterIP\"},\"status\":{\"loadBalancer\":{}}}", + }, + } + + filteredResources := filterResources(false, resources, "g", "Service", "argocd", "test-helm", true) + if len(filteredResources) != 1 { + t.Fatal("Incorrect number of resources after filter") + } + + }) +}