Skip to content

Commit

Permalink
Add support for using wildcard chars in sourceNamespaces for ArgoCD O…
Browse files Browse the repository at this point in the history
…perator (#1218)

* adding a new variable in ReconcileArgoCD struct call SourceNamespaces which store all the  SourceNamespaces defined in Argocd Spec. sourceNamespaces

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* range r.SourceNamespaces instead of  cr.Spec.SourceNamespaces,  cr.Spec.SourceNamespaces might contain wildcard which will lead to error i.e. Namespace * not found

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* range r.SourceNamespaces instead of  cr.Spec.SourceNamespaces,  cr.Spec.SourceNamespaces might contain wildcard which will lead to error i.e. Namespace * not found

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* adding func containsWildcard

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* adding func setManagedNamespaces

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* range r.SourceNamespaces instead of  cr.Spec.SourceNamespaces,  cr.Spec.SourceNamespaces might contain wildcard which will lead to error i.e. Namespace * not found

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* adding unit test for func setSourceNamespaces

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* adding unit test for func sourceNamespacemapper

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* update  unit test for func reconcileRoleForApplicationSourceNamespaces

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* update  func SetupWithManager to watch newly created ns

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* adding  func sourceNamespacemapper which filter newly created ns

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* update  func setResourceWatches to watch newly created ns. added func sourceNamespaceFilterPredicate

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* go.mod/go.sum

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* unit tests func update

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* adding func getSourceNamespaces

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* adding unit test for func getSourceNamespaces. TestGetSourceNamespaces

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* remove SourceNamespaces from ReconcileArgoCD struct and use  func getSourceNamespaces to get SourceNamespaces

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* add to reviewers (#1196)

Signed-off-by: saumeya <saumeyakatyal@gmail.com>
Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* fix: Operator doesn't reflect deletion of SA from repo setting (#1169)

Signed-off-by: Jayendra Parsai <jparsai@jparsai-thinkpadp1gen4i.remote.csb>
Co-authored-by: Jayendra Parsai <jparsai@jparsai-thinkpadp1gen4i.remote.csb>
Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* feat: Notification service monitor (#1187)

* feat: Expose notifications controller metrics to prometheus monitoring

---------

Signed-off-by: iam-veeramalla <abhishek.veeramalla@gmail.com>
Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* Fix status operation value to allow less than 10 (#1198)

* fix logic to allow processor operation to be less than default

---------

Signed-off-by: Anand Kumar Singh <anandrkskd@gmail.com>
Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* comment remove

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* fix  goimports

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* undo changes in generated codes

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* Revert "comment remove"

This reverts commit c5d2f57.

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* remove comments and rename sourceNamespacemapper function to sourceNamespaceMapper

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* e2e  test to valitade role and rolebinding for source namespace without wildcard

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* e2e  test to valitade role and rolebinding for source namespace with wildcard pattern

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* e2e  test to valitade role and rolebinding for source namespace with wildcard '*'

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* e2e  test to valitade role and rolebinding for sourceNamespaces

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* update with master branch and remove verbs from role in e2e test assert and errors

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* comment for sourceNamespaceMapper function

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* Change the variable name sourceNamespacemapper to sourceNamespaceMapper

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* Removed unwanted blank lines in this function.

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* adeed comment for sourceNamespaceFilterPredicate and getSourceNamespaces

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* adding a new variable in ReconcileArgoCD struct call SourceNamespaces which store all the  SourceNamespaces defined in Argocd Spec. sourceNamespaces

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* range r.SourceNamespaces instead of  cr.Spec.SourceNamespaces,  cr.Spec.SourceNamespaces might contain wildcard which will lead to error i.e. Namespace * not found

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* range r.SourceNamespaces instead of  cr.Spec.SourceNamespaces,  cr.Spec.SourceNamespaces might contain wildcard which will lead to error i.e. Namespace * not found

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* adding func containsWildcard

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* adding func setManagedNamespaces

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* range r.SourceNamespaces instead of  cr.Spec.SourceNamespaces,  cr.Spec.SourceNamespaces might contain wildcard which will lead to error i.e. Namespace * not found

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* adding unit test for func setSourceNamespaces

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* adding unit test for func sourceNamespacemapper

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* update  unit test for func reconcileRoleForApplicationSourceNamespaces

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* update  func SetupWithManager to watch newly created ns

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* adding  func sourceNamespacemapper which filter newly created ns

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* update  func setResourceWatches to watch newly created ns. added func sourceNamespaceFilterPredicate

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* go.mod/go.sum

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* unit tests func update

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* adding func getSourceNamespaces

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* adding unit test for func getSourceNamespaces. TestGetSourceNamespaces

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* remove SourceNamespaces from ReconcileArgoCD struct and use  func getSourceNamespaces to get SourceNamespaces

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* Fix status operation value to allow less than 10 (#1198)

* fix logic to allow processor operation to be less than default

---------

Signed-off-by: Anand Kumar Singh <anandrkskd@gmail.com>
Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* comment remove

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* fix  goimports

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* undo changes in generated codes

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* Revert "comment remove"

This reverts commit c5d2f57.

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* remove comments and rename sourceNamespacemapper function to sourceNamespaceMapper

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* e2e  test to valitade role and rolebinding for source namespace without wildcard

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* e2e  test to valitade role and rolebinding for source namespace with wildcard pattern

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* e2e  test to valitade role and rolebinding for source namespace with wildcard '*'

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* e2e  test to valitade role and rolebinding for sourceNamespaces

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* update with master branch and remove verbs from role in e2e test assert and errors

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* comment for sourceNamespaceMapper function

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* Change the variable name sourceNamespacemapper to sourceNamespaceMapper

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* Removed unwanted blank lines in this function.

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* adeed comment for sourceNamespaceFilterPredicate and getSourceNamespaces

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* resolve merge conficts

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* resolve merge conficts

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* removed  unwanted blank lines

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* removed  unwanted blank lines

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* added unit test cases for getSourceNamespaces and sourceNamespaceMapper func

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* added unit test for multiple sourceNamespaces

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* added e2e  test for multiple sourceNamespaces

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* eremoved extra lines from sourceNamespaceMapper

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* documentation for supporting wildcard

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* documentation for supporting wildcard

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* documentation for supporting wildcard

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* documentation for supporting wildcard and Link for Allowing additional namespaces in an AppProject

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* remove placeholder code

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* removed additional lines

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* remote extra watch on namespace resource

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* remote sourceNamespaceFilterPredicate func

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* update TestReconcileApplicationSet_Deployments_Command func. getSourceNamespaces expect ns  must  exist

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* remove extra lines

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* small update on documentation

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* remove extra lines

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* remove extra lines

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* add e2e test to validate labels,role and rolebinding,after removing namespace from sourcenamespace

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

* update e2e test to check for labels in ns which is in sourceNamespace

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>

---------

Signed-off-by: Mangaal <angommeeteimangaal@gmail.com>
Signed-off-by: saumeya <saumeyakatyal@gmail.com>
Signed-off-by: Jayendra Parsai <jparsai@jparsai-thinkpadp1gen4i.remote.csb>
Signed-off-by: iam-veeramalla <abhishek.veeramalla@gmail.com>
Signed-off-by: Anand Kumar Singh <anandrkskd@gmail.com>
Co-authored-by: Saumeya Katyal <saumeyakatyal@gmail.com>
Co-authored-by: Jayendra Parsai <jparsai@redhat.com>
Co-authored-by: Jayendra Parsai <jparsai@jparsai-thinkpadp1gen4i.remote.csb>
Co-authored-by: Abhishek Veeramalla <abhishek.veeramalla@gmail.com>
Co-authored-by: Anand Kumar Singh <anandrkskd@gmail.com>
  • Loading branch information
6 people authored Feb 21, 2024
1 parent 4bd23cb commit 39d15ce
Show file tree
Hide file tree
Showing 28 changed files with 1,100 additions and 26 deletions.
12 changes: 11 additions & 1 deletion controllers/argocd/applicationset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,17 @@ func TestReconcileApplicationSet_Deployments_Command(t *testing.T) {
t.Run(test.name, func(t *testing.T) {

a := makeTestArgoCD()
resObjs := []client.Object{a}
ns1 := v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
}
ns2 := v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "bar",
},
}
resObjs := []client.Object{a, &ns1, &ns2}
subresObjs := []client.Object{a}
runtimeObjs := []runtime.Object{}
sch := makeTestReconcilerScheme(argoproj.AddToScheme)
Expand Down
24 changes: 21 additions & 3 deletions controllers/argocd/custommapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"strings"

"github.com/argoproj/argo-cd/v2/util/glob"

argoproj "github.com/argoproj-labs/argocd-operator/api/v1beta1"
"github.com/argoproj-labs/argocd-operator/common"

Expand Down Expand Up @@ -130,17 +132,16 @@ func (r *ReconcileArgoCD) tlsSecretMapper(ctx context.Context, o client.Object)
func (r *ReconcileArgoCD) namespaceResourceMapper(ctx context.Context, o client.Object) []reconcile.Request {
var result = []reconcile.Request{}

argocds := &argoproj.ArgoCDList{}
labels := o.GetLabels()
namespaceName := o.GetName()
if v, ok := labels[common.ArgoCDManagedByLabel]; ok {
argocds := &argoproj.ArgoCDList{}
if err := r.Client.List(context.TODO(), argocds, &client.ListOptions{Namespace: v}); err != nil {
return result
}

if len(argocds.Items) != 1 {
return result
}

argocd := argocds.Items[0]
namespacedName := client.ObjectKey{
Name: argocd.Name,
Expand All @@ -149,6 +150,23 @@ func (r *ReconcileArgoCD) namespaceResourceMapper(ctx context.Context, o client.
result = []reconcile.Request{
{NamespacedName: namespacedName},
}
} else {
// If the namespace does not have the expected managed-by label,
// iterate through each ArgoCD instance to identify if the observed namespace
// matches any configured sourceNamespace pattern. If a match is found,
// generate a reconcile request for the instances.
if err := r.Client.List(ctx, argocds, &client.ListOptions{}); err != nil {
return result
}
for _, argocd := range argocds.Items {
if glob.MatchStringInList(argocd.Spec.SourceNamespaces, namespaceName, false) {
namespacedName := client.ObjectKey{
Name: argocd.Name,
Namespace: argocd.Namespace,
}
result = append(result, reconcile.Request{NamespacedName: namespacedName})
}
}
}

return result
Expand Down
313 changes: 312 additions & 1 deletion controllers/argocd/custommapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ func TestReconcileArgoCD_tlsSecretMapperRedis(t *testing.T) {

}

func TestReconcileArgoCD_namespaceResourceMapper(t *testing.T) {
func TestReconcileArgoCD_namespaceResourceMapperWithManagedByLabel(t *testing.T) {
a := makeTestArgoCD()

resObjs := []client.Object{a}
Expand Down Expand Up @@ -631,3 +631,314 @@ func TestReconcileArgoCD_namespaceResourceMapper(t *testing.T) {
})
}
}

func TestReconcileArgoCD_namespaceResourceMapperForSpecificNamespaceWithoutManagedByLabel(t *testing.T) {
argocd1 := makeTestArgoCD()
resObjs := []client.Object{argocd1}
subresObjs := []client.Object{argocd1}
runtimeObjs := []runtime.Object{}
sch := makeTestReconcilerScheme(argoproj.AddToScheme)
cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs)
r := makeTestReconciler(cl, sch)

argocd1.Name = "argocd1"
argocd1.Namespace = "argo-test-1"
argocd1.Spec.SourceNamespaces = append(argocd1.Spec.SourceNamespaces, "test-namespace-1")
// Fake client returns an error if ResourceVersion is not nil
argocd1.ResourceVersion = ""

assert.NoError(t, r.Client.Create(context.TODO(), argocd1))

type test struct {
name string
o client.Object
want []reconcile.Request
}

tests := []test{
{
name: "Reconcile for Namespace 'test-namespace-1'",
o: &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-namespace-1",
},
},
want: []reconcile.Request{
{
NamespacedName: types.NamespacedName{
Name: argocd1.Name,
Namespace: argocd1.Namespace,
},
},
},
},
{
name: "No Reconcile for Namespace 'test-namespace-2'",
o: &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-namespace-2",
},
},
want: []reconcile.Request{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := r.namespaceResourceMapper(context.TODO(), tt.o); !assert.ElementsMatch(t, got, tt.want) {
t.Errorf("ReconcileArgoCD.sourceNamespaceMapper(), got = %v, want = %v", got, tt.want)
}
})
}
}

func TestReconcileArgoCD_namespaceResourceMapperForWildCardPatternNamespaceWithoutManagedByLabel(t *testing.T) {
argocd1 := makeTestArgoCD()
resObjs := []client.Object{argocd1}
subresObjs := []client.Object{argocd1}
runtimeObjs := []runtime.Object{}
sch := makeTestReconcilerScheme(argoproj.AddToScheme)
cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs)
r := makeTestReconciler(cl, sch)

argocd1.Name = "argocd1"
argocd1.Namespace = "argo-test-1"
argocd1.Spec.SourceNamespaces = append(argocd1.Spec.SourceNamespaces, "test*")
// Fake client returns an error if ResourceVersion is not nil
argocd1.ResourceVersion = ""

assert.NoError(t, r.Client.Create(context.TODO(), argocd1))

type test struct {
name string
o client.Object
want []reconcile.Request
}

tests := []test{
{
name: "Reconcile for Namespace 'test-namespace-1'",
o: &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-namespace-1",
},
},
want: []reconcile.Request{
{
NamespacedName: types.NamespacedName{
Name: argocd1.Name,
Namespace: argocd1.Namespace,
},
},
},
},
{
name: "Reconcile for Namespace 'test-namespace-2'",
o: &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-namespace-2",
},
},
want: []reconcile.Request{
{
NamespacedName: types.NamespacedName{
Name: argocd1.Name,
Namespace: argocd1.Namespace,
},
},
},
},
{
name: "No Reconcile for Namespace 'prod-namespace-1'",
o: &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "prod-namespace-1",
},
},
want: []reconcile.Request{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := r.namespaceResourceMapper(context.TODO(), tt.o); !assert.ElementsMatch(t, got, tt.want) {
t.Errorf("ReconcileArgoCD.sourceNamespaceMapper(), got = %v, want = %v", got, tt.want)
}
})
}
}

