Skip to content

Commit

Permalink
code-refactoring: refactor argo-cd configmap reconciliation (argoproj…
Browse files Browse the repository at this point in the history
…-labs#1227)

*update configmap reconciliation

---------

Signed-off-by: Jaideep Rao <jaideep.r97@gmail.com>
  • Loading branch information
jaideepr97 authored Feb 19, 2024
1 parent 27b87a6 commit fb1b5c8
Show file tree
Hide file tree
Showing 10 changed files with 1,773 additions and 596 deletions.
4 changes: 4 additions & 0 deletions common/TOBEREMOVED.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ const (
// to used for the the Redis container in HA mode.
ArgoCDRedisHAImageEnvVar = "ARGOCD_REDIS_HA_IMAGE"

// ArgoCDPolicyMatcherMode is the key for matchers function for casbin.
// There are two options for this, 'glob' for glob matcher or 'regex' for regex matcher.
ArgoCDPolicyMatcherMode = "policy.matchMode"

// ArgoCDDefaultRepoMetricsPort is the default listen port for the Argo CD repo server metrics.
ArgoCDDefaultRepoMetricsPort = 8084

Expand Down
24 changes: 7 additions & 17 deletions controllers/argocd/argocd_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/argoproj-labs/argocd-operator/common"
"github.com/argoproj-labs/argocd-operator/controllers/argocd/appcontroller"
"github.com/argoproj-labs/argocd-operator/controllers/argocd/applicationset"
"github.com/argoproj-labs/argocd-operator/controllers/argocd/configmap"
"github.com/argoproj-labs/argocd-operator/controllers/argocd/notifications"
"github.com/argoproj-labs/argocd-operator/controllers/argocd/redis"
"github.com/argoproj-labs/argocd-operator/controllers/argocd/reposerver"
Expand All @@ -44,7 +43,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
v1 "k8s.io/api/rbac/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -90,7 +89,6 @@ type ArgoCDReconciler struct {
LabelSelector string

SecretController *secret.SecretReconciler
ConfigMapController *configmap.ConfigMapReconciler
RedisController *redis.RedisReconciler
ReposerverController *reposerver.RepoServerReconciler
ServerController *server.ServerReconciler
Expand Down Expand Up @@ -381,13 +379,13 @@ func (r *ArgoCDReconciler) setAppManagedNamespaces() error {
func (r *ArgoCDReconciler) reconcileControllers() error {

// core components, return reconciliation errors
if err := r.SecretController.Reconcile(); err != nil {
r.Logger.Error(err, "failed to reconcile secret controller")
if err := r.reconcileConfigMaps(); err != nil {
r.Logger.Error(err, "failed to reconcile required config maps")
return err
}

if err := r.ConfigMapController.Reconcile(); err != nil {
r.Logger.Error(err, "failed to reconcile configmap controller")
if err := r.SecretController.Reconcile(); err != nil {
r.Logger.Error(err, "failed to reconcile secret controller")
return err
}

Expand Down Expand Up @@ -459,12 +457,6 @@ func (r *ArgoCDReconciler) InitializeControllerReconcilers() {
ManagedNamespaces: r.ResourceManagedNamespaces,
}

configMapController := &configmap.ConfigMapReconciler{
Client: &r.Client,
Scheme: r.Scheme,
Instance: r.Instance,
}

redisController := &redis.RedisReconciler{
Client: r.Client,
Scheme: r.Scheme,
Expand Down Expand Up @@ -536,8 +528,6 @@ func (r *ArgoCDReconciler) InitializeControllerReconcilers() {

r.SSOController = ssoController

r.ConfigMapController = configMapController

r.SecretController = secretController

}
Expand Down Expand Up @@ -573,9 +563,9 @@ func (r *ArgoCDReconciler) setResourceWatches(bldr *builder.Builder) *builder.Bu
// Watch for changes to Ingress sub-resources owned by ArgoCD instances.
bldr.Owns(&networkingv1.Ingress{})

bldr.Owns(&v1.Role{})
bldr.Owns(&rbacv1.Role{})

bldr.Owns(&v1.RoleBinding{})
bldr.Owns(&rbacv1.RoleBinding{})

if openshift.IsRouteAPIAvailable() {
// Watch OpenShift Route sub-resources owned by ArgoCD instances.
Expand Down
46 changes: 26 additions & 20 deletions controllers/argocd/argocd_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,32 @@ import (
"github.com/argoproj-labs/argocd-operator/common"
"github.com/argoproj-labs/argocd-operator/pkg/argoutil"
"github.com/argoproj-labs/argocd-operator/pkg/cluster"
"github.com/argoproj-labs/argocd-operator/pkg/util"
"github.com/argoproj-labs/argocd-operator/tests/test"
)

var _ reconcile.Reconciler = &ArgoCDReconciler{}

func makeTestArgoCDReconciler(client client.Client, sch *runtime.Scheme) *ArgoCDReconciler {
// func makeTestArgoCDReconciler(client client.Client, sch *runtime.Scheme) *ArgoCDReconciler {
// return &ArgoCDReconciler{
// Client: client,
// Scheme: sch,
// }
// }

func makeTestArgoCDReconciler(cr *argoproj.ArgoCD, objs ...client.Object) *ArgoCDReconciler {
schemeOpt := func(s *runtime.Scheme) {
argoproj.AddToScheme(s)
}
sch := test.MakeTestReconcilerScheme(schemeOpt)

client := test.MakeTestReconcilerClient(sch, objs, []client.Object{cr}, []runtime.Object{cr})

return &ArgoCDReconciler{
Client: client,
Scheme: sch,
Client: client,
Scheme: sch,
Instance: cr,
Logger: util.NewLogger("argocd-controller"),
}
}

Expand Down Expand Up @@ -396,10 +414,7 @@ func TestReconcileArgoCD_CleanUp(t *testing.T) {

func TestSetResourceManagedNamespaces(t *testing.T) {

resources := []client.Object{}
subresObjs := []client.Object{}

runtimeObjs := []runtime.Object{
objs := []client.Object{
makeTestNs(func(n *corev1.Namespace) {
n.Name = "instance-1"
}),
Expand Down Expand Up @@ -432,10 +447,6 @@ func TestSetResourceManagedNamespaces(t *testing.T) {
}),
}

sch := makeTestReconcilerScheme(argoproj.AddToScheme)
cl := makeTestReconcilerClient(sch, resources, subresObjs, runtimeObjs)
r := makeTestArgoCDReconciler(cl, sch)

instanceOne := makeTestArgoCD(func(ac *argoproj.ArgoCD) {
ac.Namespace = "instance-1"
})
Expand All @@ -448,7 +459,7 @@ func TestSetResourceManagedNamespaces(t *testing.T) {
"test-ns-1": "",
"test-ns-4": "",
}
r.Instance = instanceOne
r := makeTestArgoCDReconciler(instanceOne, objs...)
r.setResourceManagedNamespaces()
assert.Equal(t, expectedNsMap, r.ResourceManagedNamespaces)

Expand All @@ -464,10 +475,7 @@ func TestSetResourceManagedNamespaces(t *testing.T) {

func TestSetAppManagedNamespaces(t *testing.T) {

resources := []client.Object{}
subresObjs := []client.Object{}

runtimeObjs := []runtime.Object{
objs := []client.Object{
makeTestNs(func(n *corev1.Namespace) {
n.Name = "instance-1"
}),
Expand Down Expand Up @@ -497,15 +505,13 @@ func TestSetAppManagedNamespaces(t *testing.T) {
}),
}

sch := makeTestReconcilerScheme(argoproj.AddToScheme)
cl := makeTestReconcilerClient(sch, resources, subresObjs, runtimeObjs)
r := makeTestArgoCDReconciler(cl, sch)

instance := makeTestArgoCD(func(ac *argoproj.ArgoCD) {
ac.Namespace = "instance-1"
ac.Spec.SourceNamespaces = []string{"test-ns-1", "test-ns-2", "test-ns-3"}
})

r := makeTestArgoCDReconciler(instance, objs...)

// test with namespace scoped instance
r.Instance = instance
r.ClusterScoped = false
Expand Down
11 changes: 8 additions & 3 deletions controllers/argocd/argocdcommon/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,25 @@ func GetValueOrDefault(value interface{}, defaultValue interface{}) interface{}
if reflect.ValueOf(value).IsNil() {
return defaultValue
}
return reflect.ValueOf(value).String()
ptVal := reflect.Indirect(reflect.ValueOf(value))

switch ptVal.Kind() {
case reflect.String:
return reflect.Indirect(reflect.ValueOf(value)).String()
}
}

switch v := value.(type) {
case string:
if len(v) > 0 {
return v
}
return defaultValue
return defaultValue.(string)
case map[string]string:
if len(v) > 0 {
return v
}
return defaultValue
return defaultValue.(map[string]string)
}

return defaultValue
Expand Down
Loading

0 comments on commit fb1b5c8

Please sign in to comment.