From 7e22bb5660f5bf91b2fa70a42b7c84a810a6696f Mon Sep 17 00:00:00 2001
From: Kevin Coleman <kcaws@amazon.com>
Date: Wed, 3 Nov 2021 16:22:21 -0700
Subject: [PATCH] Adding support for ArgoCD bootstrap apps.

---
 docs/add-ons/argocd.md                | 56 +++++++++++++++++++--------
 kubernetes-addons.tf                  |  7 ++--
 kubernetes-addons/argocd/locals.tf    |  7 ++--
 kubernetes-addons/argocd/main.tf      | 37 ++++++++++++++++++
 kubernetes-addons/argocd/variables.tf |  5 +++
 variables.tf                          |  5 +++
 6 files changed, 95 insertions(+), 22 deletions(-)

diff --git a/docs/add-ons/argocd.md b/docs/add-ons/argocd.md
index a06ed1d3..943d193c 100644
--- a/docs/add-ons/argocd.md
+++ b/docs/add-ons/argocd.md
@@ -3,25 +3,49 @@
 [ArgoCD](https://argo-cd.readthedocs.io/en/stable/) Argo CD is a declarative, GitOps continuous delivery tool for Kubernetes.
 
 Application definitions, configurations, and environments should be declarative and version controlled. Application deployment and lifecycle management should be automated, auditable, and easy to understand.
+
 ## Usage
 
 ArgoCD can be deployed by enabling the add-on via the following.
 
 ```hcl
-  #---------------------------------------
-  # ENABLE ARGOCD
-  #---------------------------------------
-  argocd_enable = true
-
-  # Optional Map value - Override values.yaml for Argo CD
-  argocd_helm_chart = {
-    name             = "argo-cd"
-    chart            = "argo-cd"
-    repository       = "https://argoproj.github.io/argo-helm"
-    version          = "3.26.3"
-    namespace        = "argocd"
-    timeout          = "1200"
-    create_namespace = true
-    values = [templatefile("${path.module}/argocd-values.yaml", {})]
-  }
+#---------------------------------------
+# ENABLE ARGOCD
+#---------------------------------------
+argocd_enable = true
+```
+
+You can optionally customize the Helm chart that deploys ArgoCD via the following configuration. 
+
+```hcl
+argocd_helm_chart = {
+  name             = "argo-cd"
+  chart            = "argo-cd"
+  repository       = "https://argoproj.github.io/argo-helm"
+  version          = "3.26.3"
+  namespace        = "argocd"
+  timeout          = "1200"
+  create_namespace = true
+  values = [templatefile("${path.module}/argocd-values.yaml", {})]
+}
 ```
+
+### Boostrapping 
+
+The framework provides an approach to bootstraping workloads and/or additional add-ons by leveraging the ArgoCD [App of Apps](https://argo-cd.readthedocs.io/en/stable/operator-manual/cluster-bootstrapping/) pattern. 
+
+ The following code example demonstrates how you can supply information for a repository in order to bootstrap multiple workloads in a new EKS cluster. The example leverages a [sample App of Apps repository](https://github.com/aws-samples/ssp-eks-workloads.git) that ships with the EKS SSP solution.
+
+```hcl
+argocd_applications = {
+  workloads = {
+    namespace         = "argocd"
+    repo_path         = "envs/dev"
+    repo_url          = "https://github.com/aws-samples/ssp-eks-workloads.git"
+    target_revision   = "HEAD"
+    destination       = "https://kubernetes.default.svc"
+    project           = "default"
+    values            = {}
+  }
+}
+```
\ No newline at end of file
diff --git a/kubernetes-addons.tf b/kubernetes-addons.tf
index 858248ac..6a9bac9a 100644
--- a/kubernetes-addons.tf
+++ b/kubernetes-addons.tf
@@ -140,9 +140,10 @@ module "aws_opentelemetry_collector" {
 }
 
 module "argocd" {
-  count             = var.create_eks && var.argocd_enable ? 1 : 0
-  source            = "./kubernetes-addons/argocd"
-  argocd_helm_chart = var.argocd_helm_chart
+  count                 = var.create_eks && var.argocd_enable ? 1 : 0
+  source                = "./kubernetes-addons/argocd"
+  argocd_helm_chart     = var.argocd_helm_chart
+  argocd_applications   = var.argocd_applications
 
   depends_on = [module.aws_eks]
 }
diff --git a/kubernetes-addons/argocd/locals.tf b/kubernetes-addons/argocd/locals.tf
index e50ebe46..b1526347 100644
--- a/kubernetes-addons/argocd/locals.tf
+++ b/kubernetes-addons/argocd/locals.tf
@@ -1,11 +1,11 @@
 
 locals {
-  default_argocd_helm_app = {
+  default_argocd_helm_chart = {
     name             = "argo-cd"
     chart            = "argo-cd"
     repository       = "https://argoproj.github.io/argo-helm"
     version          = "3.26.3"
-    namespace        = "argocd"
+    namespace        = "argocd-infra"
     timeout          = "1200"
     create_namespace = true
     values           = local.default_argocd_helm_values
@@ -40,8 +40,9 @@ locals {
     description                = "The argocd HelmChart Ingress Controller deployment configuration"
     postrender                 = ""
   }
+
   argocd_helm_app = merge(
-    local.default_argocd_helm_app,
+    local.default_argocd_helm_chart,
     var.argocd_helm_chart
   )
   default_argocd_helm_values = [templatefile("${path.module}/argocd-values.yaml", {})]
diff --git a/kubernetes-addons/argocd/main.tf b/kubernetes-addons/argocd/main.tf
index 223600df..b1a8b6c4 100644
--- a/kubernetes-addons/argocd/main.tf
+++ b/kubernetes-addons/argocd/main.tf
@@ -73,5 +73,42 @@ resource "helm_release" "argocd" {
       value = each_item.value.value
     }
   }
+}
+
+# ---------------------------------------------------------------------------------------------------------------------
+# ArgoCD App of Apps Bootstrapping
+# ---------------------------------------------------------------------------------------------------------------------
 
+resource "kubernetes_manifest" "argocd_application" {
+    for_each = var.argocd_applications
+    manifest = {
+        apiVersion: "argoproj.io/v1alpha1"
+        kind: "Application"
+        metadata: {
+            name: each.key
+            namespace: each.value.namespace
+        }
+        spec: {
+            destination: {
+                namespace: each.value.namespace
+                server: each.value.destination
+            }
+            project: each.value.project
+            source: {
+                helm: {
+                    values: yamlencode(each.value.values)
+                }
+                path: each.value.repo_path
+                repoURL: each.value.repo_url
+                targetRevision: each.value.target_revision
+            }
+            syncPolicy: {
+                automated: {
+                    prune: true
+                }
+            }
+        }
+    }
+    depends_on = [helm_release.argocd]
 }
+
diff --git a/kubernetes-addons/argocd/variables.tf b/kubernetes-addons/argocd/variables.tf
index ec51c1aa..091bb13d 100644
--- a/kubernetes-addons/argocd/variables.tf
+++ b/kubernetes-addons/argocd/variables.tf
@@ -20,3 +20,8 @@ variable "argocd_helm_chart" {
   type    = any
   default = {}
 }
+
+variable "argocd_applications" {
+  type    = any
+  default = {}
+}
\ No newline at end of file
diff --git a/variables.tf b/variables.tf
index 8892eb19..4344a83f 100644
--- a/variables.tf
+++ b/variables.tf
@@ -371,3 +371,8 @@ variable "argocd_helm_chart" {
   default     = {}
   description = "ARGO CD Kubernetes Addon Configuration"
 }
+variable "argocd_applications" {
+  type    = any
+  default = {}
+  description = "ARGO CD Applications used to bootstrap the cluster."
+}
-- 
GitLab