diff --git a/lab.sh b/lab.sh index 8653d12..c5651b7 100755 --- a/lab.sh +++ b/lab.sh @@ -94,6 +94,28 @@ adopt_tofu_kubernetes_resource() { tofu -chdir="${REPO_ROOT}/${stack}" import -input=false "${resource_address}" "${import_id}" } +adopt_tofu_kubernetes_manifest() { + local stack="$1" + local resource_address="$2" + local namespace="$3" + local kubectl_kind="$4" + local api_version="$5" + local manifest_kind="$6" + local resource_name="$7" + local import_id + + if tofu_state_has_resource "${stack}" "${resource_address}"; then + return 0 + fi + if ! kubernetes_resource_exists "${namespace}" "${kubectl_kind}" "${resource_name}"; then + return 0 + fi + + import_id="apiVersion=${api_version},kind=${manifest_kind},namespace=${namespace},name=${resource_name}" + echo "Importing existing Kubernetes ${manifest_kind} ${namespace}/${resource_name} into ${stack} state (${resource_address})..." + tofu -chdir="${REPO_ROOT}/${stack}" import -input=false "${resource_address}" "${import_id}" +} + adopt_platform_existing_resources() { local stack="bootstrap/platform" @@ -122,6 +144,36 @@ adopt_platform_existing_resources() { "monitoring" } +adopt_apps_existing_resources() { + local stack="bootstrap/apps" + local namespace="${TF_VAR_argocd_namespace:-argocd}" + + adopt_tofu_kubernetes_manifest \ + "${stack}" \ + 'kubernetes_manifest.argocd_application["container-registry"]' \ + "${namespace}" \ + "applications.argoproj.io" \ + "argoproj.io/v1alpha1" \ + "Application" \ + "container-registry" + adopt_tofu_kubernetes_manifest \ + "${stack}" \ + 'kubernetes_manifest.argocd_application["website-production"]' \ + "${namespace}" \ + "applications.argoproj.io" \ + "argoproj.io/v1alpha1" \ + "Application" \ + "website-production" + adopt_tofu_kubernetes_manifest \ + "${stack}" \ + 'kubernetes_manifest.argocd_application["demos-static"]' \ + "${namespace}" \ + "applications.argoproj.io" \ + "argoproj.io/v1alpha1" \ + "Application" \ + "demos-static" +} + run_tofu_stack() { local stack="$1" local -a apply_args=(-auto-approve) @@ -134,6 +186,9 @@ run_tofu_stack() { if [[ "${stack}" == "bootstrap/platform" ]]; then adopt_platform_existing_resources fi + if [[ "${stack}" == "bootstrap/apps" ]]; then + adopt_apps_existing_resources + fi tofu -chdir="${REPO_ROOT}/${stack}" apply "${apply_args[@]}" }