Last active
October 14, 2025 10:42
-
-
Save cfstras/b4d0cc206a157844894062d7fa439009 to your computer and use it in GitHub Desktop.
Hack to manually re-import kubectl manifests that are already existing in the cluster
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -euo pipefail | |
| backup() { | |
| terraform state pull > state-$(gdate -Iseconds) | |
| } | |
| plan() { | |
| backup | |
| terraform plan -out=tfplan && terraform show -json tfplan | jq . > tfplan.json | |
| } | |
| plan | |
| jq \ | |
| '.resource_changes[] | select(.change.actions[] | contains("create")) | select(.type == "kubectl_manifest") | {address:.address, index:.index, kind: .change.after.kind, apiVersion: .change.after.api_version, namespace: .change.after.namespace, name: .change.after.name} | "terraform import \(.address | gsub("\""; "\\\"")) \(.apiVersion)//\(.kind)//\(.name)\(if .namespace then "//\(.namespace)" else "" end)"' \ | |
| tfplan.json -r | bash -xe | |
| plan | |
| jq \ | |
| '.resource_changes[] | select(.change.actions[] | contains("create")) | select(.type == "kubernetes_namespace") | {address:.address, name: .change.after.metadata[0].name} | "terraform import \(.address | gsub("\""; "\\\"")) \(.name)"' \ | |
| tfplan.json -r | bash -xe | |
| plan | |
| # terraform import module.k8s_resources.helm_release.charts["nvidia-gpu-operator"] gpu-operator/nvidia-gpu-operator | |
| _operator | |
| jq \ | |
| '.resource_changes[] | select(.change.actions[] | contains("create")) | select(.type == "helm_release") | {address:.address, index:.index, namespace: .change.after.namespace, name: .change.after.name} | "terraform import \(.address | gsub("\""; "\\\"")) \(.namespace)/\(.name)"' \ | |
| tfplan.json -r | bash -xe | |
| backup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment