From 48ab3573890efa4e00f143c251d79d72b547befc Mon Sep 17 00:00:00 2001
From: Julien Maupetit <julien@maupetit.net>
Date: Wed, 5 Sep 2018 11:40:03 +0200
Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=90=9B(deploy)=20remove=20BCs=20creat?=
 =?UTF-8?q?ed=20during=20a=20deployment?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When Arnold clean a running stack labelled with a given
deployment_stamp, we also need to remove created BuildConfigs.
---
 tasks/delete_app.yml | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tasks/delete_app.yml b/tasks/delete_app.yml
index 29ccccd5d..db04eafc9 100644
--- a/tasks/delete_app.yml
+++ b/tasks/delete_app.yml
@@ -13,6 +13,7 @@
     obj: "{{ lookup('openshift', api_version='v1', namespace=project_name, kind=item, label_selector='app=' + app.name + ',deployment_stamp=' + targeted_deployment_stamp) | zip_longest([{'kind': item}], fillvalue={'kind': item}) | list }}"
   register: raw_selected_objects
   loop:
+    - BuildConfig
     - ConfigMap
     - DeploymentConfig
     - Job

From 945b675be2cb1b8c71e10de8444e8139eda24bb9 Mon Sep 17 00:00:00 2001
From: Julien Maupetit <julien@maupetit.net>
Date: Wed, 5 Sep 2018 11:42:20 +0200
Subject: [PATCH 2/4] =?UTF-8?q?=E2=9C=A8(build)=20add=20variables=20to=20e?=
 =?UTF-8?q?nable=20APM?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

APM layer must be enabled or disabled given particular env_types or
customers. This can be achieved through the is_apm_enabled
configuration. BuildConfigs for apps should use this variable to
conditionally activate this feature during the build.
---
 group_vars/all/main.yml               | 6 ++++++
 group_vars/env_type/preproduction.yml | 3 +++
 group_vars/env_type/production.yml    | 3 +++
 group_vars/env_type/staging.yml       | 3 +++
 4 files changed, 15 insertions(+)

diff --git a/group_vars/all/main.yml b/group_vars/all/main.yml
index f6e7e9a70..f06dacfb6 100644
--- a/group_vars/all/main.yml
+++ b/group_vars/all/main.yml
@@ -65,6 +65,12 @@ secret_id: "1.0.0"
 # OpenShift's internal docker registry server
 internal_docker_registry: "docker-registry.default.svc:5000"
 
+# Set is_apm_enabled to "true" to enable APM in all apps where defined templates
+# include this feature (default: "false")
+is_apm_enabled: false
+# APM agent port defaults to Datadog agent service port
+apm_agent_port: 8126
+
 # TODO: move the following settings to the redirect app
 # Ports
 aliases_port: 8999
diff --git a/group_vars/env_type/preproduction.yml b/group_vars/env_type/preproduction.yml
index 11dca4bba..2e069804c 100644
--- a/group_vars/env_type/preproduction.yml
+++ b/group_vars/env_type/preproduction.yml
@@ -1,2 +1,5 @@
 # Variables specific to the pre-production environment
 richie_django_configuration: PreProduction
+
+# Enable APM in all apps where defined templates include this feature
+is_apm_enabled: true
diff --git a/group_vars/env_type/production.yml b/group_vars/env_type/production.yml
index 3b682b513..deb931a61 100644
--- a/group_vars/env_type/production.yml
+++ b/group_vars/env_type/production.yml
@@ -1,3 +1,6 @@
 # Variables specific to the production environment
 richie_django_configuration: Production
 acme_env: live
+
+# Enable APM in all apps where defined templates include this feature
+is_apm_enabled: true
diff --git a/group_vars/env_type/staging.yml b/group_vars/env_type/staging.yml
index 452f92c87..256726bfb 100644
--- a/group_vars/env_type/staging.yml
+++ b/group_vars/env_type/staging.yml
@@ -1,2 +1,5 @@
 # Variables specific to the staging environment
 richie_django_configuration: Staging
