Adopt existing Argo CD applications before apply
Homelab Main / deploy (push) Successful in 1m19s Details

This commit is contained in:
juvdiaz 2026-05-28 00:31:39 -06:00
parent ab91142b06
commit c3f08e3fdd
1 changed files with 55 additions and 0 deletions

55
lab.sh
View File

@ -94,6 +94,28 @@ adopt_tofu_kubernetes_resource() {
tofu -chdir="${REPO_ROOT}/${stack}" import -input=false "${resource_address}" "${import_id}" 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() { adopt_platform_existing_resources() {
local stack="bootstrap/platform" local stack="bootstrap/platform"
@ -122,6 +144,36 @@ adopt_platform_existing_resources() {
"monitoring" "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() { run_tofu_stack() {
local stack="$1" local stack="$1"
local -a apply_args=(-auto-approve) local -a apply_args=(-auto-approve)
@ -134,6 +186,9 @@ run_tofu_stack() {
if [[ "${stack}" == "bootstrap/platform" ]]; then if [[ "${stack}" == "bootstrap/platform" ]]; then
adopt_platform_existing_resources adopt_platform_existing_resources
fi fi
if [[ "${stack}" == "bootstrap/apps" ]]; then
adopt_apps_existing_resources
fi
tofu -chdir="${REPO_ROOT}/${stack}" apply "${apply_args[@]}" tofu -chdir="${REPO_ROOT}/${stack}" apply "${apply_args[@]}"
} }