From 0c14bde17ecde96c652e40e7bdef5cfb736879c5 Mon Sep 17 00:00:00 2001
From: Andrii Ostapenko <andrii.ostapenko@att.com>
Date: Thu, 25 Mar 2021 21:42:42 -0500
Subject: [PATCH] Allow to pass iso url from env for remotedirect-ephemeral
 phase

This commit introduces an ability to consume
AIRSHIPCTL_EPHEMERAL_ISO_URL environment variable. Also
it switches from phase patch to catalogue and implements
corresponding replacement transformer for remotedirect
variables.

With this code isoURL catalogue value is only used in an
absense of AIRSHIPCTL_EPHEMERAL_ISO_URL.

Airshipctl does not control how an iso is served leaving it on
discretion of deployment pipeline, also iso contents are fully
determined by image-builder image and manifests, thus this change
does not violate GitOps principles.

Change-Id: Ie6eebe0f0502b2f8e41b081281336299c63f5e4d
Signed-off-by: Andrii Ostapenko <andrii.ostapenko@att.com>
---
 .../function/bootstrap/kustomization.yaml     |  2 ++
 .../bootstrap/replacements/kustomization.yaml |  4 +++
 .../replacements/remotedirect-vars.yaml       | 27 ++++++++++++++++++
 manifests/function/bootstrap/template.yaml    | 28 +++++++++++++++++++
 manifests/phases/executors.yaml               |  4 +--
 manifests/phases/kustomization.yaml           |  3 ++
 .../site/test-site/phases/catalogue.yaml      | 10 +++++++
 .../site/test-site/phases/kustomization.yaml  |  6 ++--
 .../site/test-site/phases/phase-patch.yaml    | 10 -------
 9 files changed, 79 insertions(+), 15 deletions(-)
 create mode 100644 manifests/function/bootstrap/kustomization.yaml
 create mode 100644 manifests/function/bootstrap/replacements/kustomization.yaml
 create mode 100644 manifests/function/bootstrap/replacements/remotedirect-vars.yaml
 create mode 100644 manifests/function/bootstrap/template.yaml
 create mode 100644 manifests/site/test-site/phases/catalogue.yaml
 delete mode 100644 manifests/site/test-site/phases/phase-patch.yaml

diff --git a/manifests/function/bootstrap/kustomization.yaml b/manifests/function/bootstrap/kustomization.yaml
new file mode 100644
index 000000000..b1e944410
--- /dev/null
+++ b/manifests/function/bootstrap/kustomization.yaml
@@ -0,0 +1,2 @@
+generators:
+  - template.yaml
diff --git a/manifests/function/bootstrap/replacements/kustomization.yaml b/manifests/function/bootstrap/replacements/kustomization.yaml
new file mode 100644
index 000000000..59d5ea989
--- /dev/null
+++ b/manifests/function/bootstrap/replacements/kustomization.yaml
@@ -0,0 +1,4 @@
+apiVersion: kustomize.config.k8s.io/v1beta1
+kind: Kustomization
+resources:
+  - remotedirect-vars.yaml
diff --git a/manifests/function/bootstrap/replacements/remotedirect-vars.yaml b/manifests/function/bootstrap/replacements/remotedirect-vars.yaml
new file mode 100644
index 000000000..a42c84f55
--- /dev/null
+++ b/manifests/function/bootstrap/replacements/remotedirect-vars.yaml
@@ -0,0 +1,27 @@
+apiVersion: airshipit.org/v1alpha1
+kind: ReplacementTransformer
+metadata:
+  name: versions-remotedirect-replacement
+  annotations:
+    config.kubernetes.io/function: |-
+      container:
+        image: quay.io/airshipit/replacement-transformer:v2
+replacements:
+- source:
+    objref:
+      name: versions-remotedirect
+    fieldref: spec.remotedirect.isoURL
+  target:
+    objref:
+      kind: BaremetalManager
+      name: RemoteDirectEphemeral
+    fieldrefs: ["spec.operationOptions.remoteDirect.isoURL%ISO_URL%"]
+- source:
+    objref:
+      name: versions-remotedirect
+    fieldref: spec.remotedirect.node
+  target:
+    objref:
+      kind: BaremetalManager
+      name: RemoteDirectEphemeral
+    fieldrefs: ["spec.hostSelector.name%EPHEMERAL_NODE%"]
diff --git a/manifests/function/bootstrap/template.yaml b/manifests/function/bootstrap/template.yaml
new file mode 100644
index 000000000..611e9a313
--- /dev/null
+++ b/manifests/function/bootstrap/template.yaml
@@ -0,0 +1,28 @@
+apiVersion: airshipit.org/v1alpha1
+kind: Templater
+metadata:
+  name: iso-patch-template
+  annotations:
+    config.kubernetes.io/function: |
+      container:
+        image: quay.io/airshipit/templater:latest
+        envs:
+        - AIRSHIPCTL_EPHEMERAL_ISO_URL
+template: |
+  {{- $isoURL := env "AIRSHIPCTL_EPHEMERAL_ISO_URL" }}
+  {{- if not (empty $isoURL) }}
+  apiVersion: builtin
+  kind: PatchStrategicMergeTransformer
+  metadata:
+    name: smp
+  patches: |-
+    ---
+    apiVersion: airshipit.org/v1alpha1
+    kind: BaremetalManager
+    metadata:
+      name: RemoteDirectEphemeral
+    spec:
+      operationOptions:
+        remoteDirect:
+          isoURL: {{ $isoURL }}
+  {{- end }}
diff --git a/manifests/phases/executors.yaml b/manifests/phases/executors.yaml
index 1a8929465..aeb72b3ac 100644
--- a/manifests/phases/executors.yaml
+++ b/manifests/phases/executors.yaml
@@ -196,10 +196,10 @@ metadata:
 spec:
   operation: remote-direct
   hostSelector:
