Terraform module to deploy alertmanager webhook servicenow on kubernetes.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
4.9KB

  1. #####
  2. # Locals
  3. #####
  4. locals {
  5. application_version = var.application_version
  6. labels = {
  7. name = "alertmanager-webhook-servicenow"
  8. component = "exporter"
  9. part-of = "monitoring"
  10. managed-by = "terraform"
  11. version = local.application_version
  12. }
  13. }
  14. #####
  15. # Randoms
  16. #####
  17. resource "random_string" "selector" {
  18. special = false
  19. upper = false
  20. number = false
  21. length = 8
  22. }
  23. #####
  24. # Deployment
  25. #####
  26. resource "kubernetes_deployment" "this" {
  27. count = var.enabled ? 1 : 0
  28. metadata {
  29. name = var.deployment_name
  30. namespace = var.namespace
  31. annotations = merge(
  32. var.annotations,
  33. var.deployment_annotations
  34. )
  35. labels = merge(
  36. {
  37. instance = var.deployment_name
  38. },
  39. local.labels,
  40. var.labels,
  41. var.deployment_labels
  42. )
  43. }
  44. spec {
  45. replicas = var.replicas
  46. selector {
  47. match_labels = {
  48. app = random_string.selector.result
  49. }
  50. }
  51. template {
  52. metadata {
  53. annotations = merge(
  54. {
  55. "configuration/hash" = sha256(var.configuration)
  56. "secret/hash" = sha256("${var.user_name}${var.password}${var.instance_name}")
  57. },
  58. var.annotations,
  59. var.deployment_annotations
  60. )
  61. labels = merge(
  62. {
  63. instance = var.deployment_name
  64. app = random_string.selector.result
  65. },
  66. local.labels,
  67. var.labels,
  68. var.deployment_labels
  69. )
  70. }
  71. spec {
  72. volume {
  73. name = "configuration-volume"
  74. config_map {
  75. name = element(concat(kubernetes_config_map.this.*.metadata.0.name, list("")), 0)
  76. }
  77. }
  78. container {
  79. name = "alertmanager-webhook-servicenow"
  80. image = "fxinnovation/alertmanager-webhook-servicenow:${local.application_version}"
  81. image_pull_policy = var.image_pull_policy
  82. volume_mount {
  83. name = "configuration-volume"
  84. mount_path = "/config"
  85. }
  86. port {
  87. name = "http"
  88. container_port = 9877
  89. protocol = "TCP"
  90. }
  91. env {
  92. name = "SERVICENOW_INSTANCE_NAME"
  93. value_from {
  94. secret_key_ref {
  95. name = element(concat(kubernetes_secret.this.*.metadata.0.name, list("")), 0)
  96. key = "instance_name"
  97. }
  98. }
  99. }
  100. env {
  101. name = "SERVICENOW_USERNAME"
  102. value_from {
  103. secret_key_ref {
  104. name = element(concat(kubernetes_secret.this.*.metadata.0.name, list("")), 0)
  105. key = "user_name"
  106. }
  107. }
  108. }
  109. env {
  110. name = "SERVICENOW_PASSWORD"
  111. value_from {
  112. secret_key_ref {
  113. name = element(concat(kubernetes_secret.this.*.metadata.0.name, list("")), 0)
  114. key = "password"
  115. }
  116. }
  117. }
  118. resources {
  119. requests {
  120. memory = "64Mi"
  121. cpu = "50m"
  122. }
  123. limits {
  124. memory = "128Mi"
  125. cpu = "100m"
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }
  132. }
  133. #####
  134. # Service
  135. #####
  136. resource "kubernetes_service" "this" {
  137. count = var.enabled ? 1 : 0
  138. metadata {
  139. name = var.service_name
  140. namespace = var.namespace
  141. annotations = merge(
  142. var.annotations,
  143. var.service_annotations
  144. )
  145. labels = merge(
  146. {
  147. instance = var.service_name
  148. },
  149. local.labels,
  150. var.labels,
  151. var.service_labels
  152. )
  153. }
  154. spec {
  155. selector = {
  156. app = random_string.selector.result
  157. }
  158. type = "ClusterIP"
  159. port {
  160. port = var.port
  161. target_port = "http"
  162. protocol = "TCP"
  163. name = "http"
  164. }
  165. }
  166. }
  167. #####
  168. # ConfigMap
  169. #####
  170. resource "kubernetes_config_map" "this" {
  171. count = var.enabled ? 1 : 0
  172. metadata {
  173. name = var.config_map_name
  174. namespace = var.namespace
  175. annotations = merge(
  176. var.annotations,
  177. var.config_map_annotations
  178. )
  179. labels = merge(
  180. {
  181. instance = var.config_map_name
  182. },
  183. local.labels,
  184. var.labels,
  185. var.config_map_labels
  186. )
  187. }
  188. data = {
  189. "servicenow.yml" = var.configuration
  190. }
  191. }
  192. #####
  193. # Secret
  194. #####
  195. resource "kubernetes_secret" "this" {
  196. count = var.enabled ? 1 : 0
  197. metadata {
  198. name = var.secret_name
  199. namespace = var.namespace
  200. annotations = merge(
  201. var.annotations,
  202. var.secret_annotations
  203. )
  204. labels = merge(
  205. {
  206. "instance" = var.secret_name
  207. },
  208. local.labels,
  209. var.labels,
  210. var.secret_labels
  211. )
  212. }
  213. data = {
  214. instance_name = var.instance_name
  215. user_name = var.user_name
  216. password = var.password
  217. }
  218. type = "Opaque"
  219. }