func TestReconcileArgoCD_namespaceResourceMapperForMultipleSourceNamespacesWithoutManagedByLabel(t *testing.T) {
argocd1 := makeTestArgoCD()
resObjs := []client.Object{argocd1}
subresObjs := []client.Object{argocd1}
runtimeObjs := []runtime.Object{}
sch := makeTestReconcilerScheme(argoproj.AddToScheme)
cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs)
r := makeTestReconciler(cl, sch)

argocd1.Name = "argocd1"
argocd1.Namespace = "argo-test-1"
argocd1.Spec.SourceNamespaces = append(argocd1.Spec.SourceNamespaces, "test*", "dev*")
// Fake client returns an error if ResourceVersion is not nil
argocd1.ResourceVersion = ""

assert.NoError(t, r.Client.Create(context.TODO(), argocd1))

type test struct {
name string
o client.Object
want []reconcile.Request
}

tests := []test{
{
name: "Reconcile for Namespace 'test-namespace-1'",
o: &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-namespace-1",
},
},
want: []reconcile.Request{
{
NamespacedName: types.NamespacedName{
Name: argocd1.Name,
Namespace: argocd1.Namespace,
},
},
},
},
{
name: "Reconcile for Namespace 'test-namespace-2'",
o: &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-namespace-2",
},
},
want: []reconcile.Request{
{
NamespacedName: types.NamespacedName{
Name: argocd1.Name,
Namespace: argocd1.Namespace,
},
},
},
},
{
name: "Reconcile for Namespace 'dev-namespace-1'",
o: &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "dev-namespace-1",
},
},
want: []reconcile.Request{
{
NamespacedName: types.NamespacedName{
Name: argocd1.Name,
Namespace: argocd1.Namespace,
},
},
},
},
{
name: "No Reconcile for Namespace 'prod-namespace-1'",
o: &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "prod-namespace-1",
},
},
want: []reconcile.Request{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := r.namespaceResourceMapper(context.TODO(), tt.o); !assert.ElementsMatch(t, got, tt.want) {
t.Errorf("ReconcileArgoCD.sourceNamespaceMapper(), got = %v, want = %v", got, tt.want)
}
})
}
}

