-
Notifications
You must be signed in to change notification settings - Fork 86
/
main.tf
138 lines (113 loc) · 3.86 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
terraform {
required_version = ">= 1.7.0"
required_providers {
github = {
source = "integrations/github"
version = ">= 6.1"
}
helm = {
source = "hashicorp/helm"
version = ">= 2.12"
}
kind = {
source = "tehcyx/kind"
version = ">= 0.4"
}
kubernetes = {
source = "hashicorp/kubernetes"
version = ">= 2.27"
}
tls = {
source = "hashicorp/tls"
version = ">= 4.0"
}
}
}
# =============================================================================================
# Construct KinD cluster
# =============================================================================================
resource "kind_cluster" "this" {
name = "flux-e2e"
}
# ==========================================
# Initialise a Github project
# ==========================================
resource "github_repository" "this" {
name = var.github_repository
description = var.github_repository
visibility = "public"
auto_init = true
}
# =============================================================================================
# Add deploy key to GitHub repository
# =============================================================================================
resource "tls_private_key" "flux" {
algorithm = "ECDSA"
ecdsa_curve = "P256"
}
resource "github_repository_deploy_key" "this" {
title = "Flux"
repository = github_repository.this.name
key = tls_private_key.flux.public_key_openssh
read_only = "false"
}
# =============================================================================================
# Bootstrap KinD cluster using flux2 helm chart. This chart simply installs flux2
# Ref: https://artifacthub.io/packages/helm/fluxcd-community/flux2
# =============================================================================================
resource "kubernetes_namespace" "flux_system" {
metadata {
name = "flux-system"
}
}
resource "helm_release" "flux2" {
repository = "https://fluxcd-community.github.io/helm-charts"
chart = "flux2"
version = "2.12.4"
name = "flux2"
namespace = "flux-system"
depends_on = [kubernetes_namespace.flux_system]
}
# =============================================================================================
# Bootstrap KinD cluster using flux2-sync helm chart to start reconciliation of resources.
# Ref https://artifacthub.io/packages/helm/fluxcd-community/flux2-sync
# flux2-sync is used to setup the GitRepository and Kustomization resources
# =============================================================================================
resource "kubernetes_secret" "ssh_keypair" {
metadata {
name = "ssh-keypair"
namespace = "flux-system"
}
type = "Opaque"
data = {
"identity.pub" = tls_private_key.flux.public_key_openssh
"identity" = tls_private_key.flux.private_key_pem
"known_hosts" = "github.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEmKSENjQEezOmxkZMy7opKgwFB9nkt5YRrYMjNuG5N87uRgg6CLrbo5wAdT/y6v0mKV0U2w0WZ2YB/++Tpockg="
}
depends_on = [kubernetes_namespace.flux_system]
}
resource "helm_release" "flux2_sync" {
repository = "https://fluxcd-community.github.io/helm-charts"
chart = "flux2-sync"
version = "1.8.2"
# Note: Do not change the name or namespace of this resource. The below mimics the behaviour of "flux bootstrap".
name = "flux-system"
namespace = "flux-system"
set {
name = "gitRepository.spec.url"
value = "ssh://[email protected]/${var.github_org}/${var.github_repository}.git"
}
set {
name = "gitRepository.spec.ref.branch"
value = "main"
}
set {
name = "gitRepository.spec.secretRef.name"
value = kubernetes_secret.ssh_keypair.metadata[0].name
}
set {
name = "gitRepository.spec.interval"
value = "1m"
}
depends_on = [helm_release.flux2]
}