-
Notifications
You must be signed in to change notification settings - Fork 1
/
14_service.tf
178 lines (144 loc) · 6.18 KB
/
14_service.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
locals {
serviceClusterIpEnabled = length(local.serviceClusterIpPorts) > 0
serviceHeadlessEnabled = length(local.serviceHeadlessPorts) > 0
serviceLoadBalancerTcpRequired = length(local.serviceLoadBalancerPortsTcp) > 0
serviceLoadBalancerTcpEnabled = local.serviceLoadBalancerTcpRequired && length(var.service.loadBalancer) > 0
serviceLoadBalancerUdpRequired = length(local.serviceLoadBalancerPortsUdp) > 0
serviceLoadBalancerUdpEnabled = local.serviceLoadBalancerUdpRequired && length(var.service.loadBalancer) > 0
serviceLoadBalancerEnabled = local.serviceLoadBalancerTcpEnabled || local.serviceLoadBalancerUdpEnabled
ports = flatten([
for k, v in var.containers : v.ports
])
serviceClusterIpPorts = flatten([
for k, v in local.ports : contains(v.serviceTypes, "ClusterIP") ? [v] : []
])
serviceHeadlessPorts = flatten([
for k, v in local.ports : contains(v.serviceTypes, "Headless") ? [v] : []
])
serviceLoadBalancerPorts = flatten([
for k, v in local.ports : contains(v.serviceTypes, "LoadBalancer") ? [v] : []
])
serviceLoadBalancerPortsTcp = flatten([
for k, v in local.serviceLoadBalancerPorts : v.protocol == "TCP" ? [v] : []
])
serviceLoadBalancerPortsUdp = flatten([
for k, v in local.serviceLoadBalancerPorts : v.protocol == "UDP" ? [v] : []
])
serviceName = var.consistency.hard.namespaceUniqueName
headlessServiceName = "${var.consistency.hard.namespaceUniqueName}-headless"
}
variable "service" {
type = object({
clusterIp = optional(object({
ip = optional(string, null)
publishNotReadyAddresses = optional(bool, false)
sessionAffinity = optional(string, "None")
annotations = optional(map(string), {})
remapPorts = optional(map(string), {})
}), {})
headless = optional(object({
publishNotReadyAddresses = optional(bool, false)
sessionAffinity = optional(string, "None")
annotations = optional(map(string), {})
remapPorts = optional(map(string), {})
}), {})
loadBalancer = optional(list(object({
publishNotReadyAddresses = optional(bool, false)
allocateLoadBalancerNodePorts = optional(bool, false)
sessionAffinity = optional(string, "None")
externalTrafficPolicy = optional(string, "Cluster")
sourceRanges = optional(list(string), [])
annotations = optional(map(string), {})
remapPorts = optional(map(string), {})
forceNodePortType = optional(bool, false)
})), [])
})
default = {}
}
resource "kubernetes_service_v1" "clusterIp" {
count = local.serviceClusterIpEnabled ? 1 : 0
metadata {
name = local.serviceName
namespace = var.consistency.hard.namespace
labels = var.consistency.soft.labels
annotations = var.service.clusterIp.annotations
}
spec {
type = "ClusterIP"
cluster_ip = var.service.clusterIp.ip
publish_not_ready_addresses = var.service.clusterIp.publishNotReadyAddresses
session_affinity = var.service.clusterIp.sessionAffinity
dynamic "port" {
for_each = local.serviceClusterIpPorts
content {
name = port.value.name
target_port = port.value.name
port = lookup(var.service.clusterIp.remapPorts, port.value.name, port.value.port)
protocol = port.value.protocol
}
}
selector = var.consistency.soft.matchLabels
}
}
resource "kubernetes_service_v1" "headless" {
count = local.serviceHeadlessEnabled ? 1 : 0
metadata {
name = local.headlessServiceName
namespace = var.consistency.hard.namespace
labels = var.consistency.soft.labels
annotations = var.service.headless.annotations
}
spec {
type = "ClusterIP"
cluster_ip = "None"
publish_not_ready_addresses = var.service.headless.publishNotReadyAddresses
session_affinity = var.service.headless.sessionAffinity
dynamic "port" {
for_each = local.serviceHeadlessPorts
content {
name = port.value.name
target_port = port.value.name
port = lookup(var.service.headless.remapPorts, port.value.name, port.value.port)
protocol = port.value.protocol
}
}
selector = var.consistency.soft.matchLabels
}
}
resource "kubernetes_service_v1" "loadBalancer" {
count = local.serviceLoadBalancerEnabled ? length(var.service.loadBalancer) : 0
wait_for_load_balancer = var.tfWaitForRollout
metadata {
name = "${local.serviceName}-${count.index}"
namespace = var.consistency.hard.namespace
labels = var.consistency.soft.labels
annotations = var.service.loadBalancer[count.index].annotations
}
spec {
type = var.service.loadBalancer[count.index].forceNodePortType ? "NodePort" : "LoadBalancer"
publish_not_ready_addresses = var.service.loadBalancer[count.index].publishNotReadyAddresses
allocate_load_balancer_node_ports = var.service.loadBalancer[count.index].allocateLoadBalancerNodePorts
session_affinity = var.service.loadBalancer[count.index].sessionAffinity
external_traffic_policy = var.service.loadBalancer[count.index].externalTrafficPolicy
load_balancer_source_ranges = var.service.loadBalancer[count.index].sourceRanges
dynamic "port" {
for_each = local.serviceLoadBalancerPortsTcp
content {
name = port.value.name
target_port = port.value.name
port = lookup(var.service.loadBalancer[count.index].remapPorts, port.value.name, port.value.port)
protocol = port.value.protocol
}
}
dynamic "port" {
for_each = local.serviceLoadBalancerPortsUdp
content {
name = port.value.name
target_port = port.value.name
port = lookup(var.service.loadBalancer[count.index].remapPorts, port.value.name, port.value.port)
protocol = port.value.protocol
}
}
selector = var.consistency.soft.matchLabels
}
}