From 30691affe8e49571278791d64f8fbcf64bd2bb42 Mon Sep 17 00:00:00 2001
From: Pallav Gupta <pallavgupta84@gmail.com>
Date: Mon, 21 Jun 2021 18:00:01 -0700
Subject: [PATCH] Added Image Repositories replacement for KCP

Change-Id: I454f5e9b92290d0c5b24830621a396f07354f90b
---
 .../versions-airshipctl.yaml                  |  2 ++
 .../airshipit.org_versionscatalogues.yaml     |  4 ++++
 .../ephemeral/replacements/kustomization.yaml |  1 +
 .../ephemeral/replacements/versions.yaml      | 21 +++++++++++++++++++
 manifests/function/ephemeral/secret.yaml      |  3 ++-
 .../function/k8scontrol/controlplane.yaml     |  1 +
 .../k8scontrol/replacements/versions.yaml     | 12 ++++++++++-
 7 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100644 manifests/function/ephemeral/replacements/versions.yaml

diff --git a/manifests/function/airshipctl-base-catalogues/versions-airshipctl.yaml b/manifests/function/airshipctl-base-catalogues/versions-airshipctl.yaml
index cb646c081..b7cb6d840 100644
--- a/manifests/function/airshipctl-base-catalogues/versions-airshipctl.yaml
+++ b/manifests/function/airshipctl-base-catalogues/versions-airshipctl.yaml
@@ -161,3 +161,5 @@ spec:
           image: quay.io/airshipit/image-builder:latest-ubuntu_focal
 
   kubernetes: v1.18.6
+
+  image_repositories: k8s.gcr.io
diff --git a/manifests/function/airshipctl-schemas/airshipit.org_versionscatalogues.yaml b/manifests/function/airshipctl-schemas/airshipit.org_versionscatalogues.yaml
index 7a8b6b4c2..450fee54f 100644
--- a/manifests/function/airshipctl-schemas/airshipit.org_versionscatalogues.yaml
+++ b/manifests/function/airshipctl-schemas/airshipit.org_versionscatalogues.yaml
@@ -227,6 +227,10 @@ spec:
                 description: Allows for the specification of the kubernetes version
                   being used.
                 type: string
+              image_repositories:
+                description: Allows for the specification of the image repositories
+                  being used.
+                type: string
             type: object
         type: object
     served: true
diff --git a/manifests/function/ephemeral/replacements/kustomization.yaml b/manifests/function/ephemeral/replacements/kustomization.yaml
index 1947a24c4..7eff830ac 100644
--- a/manifests/function/ephemeral/replacements/kustomization.yaml
+++ b/manifests/function/ephemeral/replacements/kustomization.yaml
@@ -3,6 +3,7 @@ kind: Kustomization
 resources:
   - ephemeral-env-vars.yaml
   - networking.yaml
+  - versions.yaml
   - generated-secrets.yaml
   - stringData-encode.yaml
   - cleanup.yaml
diff --git a/manifests/function/ephemeral/replacements/versions.yaml b/manifests/function/ephemeral/replacements/versions.yaml
new file mode 100644
index 000000000..5977eecc5
--- /dev/null
+++ b/manifests/function/ephemeral/replacements/versions.yaml
@@ -0,0 +1,21 @@
+# These rules inject versioned artifacts into the ephemeral function.
+apiVersion: airshipit.org/v1alpha1
+kind: ReplacementTransformer
+metadata:
+  name: ephemeral-versions-replacements
+  annotations:
+    config.kubernetes.io/function: |-
+      container:
+        image: localhost/replacement-transformer
+replacements:
+# Replace the imageRepository in the KubeadmEphemeral
+- source:
+    objref:
+      kind: VersionsCatalogue
+      name: versions-airshipctl
+    fieldref: "{.spec.image_repositories}"
+  target:
+    objref:
+      name: ephemeral-bmc-secret
+      kind: Secret
+    fieldrefs: ["stringData.userData%REPLACEMENT_IMAGE_REPOSITORY%"]
diff --git a/manifests/function/ephemeral/secret.yaml b/manifests/function/ephemeral/secret.yaml
index 2e6abbcd7..24af97d1f 100644
--- a/manifests/function/ephemeral/secret.yaml
+++ b/manifests/function/ephemeral/secret.yaml
@@ -81,6 +81,7 @@ stringData:
         ---
         apiServer:
           certSANs: REPLACEMENT_CERT_SANS
+        imageRepository: REPLACEMENT_IMAGE_REPOSITORY
         apiVersion: kubeadm.k8s.io/v1beta2
         controllerManager: {}
         dns:
@@ -132,4 +133,4 @@ stringData:
     - mkdir -p /var/lib/docker-engine /mnt/ephemeral/var/lib/docker-engine
     - mount --bind /mnt/ephemeral/var/lib/docker-engine /var/lib/docker-engine
     - mkdir -p /mnt/ephemeral/var/lib/kubelet/ /var/lib/kubelet/
-    - mount --bind /mnt/ephemeral/var/lib/kubelet/ /var/lib/kubelet/
\ No newline at end of file
+    - mount --bind /mnt/ephemeral/var/lib/kubelet/ /var/lib/kubelet/
diff --git a/manifests/function/k8scontrol/controlplane.yaml b/manifests/function/k8scontrol/controlplane.yaml
index 705bde3bb..5f86956fd 100644
--- a/manifests/function/k8scontrol/controlplane.yaml
+++ b/manifests/function/k8scontrol/controlplane.yaml
@@ -13,6 +13,7 @@ spec:
     clusterConfiguration:
       apiServer:
         timeoutForControlPlane: 1000s
+      imageRepository: k8s.gcr.io
     files:
       - path: "/etc/systemd/system/containerd.service.d/http-proxy.conf"
         content: |
diff --git a/manifests/function/k8scontrol/replacements/versions.yaml b/manifests/function/k8scontrol/replacements/versions.yaml
index c5461cd44..0e288a77b 100644
--- a/manifests/function/k8scontrol/replacements/versions.yaml
+++ b/manifests/function/k8scontrol/replacements/versions.yaml
@@ -30,4 +30,14 @@ replacements:
       kind: Metal3MachineTemplate
       name: cluster-controlplane
     fieldrefs: ["{.spec.template.spec.image}"]
-
+# Replace the imageRepository in the KubeadmControlPlane
+- source:
+    objref:
+      kind: VersionsCatalogue
+      name: versions-airshipctl
+    fieldref: "{.spec.image_repositories}"
+  target:
+    objref:
+      kind: KubeadmControlPlane
+      name: cluster-controlplane
+    fieldrefs: ["{.spec.kubeadmConfigSpec.clusterConfiguration.imageRepository}"]