Skip to content

Commit 31a43b3

Browse files
committed
feat: refactor request-scoepd reconciler variables to new struct
Signed-off-by: Jonathan West <jgwest@gmail.com>
1 parent f9eb0a3 commit 31a43b3

29 files changed

Lines changed: 618 additions & 363 deletions

cmd/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ func main() {
277277
TokenRenewalTimers: map[string]*argocd.TokenRenewalTimer{},
278278
},
279279
FipsConfigChecker: argoutil.NewLinuxFipsConfigChecker(),
280+
ActiveInstanceMap: map[string]string{},
280281
}).SetupWithManager(mgr); err != nil {
281282
setupLog.Error(err, "unable to create controller", "controller", "ArgoCD")
282283
os.Exit(1)

controllers/argocd/applicationset.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const (
4646
)
4747

4848
// getArgoApplicationSetCommand will return the command for the ArgoCD ApplicationSet component.
49-
func (r *ReconcileArgoCD) getArgoApplicationSetCommand(cr *argoproj.ArgoCD) []string {
49+
func (r *ReconcileArgoCD) getArgoApplicationSetCommand(cr *argoproj.ArgoCD, reqState *RequestState) []string {
5050
cmd := make([]string, 0)
5151

5252
cmd = append(cmd, "entrypoint.sh")
@@ -77,7 +77,7 @@ func (r *ReconcileArgoCD) getArgoApplicationSetCommand(cr *argoproj.ArgoCD) []st
7777
if err != nil {
7878
log.Error(err, "failed to getting ApplicationSet source namespaces")
7979
} else {
80-
appsNamespaces, err := r.getSourceNamespaces(cr)
80+
appsNamespaces, err := r.getSourceNamespaces(cr, reqState)
8181
if err == nil {
8282
appsetsSourceNamespaces := []string{}
8383
for _, ns := range appsetsSourceNamespacesExpanded {
@@ -112,7 +112,7 @@ func (r *ReconcileArgoCD) getArgoApplicationSetCommand(cr *argoproj.ArgoCD) []st
112112
return cmd
113113
}
114114

115-
func (r *ReconcileArgoCD) reconcileApplicationSetController(cr *argoproj.ArgoCD) error {
115+
func (r *ReconcileArgoCD) reconcileApplicationSetController(cr *argoproj.ArgoCD, reqState *RequestState) error {
116116

117117
log.Info("reconciling applicationset serviceaccounts")
118118
sa, err := r.reconcileApplicationSetServiceAccount(cr)
@@ -132,7 +132,7 @@ func (r *ReconcileArgoCD) reconcileApplicationSetController(cr *argoproj.ArgoCD)
132132
}
133133

134134
log.Info("reconciling applicationset deployments")
135-
if err := r.reconcileApplicationSetDeployment(cr, sa); err != nil {
135+
if err := r.reconcileApplicationSetDeployment(cr, sa, reqState); err != nil {
136136
return err
137137
}
138138

@@ -155,21 +155,21 @@ func (r *ReconcileArgoCD) reconcileApplicationSetController(cr *argoproj.ArgoCD)
155155

156156
// reconcile source namespace roles & rolebindings
157157
log.Info("reconciling applicationset roles & rolebindings in source namespaces")
158-
if err := r.reconcileApplicationSetSourceNamespacesResources(cr); err != nil {
158+
if err := r.reconcileApplicationSetSourceNamespacesResources(cr, reqState); err != nil {
159159
return err
160160
}
161161

162162
// remove resources for namespaces not part of SourceNamespaces
163163
log.Info("performing cleanup for applicationset source namespaces")
164-
if err := r.removeUnmanagedApplicationSetSourceNamespaceResources(cr); err != nil {
164+
if err := r.removeUnmanagedApplicationSetSourceNamespaceResources(cr, reqState); err != nil {
165165
return err
166166
}
167167

168168
return nil
169169
}
170170

171171
// reconcileApplicationControllerDeployment will ensure the Deployment resource is present for the ArgoCD Application Controller component.
172-
func (r *ReconcileArgoCD) reconcileApplicationSetDeployment(cr *argoproj.ArgoCD, sa *corev1.ServiceAccount) error {
172+
func (r *ReconcileArgoCD) reconcileApplicationSetDeployment(cr *argoproj.ArgoCD, sa *corev1.ServiceAccount, reqState *RequestState) error {
173173

174174
existing := newDeploymentWithSuffix("applicationset-controller", "controller", cr)
175175

@@ -282,7 +282,7 @@ func (r *ReconcileArgoCD) reconcileApplicationSetDeployment(cr *argoproj.ArgoCD,
282282
}
283283

284284
podSpec.Containers = []corev1.Container{
285-
r.applicationSetContainer(cr, addSCMGitlabVolumeMount),
285+
r.applicationSetContainer(cr, addSCMGitlabVolumeMount, reqState),
286286
}
287287
AddSeccompProfileForOpenShift(r.Client, podSpec)
288288

@@ -374,7 +374,7 @@ func identifyDeploymentDifference(x appsv1.Deployment, y appsv1.Deployment) stri
374374
return ""
375375
}
376376

377-
func (r *ReconcileArgoCD) applicationSetContainer(cr *argoproj.ArgoCD, addSCMGitlabVolumeMount bool) corev1.Container {
377+
func (r *ReconcileArgoCD) applicationSetContainer(cr *argoproj.ArgoCD, addSCMGitlabVolumeMount bool, reqState *RequestState) corev1.Container {
378378
// Global proxy env vars go first
379379
appSetEnv := []corev1.EnvVar{{
380380
Name: "NAMESPACE",
@@ -428,7 +428,7 @@ func (r *ReconcileArgoCD) applicationSetContainer(cr *argoproj.ArgoCD, addSCMGit
428428
}
429429

430430
container := corev1.Container{
431-
Command: r.getArgoApplicationSetCommand(cr),
431+
Command: r.getArgoApplicationSetCommand(cr, reqState),
432432
Env: appSetEnv,
433433
Image: getApplicationSetContainerImage(cr),
434434
ImagePullPolicy: argoutil.GetImagePullPolicy(cr.Spec.ImagePullPolicy),
@@ -640,7 +640,7 @@ func (r *ReconcileArgoCD) reconcileApplicationSetClusterRoleBinding(cr *argoproj
640640

641641
// reconcileApplicationSetSourceNamespacesResources creates role & rolebinding in target source namespaces for appset controller
642642
// Appset resources are only created if target source ns is subset of apps source namespaces
643-
func (r *ReconcileArgoCD) reconcileApplicationSetSourceNamespacesResources(cr *argoproj.ArgoCD) error {
643+
func (r *ReconcileArgoCD) reconcileApplicationSetSourceNamespacesResources(cr *argoproj.ArgoCD, reqState *RequestState) error {
644644

645645
var reconciliationErrors []error
646646

@@ -666,7 +666,7 @@ func (r *ReconcileArgoCD) reconcileApplicationSetSourceNamespacesResources(cr *a
666666
}
667667

668668
// source ns should be part of app-in-any-ns
669-
appsNamespaces, err := r.getSourceNamespaces(cr)
669+
appsNamespaces, err := r.getSourceNamespaces(cr, reqState)
670670
if err != nil {
671671
return fmt.Errorf("failed to get apps source namespaces: %w", err)
672672
}
@@ -694,7 +694,7 @@ func (r *ReconcileArgoCD) reconcileApplicationSetSourceNamespacesResources(cr *a
694694
log.Info(fmt.Sprintf("Skipping reconciling resources for namespace %s as it is already managed-by namespace %s.", namespace.Name, value))
695695
// remove any source namespace resources
696696
if val, ok1 := namespace.Labels[common.ArgoCDApplicationSetManagedByClusterArgoCDLabel]; ok1 && val != cr.Namespace {
697-
delete(r.ManagedApplicationSetSourceNamespaces, namespace.Name)
697+
delete(reqState.ManagedApplicationSetSourceNamespaces, namespace.Name)
698698
if err := r.cleanupUnmanagedApplicationSetSourceNamespaceResources(cr, namespace.Name); err != nil {
699699
log.Error(err, fmt.Sprintf("error cleaning up resources for namespace %s", namespace.Name))
700700
}
@@ -762,11 +762,11 @@ func (r *ReconcileArgoCD) reconcileApplicationSetSourceNamespacesResources(cr *a
762762

763763
// appset permissions for argocd server in source namespaces are handled by apps-in-any-ns code
764764

765-
if _, ok := r.ManagedApplicationSetSourceNamespaces[sourceNamespace]; !ok {
766-
if r.ManagedApplicationSetSourceNamespaces == nil {
767-
r.ManagedApplicationSetSourceNamespaces = make(map[string]string)
765+
if _, ok := reqState.ManagedApplicationSetSourceNamespaces[sourceNamespace]; !ok {
766+
if reqState.ManagedApplicationSetSourceNamespaces == nil {
767+
reqState.ManagedApplicationSetSourceNamespaces = make(map[string]string)
768768
}
769-
r.ManagedApplicationSetSourceNamespaces[sourceNamespace] = ""
769+
reqState.ManagedApplicationSetSourceNamespaces[sourceNamespace] = ""
770770
}
771771
}
772772

@@ -953,10 +953,10 @@ func getResourceNameForApplicationSetSourceNamespaces(cr *argoproj.ArgoCD) strin
953953

954954
// removeUnmanagedApplicationSetSourceNamespaceResources cleansup resources from ApplicationSetSourceNamespaces if namespace is not managed by argocd instance.
955955
// ManagedApplicationSetSourceNamespaces var keeps track of namespaces with appset resources.
956-
func (r *ReconcileArgoCD) removeUnmanagedApplicationSetSourceNamespaceResources(cr *argoproj.ArgoCD) error {
956+
func (r *ReconcileArgoCD) removeUnmanagedApplicationSetSourceNamespaceResources(cr *argoproj.ArgoCD, reqState *RequestState) error {
957957

958958
// For each namespace the ArgoCDApplicationSetManagedByClusterArgoCDLabel label.
959-
for appsetsInAnyNamespaceLabelledNS := range r.ManagedApplicationSetSourceNamespaces {
959+
for appsetsInAnyNamespaceLabelledNS := range reqState.ManagedApplicationSetSourceNamespaces {
960960

961961
// Retrieve the namespace object in the 'managed application source namespaces' list
962962
ns := &corev1.Namespace{
@@ -1000,7 +1000,7 @@ func (r *ReconcileArgoCD) removeUnmanagedApplicationSetSourceNamespaceResources(
10001000
if cr.Spec.ApplicationSet != nil && cr.GetDeletionTimestamp() == nil {
10011001

10021002
// namespace is valid if it matches any pattern in cr.Spec.ApplicationSet.SourceNamespaces AND is in cr.Spec.SourceNamespaces
1003-
appsNamespaces, err := r.getSourceNamespaces(cr)
1003+
appsNamespaces, err := r.getSourceNamespaces(cr, reqState)
10041004
if err != nil {
10051005
return err
10061006
}
@@ -1020,7 +1020,7 @@ func (r *ReconcileArgoCD) removeUnmanagedApplicationSetSourceNamespaceResources(
10201020
log.Error(err, fmt.Sprintf("error cleaning up applicationset resources for namespace %s", appsetsInAnyNamespaceLabelledNS))
10211021
continue
10221022
}
1023-
delete(r.ManagedApplicationSetSourceNamespaces, appsetsInAnyNamespaceLabelledNS)
1023+
delete(reqState.ManagedApplicationSetSourceNamespaces, appsetsInAnyNamespaceLabelledNS)
10241024
}
10251025
}
10261026
return nil
@@ -1080,9 +1080,9 @@ func (r *ReconcileArgoCD) cleanupUnmanagedApplicationSetSourceNamespaceResources
10801080

10811081
// setManagedApplicationSetSourceNamespaces populates ManagedApplicationSetSourceNamespaces var with namespaces
10821082
// with "argocd.argoproj.io/applicationset-managed-by-cluster-argocd" label.
1083-
func (r *ReconcileArgoCD) setManagedApplicationSetSourceNamespaces(cr *argoproj.ArgoCD) error {
1084-
if r.ManagedApplicationSetSourceNamespaces == nil {
1085-
r.ManagedApplicationSetSourceNamespaces = make(map[string]string)
1083+
func (r *ReconcileArgoCD) setManagedApplicationSetSourceNamespaces(cr *argoproj.ArgoCD, reqState *RequestState) error {
1084+
if reqState.ManagedApplicationSetSourceNamespaces == nil {
1085+
reqState.ManagedApplicationSetSourceNamespaces = make(map[string]string)
10861086
}
10871087
namespaces := &corev1.NamespaceList{}
10881088
listOption := client.MatchingLabels{
@@ -1095,7 +1095,7 @@ func (r *ReconcileArgoCD) setManagedApplicationSetSourceNamespaces(cr *argoproj.
10951095
}
10961096

10971097
for _, namespace := range namespaces.Items {
1098-
r.ManagedApplicationSetSourceNamespaces[namespace.Name] = ""
1098+
reqState.ManagedApplicationSetSourceNamespaces[namespace.Name] = ""
10991099
}
11001100

11011101
return nil

0 commit comments

Comments
 (0)