|
1 | 1 | # terraform-provider-flux |
2 | 2 |
|
3 | | -This is a Terraform provider for Flux v2, it enables bootstrap a Kubernetes custer with Flux v2 using terraform. |
| 3 | +This is the Terraform provider for Flux v2. |
| 4 | +The provider allows you to install Flux on Kubernetes |
| 5 | +and configure it to reconcile the cluster state from a Git repository. |
4 | 6 |
|
5 | 7 | ## Example Usage |
6 | 8 |
|
7 | | -The provider is consists of two data sources `flux_install` and `flux_sync` the data sources are corresponding to [fluxv2 manifests ](https://pkg.go.dev/github.com/fluxcd/[email protected]/pkg/manifestgen) |
| 9 | +The provider consists of two data sources `flux_install` and `flux_sync`, |
| 10 | +the data sources are corresponding to [fluxv2 manifests ](https://pkg.go.dev/github.com/fluxcd/[email protected]/pkg/manifestgen). |
8 | 11 |
|
9 | | -The data sources are returing `YAML` manifest so a second provider is needed to apply the manifest into the Kubernetes cluster. See example folder. |
10 | | - |
11 | | -The `flux_install` generates manifests to install the `fluxv2` components. |
| 12 | +The `flux_install` data source generates a multi-doc YAML with Kubernetes manifests that can be used to install or upgrade Flux: |
12 | 13 |
|
13 | 14 | ```hcl |
14 | | -# Flux |
| 15 | +# Generate manifests |
15 | 16 | data "flux_install" "main" { |
16 | | - target_path = "staging-cluster" |
| 17 | + target_path = "production" |
| 18 | + arch = "amd64" |
| 19 | + network_policy = false |
| 20 | + version = "latest" |
| 21 | +} |
| 22 | +
|
| 23 | +# Split multi-doc YAML with |
| 24 | +# https://registry.terraform.io/providers/gavinbunney/kubectl/latest |
| 25 | +data "kubectl_file_documents" "apply" { |
| 26 | + content = data.flux_install.main.content |
| 27 | +} |
| 28 | +
|
| 29 | +# Apply manifests on the cluster |
| 30 | +resource "kubectl_manifest" "apply" { |
| 31 | + for_each = { for v in data.kubectl_file_documents.apply.documents : sha1(v) => v } |
| 32 | + yaml_body = each.value |
17 | 33 | } |
18 | 34 | ``` |
19 | 35 |
|
20 | | -`flux_sync` the initial source manifest. |
| 36 | +The `flux_sync` data source generates a multi-doc YAML containing the `GitRepository` and `Kustomization` |
| 37 | +manifests that configure Flux to sync the cluster with the specified repository: |
21 | 38 |
|
22 | 39 | ```hcl |
| 40 | +# Generate manifests |
23 | 41 | data "flux_sync" "main" { |
24 | | - target_path = "staging-cluster" |
25 | | - url = "ssh://[email protected]/${var.github_owner}/${var.repository_name}.git" |
| 42 | + target_path = "production" |
| 43 | + url = "https://github.com/${var.github_owner}/${var.repository_name}" |
| 44 | +} |
| 45 | +
|
| 46 | +# Split multi-doc YAML with |
| 47 | +# https://registry.terraform.io/providers/gavinbunney/kubectl/latest |
| 48 | +data "kubectl_file_documents" "sync" { |
| 49 | + content = data.flux_sync.main.content |
| 50 | +} |
| 51 | +
|
| 52 | +# Apply manifests on the cluster |
| 53 | +resource "kubectl_manifest" "sync" { |
| 54 | + depends_on = [kubectl_manifest.apply] |
| 55 | +
|
| 56 | + for_each = { for v in data.kubectl_file_documents.sync.documents : sha1(v) => v } |
| 57 | + yaml_body = each.value |
| 58 | +} |
| 59 | +
|
| 60 | +# Generate a Kubernetes secret with the Git credentials |
| 61 | +resource "kubernetes_secret" "main" { |
| 62 | + depends_on = [kubectl_manifest.apply] |
| 63 | +
|
| 64 | + metadata { |
| 65 | + name = data.flux_sync.main.name |
| 66 | + namespace = data.flux_sync.main.namespace |
| 67 | + } |
| 68 | +
|
| 69 | + data = { |
| 70 | + username = "git" |
| 71 | + password = var.flux_token |
| 72 | + } |
26 | 73 | } |
27 | 74 | ``` |
28 | 75 |
|
|
0 commit comments