+
+# Enable APM in all apps where defined templates include this feature
+is_apm_enabled: true

From bf5c135b34e5cef977c031db72a41d98972c44a5 Mon Sep 17 00:00:00 2001
From: Julien Maupetit <julien@maupetit.net>
Date: Wed, 5 Sep 2018 11:45:50 +0200
Subject: [PATCH 3/4] =?UTF-8?q?=E2=9C=A8(richie)=20enable=20datadog=20APM?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Enabling datadog APM can be summed as:

* installing datadog agent
* wrap the original command with the agent
* add configs (env vars) + secrets
---
 apps/richie/templates/app/bc.yml.j2 |  7 ++++++-
 apps/richie/templates/app/dc.yml.j2 | 12 ++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/apps/richie/templates/app/bc.yml.j2 b/apps/richie/templates/app/bc.yml.j2
index 4e80b9128..cd25b0478 100644
--- a/apps/richie/templates/app/bc.yml.j2
+++ b/apps/richie/templates/app/bc.yml.j2
@@ -29,7 +29,12 @@ spec:
   source:
     dockerfile: |-
       FROM {{ richie_image_name }}:{{ richie_image_tag }}
-      # Add new statements here
+      USER 0
+      {% if is_apm_enabled -%}
+      RUN pip install ddtrace
+      CMD ddtrace-run gunicorn -c /usr/local/etc/gunicorn/richie.py richie.wsgi:application
+      {% endif -%}
+      USER 10000
   triggers:
     - type: "ConfigChange"
   output:
diff --git a/apps/richie/templates/app/dc.yml.j2 b/apps/richie/templates/app/dc.yml.j2
index 974835e81..0cfac0bdd 100644
--- a/apps/richie/templates/app/dc.yml.j2
+++ b/apps/richie/templates/app/dc.yml.j2
@@ -40,6 +40,18 @@ spec:
               value: "{{ richie_host }}"
             - name: ES_CLIENT
               value: "richie-{{ richie_elasticsearch_host }}-{{ deployment_stamp }}"
+            {% if is_apm_enabled -%}
+            - name: DATADOG_TRACE_AGENT_HOSTNAME
+              valueFrom:
+                fieldRef:
+                  fieldPath: status.hostIP
+            - name: DD_AGENT_SERVICE_HOST
+              valueFrom:
+                fieldRef:
+                  fieldPath: status.hostIP
+            - name: DD_AGENT_SERVICE_PORT
+              value: "{{ apm_agent_port }}"
+            {% endif -%}
           envFrom:
             - secretRef:
                 name: richie-{{ secret_id }}

From 60fc2a97ac77e5ae81d443544b814fd61d32925d Mon Sep 17 00:00:00 2001
From: Julien Maupetit <julien@maupetit.net>
Date: Wed, 5 Sep 2018 17:17:43 +0200
Subject: [PATCH 4/4] =?UTF-8?q?fixup!=20=E2=9C=A8(richie)=20enable=20datad?=
 =?UTF-8?q?og=20APM?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 apps/richie/templates/app/dc.yml.j2 | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/apps/richie/templates/app/dc.yml.j2 b/apps/richie/templates/app/dc.yml.j2
index 0cfac0bdd..71018dc23 100644
--- a/apps/richie/templates/app/dc.yml.j2
+++ b/apps/richie/templates/app/dc.yml.j2
@@ -40,6 +40,7 @@ spec:
               value: "{{ richie_host }}"
             - name: ES_CLIENT
               value: "richie-{{ richie_elasticsearch_host }}-{{ deployment_stamp }}"
+
             {% if is_apm_enabled -%}
             - name: DATADOG_TRACE_AGENT_HOSTNAME
               valueFrom:
@@ -51,7 +52,8 @@ spec:
                   fieldPath: status.hostIP
             - name: DD_AGENT_SERVICE_PORT
               value: "{{ apm_agent_port }}"
-            {% endif -%}
+            {%- endif %}
+
           envFrom:
             - secretRef:
                 name: richie-{{ secret_id }}
