-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.tf
129 lines (123 loc) · 3.58 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
locals {
labels = merge({ app = "app-${var.name}" }, var.labels)
}
resource "kubernetes_namespace" "portainer" {
metadata {
name = "${var.name}-ns"
}
}
resource "kubernetes_service_account" "portainer" {
metadata {
name = "${var.name}-sa-clusteradmin"
namespace = kubernetes_namespace.portainer.metadata.0.name
}
automount_service_account_token = true
}
resource "kubernetes_cluster_role_binding" "portainer" {
metadata {
name = "${var.name}-crb-clusteradmin"
}
role_ref {
api_group = "rbac.authorization.k8s.io"
kind = "ClusterRole"
name = "cluster-admin"
}
subject {
kind = "ServiceAccount"
name = kubernetes_service_account.portainer.metadata.0.name
namespace = kubernetes_namespace.portainer.metadata.0.name
}
}
resource "kubernetes_persistent_volume_claim" "portainer" {
metadata {
name = "${var.name}-pvc"
namespace = kubernetes_namespace.portainer.metadata.0.name
}
spec {
access_modes = ["ReadWriteOnce"]
storage_class_name = var.storage_class
resources {
requests = {
storage = var.storage_size
}
}
}
}
resource "kubernetes_deployment" "portainer" {
metadata {
name = "${var.name}-deployment"
namespace = kubernetes_namespace.portainer.metadata.0.name
}
spec {
selector {
match_labels = local.labels
}
template {
metadata {
labels = local.labels
}
spec {
service_account_name = kubernetes_service_account.portainer.metadata.0.name
container {
name = var.name
image = "portainer/portainer-k8s-beta:linux-amd64"
image_pull_policy = "Always"
port {
name = "edge"
protocol = "TCP"
container_port = 8000
}
port {
name = "http"
protocol = "TCP"
container_port = 9000
}
volume_mount {
mount_path = "/var/run/secrets/kubernetes.io/serviceaccount"
name = kubernetes_service_account.portainer.default_secret_name
read_only = true
}
volume_mount {
mount_path = "/data"
name = "data"
read_only = false
}
}
volume {
name = kubernetes_service_account.portainer.default_secret_name
secret {
secret_name = kubernetes_service_account.portainer.default_secret_name
}
}
volume {
name = "data"
persistent_volume_claim {
claim_name = kubernetes_persistent_volume_claim.portainer.metadata.0.name
}
}
}
}
}
}
resource "kubernetes_service" "portainer" {
metadata {
name = "${var.name}-service"
namespace = kubernetes_namespace.portainer.metadata.0.name
}
spec {
type = var.service_type
selector = kubernetes_deployment.portainer.spec.0.selector.0.match_labels
port {
name = "edge"
protocol = kubernetes_deployment.portainer.spec.0.template.0.spec.0.container.0.port.0.protocol
port = var.edge_port
target_port = kubernetes_deployment.portainer.spec.0.template.0.spec.0.container.0.port.0.container_port
}
port {
name = "http"
protocol = kubernetes_deployment.portainer.spec.0.template.0.spec.0.container.0.port.1.protocol
port = var.http_port
target_port = kubernetes_deployment.portainer.spec.0.template.0.spec.0.container.0.port.1.container_port
}
}
}