func TestReconcileArgoCD_namespaceResourceMapperForWildCardNamespaceWithoutManagedByLabel(t *testing.T) {
argocd1 := makeTestArgoCD()
resObjs := []client.Object{argocd1}
subresObjs := []client.Object{argocd1}
runtimeObjs := []runtime.Object{}
sch := makeTestReconcilerScheme(argoproj.AddToScheme)
cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs)
r := makeTestReconciler(cl, sch)

argocd1.Name = "argocd1"
argocd1.Namespace = "argo-test-1"
argocd1.Spec.SourceNamespaces = append(argocd1.Spec.SourceNamespaces, "*")
// Fake client returns an error if ResourceVersion is not nil
argocd1.ResourceVersion = ""

assert.NoError(t, r.Client.Create(context.TODO(), argocd1))

type test struct {
name string
o client.Object
want []reconcile.Request
}

tests := []test{
{
name: "Reconcile for Namespace 'test-namespace-1'",
o: &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-namespace-1",
},
},
want: []reconcile.Request{
{
NamespacedName: types.NamespacedName{
Name: argocd1.Name,
Namespace: argocd1.Namespace,
},
},
},
},
{
name: "Reconcile for Namespace 'test-namespace-2'",
o: &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-namespace-2",
},
},
want: []reconcile.Request{
{
NamespacedName: types.NamespacedName{
Name: argocd1.Name,
Namespace: argocd1.Namespace,
},
},
},
},
{
name: "Reconcile for Namespace 'prod-namespace-1'",
o: &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "prod-namespace-1",
},
},
want: []reconcile.Request{
{
NamespacedName: types.NamespacedName{
Name: argocd1.Name,
Namespace: argocd1.Namespace,
},
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := r.namespaceResourceMapper(context.TODO(), tt.o); !assert.ElementsMatch(t, got, tt.want) {
t.Errorf("ReconcileArgoCD.sourceNamespaceMapper(), got = %v, want = %v", got, tt.want)
}
})
}
}
Loading

0 comments on commit 39d15ce

Please sign in to comment.