A tool for converting Kubernetes YAML manifests to Terraform HCL
A tool for converting Kubernetes YAML manifests to Terraform HCL
tfk8s is a tool that makes it easier to work with the Terraform Kubernetes Provider.
If you want to copy examples from the Kubernetes documentation or migrate existing YAML manifests and use them with Terraform without having to convert YAML to HCL by hand, this tool is for you.
kubectl get $R -o yaml | tfk8s --stripgo install github.com/jrhouston/tfk8s@latestAlternatively, clone this repo and run:
make installIf Go's bin directory is not in your PATH you will need to add it:
export PATH=$PATH:$(go env GOPATH)/binOr you can install via brew for macOS/Linux:
brew install tfk8sOn macOS, you can also install via MacPorts:
sudo port install tfk8sUsage of tfk8s:
-f, --file string Input file containing Kubernetes YAML manifests (default "-")
-M, --map-only Output only an HCL map structure
-o, --output string Output file to write Terraform config (default "-")
-p, --provider provider Provider alias to populate the provider attribute
-s, --strip Strip out server side fields - use if you are piping from kubectl get
-Q, --strip-key-quotes Strip out quotes from HCL map keys unless they are required.
-V, --version Show tool versiontfk8s -f input.yaml -o output.tfor, using pipes:
cat input.yaml | tfk8s > output.tfinput.yaml:
---
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
TEST: test✨✨ magically becomes ✨✨
output.tf:
resource "kubernetes_manifest" "configmap_test" {
manifest = {
"apiVersion" = "v1"
"data" = {
"TEST" = "test"
}
"kind" = "ConfigMap"
"metadata" = {
"name" = "test"
}
}
}kubectl get ns default -o yaml | tfk8s -M{
"apiVersion" = "v1"
"kind" = "Namespace"
"metadata" = {
"creationTimestamp" = "2020-05-02T15:01:32Z"
"name" = "default"
"resourceVersion" = "147"
"selfLink" = "/api/v1/namespaces/default"
"uid" = "6ac3424c-07a4-4a69-86ae-cc7a4ae72be3"
}
"spec" = {
"finalizers" = [
"kubernetes",
]
}
"status" = {
"phase" = "Active"
}
}You can use helm template to generate a manifest from the chart, then pipe it into tfk8s:
helm template ./chart-path -f values.yaml | tfk8sYou can use tfk8s in conjunction with find to convert an entire directory recursively:
find dirname/ -name '*.yaml' -type f -exec sh -c 'tfk8s -f {} -o $(echo {} | sed "s/\.[^.]*$//").tf' \;No open issues yet, or sync has not completed.