-    name: REPLACE_ME
+    name: EPHEMERAL_NODE
   operationOptions:
     remoteDirect:
-      isoURL: REPLACE_ME
+      isoURL: ISO_URL
 ---
 apiVersion: airshipit.org/v1alpha1
 kind: GenericContainer
diff --git a/manifests/phases/kustomization.yaml b/manifests/phases/kustomization.yaml
index 1d53edebf..c24b42d19 100644
--- a/manifests/phases/kustomization.yaml
+++ b/manifests/phases/kustomization.yaml
@@ -9,3 +9,6 @@ patchesStrategicMerge:
   - clusterctl_init_options.yaml
 transformers:
   - ../function/clusterctl/replacements
+  # NOTE(aostapenko) Consume AIRSHIPCTL_EPHEMERAL_ISO_URL here that will
+  # have precedence over phase catalogue isoURL value
+  - ../function/bootstrap
diff --git a/manifests/site/test-site/phases/catalogue.yaml b/manifests/site/test-site/phases/catalogue.yaml
new file mode 100644
index 000000000..e7feea484
--- /dev/null
+++ b/manifests/site/test-site/phases/catalogue.yaml
@@ -0,0 +1,10 @@
+apiVersion: airshipit.org/v1alpha1
+kind: VersionsCatalogue
+metadata:
+  name: versions-remotedirect
+spec:
+  remotedirect:
+    # NOTE(aostapenko) This value will only be used in absense of
+    # AIRSHIPCTL_EPHEMERAL_ISO_URL environment variable
+    isoURL: http://localhost:8099/ephemeral.iso
+    node: node02
diff --git a/manifests/site/test-site/phases/kustomization.yaml b/manifests/site/test-site/phases/kustomization.yaml
index ec6686c02..55159500e 100644
--- a/manifests/site/test-site/phases/kustomization.yaml
+++ b/manifests/site/test-site/phases/kustomization.yaml
@@ -1,6 +1,6 @@
 resources:
   - ../kubeconfig
   - ../../../phases
-## TODO Consider making a catalogue combined with variable substitution instead
-patchesStrategicMerge:
-  - phase-patch.yaml
\ No newline at end of file
+  - catalogue.yaml
+transformers:
+  - ../../../function/bootstrap/replacements
diff --git a/manifests/site/test-site/phases/phase-patch.yaml b/manifests/site/test-site/phases/phase-patch.yaml
deleted file mode 100644
index 18f031368..000000000
--- a/manifests/site/test-site/phases/phase-patch.yaml
+++ /dev/null
@@ -1,10 +0,0 @@
-apiVersion: airshipit.org/v1alpha1
-kind: BaremetalManager
-metadata:
-  name: RemoteDirectEphemeral
-spec:
-  hostSelector:
-    name: node02
-  operationOptions:
-    remoteDirect:
-      isoURL: http://localhost:8099/ephemeral.iso
\ No newline at end of file